GDB (API)
|
00001 /* Support for printing Java values for GDB, the GNU debugger. 00002 00003 Copyright (C) 1997-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "symtab.h" 00022 #include "gdbtypes.h" 00023 #include "gdbcore.h" 00024 #include "expression.h" 00025 #include "value.h" 00026 #include "demangle.h" 00027 #include "valprint.h" 00028 #include "language.h" 00029 #include "jv-lang.h" 00030 #include "c-lang.h" 00031 #include "annotate.h" 00032 #include "gdb_string.h" 00033 00034 /* Local functions */ 00035 00036 void 00037 java_value_print (struct value *val, struct ui_file *stream, 00038 const struct value_print_options *options) 00039 { 00040 struct gdbarch *gdbarch = get_type_arch (value_type (val)); 00041 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); 00042 struct type *type; 00043 CORE_ADDR address; 00044 int i; 00045 const char *name; 00046 struct value_print_options opts; 00047 00048 type = value_type (val); 00049 address = value_address (val); 00050 00051 if (is_object_type (type)) 00052 { 00053 CORE_ADDR obj_addr; 00054 struct value *tem = val; 00055 00056 /* Get the run-time type, and cast the object into that. */ 00057 while (TYPE_CODE (value_type (tem)) == TYPE_CODE_PTR) 00058 tem = value_ind (tem); 00059 00060 obj_addr = value_address (tem); 00061 00062 if (obj_addr != 0) 00063 { 00064 type = type_from_class (gdbarch, java_class_from_object (val)); 00065 type = lookup_pointer_type (type); 00066 00067 val = value_at (type, address); 00068 } 00069 } 00070 00071 if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val)) 00072 type_print (TYPE_TARGET_TYPE (type), "", stream, -1); 00073 00074 name = TYPE_TAG_NAME (type); 00075 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL 00076 && (i = strlen (name), name[i - 1] == ']')) 00077 { 00078 gdb_byte buf4[4]; 00079 long length; 00080 unsigned int things_printed = 0; 00081 int reps; 00082 struct type *el_type 00083 = java_primitive_type_from_name (gdbarch, name, i - 2); 00084 00085 i = 0; 00086 read_memory (address + get_java_object_header_size (gdbarch), buf4, 4); 00087 00088 length = (long) extract_signed_integer (buf4, 4, byte_order); 00089 fprintf_filtered (stream, "{length: %ld", length); 00090 00091 if (el_type == NULL) 00092 { 00093 CORE_ADDR element; 00094 CORE_ADDR next_element = -1; /* Dummy initial value. */ 00095 00096 /* Skip object header and length. */ 00097 address += get_java_object_header_size (gdbarch) + 4; 00098 00099 while (i < length && things_printed < options->print_max) 00100 { 00101 gdb_byte *buf; 00102 00103 buf = alloca (gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT); 00104 fputs_filtered (", ", stream); 00105 wrap_here (n_spaces (2)); 00106 00107 if (i > 0) 00108 element = next_element; 00109 else 00110 { 00111 read_memory (address, buf, sizeof (buf)); 00112 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT; 00113 /* FIXME: cagney/2003-05-24: Bogus or what. It 00114 pulls a host sized pointer out of the target and 00115 then extracts that as an address (while assuming 00116 that the address is unsigned)! */ 00117 element = extract_unsigned_integer (buf, sizeof (buf), 00118 byte_order); 00119 } 00120 00121 for (reps = 1; i + reps < length; reps++) 00122 { 00123 read_memory (address, buf, sizeof (buf)); 00124 address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT; 00125 /* FIXME: cagney/2003-05-24: Bogus or what. It 00126 pulls a host sized pointer out of the target and 00127 then extracts that as an address (while assuming 00128 that the address is unsigned)! */ 00129 next_element = extract_unsigned_integer (buf, sizeof (buf), 00130 byte_order); 00131 if (next_element != element) 00132 break; 00133 } 00134 00135 if (reps == 1) 00136 fprintf_filtered (stream, "%d: ", i); 00137 else 00138 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1); 00139 00140 if (element == 0) 00141 fprintf_filtered (stream, "null"); 00142 else 00143 fprintf_filtered (stream, "@%s", paddress (gdbarch, element)); 00144 00145 things_printed++; 00146 i += reps; 00147 } 00148 } 00149 else 00150 { 00151 struct value *v = allocate_value (el_type); 00152 struct value *next_v = allocate_value (el_type); 00153 00154 set_value_address (v, (address 00155 + get_java_object_header_size (gdbarch) + 4)); 00156 set_value_address (next_v, value_raw_address (v)); 00157 00158 while (i < length && things_printed < options->print_max) 00159 { 00160 fputs_filtered (", ", stream); 00161 wrap_here (n_spaces (2)); 00162 00163 if (i > 0) 00164 { 00165 struct value *tmp; 00166 00167 tmp = next_v; 00168 next_v = v; 00169 v = tmp; 00170 } 00171 else 00172 { 00173 set_value_lazy (v, 1); 00174 set_value_offset (v, 0); 00175 } 00176 00177 set_value_offset (next_v, value_offset (v)); 00178 00179 for (reps = 1; i + reps < length; reps++) 00180 { 00181 set_value_lazy (next_v, 1); 00182 set_value_offset (next_v, value_offset (next_v) 00183 + TYPE_LENGTH (el_type)); 00184 value_fetch_lazy (next_v); 00185 if (!(value_available_contents_eq 00186 (v, value_embedded_offset (v), 00187 next_v, value_embedded_offset (next_v), 00188 TYPE_LENGTH (el_type)))) 00189 break; 00190 } 00191 00192 if (reps == 1) 00193 fprintf_filtered (stream, "%d: ", i); 00194 else 00195 fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1); 00196 00197 opts = *options; 00198 opts.deref_ref = 1; 00199 common_val_print (v, stream, 1, &opts, current_language); 00200 00201 things_printed++; 00202 i += reps; 00203 } 00204 } 00205 00206 if (i < length) 00207 fprintf_filtered (stream, "..."); 00208 00209 fprintf_filtered (stream, "}"); 00210 00211 return; 00212 } 00213 00214 /* If it's type String, print it. */ 00215 00216 if (TYPE_CODE (type) == TYPE_CODE_PTR 00217 && TYPE_TARGET_TYPE (type) 00218 && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)) 00219 && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)), 00220 "java.lang.String") == 0 00221 && (options->format == 0 || options->format == 's') 00222 && address != 0 00223 && value_as_address (val) != 0) 00224 { 00225 struct type *char_type; 00226 struct value *data_val; 00227 CORE_ADDR data; 00228 struct value *boffset_val; 00229 unsigned long boffset; 00230 struct value *count_val; 00231 unsigned long count; 00232 struct value *mark; 00233 00234 fputs_filtered (" ", stream); 00235 00236 mark = value_mark (); /* Remember start of new values. */ 00237 00238 data_val = value_struct_elt (&val, NULL, "data", NULL, NULL); 00239 data = value_as_address (data_val); 00240 00241 boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL); 00242 boffset = value_as_address (boffset_val); 00243 00244 count_val = value_struct_elt (&val, NULL, "count", NULL, NULL); 00245 count = value_as_address (count_val); 00246 00247 value_free_to_mark (mark); /* Release unnecessary values. */ 00248 00249 char_type = builtin_java_type (gdbarch)->builtin_char; 00250 val_print_string (char_type, NULL, data + boffset, count, stream, 00251 options); 00252 00253 return; 00254 } 00255 00256 opts = *options; 00257 opts.deref_ref = 1; 00258 common_val_print (val, stream, 0, &opts, current_language); 00259 } 00260 00261 /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the 00262 same meanings as in cp_print_value and c_val_print. 00263 00264 DONT_PRINT is an array of baseclass types that we 00265 should not print, or zero if called from top level. */ 00266 00267 static void 00268 java_print_value_fields (struct type *type, const gdb_byte *valaddr, 00269 int offset, 00270 CORE_ADDR address, struct ui_file *stream, 00271 int recurse, 00272 const struct value *val, 00273 const struct value_print_options *options) 00274 { 00275 int i, len, n_baseclasses; 00276 00277 CHECK_TYPEDEF (type); 00278 00279 fprintf_filtered (stream, "{"); 00280 len = TYPE_NFIELDS (type); 00281 n_baseclasses = TYPE_N_BASECLASSES (type); 00282 00283 if (n_baseclasses > 0) 00284 { 00285 int i, n_baseclasses = TYPE_N_BASECLASSES (type); 00286 00287 for (i = 0; i < n_baseclasses; i++) 00288 { 00289 int boffset; 00290 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); 00291 const char *basename = TYPE_NAME (baseclass); 00292 const gdb_byte *base_valaddr; 00293 00294 if (BASETYPE_VIA_VIRTUAL (type, i)) 00295 continue; 00296 00297 if (basename != NULL && strcmp (basename, "java.lang.Object") == 0) 00298 continue; 00299 00300 boffset = 0; 00301 00302 if (options->prettyformat) 00303 { 00304 fprintf_filtered (stream, "\n"); 00305 print_spaces_filtered (2 * (recurse + 1), stream); 00306 } 00307 fputs_filtered ("<", stream); 00308 /* Not sure what the best notation is in the case where there is no 00309 baseclass name. */ 00310 fputs_filtered (basename ? basename : "", stream); 00311 fputs_filtered ("> = ", stream); 00312 00313 base_valaddr = valaddr; 00314 00315 java_print_value_fields (baseclass, base_valaddr, 00316 offset + boffset, address, 00317 stream, recurse + 1, val, options); 00318 fputs_filtered (", ", stream); 00319 } 00320 } 00321 00322 if (!len && n_baseclasses == 1) 00323 fprintf_filtered (stream, "<No data fields>"); 00324 else 00325 { 00326 int fields_seen = 0; 00327 00328 for (i = n_baseclasses; i < len; i++) 00329 { 00330 /* If requested, skip printing of static fields. */ 00331 if (field_is_static (&TYPE_FIELD (type, i))) 00332 { 00333 const char *name = TYPE_FIELD_NAME (type, i); 00334 00335 if (!options->static_field_print) 00336 continue; 00337 if (name != NULL && strcmp (name, "class") == 0) 00338 continue; 00339 } 00340 if (fields_seen) 00341 fprintf_filtered (stream, ", "); 00342 else if (n_baseclasses > 0) 00343 { 00344 if (options->prettyformat) 00345 { 00346 fprintf_filtered (stream, "\n"); 00347 print_spaces_filtered (2 + 2 * recurse, stream); 00348 fputs_filtered ("members of ", stream); 00349 fputs_filtered (type_name_no_tag (type), stream); 00350 fputs_filtered (": ", stream); 00351 } 00352 } 00353 fields_seen = 1; 00354 00355 if (options->prettyformat) 00356 { 00357 fprintf_filtered (stream, "\n"); 00358 print_spaces_filtered (2 + 2 * recurse, stream); 00359 } 00360 else 00361 { 00362 wrap_here (n_spaces (2 + 2 * recurse)); 00363 } 00364 00365 annotate_field_begin (TYPE_FIELD_TYPE (type, i)); 00366 00367 if (field_is_static (&TYPE_FIELD (type, i))) 00368 fputs_filtered ("static ", stream); 00369 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), 00370 language_cplus, 00371 DMGL_PARAMS | DMGL_ANSI); 00372 annotate_field_name_end (); 00373 fputs_filtered (": ", stream); 00374 annotate_field_value (); 00375 00376 if (!field_is_static (&TYPE_FIELD (type, i)) 00377 && TYPE_FIELD_PACKED (type, i)) 00378 { 00379 struct value *v; 00380 00381 /* Bitfields require special handling, especially due to byte 00382 order problems. */ 00383 if (TYPE_FIELD_IGNORE (type, i)) 00384 { 00385 fputs_filtered ("<optimized out or zero length>", stream); 00386 } 00387 else if (value_bits_synthetic_pointer (val, 00388 TYPE_FIELD_BITPOS (type, 00389 i), 00390 TYPE_FIELD_BITSIZE (type, 00391 i))) 00392 { 00393 fputs_filtered (_("<synthetic pointer>"), stream); 00394 } 00395 else if (!value_bits_valid (val, TYPE_FIELD_BITPOS (type, i), 00396 TYPE_FIELD_BITSIZE (type, i))) 00397 { 00398 val_print_optimized_out (val, stream); 00399 } 00400 else 00401 { 00402 struct value_print_options opts; 00403 00404 v = value_field_bitfield (type, i, valaddr, offset, val); 00405 00406 opts = *options; 00407 opts.deref_ref = 0; 00408 common_val_print (v, stream, recurse + 1, 00409 &opts, current_language); 00410 } 00411 } 00412 else 00413 { 00414 if (TYPE_FIELD_IGNORE (type, i)) 00415 { 00416 fputs_filtered ("<optimized out or zero length>", stream); 00417 } 00418 else if (field_is_static (&TYPE_FIELD (type, i))) 00419 { 00420 struct value *v = value_static_field (type, i); 00421 00422 if (v == NULL) 00423 val_print_optimized_out (NULL, stream); 00424 else 00425 { 00426 struct value_print_options opts; 00427 struct type *t = check_typedef (value_type (v)); 00428 00429 if (TYPE_CODE (t) == TYPE_CODE_STRUCT) 00430 v = value_addr (v); 00431 opts = *options; 00432 opts.deref_ref = 0; 00433 common_val_print (v, stream, recurse + 1, 00434 &opts, current_language); 00435 } 00436 } 00437 else if (TYPE_FIELD_TYPE (type, i) == NULL) 00438 fputs_filtered ("<unknown type>", stream); 00439 else 00440 { 00441 struct value_print_options opts = *options; 00442 00443 opts.deref_ref = 0; 00444 val_print (TYPE_FIELD_TYPE (type, i), 00445 valaddr, 00446 offset + TYPE_FIELD_BITPOS (type, i) / 8, 00447 address, stream, recurse + 1, val, &opts, 00448 current_language); 00449 } 00450 } 00451 annotate_field_end (); 00452 } 00453 00454 if (options->prettyformat) 00455 { 00456 fprintf_filtered (stream, "\n"); 00457 print_spaces_filtered (2 * recurse, stream); 00458 } 00459 } 00460 fprintf_filtered (stream, "}"); 00461 } 00462 00463 /* See val_print for a description of the various parameters of this 00464 function; they are identical. */ 00465 00466 void 00467 java_val_print (struct type *type, const gdb_byte *valaddr, 00468 int embedded_offset, CORE_ADDR address, 00469 struct ui_file *stream, int recurse, 00470 const struct value *val, 00471 const struct value_print_options *options) 00472 { 00473 struct gdbarch *gdbarch = get_type_arch (type); 00474 struct type *target_type; 00475 CORE_ADDR addr; 00476 00477 CHECK_TYPEDEF (type); 00478 switch (TYPE_CODE (type)) 00479 { 00480 case TYPE_CODE_PTR: 00481 if (options->format && options->format != 's') 00482 { 00483 val_print_scalar_formatted (type, valaddr, embedded_offset, 00484 val, options, 0, stream); 00485 break; 00486 } 00487 addr = unpack_pointer (type, valaddr + embedded_offset); 00488 if (addr == 0) 00489 { 00490 fputs_filtered ("null", stream); 00491 return; 00492 } 00493 target_type = check_typedef (TYPE_TARGET_TYPE (type)); 00494 00495 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC) 00496 { 00497 /* Try to print what function it points to. */ 00498 print_address_demangle (options, gdbarch, addr, stream, demangle); 00499 return; 00500 } 00501 00502 if (options->addressprint && options->format != 's') 00503 { 00504 fputs_filtered ("@", stream); 00505 print_longest (stream, 'x', 0, (ULONGEST) addr); 00506 } 00507 00508 return; 00509 00510 case TYPE_CODE_CHAR: 00511 case TYPE_CODE_INT: 00512 /* Can't just call c_val_print because that prints bytes as C 00513 chars. */ 00514 if (options->format || options->output_format) 00515 { 00516 struct value_print_options opts = *options; 00517 00518 opts.format = (options->format ? options->format 00519 : options->output_format); 00520 val_print_scalar_formatted (type, valaddr, embedded_offset, 00521 val, &opts, 0, stream); 00522 } 00523 else if (TYPE_CODE (type) == TYPE_CODE_CHAR 00524 || (TYPE_CODE (type) == TYPE_CODE_INT 00525 && TYPE_LENGTH (type) == 2 00526 && strcmp (TYPE_NAME (type), "char") == 0)) 00527 LA_PRINT_CHAR ((int) unpack_long (type, valaddr + embedded_offset), 00528 type, stream); 00529 else 00530 val_print_type_code_int (type, valaddr + embedded_offset, stream); 00531 break; 00532 00533 case TYPE_CODE_STRUCT: 00534 java_print_value_fields (type, valaddr, embedded_offset, 00535 address, stream, recurse, val, options); 00536 break; 00537 00538 default: 00539 c_val_print (type, valaddr, embedded_offset, address, stream, 00540 recurse, val, options); 00541 break; 00542 } 00543 }