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