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