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