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