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