Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Support for printing Java values for GDB, the GNU debugger. |
1e4728e7 AC |
2 | |
3 | Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free | |
4 | Software Foundation, Inc. | |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
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. | |
c906108c | 12 | |
c5aa993b JM |
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. | |
c906108c | 17 | |
c5aa993b JM |
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. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
24 | #include "symtab.h" | |
25 | #include "gdbtypes.h" | |
7a292a7a | 26 | #include "gdbcore.h" |
c906108c SS |
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" | |
7a292a7a | 34 | #include "annotate.h" |
309367d4 | 35 | #include "gdb_string.h" |
c906108c | 36 | |
392a587b JM |
37 | /* Local functions */ |
38 | ||
d9fcf2fb JM |
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); | |
392a587b JM |
44 | |
45 | ||
c906108c | 46 | int |
75c9979e | 47 | java_value_print (struct value *val, struct ui_file *stream, int format, |
fba45db2 | 48 | enum val_prettyprint pretty) |
c906108c SS |
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 | ||
c5aa993b | 75 | if (TYPE_CODE (type) == TYPE_CODE_PTR && !value_logical_not (val)) |
c906108c SS |
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 | |
c5aa993b | 80 | && (i = strlen (name), name[i - 1] == ']')) |
c906108c SS |
81 | { |
82 | char buf4[4]; | |
83 | long length; | |
84 | unsigned int things_printed = 0; | |
c5aa993b | 85 | int reps; |
c906108c SS |
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 | { | |
c65ecaf3 AC |
96 | CORE_ADDR element; |
97 | CORE_ADDR next_element = -1; /* dummy initial value */ | |
c906108c | 98 | |
c5aa993b | 99 | address += JAVA_OBJECT_SIZE + 4; /* Skip object header and length. */ |
c906108c SS |
100 | |
101 | while (i < length && things_printed < print_max) | |
102 | { | |
35fc8285 | 103 | char *buf; |
c906108c | 104 | |
35fc8285 | 105 | buf = alloca (TARGET_PTR_BIT / HOST_CHAR_BIT); |
c906108c SS |
106 | fputs_filtered (", ", stream); |
107 | wrap_here (n_spaces (2)); | |
108 | ||
109 | if (i > 0) | |
110 | element = next_element; | |
111 | else | |
112 | { | |
c5aa993b | 113 | read_memory (address, buf, sizeof (buf)); |
c906108c | 114 | address += TARGET_PTR_BIT / HOST_CHAR_BIT; |
b276f1bb AC |
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)); | |
c906108c SS |
120 | } |
121 | ||
c5aa993b | 122 | for (reps = 1; i + reps < length; reps++) |
c906108c | 123 | { |
c5aa993b | 124 | read_memory (address, buf, sizeof (buf)); |
c906108c | 125 | address += TARGET_PTR_BIT / HOST_CHAR_BIT; |
b276f1bb AC |
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)); | |
c906108c SS |
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 | |
d4f3574e | 143 | fprintf_filtered (stream, "@%s", paddr_nz (element)); |
c906108c SS |
144 | |
145 | things_printed++; | |
146 | i += reps; | |
147 | } | |
148 | } | |
149 | else | |
150 | { | |
75c9979e AC |
151 | struct value *v = allocate_value (el_type); |
152 | struct value *next_v = allocate_value (el_type); | |
c906108c SS |
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 | { | |
75c9979e | 164 | struct value *tmp; |
c906108c SS |
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 | ||
c5aa993b | 178 | for (reps = 1; i + reps < length; reps++) |
c906108c SS |
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) | |
d4cad8db TT |
212 | && TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)) |
213 | && strcmp (TYPE_TAG_NAME (TYPE_TARGET_TYPE (type)), | |
214 | "java.lang.String") == 0 | |
c906108c | 215 | && (format == 0 || format == 's') |
8dccf761 | 216 | && address != 0 |
1aa20aa8 | 217 | && value_as_address (val) != 0) |
c906108c | 218 | { |
75c9979e | 219 | struct value *data_val; |
c906108c | 220 | CORE_ADDR data; |
75c9979e | 221 | struct value *boffset_val; |
c906108c | 222 | unsigned long boffset; |
75c9979e | 223 | struct value *count_val; |
c906108c | 224 | unsigned long count; |
75c9979e | 225 | struct value *mark; |
c906108c SS |
226 | |
227 | mark = value_mark (); /* Remember start of new values */ | |
228 | ||
229 | data_val = value_struct_elt (&val, NULL, "data", NULL, NULL); | |
1aa20aa8 | 230 | data = value_as_address (data_val); |
c906108c SS |
231 | |
232 | boffset_val = value_struct_elt (&val, NULL, "boffset", NULL, NULL); | |
1aa20aa8 | 233 | boffset = value_as_address (boffset_val); |
c906108c SS |
234 | |
235 | count_val = value_struct_elt (&val, NULL, "count", NULL, NULL); | |
1aa20aa8 | 236 | count = value_as_address (count_val); |
c906108c | 237 | |
c5aa993b | 238 | value_free_to_mark (mark); /* Release unnecessary values */ |
c906108c SS |
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 | ||
392a587b | 255 | static void |
fba45db2 KB |
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) | |
c906108c SS |
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; | |
c5aa993b | 278 | |
c906108c SS |
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"); | |
c5aa993b | 290 | print_spaces_filtered (2 * (recurse + 1), stream); |
c906108c SS |
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, | |
c5aa993b | 301 | stream, format, recurse + 1, pretty); |
c906108c | 302 | fputs_filtered (", ", stream); |
c906108c SS |
303 | } |
304 | ||
305 | } | |
306 | ||
307 | if (!len && n_baseclasses == 1) | |
308 | fprintf_filtered (stream, "<No data fields>"); | |
309 | else | |
310 | { | |
c906108c SS |
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 | } | |
c5aa993b | 344 | else |
c906108c SS |
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 | { | |
75c9979e | 381 | struct value *v; |
c906108c SS |
382 | |
383 | /* Bitfields require special handling, especially due to byte | |
c5aa993b | 384 | order problems. */ |
c906108c SS |
385 | if (TYPE_FIELD_IGNORE (type, i)) |
386 | { | |
c5aa993b | 387 | fputs_filtered ("<optimized out or zero length>", stream); |
c906108c SS |
388 | } |
389 | else | |
390 | { | |
c5aa993b | 391 | v = value_from_longest (TYPE_FIELD_TYPE (type, i), |
c906108c SS |
392 | unpack_field_as_long (type, valaddr, i)); |
393 | ||
c5aa993b JM |
394 | val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, |
395 | 0, stream, format, 0, recurse + 1, pretty); | |
c906108c SS |
396 | } |
397 | } | |
398 | else | |
399 | { | |
400 | if (TYPE_FIELD_IGNORE (type, i)) | |
401 | { | |
c5aa993b | 402 | fputs_filtered ("<optimized out or zero length>", stream); |
c906108c SS |
403 | } |
404 | else if (TYPE_FIELD_STATIC (type, i)) | |
405 | { | |
75c9979e | 406 | struct value *v = value_static_field (type, i); |
c906108c SS |
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), | |
c5aa993b | 416 | stream, format, 0, recurse + 1, pretty); |
c906108c SS |
417 | } |
418 | } | |
7a292a7a SS |
419 | else if (TYPE_FIELD_TYPE (type, i) == NULL) |
420 | fputs_filtered ("<unknown type>", stream); | |
c906108c SS |
421 | else |
422 | { | |
c5aa993b JM |
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); | |
c906108c SS |
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 | |
fba45db2 KB |
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) | |
c906108c | 458 | { |
52f0bd74 | 459 | unsigned int i = 0; /* Number of characters printed */ |
c906108c SS |
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 | |
c5aa993b | 473 | if (vtblprint && cp_is_vtbl_ptr_type (type)) |
c906108c | 474 | { |
c5aa993b | 475 | /* Print the unmangled name if desired. */ |
c906108c SS |
476 | /* Print vtable entry - we only get here if we ARE using |
477 | -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */ | |
b276f1bb AC |
478 | /* Extract an address, assume that it is unsigned. */ |
479 | print_address_demangle (extract_unsigned_integer (valaddr, TYPE_LENGTH (type)), | |
c5aa993b | 480 | stream, demangle); |
c906108c SS |
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: | |
c906108c | 509 | case TYPE_CODE_INT: |
c55a3f73 TT |
510 | /* Can't just call c_val_print because that prints bytes as C |
511 | chars. */ | |
c906108c SS |
512 | format = format ? format : output_format; |
513 | if (format) | |
514 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
c55a3f73 TT |
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); | |
c906108c SS |
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 | } |