Commit | Line | Data |
---|---|---|
a8a69e63 FF |
1 | /* Support for printing C values for GDB, the GNU debugger. |
2 | Copyright 1986, 1988, 1989, 1991 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "symtab.h" | |
22 | #include "gdbtypes.h" | |
23 | #include "expression.h" | |
24 | #include "value.h" | |
25 | #include "demangle.h" | |
26 | #include "valprint.h" | |
27 | #include "language.h" | |
28 | ||
29 | /* BEGIN-FIXME */ | |
30 | ||
31 | extern int vtblprint; /* Controls printing of vtbl's */ | |
32 | extern int demangle; /* whether to print C++ syms raw or src-form */ | |
33 | ||
34 | extern void | |
35 | cp_print_class_member PARAMS ((char *, struct type *, FILE *, char *)); | |
36 | ||
37 | extern int | |
38 | cp_is_vtbl_ptr_type PARAMS ((struct type *)); | |
39 | ||
40 | extern void | |
41 | cp_print_value_fields PARAMS ((struct type *, char *, FILE *, int, int, | |
42 | enum val_prettyprint, struct type **)); | |
43 | ||
44 | extern int | |
45 | cp_is_vtbl_member PARAMS ((struct type *)); | |
46 | ||
47 | /* END-FIXME */ | |
48 | ||
49 | ||
50 | /* BEGIN-FIXME: Hooks into c-typeprint.c */ | |
51 | ||
52 | extern void | |
53 | c_type_print_varspec_prefix PARAMS ((struct type *, FILE *, int, int)); | |
54 | ||
55 | extern void | |
56 | cp_type_print_method_args PARAMS ((struct type **, char *, char *, int, | |
57 | FILE *)); | |
58 | /* END-FIXME */ | |
59 | ||
60 | ||
61 | extern struct obstack dont_print_obstack; | |
62 | ||
63 | \f | |
64 | /* Print data of type TYPE located at VALADDR (within GDB), which came from | |
65 | the inferior at address ADDRESS, onto stdio stream STREAM according to | |
66 | FORMAT (a letter or 0 for natural format). The data at VALADDR is in | |
67 | target byte order. | |
68 | ||
69 | If the data are a string pointer, returns the number of string characters | |
70 | printed. | |
71 | ||
72 | If DEREF_REF is nonzero, then dereference references, otherwise just print | |
73 | them like pointers. | |
74 | ||
75 | The PRETTY parameter controls prettyprinting. */ | |
76 | ||
77 | int | |
78 | c_val_print (type, valaddr, address, stream, format, deref_ref, recurse, | |
79 | pretty) | |
80 | struct type *type; | |
81 | char *valaddr; | |
82 | CORE_ADDR address; | |
83 | FILE *stream; | |
84 | int format; | |
85 | int deref_ref; | |
86 | int recurse; | |
87 | enum val_prettyprint pretty; | |
88 | { | |
89 | register unsigned int i; | |
90 | unsigned len; | |
91 | struct type *elttype; | |
92 | unsigned eltlen; | |
93 | LONGEST val; | |
94 | unsigned char c; | |
95 | ||
96 | switch (TYPE_CODE (type)) | |
97 | { | |
98 | case TYPE_CODE_ARRAY: | |
99 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0) | |
100 | { | |
101 | elttype = TYPE_TARGET_TYPE (type); | |
102 | eltlen = TYPE_LENGTH (elttype); | |
103 | len = TYPE_LENGTH (type) / eltlen; | |
104 | if (prettyprint_arrays) | |
105 | { | |
106 | print_spaces_filtered (2 + 2 * recurse, stream); | |
107 | } | |
108 | fprintf_filtered (stream, "{"); | |
109 | /* For an array of chars, print with string syntax. */ | |
110 | if (eltlen == 1 && TYPE_CODE (elttype) == TYPE_CODE_INT | |
111 | && (format == 0 || format == 's') ) | |
112 | { | |
113 | LA_PRINT_STRING (stream, valaddr, len, 0); | |
114 | } | |
115 | else | |
116 | { | |
117 | /* If this is a virtual function table, print the 0th | |
118 | entry specially, and the rest of the members normally. */ | |
119 | if (cp_is_vtbl_ptr_type (elttype)) | |
120 | { | |
121 | i = 1; | |
122 | fprintf_filtered (stream, "%d vtable entries", len - 1); | |
123 | } | |
124 | else | |
125 | { | |
126 | i = 0; | |
127 | } | |
128 | val_print_array_elements (type, valaddr, address, stream, | |
129 | format, deref_ref, recurse, pretty, i); | |
130 | } | |
131 | fprintf_filtered (stream, "}"); | |
132 | break; | |
133 | } | |
134 | /* Array of unspecified length: treat like pointer to first elt. */ | |
135 | valaddr = (char *) &address; | |
136 | ||
137 | case TYPE_CODE_PTR: | |
138 | if (format && format != 's') | |
139 | { | |
140 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
141 | break; | |
142 | } | |
143 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD) | |
144 | { | |
145 | struct type *domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)); | |
146 | struct fn_field *f; | |
147 | int j, len2; | |
148 | char *kind = ""; | |
149 | CORE_ADDR addr; | |
150 | ||
151 | addr = unpack_pointer (lookup_pointer_type (builtin_type_void), | |
152 | valaddr); | |
153 | if (METHOD_PTR_IS_VIRTUAL(addr)) | |
154 | { | |
155 | int offset = METHOD_PTR_TO_VOFFSET(addr); | |
156 | len = TYPE_NFN_FIELDS (domain); | |
157 | for (i = 0; i < len; i++) | |
158 | { | |
159 | f = TYPE_FN_FIELDLIST1 (domain, i); | |
160 | len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i); | |
161 | ||
162 | for (j = 0; j < len2; j++) | |
163 | { | |
164 | QUIT; | |
165 | if (TYPE_FN_FIELD_VOFFSET (f, j) == offset) | |
166 | { | |
167 | kind = "virtual "; | |
168 | goto common; | |
169 | } | |
170 | } | |
171 | } | |
172 | } | |
173 | else | |
174 | { | |
175 | struct symbol *sym = find_pc_function (addr); | |
176 | if (sym == 0) | |
177 | { | |
178 | error ("invalid pointer to member function"); | |
179 | } | |
180 | len = TYPE_NFN_FIELDS (domain); | |
181 | for (i = 0; i < len; i++) | |
182 | { | |
183 | f = TYPE_FN_FIELDLIST1 (domain, i); | |
184 | len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i); | |
185 | ||
186 | for (j = 0; j < len2; j++) | |
187 | { | |
188 | QUIT; | |
189 | if (!strcmp (SYMBOL_NAME (sym), | |
190 | TYPE_FN_FIELD_PHYSNAME (f, j))) | |
191 | { | |
192 | goto common; | |
193 | } | |
194 | } | |
195 | } | |
196 | } | |
197 | common: | |
198 | if (i < len) | |
199 | { | |
200 | fprintf_filtered (stream, "&"); | |
201 | c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0); | |
202 | fprintf (stream, kind); | |
203 | if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_' | |
204 | && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER) | |
205 | cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j) + 1, "~", | |
206 | TYPE_FN_FIELDLIST_NAME (domain, i), | |
207 | 0, stream); | |
208 | else | |
209 | cp_type_print_method_args (TYPE_FN_FIELD_ARGS (f, j), "", | |
210 | TYPE_FN_FIELDLIST_NAME (domain, i), | |
211 | 0, stream); | |
212 | break; | |
213 | } | |
214 | fprintf_filtered (stream, "("); | |
215 | type_print (type, "", stream, -1); | |
216 | fprintf_filtered (stream, ") %d", (int) addr >> 3); | |
217 | } | |
218 | else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER) | |
219 | { | |
220 | cp_print_class_member (valaddr, | |
221 | TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)), | |
222 | stream, "&"); | |
223 | } | |
224 | else | |
225 | { | |
226 | CORE_ADDR addr = unpack_pointer (type, valaddr); | |
227 | elttype = TYPE_TARGET_TYPE (type); | |
228 | ||
229 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) | |
230 | { | |
231 | /* Try to print what function it points to. */ | |
232 | print_address_demangle (addr, stream, demangle); | |
233 | /* Return value is irrelevant except for string pointers. */ | |
234 | return (0); | |
235 | } | |
236 | ||
237 | if (addressprint && format != 's') | |
238 | { | |
239 | fprintf_filtered (stream, "0x%x", addr); | |
240 | } | |
241 | ||
242 | /* For a pointer to char or unsigned char, also print the string | |
243 | pointed to, unless pointer is null. */ | |
244 | i = 0; /* Number of characters printed. */ | |
245 | if (TYPE_LENGTH (elttype) == 1 && | |
246 | TYPE_CODE (elttype) == TYPE_CODE_INT && | |
247 | (format == 0 || format == 's') && | |
248 | addr != 0 && | |
249 | /* If print_max is UINT_MAX, the alloca below will fail. | |
250 | In that case don't try to print the string. */ | |
251 | print_max < UINT_MAX) | |
252 | { | |
253 | int first_addr_err = 0; | |
254 | int errcode = 0; | |
255 | ||
256 | /* Get first character. */ | |
257 | errcode = target_read_memory (addr, (char *)&c, 1); | |
258 | if (errcode != 0) | |
259 | { | |
260 | /* First address out of bounds. */ | |
261 | first_addr_err = 1; | |
262 | } | |
263 | else | |
264 | { | |
265 | /* A real string. */ | |
266 | char *string = (char *) alloca (print_max); | |
267 | ||
268 | /* If the loop ends by us hitting print_max characters, | |
269 | we need to have elipses at the end. */ | |
270 | int force_ellipses = 1; | |
271 | ||
272 | /* This loop always fetches print_max characters, even | |
273 | though LA_PRINT_STRING might want to print more or fewer | |
274 | (with repeated characters). This is so that | |
275 | we don't spend forever fetching if we print | |
276 | a long string consisting of the same character | |
277 | repeated. Also so we can do it all in one memory | |
278 | operation, which is faster. However, this will be | |
279 | slower if print_max is set high, e.g. if you set | |
280 | print_max to 1000, not only will it take a long | |
281 | time to fetch short strings, but if you are near | |
282 | the end of the address space, it might not work. */ | |
283 | QUIT; | |
284 | errcode = target_read_memory (addr, string, print_max); | |
285 | if (errcode != 0) | |
286 | { | |
287 | /* Try reading just one character. If that succeeds, | |
288 | assume we hit the end of the address space, but | |
289 | the initial part of the string is probably safe. */ | |
290 | char x[1]; | |
291 | errcode = target_read_memory (addr, x, 1); | |
292 | } | |
293 | if (errcode != 0) | |
294 | force_ellipses = 0; | |
295 | else | |
296 | for (i = 0; i < print_max; i++) | |
297 | if (string[i] == '\0') | |
298 | { | |
299 | force_ellipses = 0; | |
300 | break; | |
301 | } | |
302 | QUIT; | |
303 | ||
304 | if (addressprint) | |
305 | { | |
306 | fputs_filtered (" ", stream); | |
307 | } | |
308 | LA_PRINT_STRING (stream, string, i, force_ellipses); | |
309 | } | |
310 | ||
311 | if (errcode != 0) | |
312 | { | |
313 | if (errcode == EIO) | |
314 | { | |
315 | fprintf_filtered (stream, | |
316 | (" <Address 0x%x out of bounds>" + | |
317 | first_addr_err), | |
318 | addr + i); | |
319 | } | |
320 | else | |
321 | { | |
322 | error ("Error reading memory address 0x%x: %s.", | |
323 | addr + i, safe_strerror (errcode)); | |
324 | } | |
325 | } | |
326 | ||
327 | fflush (stream); | |
328 | } | |
329 | else if (cp_is_vtbl_member(type)) | |
330 | { | |
331 | /* print vtbl's nicely */ | |
332 | CORE_ADDR vt_address = unpack_pointer (type, valaddr); | |
333 | ||
334 | struct minimal_symbol *msymbol = | |
335 | lookup_minimal_symbol_by_pc (vt_address); | |
336 | if ((msymbol != NULL) && (vt_address == msymbol -> address)) | |
337 | { | |
338 | fputs_filtered (" <", stream); | |
339 | fputs_demangled (msymbol -> name, stream, | |
340 | DMGL_ANSI | DMGL_PARAMS); | |
341 | fputs_filtered (">", stream); | |
342 | } | |
343 | if (vtblprint) | |
344 | { | |
345 | value vt_val; | |
346 | ||
347 | vt_val = value_at (TYPE_TARGET_TYPE (type), vt_address); | |
348 | val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val), | |
349 | VALUE_ADDRESS (vt_val), stream, format, | |
350 | deref_ref, recurse + 1, pretty); | |
351 | if (pretty) | |
352 | { | |
353 | fprintf_filtered (stream, "\n"); | |
354 | print_spaces_filtered (2 + 2 * recurse, stream); | |
355 | } | |
356 | } | |
357 | } | |
358 | ||
359 | /* Return number of characters printed, plus one for the | |
360 | terminating null if we have "reached the end". */ | |
361 | return (i + (print_max && i != print_max)); | |
362 | } | |
363 | break; | |
364 | ||
365 | case TYPE_CODE_MEMBER: | |
366 | error ("not implemented: member type in c_val_print"); | |
367 | break; | |
368 | ||
369 | case TYPE_CODE_REF: | |
370 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER) | |
371 | { | |
372 | cp_print_class_member (valaddr, | |
373 | TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)), | |
374 | stream, ""); | |
375 | break; | |
376 | } | |
377 | if (addressprint) | |
378 | { | |
379 | fprintf_filtered (stream, "@0x%lx", | |
380 | unpack_long (builtin_type_int, valaddr)); | |
381 | if (deref_ref) | |
382 | fputs_filtered (": ", stream); | |
383 | } | |
384 | /* De-reference the reference. */ | |
385 | if (deref_ref) | |
386 | { | |
387 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF) | |
388 | { | |
389 | value deref_val = | |
390 | value_at | |
391 | (TYPE_TARGET_TYPE (type), | |
392 | unpack_pointer (lookup_pointer_type (builtin_type_void), | |
393 | valaddr)); | |
394 | val_print (VALUE_TYPE (deref_val), | |
395 | VALUE_CONTENTS (deref_val), | |
396 | VALUE_ADDRESS (deref_val), stream, format, | |
397 | deref_ref, recurse + 1, pretty); | |
398 | } | |
399 | else | |
400 | fputs_filtered ("???", stream); | |
401 | } | |
402 | break; | |
403 | ||
404 | case TYPE_CODE_UNION: | |
405 | if (recurse && !unionprint) | |
406 | { | |
407 | fprintf_filtered (stream, "{...}"); | |
408 | break; | |
409 | } | |
410 | /* Fall through. */ | |
411 | case TYPE_CODE_STRUCT: | |
412 | if (vtblprint && cp_is_vtbl_ptr_type(type)) | |
413 | { | |
414 | /* Print the unmangled name if desired. */ | |
415 | print_address_demangle(*((int *) (valaddr + /* FIXME bytesex */ | |
416 | TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8)), | |
417 | stream, demangle); | |
418 | break; | |
419 | } | |
420 | cp_print_value_fields (type, valaddr, stream, format, recurse, pretty, | |
421 | 0); | |
422 | break; | |
423 | ||
424 | case TYPE_CODE_ENUM: | |
425 | if (format) | |
426 | { | |
427 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
428 | break; | |
429 | } | |
430 | len = TYPE_NFIELDS (type); | |
431 | val = unpack_long (builtin_type_int, valaddr); | |
432 | for (i = 0; i < len; i++) | |
433 | { | |
434 | QUIT; | |
435 | if (val == TYPE_FIELD_BITPOS (type, i)) | |
436 | { | |
437 | break; | |
438 | } | |
439 | } | |
440 | if (i < len) | |
441 | { | |
442 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
443 | } | |
444 | else | |
445 | { | |
446 | #ifdef LONG_LONG | |
447 | fprintf_filtered (stream, "%lld", val); | |
448 | #else | |
449 | fprintf_filtered (stream, "%ld", val); | |
450 | #endif | |
451 | } | |
452 | break; | |
453 | ||
454 | case TYPE_CODE_FUNC: | |
455 | if (format) | |
456 | { | |
457 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
458 | break; | |
459 | } | |
460 | /* FIXME, we should consider, at least for ANSI C language, eliminating | |
461 | the distinction made between FUNCs and POINTERs to FUNCs. */ | |
462 | fprintf_filtered (stream, "{"); | |
463 | type_print (type, "", stream, -1); | |
464 | fprintf_filtered (stream, "} "); | |
465 | /* Try to print what function it points to, and its address. */ | |
466 | print_address_demangle (address, stream, demangle); | |
467 | break; | |
468 | ||
469 | case TYPE_CODE_INT: | |
470 | format = format ? format : output_format; | |
471 | if (format) | |
472 | { | |
473 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
474 | } | |
475 | else | |
476 | { | |
477 | val_print_type_code_int (type, valaddr, stream); | |
478 | /* C and C++ has no single byte int type, char is used instead. | |
479 | Since we don't know whether the value is really intended to | |
480 | be used as an integer or a character, print the character | |
481 | equivalent as well. */ | |
482 | if (TYPE_LENGTH (type) == 1) | |
483 | { | |
484 | fputs_filtered (" ", stream); | |
485 | LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr), | |
486 | stream); | |
487 | } | |
488 | } | |
489 | break; | |
490 | ||
491 | case TYPE_CODE_CHAR: | |
492 | format = format ? format : output_format; | |
493 | if (format) | |
494 | { | |
495 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
496 | } | |
497 | else | |
498 | { | |
499 | fprintf_filtered (stream, TYPE_UNSIGNED (type) ? "%u" : "%d", | |
500 | unpack_long (type, valaddr)); | |
501 | fputs_filtered (" ", stream); | |
502 | LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr), stream); | |
503 | } | |
504 | break; | |
505 | ||
506 | case TYPE_CODE_FLT: | |
507 | if (format) | |
508 | { | |
509 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
510 | } | |
511 | else | |
512 | { | |
513 | print_floating (valaddr, type, stream); | |
514 | } | |
515 | break; | |
516 | ||
517 | case TYPE_CODE_VOID: | |
518 | fprintf_filtered (stream, "void"); | |
519 | break; | |
520 | ||
521 | case TYPE_CODE_ERROR: | |
522 | fprintf_filtered (stream, "<error type>"); | |
523 | break; | |
524 | ||
525 | case TYPE_CODE_RANGE: | |
526 | /* FIXME, we should not ever have to print one of these yet. */ | |
527 | fprintf_filtered (stream, "<range type>"); | |
528 | break; | |
529 | ||
530 | case TYPE_CODE_UNDEF: | |
531 | /* This happens (without TYPE_FLAG_STUB set) on systems which don't use | |
532 | dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" | |
533 | and no complete type for struct foo in that file. */ | |
534 | fprintf_filtered (stream, "<incomplete type>"); | |
535 | break; | |
536 | ||
537 | default: | |
538 | error ("Invalid C/C++ type code %d in symbol table.", TYPE_CODE (type)); | |
539 | } | |
540 | fflush (stream); | |
541 | return (0); | |
542 | } |