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