* A few more improvements to gx jit prototype.
[deliverable/binutils-gdb.git] / gdb / c-valprint.c
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, address, stream, format, deref_ref, recurse,
47 pretty)
48 struct type *type;
49 char *valaddr;
50 CORE_ADDR address;
51 GDB_FILE *stream;
52 int format;
53 int deref_ref;
54 int recurse;
55 enum val_prettyprint pretty;
56 {
57 register unsigned int i = 0; /* Number of characters printed */
58 unsigned len;
59 struct type *elttype;
60 unsigned eltlen;
61 LONGEST val;
62 CORE_ADDR addr;
63
64 CHECK_TYPEDEF (type);
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 {
70 elttype = check_typedef (TYPE_TARGET_TYPE (type));
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 }
77 /* For an array of chars, print with string syntax. */
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)))
82 && (format == 0 || format == 's'))
83 {
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
98 LA_PRINT_STRING (stream, valaddr, len, eltlen, 0);
99 i = len;
100 }
101 else
102 {
103 fprintf_filtered (stream, "{");
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);
117 fprintf_filtered (stream, "}");
118 }
119 break;
120 }
121 /* Array of unspecified length: treat like pointer to first elt. */
122 addr = address;
123 goto print_unpacked_pointer;
124
125 case TYPE_CODE_PTR:
126 if (format && format != 's')
127 {
128 print_scalar_formatted (valaddr, type, format, 0, stream);
129 break;
130 }
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 }
140 elttype = check_typedef (TYPE_TARGET_TYPE (type));
141 if (TYPE_CODE (elttype) == TYPE_CODE_METHOD)
142 {
143 cp_print_class_method (valaddr, type, stream);
144 }
145 else if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
146 {
147 cp_print_class_member (valaddr,
148 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)),
149 stream, "&");
150 }
151 else
152 {
153 addr = unpack_pointer (type, valaddr);
154 print_unpacked_pointer:
155 elttype = check_typedef (TYPE_TARGET_TYPE (type));
156
157 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
158 {
159 /* Try to print what function it points to. */
160 print_address_demangle (addr, stream, demangle);
161 /* Return value is irrelevant except for string pointers. */
162 return (0);
163 }
164
165 if (addressprint && format != 's')
166 {
167 print_address_numeric (addr, 1, stream);
168 }
169
170 /* For a pointer to char or unsigned char, also print the string
171 pointed to, unless pointer is null. */
172 /* FIXME: need to handle wchar_t here... */
173
174 if (TYPE_LENGTH (elttype) == 1
175 && TYPE_CODE (elttype) == TYPE_CODE_INT
176 && (format == 0 || format == 's')
177 && addr != 0)
178 {
179 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
180 }
181 else if (cp_is_vtbl_member(type))
182 {
183 /* print vtbl's nicely */
184 CORE_ADDR vt_address = unpack_pointer (type, valaddr);
185
186 struct minimal_symbol *msymbol =
187 lookup_minimal_symbol_by_pc (vt_address);
188 if ((msymbol != NULL) &&
189 (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
190 {
191 fputs_filtered (" <", stream);
192 fputs_filtered (SYMBOL_SOURCE_NAME (msymbol), stream);
193 fputs_filtered (">", stream);
194 }
195 if (vt_address && vtblprint)
196 {
197 value_ptr vt_val;
198 struct symbol *wsym = (struct symbol *)NULL;
199 struct type *wtype;
200 struct symtab *s;
201 struct block *block = (struct block *)NULL;
202 int is_this_fld;
203
204 if (msymbol != NULL)
205 wsym = lookup_symbol (SYMBOL_NAME(msymbol), block,
206 VAR_NAMESPACE, &is_this_fld, &s);
207
208 if (wsym)
209 {
210 wtype = SYMBOL_TYPE(wsym);
211 }
212 else
213 {
214 wtype = TYPE_TARGET_TYPE(type);
215 }
216 vt_val = value_at (wtype, vt_address, NULL);
217 val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val),
218 VALUE_ADDRESS (vt_val), stream, format,
219 deref_ref, recurse + 1, pretty);
220 if (pretty)
221 {
222 fprintf_filtered (stream, "\n");
223 print_spaces_filtered (2 + 2 * recurse, stream);
224 }
225 }
226 }
227
228 /* Return number of characters printed, including the terminating
229 '\0' if we reached the end. val_print_string takes care including
230 the terminating '\0' if necessary. */
231 return i;
232 }
233 break;
234
235 case TYPE_CODE_MEMBER:
236 error ("not implemented: member type in c_val_print");
237 break;
238
239 case TYPE_CODE_REF:
240 elttype = check_typedef (TYPE_TARGET_TYPE (type));
241 if (TYPE_CODE (elttype) == TYPE_CODE_MEMBER)
242 {
243 cp_print_class_member (valaddr,
244 TYPE_DOMAIN_TYPE (elttype),
245 stream, "");
246 break;
247 }
248 if (addressprint)
249 {
250 fprintf_filtered (stream, "@");
251 print_address_numeric
252 (extract_address (valaddr,
253 TARGET_PTR_BIT / HOST_CHAR_BIT), 1, stream);
254 if (deref_ref)
255 fputs_filtered (": ", stream);
256 }
257 /* De-reference the reference. */
258 if (deref_ref)
259 {
260 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
261 {
262 value_ptr deref_val =
263 value_at
264 (TYPE_TARGET_TYPE (type),
265 unpack_pointer (lookup_pointer_type (builtin_type_void),
266 valaddr),
267 NULL);
268 val_print (VALUE_TYPE (deref_val),
269 VALUE_CONTENTS (deref_val),
270 VALUE_ADDRESS (deref_val), stream, format,
271 deref_ref, recurse + 1, pretty);
272 }
273 else
274 fputs_filtered ("???", stream);
275 }
276 break;
277
278 case TYPE_CODE_UNION:
279 if (recurse && !unionprint)
280 {
281 fprintf_filtered (stream, "{...}");
282 break;
283 }
284 /* Fall through. */
285 case TYPE_CODE_STRUCT:
286 if (vtblprint && cp_is_vtbl_ptr_type(type))
287 {
288 /* Print the unmangled name if desired. */
289 /* Print vtable entry - we only get here if NOT using
290 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
291 print_address_demangle (extract_address (
292 valaddr + TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8,
293 TYPE_LENGTH (TYPE_FIELD_TYPE (type, VTBL_FNADDR_OFFSET))),
294 stream, demangle);
295 }
296 else
297 cp_print_value_fields (type, valaddr, address, stream, format,
298 recurse, pretty, NULL, 0);
299 break;
300
301 case TYPE_CODE_ENUM:
302 if (format)
303 {
304 print_scalar_formatted (valaddr, type, format, 0, stream);
305 break;
306 }
307 len = TYPE_NFIELDS (type);
308 val = unpack_long (type, valaddr);
309 for (i = 0; i < len; i++)
310 {
311 QUIT;
312 if (val == TYPE_FIELD_BITPOS (type, i))
313 {
314 break;
315 }
316 }
317 if (i < len)
318 {
319 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
320 }
321 else
322 {
323 print_longest (stream, 'd', 0, val);
324 }
325 break;
326
327 case TYPE_CODE_FUNC:
328 if (format)
329 {
330 print_scalar_formatted (valaddr, type, format, 0, stream);
331 break;
332 }
333 /* FIXME, we should consider, at least for ANSI C language, eliminating
334 the distinction made between FUNCs and POINTERs to FUNCs. */
335 fprintf_filtered (stream, "{");
336 type_print (type, "", stream, -1);
337 fprintf_filtered (stream, "} ");
338 /* Try to print what function it points to, and its address. */
339 print_address_demangle (address, stream, demangle);
340 break;
341
342 case TYPE_CODE_BOOL:
343 format = format ? format : output_format;
344 if (format)
345 print_scalar_formatted (valaddr, type, format, 0, stream);
346 else
347 {
348 val = unpack_long (type, valaddr);
349 if (val == 0)
350 fputs_filtered ("false", stream);
351 else if (val == 1)
352 fputs_filtered ("true", stream);
353 else
354 print_longest (stream, 'd', 0, val);
355 }
356 break;
357
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
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
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 }
434 gdb_flush (stream);
435 return (0);
436 }
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 struct type *type = VALUE_TYPE (val);
446
447 /* If it is a pointer, indicate what it points to.
448
449 Print type also if it is a reference.
450
451 C++: if it is a member pointer, we will take care
452 of that when we print it. */
453 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
454 TYPE_CODE (type) == TYPE_CODE_REF)
455 {
456 /* Hack: remove (char *) for char strings. Their
457 type is indicated by the quoted string anyway. */
458 if (TYPE_CODE (type) == TYPE_CODE_PTR &&
459 TYPE_NAME (type) == NULL &&
460 TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL &&
461 STREQ (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char"))
462 {
463 /* Print nothing */
464 }
465 else
466 {
467 fprintf_filtered (stream, "(");
468 type_print (type, "", stream, -1);
469 fprintf_filtered (stream, ") ");
470 }
471 }
472 return val_print (type, VALUE_CONTENTS (val),
473 VALUE_ADDRESS (val) + VALUE_OFFSET (val),
474 stream, format, 1, 0, pretty);
475 }
This page took 0.038561 seconds and 4 git commands to generate.