Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Support for printing C values for GDB, the GNU debugger. |
2 | Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997 | |
3 | Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "defs.h" | |
22 | #include "symtab.h" | |
23 | #include "gdbtypes.h" | |
24 | #include "expression.h" | |
25 | #include "value.h" | |
26 | #include "demangle.h" | |
27 | #include "valprint.h" | |
28 | #include "language.h" | |
29 | #include "c-lang.h" | |
30 | ||
31 | \f | |
32 | /* Print data of type TYPE located at VALADDR (within GDB), which came from | |
33 | the inferior at address ADDRESS, onto stdio stream STREAM according to | |
34 | FORMAT (a letter or 0 for natural format). The data at VALADDR is in | |
35 | target byte order. | |
36 | ||
37 | If the data are a string pointer, returns the number of string characters | |
38 | printed. | |
39 | ||
40 | If DEREF_REF is nonzero, then dereference references, otherwise just print | |
41 | them like pointers. | |
42 | ||
43 | The PRETTY parameter controls prettyprinting. */ | |
44 | ||
45 | int | |
46 | c_val_print (type, valaddr, embedded_offset, address, stream, format, deref_ref, recurse, | |
47 | pretty) | |
48 | struct type *type; | |
49 | char *valaddr; | |
50 | int embedded_offset; | |
51 | CORE_ADDR address; | |
52 | GDB_FILE *stream; | |
53 | int format; | |
54 | int deref_ref; | |
55 | int recurse; | |
56 | enum val_prettyprint pretty; | |
57 | { | |
58 | register unsigned int i = 0; /* Number of characters printed */ | |
59 | unsigned len; | |
60 | struct type *elttype; | |
61 | unsigned eltlen; | |
62 | LONGEST val; | |
63 | CORE_ADDR addr; | |
64 | ||
65 | CHECK_TYPEDEF (type); | |
66 | switch (TYPE_CODE (type)) | |
67 | { | |
68 | case TYPE_CODE_ARRAY: | |
69 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); | |
70 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0) | |
71 | { | |
72 | eltlen = TYPE_LENGTH (elttype); | |
73 | len = TYPE_LENGTH (type) / eltlen; | |
74 | if (prettyprint_arrays) | |
75 | { | |
76 | print_spaces_filtered (2 + 2 * recurse, stream); | |
77 | } | |
78 | /* For an array of chars, print with string syntax. */ | |
79 | if (eltlen == 1 && | |
80 | ((TYPE_CODE (elttype) == TYPE_CODE_INT) | |
81 | || ((current_language->la_language == language_m2) | |
82 | && (TYPE_CODE (elttype) == TYPE_CODE_CHAR))) | |
83 | && (format == 0 || format == 's')) | |
84 | { | |
85 | /* If requested, look for the first null char and only print | |
86 | elements up to it. */ | |
87 | if (stop_print_at_null) | |
88 | { | |
89 | int temp_len; | |
90 | ||
91 | /* Look for a NULL char. */ | |
92 | for (temp_len = 0; | |
93 | (valaddr + embedded_offset)[temp_len] | |
94 | && temp_len < len && temp_len < print_max; | |
95 | temp_len++); | |
96 | len = temp_len; | |
97 | } | |
98 | ||
99 | LA_PRINT_STRING (stream, valaddr + embedded_offset, len, eltlen, 0); | |
100 | i = len; | |
101 | } | |
102 | else | |
103 | { | |
104 | fprintf_filtered (stream, "{"); | |
105 | /* If this is a virtual function table, print the 0th | |
106 | entry specially, and the rest of the members normally. */ | |
107 | if (cp_is_vtbl_ptr_type (elttype)) | |
108 | { | |
109 | i = 1; | |
110 | fprintf_filtered (stream, "%d vtable entries", len - 1); | |
111 | } | |
112 | else | |
113 | { | |
114 | i = 0; | |
115 | } | |
116 | val_print_array_elements (type, valaddr + embedded_offset, address, stream, | |
117 | format, deref_ref, recurse, pretty, i); | |
118 | fprintf_filtered (stream, "}"); | |
119 | } | |
120 | break; | |
121 | } | |
122 | /* Array of unspecified length: treat like pointer to first elt. */ | |
123 | addr = address; | |
124 | goto print_unpacked_pointer; | |
125 | ||
126 | case TYPE_CODE_PTR: | |
127 | if (format && format != 's') | |
128 | { | |
129 | print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream); | |
130 | break; | |
131 | } | |
132 | if (vtblprint && cp_is_vtbl_ptr_type(type)) | |
133 | { | |
134 | /* Print the unmangled name if desired. */ | |
135 | /* Print vtable entry - we only get here if we ARE using | |
136 | -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */ | |
137 | print_address_demangle(extract_address (valaddr + embedded_offset, TYPE_LENGTH (type)), | |
138 | stream, demangle); | |
139 | break; | |
140 | } | |
141 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); | |
142 | if (TYPE_CODE (elttype) == TYPE_CODE_METHOD) | |
143 | { | |
144 | cp_print_class_method (valaddr + embedded_offset, type, stream); | |
145 | } | |
146 | else if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER) | |
147 | { | |
148 | cp_print_class_member (valaddr + embedded_offset, | |
149 | TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)), | |
150 | stream, "&"); | |
151 | } | |
152 | else | |
153 | { | |
154 | addr = unpack_pointer (type, valaddr + embedded_offset); | |
155 | print_unpacked_pointer: | |
156 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); | |
157 | ||
158 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) | |
159 | { | |
160 | /* Try to print what function it points to. */ | |
161 | print_address_demangle (addr, stream, demangle); | |
162 | /* Return value is irrelevant except for string pointers. */ | |
163 | return (0); | |
164 | } | |
165 | ||
166 | if (addressprint && format != 's') | |
167 | { | |
168 | print_address_numeric (addr, 1, stream); | |
169 | } | |
170 | ||
171 | /* For a pointer to char or unsigned char, also print the string | |
172 | pointed to, unless pointer is null. */ | |
173 | /* FIXME: need to handle wchar_t here... */ | |
174 | ||
175 | if (TYPE_LENGTH (elttype) == 1 | |
176 | && TYPE_CODE (elttype) == TYPE_CODE_INT | |
177 | && (format == 0 || format == 's') | |
178 | && addr != 0) | |
179 | { | |
180 | i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream); | |
181 | } | |
182 | else if (cp_is_vtbl_member(type)) | |
183 | { | |
184 | /* print vtbl's nicely */ | |
185 | CORE_ADDR vt_address = unpack_pointer (type, valaddr + embedded_offset); | |
186 | ||
187 | struct minimal_symbol *msymbol = | |
188 | lookup_minimal_symbol_by_pc (vt_address); | |
189 | if ((msymbol != NULL) && | |
190 | (vt_address == SYMBOL_VALUE_ADDRESS (msymbol))) | |
191 | { | |
192 | fputs_filtered (" <", stream); | |
193 | fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream); | |
194 | fputs_filtered (">", stream); | |
195 | } | |
196 | if (vt_address && vtblprint) | |
197 | { | |
198 | value_ptr vt_val; | |
199 | struct symbol *wsym = (struct symbol *)NULL; | |
200 | struct type *wtype; | |
201 | struct symtab *s; | |
202 | struct block *block = (struct block *)NULL; | |
203 | int is_this_fld; | |
204 | ||
205 | if (msymbol != NULL) | |
206 | wsym = lookup_symbol (SYMBOL_NAME(msymbol), block, | |
207 | VAR_NAMESPACE, &is_this_fld, &s); | |
208 | ||
209 | if (wsym) | |
210 | { | |
211 | wtype = SYMBOL_TYPE(wsym); | |
212 | } | |
213 | else | |
214 | { | |
215 | wtype = TYPE_TARGET_TYPE(type); | |
216 | } | |
217 | vt_val = value_at (wtype, vt_address, NULL); | |
218 | val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val), 0, | |
219 | VALUE_ADDRESS (vt_val), stream, format, | |
220 | deref_ref, recurse + 1, pretty); | |
221 | if (pretty) | |
222 | { | |
223 | fprintf_filtered (stream, "\n"); | |
224 | print_spaces_filtered (2 + 2 * recurse, stream); | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
229 | /* Return number of characters printed, including the terminating | |
230 | '\0' if we reached the end. val_print_string takes care including | |
231 | the terminating '\0' if necessary. */ | |
232 | return i; | |
233 | } | |
234 | break; | |
235 | ||
236 | case TYPE_CODE_MEMBER: | |
237 | error ("not implemented: member type in c_val_print"); | |
238 | break; | |
239 | ||
240 | case TYPE_CODE_REF: | |
241 | elttype = check_typedef (TYPE_TARGET_TYPE (type)); | |
242 | if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER) | |
243 | { | |
244 | cp_print_class_member (valaddr + embedded_offset, | |
245 | TYPE_DOMAIN_TYPE (elttype), | |
246 | stream, ""); | |
247 | break; | |
248 | } | |
249 | if (addressprint) | |
250 | { | |
251 | fprintf_filtered (stream, "@"); | |
252 | print_address_numeric | |
253 | (extract_address (valaddr + embedded_offset, | |
254 | TARGET_PTR_BIT / HOST_CHAR_BIT), 1, stream); | |
255 | if (deref_ref) | |
256 | fputs_filtered (": ", stream); | |
257 | } | |
258 | /* De-reference the reference. */ | |
259 | if (deref_ref) | |
260 | { | |
261 | if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF) | |
262 | { | |
263 | value_ptr deref_val = | |
264 | value_at | |
265 | (TYPE_TARGET_TYPE (type), | |
266 | unpack_pointer (lookup_pointer_type (builtin_type_void), | |
267 | valaddr + embedded_offset), | |
268 | NULL); | |
269 | val_print (VALUE_TYPE (deref_val), | |
270 | VALUE_CONTENTS (deref_val), | |
271 | 0, | |
272 | VALUE_ADDRESS (deref_val), | |
273 | stream, | |
274 | format, | |
275 | deref_ref, | |
276 | recurse, | |
277 | pretty); | |
278 | } | |
279 | else | |
280 | fputs_filtered ("???", stream); | |
281 | } | |
282 | break; | |
283 | ||
284 | case TYPE_CODE_UNION: | |
285 | if (recurse && !unionprint) | |
286 | { | |
287 | fprintf_filtered (stream, "{...}"); | |
288 | break; | |
289 | } | |
290 | /* Fall through. */ | |
291 | case TYPE_CODE_STRUCT: | |
292 | if (vtblprint && cp_is_vtbl_ptr_type(type)) | |
293 | { | |
294 | /* Print the unmangled name if desired. */ | |
295 | /* Print vtable entry - we only get here if NOT using | |
296 | -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */ | |
297 | print_address_demangle (extract_address ( | |
298 | valaddr + embedded_offset + | |
299 | TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8, | |
300 | TYPE_LENGTH (TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET))), | |
301 | stream, demangle); | |
302 | } | |
303 | else | |
304 | cp_print_value_fields (type, type, valaddr, embedded_offset, address, stream, format, | |
305 | recurse, pretty, NULL, 0); | |
306 | break; | |
307 | ||
308 | case TYPE_CODE_ENUM: | |
309 | if (format) | |
310 | { | |
311 | print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream); | |
312 | break; | |
313 | } | |
314 | len = TYPE_NFIELDS (type); | |
315 | val = unpack_long (type, valaddr + embedded_offset); | |
316 | for (i = 0; i < len; i++) | |
317 | { | |
318 | QUIT; | |
319 | if (val == TYPE_FIELD_BITPOS (type, i)) | |
320 | { | |
321 | break; | |
322 | } | |
323 | } | |
324 | if (i < len) | |
325 | { | |
326 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
327 | } | |
328 | else | |
329 | { | |
330 | print_longest (stream, 'd', 0, val); | |
331 | } | |
332 | break; | |
333 | ||
334 | case TYPE_CODE_FUNC: | |
335 | if (format) | |
336 | { | |
337 | print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream); | |
338 | break; | |
339 | } | |
340 | /* FIXME, we should consider, at least for ANSI C language, eliminating | |
341 | the distinction made between FUNCs and POINTERs to FUNCs. */ | |
342 | fprintf_filtered (stream, "{"); | |
343 | type_print (type, "", stream, -1); | |
344 | fprintf_filtered (stream, "} "); | |
345 | /* Try to print what function it points to, and its address. */ | |
346 | print_address_demangle (address, stream, demangle); | |
347 | break; | |
348 | ||
349 | case TYPE_CODE_BOOL: | |
350 | format = format ? format : output_format; | |
351 | if (format) | |
352 | print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream); | |
353 | else | |
354 | { | |
355 | val = unpack_long (type, valaddr + embedded_offset); | |
356 | if (val == 0) | |
357 | fputs_filtered ("false", stream); | |
358 | else if (val == 1) | |
359 | fputs_filtered ("true", stream); | |
360 | else | |
361 | print_longest (stream, 'd', 0, val); | |
362 | } | |
363 | break; | |
364 | ||
365 | case TYPE_CODE_RANGE: | |
366 | /* FIXME: create_range_type does not set the unsigned bit in a | |
367 | range type (I think it probably should copy it from the target | |
368 | type), so we won't print values which are too large to | |
369 | fit in a signed integer correctly. */ | |
370 | /* FIXME: Doesn't handle ranges of enums correctly. (Can't just | |
371 | print with the target type, though, because the size of our type | |
372 | and the target type might differ). */ | |
373 | /* FALLTHROUGH */ | |
374 | ||
375 | case TYPE_CODE_INT: | |
376 | format = format ? format : output_format; | |
377 | if (format) | |
378 | { | |
379 | print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream); | |
380 | } | |
381 | else | |
382 | { | |
383 | val_print_type_code_int (type, valaddr + embedded_offset, stream); | |
384 | /* C and C++ has no single byte int type, char is used instead. | |
385 | Since we don't know whether the value is really intended to | |
386 | be used as an integer or a character, print the character | |
387 | equivalent as well. */ | |
388 | if (TYPE_LENGTH (type) == 1) | |
389 | { | |
390 | fputs_filtered (" ", stream); | |
391 | LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset), | |
392 | stream); | |
393 | } | |
394 | } | |
395 | break; | |
396 | ||
397 | case TYPE_CODE_CHAR: | |
398 | format = format ? format : output_format; | |
399 | if (format) | |
400 | { | |
401 | print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream); | |
402 | } | |
403 | else | |
404 | { | |
405 | fprintf_filtered (stream, TYPE_UNSIGNED (type) ? "%u" : "%d", | |
406 | unpack_long (type, valaddr + embedded_offset)); | |
407 | fputs_filtered (" ", stream); | |
408 | LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr + embedded_offset), stream); | |
409 | } | |
410 | break; | |
411 | ||
412 | case TYPE_CODE_FLT: | |
413 | if (format) | |
414 | { | |
415 | print_scalar_formatted (valaddr + embedded_offset, type, format, 0, stream); | |
416 | } | |
417 | else | |
418 | { | |
419 | print_floating (valaddr + embedded_offset, type, stream); | |
420 | } | |
421 | break; | |
422 | ||
423 | case TYPE_CODE_METHOD: | |
424 | cp_print_class_method (valaddr + embedded_offset, lookup_pointer_type (type), stream); | |
425 | break; | |
426 | ||
427 | case TYPE_CODE_VOID: | |
428 | fprintf_filtered (stream, "void"); | |
429 | break; | |
430 | ||
431 | case TYPE_CODE_ERROR: | |
432 | fprintf_filtered (stream, "<error type>"); | |
433 | break; | |
434 | ||
435 | case TYPE_CODE_UNDEF: | |
436 | /* This happens (without TYPE_FLAG_STUB set) on systems which don't use | |
437 | dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" | |
438 | and no complete type for struct foo in that file. */ | |
439 | fprintf_filtered (stream, "<incomplete type>"); | |
440 | break; | |
441 | ||
442 | default: | |
443 | error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type)); | |
444 | } | |
445 | gdb_flush (stream); | |
446 | return (0); | |
447 | } | |
448 | \f | |
449 | int | |
450 | c_value_print (val, stream, format, pretty) | |
451 | value_ptr val; | |
452 | GDB_FILE *stream; | |
453 | int format; | |
454 | enum val_prettyprint pretty; | |
455 | { | |
456 | struct type *type = VALUE_TYPE (val); | |
457 | struct type * real_type; | |
458 | int full, top, using_enc; | |
459 | ||
460 | /* If it is a pointer, indicate what it points to. | |
461 | ||
462 | Print type also if it is a reference. | |
463 | ||
464 | C++: if it is a member pointer, we will take care | |
465 | of that when we print it. */ | |
466 | if (TYPE_CODE (type) == TYPE_CODE_PTR || | |
467 | TYPE_CODE (type) == TYPE_CODE_REF) | |
468 | { | |
469 | /* Hack: remove (char *) for char strings. Their | |
470 | type is indicated by the quoted string anyway. */ | |
471 | if (TYPE_CODE (type) == TYPE_CODE_PTR && | |
472 | TYPE_NAME (type) == NULL && | |
473 | TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL && | |
474 | STREQ (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char")) | |
475 | { | |
476 | /* Print nothing */ | |
477 | } | |
478 | else if (objectprint && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS)) | |
479 | { | |
480 | /* Pointer to class, check real type of object */ | |
481 | fprintf_filtered (stream, "("); | |
482 | type = value_rtti_target_type (val, &full, &top, &using_enc); | |
483 | if (type) | |
484 | { | |
485 | /* RTTI entry found */ | |
486 | type = lookup_pointer_type (type); | |
487 | type_print (type, "", stream, -1); | |
488 | } | |
489 | else | |
490 | { | |
491 | /* No RTTI fields, do whatever we can */ | |
492 | type = VALUE_ENCLOSING_TYPE (val); | |
493 | type_print (type, "", stream, -1); | |
494 | fprintf_filtered (stream, " ?"); | |
495 | } | |
496 | fprintf_filtered (stream, ") "); | |
497 | } | |
498 | else | |
499 | { | |
500 | /* normal case */ | |
501 | fprintf_filtered (stream, "("); | |
502 | type_print (type, "", stream, -1); | |
503 | fprintf_filtered (stream, ") "); | |
504 | } | |
505 | } | |
506 | if (objectprint && (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_CLASS)) | |
507 | { | |
508 | /* Attempt to determine real type of object */ | |
509 | real_type = value_rtti_type (val, &full, &top, &using_enc); | |
510 | if (real_type) | |
511 | { | |
512 | /* We have RTTI information, so use it */ | |
513 | val = value_full_object (val, real_type, full, top, using_enc); | |
514 | fprintf_filtered (stream, "(%s%s) ", | |
515 | TYPE_NAME (real_type), | |
516 | full ? "" : " [incomplete object]"); | |
517 | /* Print out object: enclosing type is same as real_type if full */ | |
518 | return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0, | |
519 | VALUE_ADDRESS (val), stream, format, 1, 0, pretty); | |
520 | } | |
521 | else if (type != VALUE_ENCLOSING_TYPE (val)) | |
522 | { | |
523 | /* No RTTI information, so let's do our best */ | |
524 | fprintf_filtered (stream, "(%s ?) ", | |
525 | TYPE_NAME (VALUE_ENCLOSING_TYPE (val))); | |
526 | return val_print (VALUE_ENCLOSING_TYPE (val), VALUE_CONTENTS_ALL (val), 0, | |
527 | VALUE_ADDRESS (val), stream, format, 1, 0, pretty); | |
528 | } | |
529 | /* Otherwise, we end up at the return outside this "if" */ | |
530 | } | |
531 | ||
532 | return val_print (type, VALUE_CONTENTS_ALL (val), VALUE_EMBEDDED_OFFSET (val), | |
533 | VALUE_ADDRESS (val), | |
534 | stream, format, 1, 0, pretty); | |
535 | } |