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