| 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, |
| 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 | return (val_print (type, VALUE_CONTENTS (val), address, |
| 153 | stream, format, 1, 0, pretty)); |
| 154 | } |
| 155 | |
| 156 | /* TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and PRETTY have the |
| 157 | same meanings as in cp_print_value and c_val_print. |
| 158 | |
| 159 | DONT_PRINT is an array of baseclass types that we |
| 160 | should not print, or zero if called from top level. */ |
| 161 | |
| 162 | void |
| 163 | java_print_value_fields (type, valaddr, address, stream, |
| 164 | format, recurse, pretty) |
| 165 | struct type *type; |
| 166 | char *valaddr; |
| 167 | CORE_ADDR address; |
| 168 | GDB_FILE *stream; |
| 169 | int format; |
| 170 | int recurse; |
| 171 | enum val_prettyprint pretty; |
| 172 | { |
| 173 | int i, len, n_baseclasses; |
| 174 | |
| 175 | CHECK_TYPEDEF (type); |
| 176 | |
| 177 | fprintf_filtered (stream, "{"); |
| 178 | len = TYPE_NFIELDS (type); |
| 179 | n_baseclasses = TYPE_N_BASECLASSES (type); |
| 180 | |
| 181 | if (n_baseclasses > 0) |
| 182 | { |
| 183 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); |
| 184 | |
| 185 | for (i = 0; i < n_baseclasses; i++) |
| 186 | { |
| 187 | int boffset; |
| 188 | struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i)); |
| 189 | char *basename = TYPE_NAME (baseclass); |
| 190 | char *base_valaddr; |
| 191 | |
| 192 | if (BASETYPE_VIA_VIRTUAL (type, i)) |
| 193 | continue; |
| 194 | |
| 195 | if (basename != NULL && strcmp (basename, "java.lang.Object") == 0) |
| 196 | continue; |
| 197 | |
| 198 | boffset = 0; |
| 199 | |
| 200 | if (pretty) |
| 201 | { |
| 202 | fprintf_filtered (stream, "\n"); |
| 203 | print_spaces_filtered (2 * (recurse+1), stream); |
| 204 | } |
| 205 | fputs_filtered ("<", stream); |
| 206 | /* Not sure what the best notation is in the case where there is no |
| 207 | baseclass name. */ |
| 208 | fputs_filtered (basename ? basename : "", stream); |
| 209 | fputs_filtered ("> = ", stream); |
| 210 | |
| 211 | base_valaddr = valaddr; |
| 212 | |
| 213 | java_print_value_fields (baseclass, base_valaddr, address + boffset, |
| 214 | stream, format, recurse+1, pretty); |
| 215 | fputs_filtered (", ", stream); |
| 216 | |
| 217 | flush_it: |
| 218 | ; |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | if (!len && n_baseclasses == 1) |
| 224 | fprintf_filtered (stream, "<No data fields>"); |
| 225 | else |
| 226 | { |
| 227 | extern int inspect_it; |
| 228 | int fields_seen = 0; |
| 229 | |
| 230 | for (i = n_baseclasses; i < len; i++) |
| 231 | { |
| 232 | /* If requested, skip printing of static fields. */ |
| 233 | if (TYPE_FIELD_STATIC (type, i)) |
| 234 | { |
| 235 | char *name = TYPE_FIELD_NAME (type, i); |
| 236 | if (!static_field_print) |
| 237 | continue; |
| 238 | if (name != NULL && strcmp (name, "class") == 0) |
| 239 | continue; |
| 240 | } |
| 241 | if (fields_seen) |
| 242 | fprintf_filtered (stream, ", "); |
| 243 | else if (n_baseclasses > 0) |
| 244 | { |
| 245 | if (pretty) |
| 246 | { |
| 247 | fprintf_filtered (stream, "\n"); |
| 248 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 249 | fputs_filtered ("members of ", stream); |
| 250 | fputs_filtered (type_name_no_tag (type), stream); |
| 251 | fputs_filtered (": ", stream); |
| 252 | } |
| 253 | } |
| 254 | fields_seen = 1; |
| 255 | |
| 256 | if (pretty) |
| 257 | { |
| 258 | fprintf_filtered (stream, "\n"); |
| 259 | print_spaces_filtered (2 + 2 * recurse, stream); |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | wrap_here (n_spaces (2 + 2 * recurse)); |
| 264 | } |
| 265 | if (inspect_it) |
| 266 | { |
| 267 | if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR) |
| 268 | fputs_filtered ("\"( ptr \"", stream); |
| 269 | else |
| 270 | fputs_filtered ("\"( nodef \"", stream); |
| 271 | if (TYPE_FIELD_STATIC (type, i)) |
| 272 | fputs_filtered ("static ", stream); |
| 273 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), |
| 274 | language_cplus, |
| 275 | DMGL_PARAMS | DMGL_ANSI); |
| 276 | fputs_filtered ("\" \"", stream); |
| 277 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), |
| 278 | language_cplus, |
| 279 | DMGL_PARAMS | DMGL_ANSI); |
| 280 | fputs_filtered ("\") \"", stream); |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | annotate_field_begin (TYPE_FIELD_TYPE (type, i)); |
| 285 | |
| 286 | if (TYPE_FIELD_STATIC (type, i)) |
| 287 | fputs_filtered ("static ", stream); |
| 288 | fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i), |
| 289 | language_cplus, |
| 290 | DMGL_PARAMS | DMGL_ANSI); |
| 291 | annotate_field_name_end (); |
| 292 | fputs_filtered (": ", stream); |
| 293 | annotate_field_value (); |
| 294 | } |
| 295 | |
| 296 | if (!TYPE_FIELD_STATIC (type, i) && TYPE_FIELD_PACKED (type, i)) |
| 297 | { |
| 298 | value_ptr v; |
| 299 | |
| 300 | /* Bitfields require special handling, especially due to byte |
| 301 | order problems. */ |
| 302 | if (TYPE_FIELD_IGNORE (type, i)) |
| 303 | { |
| 304 | fputs_filtered ("<optimized out or zero length>", stream); |
| 305 | } |
| 306 | else |
| 307 | { |
| 308 | v = value_from_longest (TYPE_FIELD_TYPE (type, i), |
| 309 | unpack_field_as_long (type, valaddr, i)); |
| 310 | |
| 311 | val_print (TYPE_FIELD_TYPE(type, i), VALUE_CONTENTS (v), 0, |
| 312 | stream, format, 0, recurse + 1, pretty); |
| 313 | } |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | if (TYPE_FIELD_IGNORE (type, i)) |
| 318 | { |
| 319 | fputs_filtered ("<optimized out or zero length>", stream); |
| 320 | } |
| 321 | else if (TYPE_FIELD_STATIC (type, i)) |
| 322 | { |
| 323 | value_ptr v = value_static_field (type, i); |
| 324 | if (v == NULL) |
| 325 | fputs_filtered ("<optimized out>", stream); |
| 326 | else |
| 327 | { |
| 328 | struct type *t = check_typedef (VALUE_TYPE (v)); |
| 329 | if (TYPE_CODE (t) == TYPE_CODE_STRUCT) |
| 330 | v = value_addr (v); |
| 331 | val_print (VALUE_TYPE (v), |
| 332 | VALUE_CONTENTS (v), VALUE_ADDRESS (v), |
| 333 | stream, format, 0, recurse+1, pretty); |
| 334 | } |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | val_print (TYPE_FIELD_TYPE (type, i), |
| 339 | valaddr + TYPE_FIELD_BITPOS (type, i) / 8, |
| 340 | address + TYPE_FIELD_BITPOS (type, i) / 8, |
| 341 | stream, format, 0, recurse + 1, pretty); |
| 342 | } |
| 343 | } |
| 344 | annotate_field_end (); |
| 345 | } |
| 346 | |
| 347 | if (pretty) |
| 348 | { |
| 349 | fprintf_filtered (stream, "\n"); |
| 350 | print_spaces_filtered (2 * recurse, stream); |
| 351 | } |
| 352 | } |
| 353 | fprintf_filtered (stream, "}"); |
| 354 | } |
| 355 | |
| 356 | int |
| 357 | java_val_print (type, valaddr, address, stream, format, deref_ref, recurse, |
| 358 | pretty) |
| 359 | struct type *type; |
| 360 | char *valaddr; |
| 361 | CORE_ADDR address; |
| 362 | GDB_FILE *stream; |
| 363 | int format; |
| 364 | int deref_ref; |
| 365 | int recurse; |
| 366 | enum val_prettyprint pretty; |
| 367 | { |
| 368 | register unsigned int i = 0; /* Number of characters printed */ |
| 369 | struct type *elttype; |
| 370 | CORE_ADDR addr; |
| 371 | |
| 372 | CHECK_TYPEDEF (type); |
| 373 | switch (TYPE_CODE (type)) |
| 374 | { |
| 375 | case TYPE_CODE_PTR: |
| 376 | if (format && format != 's') |
| 377 | { |
| 378 | print_scalar_formatted (valaddr, type, format, 0, stream); |
| 379 | break; |
| 380 | } |
| 381 | #if 0 |
| 382 | if (vtblprint && cp_is_vtbl_ptr_type(type)) |
| 383 | { |
| 384 | /* Print the unmangled name if desired. */ |
| 385 | /* Print vtable entry - we only get here if we ARE using |
| 386 | -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */ |
| 387 | print_address_demangle(extract_address (valaddr, TYPE_LENGTH (type)), |
| 388 | stream, demangle); |
| 389 | break; |
| 390 | } |
| 391 | #endif |
| 392 | addr = unpack_pointer (type, valaddr); |
| 393 | if (addr == 0) |
| 394 | { |
| 395 | fputs_filtered ("null", stream); |
| 396 | return i; |
| 397 | } |
| 398 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); |
| 399 | { |
| 400 | print_unpacked_pointer: |
| 401 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); |
| 402 | |
| 403 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) |
| 404 | { |
| 405 | /* Try to print what function it points to. */ |
| 406 | print_address_demangle (addr, stream, demangle); |
| 407 | /* Return value is irrelevant except for string pointers. */ |
| 408 | return (0); |
| 409 | } |
| 410 | |
| 411 | if (addressprint && format != 's') |
| 412 | { |
| 413 | fputs_filtered ("@", stream); |
| 414 | print_longest (stream, 'x', 0, (ULONGEST) addr); |
| 415 | } |
| 416 | return i; |
| 417 | } |
| 418 | case TYPE_CODE_CHAR: |
| 419 | format = format ? format : output_format; |
| 420 | if (format) |
| 421 | { |
| 422 | print_scalar_formatted (valaddr, type, format, 0, stream); |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | LA_PRINT_CHAR ((int) unpack_long (type, valaddr), stream); |
| 427 | } |
| 428 | break; |
| 429 | case TYPE_CODE_STRUCT: |
| 430 | java_print_value_fields (type, valaddr, address, stream, format, |
| 431 | recurse, pretty); |
| 432 | break; |
| 433 | default: |
| 434 | return c_val_print (type, valaddr, address, stream, format, |
| 435 | deref_ref, recurse, pretty); |
| 436 | } |
| 437 | } |