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