| 1 | /* Support for printing Java values for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 1997-2015 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "symtab.h" |
| 22 | #include "gdbtypes.h" |
| 23 | #include "gdbcore.h" |
| 24 | #include "expression.h" |
| 25 | #include "value.h" |
| 26 | #include "demangle.h" |
| 27 | #include "valprint.h" |
| 28 | #include "language.h" |
| 29 | #include "jv-lang.h" |
| 30 | #include "c-lang.h" |
| 31 | #include "annotate.h" |
| 32 | /* Local functions */ |
| 33 | |
| 34 | void |
| 35 | java_value_print (struct value *val, struct ui_file *stream, |
| 36 | const struct value_print_options *options) |
| 37 | { |
| 38 | struct gdbarch *gdbarch = get_type_arch (value_type (val)); |
| 39 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
| 40 | struct type *type; |
| 41 | CORE_ADDR address; |
| 42 | int i; |
| 43 | const char *name; |
| 44 | struct value_print_options opts; |
| 45 | |
| 46 | type = value_type (val); |
| 47 | address = value_address (val); |
| 48 | |
| 49 | if (is_object_type (type)) |
| 50 | { |
| 51 | CORE_ADDR obj_addr; |
| 52 | struct value *tem = val; |
| 53 | |
| 54 | /* Get the run-time type, and cast the object into that. */ |
| 55 | while (TYPE_CODE (value_type (tem)) == TYPE_CODE_PTR) |
| 56 | tem = value_ind (tem); |
| 57 | |
| 58 | obj_addr = value_address (tem); |
| 59 | |
| 60 | if (obj_addr != 0) |
| 61 | { |
| 62 | type = type_from_class (gdbarch, java_class_from_object (val)); |
| 63 | type = lookup_pointer_type (type); |
| 64 | |
| 65 | val = value_at (type, address); |
| 66 | type = value_type (val); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val)) |
| 71 | type_print (TYPE_TARGET_TYPE (type), "", stream, -1); |
| 72 | |
| 73 | name = TYPE_TAG_NAME (type); |
| 74 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT && name != NULL |
| 75 | && (i = strlen (name), name[i - 1] == ']')) |
| 76 | { |
| 77 | gdb_byte buf4[4]; |
| 78 | long length; |
| 79 | unsigned int things_printed = 0; |
| 80 | int reps; |
| 81 | struct type *el_type |
| 82 | = java_primitive_type_from_name (gdbarch, name, i - 2); |
| 83 | |
| 84 | i = 0; |
| 85 | read_memory (address + get_java_object_header_size (gdbarch), buf4, 4); |
| 86 | |
| 87 | length = (long) extract_signed_integer (buf4, 4, byte_order); |
| 88 | fprintf_filtered (stream, "{length: %ld", length); |
| 89 | |
| 90 | if (el_type == NULL) |
| 91 | { |
| 92 | CORE_ADDR element; |
| 93 | CORE_ADDR next_element = -1; /* Dummy initial value. */ |
| 94 | |
| 95 | /* Skip object header and length. */ |
| 96 | address += get_java_object_header_size (gdbarch) + 4; |
| 97 | |
| 98 | while (i < length && things_printed < options->print_max) |
| 99 | { |
| 100 | gdb_byte *buf; |
| 101 | |
| 102 | buf = ((gdb_byte *) |
| 103 | alloca (gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT)); |
| 104 | fputs_filtered (", ", stream); |
| 105 | wrap_here (n_spaces (2)); |
| 106 | |
| 107 | if (i > 0) |
| 108 | element = next_element; |
| 109 | else |
| 110 | { |
| 111 | read_memory (address, buf, sizeof (buf)); |
| 112 | address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT; |
| 113 | /* FIXME: cagney/2003-05-24: Bogus or what. It |
| 114 | pulls a host sized pointer out of the target and |
| 115 | then extracts that as an address (while assuming |
| 116 | that the address is unsigned)! */ |
| 117 | element = extract_unsigned_integer (buf, sizeof (buf), |
| 118 | byte_order); |
| 119 | } |
| 120 | |
| 121 | for (reps = 1; i + reps < length; reps++) |
| 122 | { |
| 123 | read_memory (address, buf, sizeof (buf)); |
| 124 | address += gdbarch_ptr_bit (gdbarch) / HOST_CHAR_BIT; |
| 125 | /* FIXME: cagney/2003-05-24: Bogus or what. It |
| 126 | pulls a host sized pointer out of the target and |
| 127 | then extracts that as an address (while assuming |
| 128 | that the address is unsigned)! */ |
| 129 | next_element = extract_unsigned_integer (buf, sizeof (buf), |
| 130 | byte_order); |
| 131 | if (next_element != element) |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | if (reps == 1) |
| 136 | fprintf_filtered (stream, "%d: ", i); |
| 137 | else |
| 138 | fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1); |
| 139 | |
| 140 | if (element == 0) |
| 141 | fprintf_filtered (stream, "null"); |
| 142 | else |
| 143 | fprintf_filtered (stream, "@%s", paddress (gdbarch, element)); |
| 144 | |
| 145 | things_printed++; |
| 146 | i += reps; |
| 147 | } |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | struct value *v = allocate_value (el_type); |
| 152 | struct value *next_v = allocate_value (el_type); |
| 153 | |
| 154 | set_value_address (v, (address |
| 155 | + get_java_object_header_size (gdbarch) + 4)); |
| 156 | set_value_address (next_v, value_raw_address (v)); |
| 157 | |
| 158 | while (i < length && things_printed < options->print_max) |
| 159 | { |
| 160 | fputs_filtered (", ", stream); |
| 161 | wrap_here (n_spaces (2)); |
| 162 | |
| 163 | if (i > 0) |
| 164 | { |
| 165 | struct value *tmp; |
| 166 | |
| 167 | tmp = next_v; |
| 168 | next_v = v; |
| 169 | v = tmp; |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | set_value_lazy (v, 1); |
| 174 | set_value_offset (v, 0); |
| 175 | } |
| 176 | |
| 177 | set_value_offset (next_v, value_offset (v)); |
| 178 | |
| 179 | for (reps = 1; i + reps < length; reps++) |
| 180 | { |
| 181 | set_value_lazy (next_v, 1); |
| 182 | set_value_offset (next_v, value_offset (next_v) |
| 183 | + TYPE_LENGTH (el_type)); |
| 184 | value_fetch_lazy (next_v); |
| 185 | if (!value_contents_eq (v, value_embedded_offset (v), |
| 186 | next_v, |
| 187 | value_embedded_offset (next_v), |
| 188 | TYPE_LENGTH (el_type))) |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | if (reps == 1) |
| 193 | fprintf_filtered (stream, "%d: ", i); |
| 194 | else |
| 195 | fprintf_filtered (stream, "%d..%d: ", i, i + reps - 1); |
| 196 | |
| 197 | opts = *options; |
| 198 | opts.deref_ref = 1; |
| 199 | common_val_print (v, stream, 1, &opts, current_language); |
| 200 | |
| 201 | things_printed++; |
| 202 | i += reps; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (i < length) |
| 207 | fprintf_filtered (stream, "..."); |
| 208 | |
| 209 | fprintf_filtered (stream, "}"); |
| 210 | |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | /* If it's type String, print it. */ |
| 215 | |
| 216 | if (TYPE_CODE (type) == TYPE_CODE_PTR |
| 217 | && TYPE_TARGET_TYPE (type) |
| 218 | && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)) |
| 219 | && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)), |
| 220 | "java.lang.String") == 0 |
| 221 | && (options->format == 0 || options->format == 's') |
| 222 | && address != 0 |
| 223 | && value_as_address (val) != 0) |
| 224 | { |
| 225 | struct type *char_type; |
| 226 | struct value *data_val; |
| 227 | CORE_ADDR data; |
| 228 | struct value *boffset_val; |
| 229 | unsigned long boffset; |
| 230 | struct value *count_val; |
| 231 | unsigned long count; |
| 232 | struct value *mark; |
| 233 | |
| 234 | fputs_filtered (" ", stream); |
| 235 | |
| 236 | mark = value_mark (); /* Remember start of new values. */ |
| 237 | |
| 238 | data_val = value_struct_elt (&val, NULL, "data", NULL, NULL); |
| 239 | data = value_as_address (data_val); |
| 240 | |
| 241 | boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL); |
| 242 | boffset = value_as_address (boffset_val); |
| 243 | |
| 244 | count_val = value_struct_elt (&val, NULL, "count", NULL, NULL); |
| 245 | count = value_as_address (count_val); |
| 246 | |
| 247 | value_free_to_mark (mark); /* Release unnecessary values. */ |
| 248 | |
| 249 | char_type = builtin_java_type (gdbarch)->builtin_char; |
| 250 | val_print_string (char_type, NULL, data + boffset, count, stream, |
| 251 | options); |
| 252 | |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | opts = *options; |
| 257 | opts.deref_ref = 1; |
| 258 | common_val_print (val, stream, 0, &opts, current_language); |
| 259 | } |
| 260 | |
| 261 | /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the |
| 262 | same meanings as in cp_print_value and c_val_print. |
| 263 | |
| 264 | DONT_PRINT is an array of baseclass types that we |
| 265 | should not print, or zero if called from top level. */ |
| 266 | |
| 267 | static void |
| 268 | java_print_value_fields (struct type *type, const gdb_byte *valaddr, |
| 269 | int offset, |
| 270 | CORE_ADDR address, struct ui_file *stream, |
| 271 | int recurse, |
| 272 | const struct value *val, |
| 273 | const struct value_print_options *options) |
| 274 | { |
| 275 | int i, len, n_baseclasses; |
| 276 | |
| 277 | type = check_typedef (type); |
| 278 | |
| 279 | fprintf_filtered (stream, "{"); |
| 280 | len = TYPE_NFIELDS (type); |
| 281 | n_baseclasses = TYPE_N_BASECLASSES (type); |
| 282 | |
| 283 | if (n_baseclasses > 0) |
| 284 | { |
| 285 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); |
| 286 | |
| 287 | for (i = 0; i < n_baseclasses; i++) |
| 288 | { |
| 289 | int boffset; |
| 290 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); |
| 291 | const char *basename = TYPE_NAME (baseclass); |
| 292 | const gdb_byte *base_valaddr; |
| 293 | |
| 294 | if (BASETYPE_VIA_VIRTUAL (type, i)) |
| 295 | continue; |
| 296 | |
| 297 | if (basename != NULL && strcmp (basename, "java.lang.Object") == 0) |
| 298 | continue; |
| 299 | |
| 300 | boffset = 0; |
| 301 | |
| 302 | if (options->prettyformat) |
| 303 | { |
| 304 | fprintf_filtered (stream, "\n"); |
| 305 | print_spaces_filtered (2 * (recurse + 1), stream); |
| 306 | } |
| 307 | fputs_filtered ("<", stream); |
| 308 | /* Not sure what the best notation is in the case where there is no |
| 309 | baseclass name. */ |
| 310 | fputs_filtered (basename ? basename : "", stream); |
| 311 | fputs_filtered ("> = ", stream); |
| 312 | |
| 313 | base_valaddr = valaddr; |
| 314 | |
| 315 | java_print_value_fields (baseclass, base_valaddr, |
| 316 | offset + boffset, address, |
| 317 | stream, recurse + 1, val, options); |
| 318 | fputs_filtered (", ", stream); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (!len && n_baseclasses == 1) |
| 323 | fprintf_filtered (stream, "<No data fields>"); |
| 324 | else |
| 325 | { |
| 326 | int fields_seen = 0; |
| 327 | |
| 328 | for (i = n_baseclasses; i < len; i++) |
| 329 | { |
| 330 | /* If requested, skip printing of static fields. */ |
| 331 | if (field_is_static (&TYPE_FIELD (type, i))) |
| 332 | { |
| 333 | const char *name = TYPE_FIELD_NAME (type, i); |
| 334 | |
| 335 | if (!options->static_field_print) |
| 336 | continue; |
| 337 | if (name != NULL && strcmp (name, "class") == 0) |
| 338 | continue; |
| 339 | } |
| 340 | if (fields_seen) |
| 341 | fprintf_filtered (stream, ", "); |
| 342 | else if (n_baseclasses > 0) |
| 343 | { |
| 344 | if (options->prettyformat) |
| 345 | { |
| 346 | fprintf_filtered (stream, "\n"); |
| 347 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 348 | fputs_filtered ("members of ", stream); |
| 349 | fputs_filtered (type_name_no_tag (type), stream); |
| 350 | fputs_filtered (": ", stream); |
| 351 | } |
| 352 | } |
| 353 | fields_seen = 1; |
| 354 | |
| 355 | if (options->prettyformat) |
| 356 | { |
| 357 | fprintf_filtered (stream, "\n"); |
| 358 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | wrap_here (n_spaces (2 + 2 * recurse)); |
| 363 | } |
| 364 | |
| 365 | annotate_field_begin (TYPE_FIELD_TYPE (type, i)); |
| 366 | |
| 367 | if (field_is_static (&TYPE_FIELD (type, i))) |
| 368 | fputs_filtered ("static ", stream); |
| 369 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), |
| 370 | language_cplus, |
| 371 | DMGL_PARAMS | DMGL_ANSI); |
| 372 | annotate_field_name_end (); |
| 373 | fputs_filtered (": ", stream); |
| 374 | annotate_field_value (); |
| 375 | |
| 376 | if (!field_is_static (&TYPE_FIELD (type, i)) |
| 377 | && TYPE_FIELD_PACKED (type, i)) |
| 378 | { |
| 379 | struct value *v; |
| 380 | |
| 381 | /* Bitfields require special handling, especially due to byte |
| 382 | order problems. */ |
| 383 | if (TYPE_FIELD_IGNORE (type, i)) |
| 384 | { |
| 385 | fputs_filtered ("<optimized out or zero length>", stream); |
| 386 | } |
| 387 | else if (value_bits_synthetic_pointer (val, |
| 388 | TYPE_FIELD_BITPOS (type, |
| 389 | i), |
| 390 | TYPE_FIELD_BITSIZE (type, |
| 391 | i))) |
| 392 | { |
| 393 | fputs_filtered (_("<synthetic pointer>"), stream); |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | struct value_print_options opts; |
| 398 | |
| 399 | v = value_field_bitfield (type, i, valaddr, offset, val); |
| 400 | |
| 401 | opts = *options; |
| 402 | opts.deref_ref = 0; |
| 403 | common_val_print (v, stream, recurse + 1, |
| 404 | &opts, current_language); |
| 405 | } |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | if (TYPE_FIELD_IGNORE (type, i)) |
| 410 | { |
| 411 | fputs_filtered ("<optimized out or zero length>", stream); |
| 412 | } |
| 413 | else if (field_is_static (&TYPE_FIELD (type, i))) |
| 414 | { |
| 415 | struct value_print_options opts; |
| 416 | struct value *v = value_static_field (type, i); |
| 417 | struct type *t = check_typedef (value_type (v)); |
| 418 | |
| 419 | if (TYPE_CODE (t) == TYPE_CODE_STRUCT) |
| 420 | v = value_addr (v); |
| 421 | opts = *options; |
| 422 | opts.deref_ref = 0; |
| 423 | common_val_print (v, stream, recurse + 1, |
| 424 | &opts, current_language); |
| 425 | } |
| 426 | else if (TYPE_FIELD_TYPE (type, i) == NULL) |
| 427 | fputs_filtered ("<unknown type>", stream); |
| 428 | else |
| 429 | { |
| 430 | struct value_print_options opts = *options; |
| 431 | |
| 432 | opts.deref_ref = 0; |
| 433 | val_print (TYPE_FIELD_TYPE (type, i), |
| 434 | valaddr, |
| 435 | offset + TYPE_FIELD_BITPOS (type, i) / 8, |
| 436 | address, stream, recurse + 1, val, &opts, |
| 437 | current_language); |
| 438 | } |
| 439 | } |
| 440 | annotate_field_end (); |
| 441 | } |
| 442 | |
| 443 | if (options->prettyformat) |
| 444 | { |
| 445 | fprintf_filtered (stream, "\n"); |
| 446 | print_spaces_filtered (2 * recurse, stream); |
| 447 | } |
| 448 | } |
| 449 | fprintf_filtered (stream, "}"); |
| 450 | } |
| 451 | |
| 452 | /* See val_print for a description of the various parameters of this |
| 453 | function; they are identical. */ |
| 454 | |
| 455 | void |
| 456 | java_val_print (struct type *type, const gdb_byte *valaddr, |
| 457 | int embedded_offset, CORE_ADDR address, |
| 458 | struct ui_file *stream, int recurse, |
| 459 | const struct value *val, |
| 460 | const struct value_print_options *options) |
| 461 | { |
| 462 | struct gdbarch *gdbarch = get_type_arch (type); |
| 463 | struct type *target_type; |
| 464 | CORE_ADDR addr; |
| 465 | |
| 466 | type = check_typedef (type); |
| 467 | switch (TYPE_CODE (type)) |
| 468 | { |
| 469 | case TYPE_CODE_PTR: |
| 470 | if (options->format && options->format != 's') |
| 471 | { |
| 472 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 473 | val, options, 0, stream); |
| 474 | break; |
| 475 | } |
| 476 | addr = unpack_pointer (type, valaddr + embedded_offset); |
| 477 | if (addr == 0) |
| 478 | { |
| 479 | fputs_filtered ("null", stream); |
| 480 | return; |
| 481 | } |
| 482 | target_type = check_typedef (TYPE_TARGET_TYPE (type)); |
| 483 | |
| 484 | if (TYPE_CODE (target_type) == TYPE_CODE_FUNC) |
| 485 | { |
| 486 | /* Try to print what function it points to. */ |
| 487 | print_address_demangle (options, gdbarch, addr, stream, demangle); |
| 488 | return; |
| 489 | } |
| 490 | |
| 491 | if (options->addressprint && options->format != 's') |
| 492 | { |
| 493 | fputs_filtered ("@", stream); |
| 494 | print_longest (stream, 'x', 0, (ULONGEST) addr); |
| 495 | } |
| 496 | |
| 497 | return; |
| 498 | |
| 499 | case TYPE_CODE_CHAR: |
| 500 | case TYPE_CODE_INT: |
| 501 | /* Can't just call c_val_print because that prints bytes as C |
| 502 | chars. */ |
| 503 | if (options->format || options->output_format) |
| 504 | { |
| 505 | struct value_print_options opts = *options; |
| 506 | |
| 507 | opts.format = (options->format ? options->format |
| 508 | : options->output_format); |
| 509 | val_print_scalar_formatted (type, valaddr, embedded_offset, |
| 510 | val, &opts, 0, stream); |
| 511 | } |
| 512 | else if (TYPE_CODE (type) == TYPE_CODE_CHAR |
| 513 | || (TYPE_CODE (type) == TYPE_CODE_INT |
| 514 | && TYPE_LENGTH (type) == 2 |
| 515 | && strcmp (TYPE_NAME (type), "char") == 0)) |
| 516 | LA_PRINT_CHAR ((int) unpack_long (type, valaddr + embedded_offset), |
| 517 | type, stream); |
| 518 | else |
| 519 | val_print_type_code_int (type, valaddr + embedded_offset, stream); |
| 520 | break; |
| 521 | |
| 522 | case TYPE_CODE_STRUCT: |
| 523 | java_print_value_fields (type, valaddr, embedded_offset, |
| 524 | address, stream, recurse, val, options); |
| 525 | break; |
| 526 | |
| 527 | default: |
| 528 | c_val_print (type, valaddr, embedded_offset, address, stream, |
| 529 | recurse, val, options); |
| 530 | break; |
| 531 | } |
| 532 | } |