]>
Commit | Line | Data |
---|---|---|
a8a69e63 | 1 | /* Support for printing C values for GDB, the GNU debugger. |
67e9b3b3 PS |
2 | Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994 |
3 | Free Software Foundation, Inc. | |
a8a69e63 FF |
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., 675 Mass Ave, Cambridge, MA 02139, 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 | ||
30 | /* BEGIN-FIXME */ | |
31 | ||
32 | extern int vtblprint; /* Controls printing of vtbl's */ | |
33 | extern int demangle; /* whether to print C++ syms raw or src-form */ | |
34 | ||
35 | extern void | |
199b2450 | 36 | cp_print_class_member PARAMS ((char *, struct type *, GDB_FILE *, char *)); |
a8a69e63 | 37 | |
c7da3ed3 | 38 | extern void |
199b2450 | 39 | cp_print_class_method PARAMS ((char *, struct type *, GDB_FILE *)); |
a8a69e63 FF |
40 | |
41 | extern void | |
199b2450 | 42 | cp_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int, |
a8a69e63 FF |
43 | enum val_prettyprint, struct type **)); |
44 | ||
c7da3ed3 FF |
45 | extern int |
46 | cp_is_vtbl_ptr_type PARAMS ((struct type *)); | |
47 | ||
a8a69e63 FF |
48 | extern int |
49 | cp_is_vtbl_member PARAMS ((struct type *)); | |
50 | ||
51 | /* END-FIXME */ | |
52 | ||
53 | ||
54 | /* BEGIN-FIXME: Hooks into c-typeprint.c */ | |
55 | ||
56 | extern void | |
199b2450 | 57 | c_type_print_varspec_prefix PARAMS ((struct type *, GDB_FILE *, int, int)); |
a8a69e63 FF |
58 | |
59 | extern void | |
60 | cp_type_print_method_args PARAMS ((struct type **, char *, char *, int, | |
199b2450 | 61 | GDB_FILE *)); |
a8a69e63 FF |
62 | /* END-FIXME */ |
63 | ||
64 | ||
65 | extern struct obstack dont_print_obstack; | |
66 | ||
67 | \f | |
68 | /* Print data of type TYPE located at VALADDR (within GDB), which came from | |
69 | the inferior at address ADDRESS, onto stdio stream STREAM according to | |
70 | FORMAT (a letter or 0 for natural format). The data at VALADDR is in | |
71 | target byte order. | |
72 | ||
73 | If the data are a string pointer, returns the number of string characters | |
74 | printed. | |
75 | ||
76 | If DEREF_REF is nonzero, then dereference references, otherwise just print | |
77 | them like pointers. | |
78 | ||
79 | The PRETTY parameter controls prettyprinting. */ | |
80 | ||
81 | int | |
82 | c_val_print (type, valaddr, address, stream, format, deref_ref, recurse, | |
83 | pretty) | |
84 | struct type *type; | |
85 | char *valaddr; | |
86 | CORE_ADDR address; | |
199b2450 | 87 | GDB_FILE *stream; |
a8a69e63 FF |
88 | int format; |
89 | int deref_ref; | |
90 | int recurse; | |
91 | enum val_prettyprint pretty; | |
92 | { | |
c4413e2c | 93 | register unsigned int i = 0; /* Number of characters printed */ |
a8a69e63 FF |
94 | unsigned len; |
95 | struct type *elttype; | |
96 | unsigned eltlen; | |
97 | LONGEST val; | |
c4413e2c | 98 | CORE_ADDR addr; |
a8a69e63 FF |
99 | |
100 | switch (TYPE_CODE (type)) | |
101 | { | |
102 | case TYPE_CODE_ARRAY: | |
103 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0) | |
104 | { | |
105 | elttype = TYPE_TARGET_TYPE (type); | |
106 | eltlen = TYPE_LENGTH (elttype); | |
107 | len = TYPE_LENGTH (type) / eltlen; | |
108 | if (prettyprint_arrays) | |
109 | { | |
110 | print_spaces_filtered (2 + 2 * recurse, stream); | |
111 | } | |
a8a69e63 FF |
112 | /* For an array of chars, print with string syntax. */ |
113 | if (eltlen == 1 && TYPE_CODE (elttype) == TYPE_CODE_INT | |
c4413e2c | 114 | && (format == 0 || format == 's')) |
a8a69e63 FF |
115 | { |
116 | LA_PRINT_STRING (stream, valaddr, len, 0); | |
ec16f701 | 117 | i = len; |
a8a69e63 FF |
118 | } |
119 | else | |
120 | { | |
c4413e2c | 121 | fprintf_filtered (stream, "{"); |
a8a69e63 FF |
122 | /* If this is a virtual function table, print the 0th |
123 | entry specially, and the rest of the members normally. */ | |
124 | if (cp_is_vtbl_ptr_type (elttype)) | |
125 | { | |
126 | i = 1; | |
127 | fprintf_filtered (stream, "%d vtable entries", len - 1); | |
128 | } | |
129 | else | |
130 | { | |
131 | i = 0; | |
132 | } | |
133 | val_print_array_elements (type, valaddr, address, stream, | |
134 | format, deref_ref, recurse, pretty, i); | |
c4413e2c | 135 | fprintf_filtered (stream, "}"); |
a8a69e63 | 136 | } |
a8a69e63 FF |
137 | break; |
138 | } | |
139 | /* Array of unspecified length: treat like pointer to first elt. */ | |
1326dace JK |
140 | addr = address; |
141 | goto print_unpacked_pointer; | |
a8a69e63 FF |
142 | |
143 | case TYPE_CODE_PTR: | |
144 | if (format && format != 's') | |
145 | { | |
146 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
147 | break; | |
148 | } | |
36a2283d PB |
149 | if (vtblprint && cp_is_vtbl_ptr_type(type)) |
150 | { | |
151 | /* Print the unmangled name if desired. */ | |
152 | /* Print vtable entry - we only get here if we ARE using | |
153 | -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */ | |
154 | print_address_demangle(extract_address (valaddr, TYPE_LENGTH (type)), | |
155 | stream, demangle); | |
156 | break; | |
157 | } | |
a8a69e63 FF |
158 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD) |
159 | { | |
c7da3ed3 | 160 | cp_print_class_method (valaddr, type, stream); |
a8a69e63 FF |
161 | } |
162 | else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER) | |
163 | { | |
164 | cp_print_class_member (valaddr, | |
165 | TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)), | |
166 | stream, "&"); | |
167 | } | |
168 | else | |
169 | { | |
c4413e2c | 170 | addr = unpack_pointer (type, valaddr); |
1326dace | 171 | print_unpacked_pointer: |
a8a69e63 FF |
172 | elttype = TYPE_TARGET_TYPE (type); |
173 | ||
174 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) | |
175 | { | |
176 | /* Try to print what function it points to. */ | |
177 | print_address_demangle (addr, stream, demangle); | |
178 | /* Return value is irrelevant except for string pointers. */ | |
179 | return (0); | |
180 | } | |
181 | ||
182 | if (addressprint && format != 's') | |
183 | { | |
d24c0599 | 184 | print_address_numeric (addr, 1, stream); |
a8a69e63 FF |
185 | } |
186 | ||
187 | /* For a pointer to char or unsigned char, also print the string | |
188 | pointed to, unless pointer is null. */ | |
c4413e2c FF |
189 | if (TYPE_LENGTH (elttype) == 1 |
190 | && TYPE_CODE (elttype) == TYPE_CODE_INT | |
191 | && (format == 0 || format == 's') | |
192 | && addr != 0) | |
a8a69e63 | 193 | { |
c4413e2c | 194 | i = val_print_string (addr, 0, stream); |
a8a69e63 FF |
195 | } |
196 | else if (cp_is_vtbl_member(type)) | |
197 | { | |
198 | /* print vtbl's nicely */ | |
199 | CORE_ADDR vt_address = unpack_pointer (type, valaddr); | |
200 | ||
201 | struct minimal_symbol *msymbol = | |
202 | lookup_minimal_symbol_by_pc (vt_address); | |
2e4964ad FF |
203 | if ((msymbol != NULL) && |
204 | (vt_address == SYMBOL_VALUE_ADDRESS (msymbol))) | |
a8a69e63 FF |
205 | { |
206 | fputs_filtered (" <", stream); | |
2e4964ad | 207 | fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream); |
a8a69e63 FF |
208 | fputs_filtered (">", stream); |
209 | } | |
40f24469 | 210 | if (vt_address && vtblprint) |
a8a69e63 | 211 | { |
82a2edfb | 212 | value_ptr vt_val; |
1bd97ba8 KH |
213 | struct symbol *wsym = (struct symbol *)NULL; |
214 | struct type *wtype; | |
215 | struct symtab *s; | |
216 | struct block *block = (struct block *)NULL; | |
217 | int is_this_fld; | |
218 | ||
40f24469 KH |
219 | if (msymbol != NULL) |
220 | wsym = lookup_symbol (SYMBOL_NAME(msymbol), block, | |
1bd97ba8 KH |
221 | VAR_NAMESPACE, &is_this_fld, &s); |
222 | ||
223 | if (wsym) | |
224 | { | |
225 | wtype = SYMBOL_TYPE(wsym); | |
226 | } | |
227 | else | |
228 | { | |
229 | wtype = TYPE_TARGET_TYPE(type); | |
230 | } | |
231 | vt_val = value_at (wtype, vt_address); | |
a8a69e63 FF |
232 | val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val), |
233 | VALUE_ADDRESS (vt_val), stream, format, | |
234 | deref_ref, recurse + 1, pretty); | |
235 | if (pretty) | |
236 | { | |
237 | fprintf_filtered (stream, "\n"); | |
238 | print_spaces_filtered (2 + 2 * recurse, stream); | |
239 | } | |
240 | } | |
241 | } | |
242 | ||
79f3d586 JK |
243 | /* Return number of characters printed, including the terminating |
244 | '\0' if we reached the end. val_print_string takes care including | |
245 | the terminating '\0' if necessary. */ | |
246 | return i; | |
a8a69e63 FF |
247 | } |
248 | break; | |
249 | ||
250 | case TYPE_CODE_MEMBER: | |
251 | error ("not implemented: member type in c_val_print"); | |
252 | break; | |
253 | ||
254 | case TYPE_CODE_REF: | |
255 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER) | |
256 | { | |
257 | cp_print_class_member (valaddr, | |
258 | TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)), | |
259 | stream, ""); | |
260 | break; | |
261 | } | |
262 | if (addressprint) | |
263 | { | |
833e0d94 JK |
264 | fprintf_filtered (stream, "@"); |
265 | print_address_numeric | |
266 | (extract_address (valaddr, | |
d24c0599 | 267 | TARGET_PTR_BIT / HOST_CHAR_BIT), 1, stream); |
a8a69e63 FF |
268 | if (deref_ref) |
269 | fputs_filtered (": ", stream); | |
270 | } | |
271 | /* De-reference the reference. */ | |
272 | if (deref_ref) | |
273 | { | |
274 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF) | |
275 | { | |
82a2edfb | 276 | value_ptr deref_val = |
a8a69e63 FF |
277 | value_at |
278 | (TYPE_TARGET_TYPE (type), | |
279 | unpack_pointer (lookup_pointer_type (builtin_type_void), | |
280 | valaddr)); | |
281 | val_print (VALUE_TYPE (deref_val), | |
282 | VALUE_CONTENTS (deref_val), | |
283 | VALUE_ADDRESS (deref_val), stream, format, | |
284 | deref_ref, recurse + 1, pretty); | |
285 | } | |
286 | else | |
287 | fputs_filtered ("???", stream); | |
288 | } | |
289 | break; | |
290 | ||
291 | case TYPE_CODE_UNION: | |
292 | if (recurse && !unionprint) | |
293 | { | |
294 | fprintf_filtered (stream, "{...}"); | |
295 | break; | |
296 | } | |
297 | /* Fall through. */ | |
298 | case TYPE_CODE_STRUCT: | |
299 | if (vtblprint && cp_is_vtbl_ptr_type(type)) | |
300 | { | |
301 | /* Print the unmangled name if desired. */ | |
36a2283d PB |
302 | /* Print vtable entry - we only get here if NOT using |
303 | -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */ | |
a8a69e63 FF |
304 | print_address_demangle(*((int *) (valaddr + /* FIXME bytesex */ |
305 | TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8)), | |
306 | stream, demangle); | |
307 | break; | |
308 | } | |
309 | cp_print_value_fields (type, valaddr, stream, format, recurse, pretty, | |
310 | 0); | |
311 | break; | |
312 | ||
313 | case TYPE_CODE_ENUM: | |
314 | if (format) | |
315 | { | |
316 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
317 | break; | |
318 | } | |
319 | len = TYPE_NFIELDS (type); | |
1d9449ab | 320 | val = unpack_long (type, valaddr); |
a8a69e63 FF |
321 | for (i = 0; i < len; i++) |
322 | { | |
323 | QUIT; | |
324 | if (val == TYPE_FIELD_BITPOS (type, i)) | |
325 | { | |
326 | break; | |
327 | } | |
328 | } | |
329 | if (i < len) | |
330 | { | |
331 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
332 | } | |
333 | else | |
334 | { | |
7efb57c3 | 335 | print_longest (stream, 'd', 0, val); |
a8a69e63 FF |
336 | } |
337 | break; | |
338 | ||
339 | case TYPE_CODE_FUNC: | |
340 | if (format) | |
341 | { | |
342 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
343 | break; | |
344 | } | |
345 | /* FIXME, we should consider, at least for ANSI C language, eliminating | |
346 | the distinction made between FUNCs and POINTERs to FUNCs. */ | |
347 | fprintf_filtered (stream, "{"); | |
348 | type_print (type, "", stream, -1); | |
349 | fprintf_filtered (stream, "} "); | |
350 | /* Try to print what function it points to, and its address. */ | |
351 | print_address_demangle (address, stream, demangle); | |
352 | break; | |
353 | ||
7e71985c JK |
354 | case TYPE_CODE_BOOL: |
355 | /* Do something at least vaguely reasonable, for example if the | |
356 | language is set wrong. */ | |
357 | ||
85b8aa88 JK |
358 | case TYPE_CODE_RANGE: |
359 | /* FIXME: create_range_type does not set the unsigned bit in a | |
360 | range type (I think it probably should copy it from the target | |
361 | type), so we won't print values which are too large to | |
362 | fit in a signed integer correctly. */ | |
363 | /* FIXME: Doesn't handle ranges of enums correctly. (Can't just | |
364 | print with the target type, though, because the size of our type | |
365 | and the target type might differ). */ | |
366 | /* FALLTHROUGH */ | |
367 | ||
a8a69e63 FF |
368 | case TYPE_CODE_INT: |
369 | format = format ? format : output_format; | |
370 | if (format) | |
371 | { | |
372 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
373 | } | |
374 | else | |
375 | { | |
376 | val_print_type_code_int (type, valaddr, stream); | |
377 | /* C and C++ has no single byte int type, char is used instead. | |
378 | Since we don't know whether the value is really intended to | |
379 | be used as an integer or a character, print the character | |
380 | equivalent as well. */ | |
381 | if (TYPE_LENGTH (type) == 1) | |
382 | { | |
383 | fputs_filtered (" ", stream); | |
384 | LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr), | |
385 | stream); | |
386 | } | |
387 | } | |
388 | break; | |
389 | ||
390 | case TYPE_CODE_CHAR: | |
391 | format = format ? format : output_format; | |
392 | if (format) | |
393 | { | |
394 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
395 | } | |
396 | else | |
397 | { | |
398 | fprintf_filtered (stream, TYPE_UNSIGNED (type) ? "%u" : "%d", | |
399 | unpack_long (type, valaddr)); | |
400 | fputs_filtered (" ", stream); | |
401 | LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr), stream); | |
402 | } | |
403 | break; | |
404 | ||
405 | case TYPE_CODE_FLT: | |
406 | if (format) | |
407 | { | |
408 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
409 | } | |
410 | else | |
411 | { | |
412 | print_floating (valaddr, type, stream); | |
413 | } | |
414 | break; | |
415 | ||
416 | case TYPE_CODE_VOID: | |
417 | fprintf_filtered (stream, "void"); | |
418 | break; | |
419 | ||
420 | case TYPE_CODE_ERROR: | |
421 | fprintf_filtered (stream, "<error type>"); | |
422 | break; | |
423 | ||
a8a69e63 FF |
424 | case TYPE_CODE_UNDEF: |
425 | /* This happens (without TYPE_FLAG_STUB set) on systems which don't use | |
426 | dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" | |
427 | and no complete type for struct foo in that file. */ | |
428 | fprintf_filtered (stream, "<incomplete type>"); | |
429 | break; | |
430 | ||
431 | default: | |
432 | error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type)); | |
433 | } | |
199b2450 | 434 | gdb_flush (stream); |
a8a69e63 FF |
435 | return (0); |
436 | } | |
e10cfcaa PB |
437 | \f |
438 | int | |
439 | c_value_print (val, stream, format, pretty) | |
440 | value_ptr val; | |
441 | GDB_FILE *stream; | |
442 | int format; | |
443 | enum val_prettyprint pretty; | |
444 | { | |
445 | /* A "repeated" value really contains several values in a row. | |
446 | They are made by the @ operator. | |
447 | Print such values as if they were arrays. */ | |
448 | ||
449 | if (VALUE_REPEATED (val)) | |
450 | { | |
451 | register unsigned int n = VALUE_REPETITIONS (val); | |
452 | register unsigned int typelen = TYPE_LENGTH (VALUE_TYPE (val)); | |
453 | fprintf_filtered (stream, "{"); | |
454 | /* Print arrays of characters using string syntax. */ | |
455 | if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT | |
456 | && format == 0) | |
457 | LA_PRINT_STRING (stream, VALUE_CONTENTS (val), n, 0); | |
458 | else | |
459 | { | |
460 | value_print_array_elements (val, stream, format, pretty); | |
461 | } | |
462 | fprintf_filtered (stream, "}"); | |
463 | return (n * typelen); | |
464 | } | |
465 | else | |
466 | { | |
467 | struct type *type = VALUE_TYPE (val); | |
468 | ||
469 | /* If it is a pointer, indicate what it points to. | |
470 | ||
471 | Print type also if it is a reference. | |
472 | ||
473 | C++: if it is a member pointer, we will take care | |
474 | of that when we print it. */ | |
475 | if (TYPE_CODE (type) == TYPE_CODE_PTR || | |
476 | TYPE_CODE (type) == TYPE_CODE_REF) | |
477 | { | |
478 | /* Hack: remove (char *) for char strings. Their | |
479 | type is indicated by the quoted string anyway. */ | |
480 | if (TYPE_CODE (type) == TYPE_CODE_PTR && | |
481 | TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == sizeof(char) && | |
482 | TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_INT && | |
483 | !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type))) | |
484 | { | |
485 | /* Print nothing */ | |
486 | } | |
487 | else | |
488 | { | |
489 | fprintf_filtered (stream, "("); | |
490 | type_print (type, "", stream, -1); | |
491 | fprintf_filtered (stream, ") "); | |
492 | } | |
493 | } | |
494 | return (val_print (type, VALUE_CONTENTS (val), | |
495 | VALUE_ADDRESS (val), stream, format, 1, 0, pretty)); | |
496 | } | |
497 | } |