Commit | Line | Data |
---|---|---|
7d9884b9 JG |
1 | /* Print values for GDB, the GNU debugger. |
2 | Copyright 1986, 1988, 1989, 1991 Free Software Foundation, Inc. | |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
36b9d39c | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
36b9d39c JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
36b9d39c | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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 | |
36b9d39c JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 | 19 | |
bd5635a1 | 20 | #include "defs.h" |
2cd99985 | 21 | #include <string.h> |
bd5635a1 | 22 | #include "symtab.h" |
2cd99985 | 23 | #include "gdbtypes.h" |
bd5635a1 RP |
24 | #include "value.h" |
25 | #include "gdbcore.h" | |
26 | #include "gdbcmd.h" | |
27 | #include "target.h" | |
28 | #include "obstack.h" | |
be3bc7ad | 29 | #include "language.h" |
8f793aa5 | 30 | #include "demangle.h" |
bd5635a1 RP |
31 | |
32 | #include <errno.h> | |
2cd99985 PB |
33 | |
34 | /* Prototypes for local functions */ | |
35 | ||
36 | static void | |
37 | print_string PARAMS ((FILE *, char *, unsigned int, int)); | |
38 | ||
39 | static void | |
40 | show_print PARAMS ((char *, int)); | |
41 | ||
42 | static void | |
43 | set_print PARAMS ((char *, int)); | |
44 | ||
45 | static void | |
46 | set_radix PARAMS ((char *, int, struct cmd_list_element *)); | |
47 | ||
48 | static void | |
49 | set_output_radix PARAMS ((char *, int, struct cmd_list_element *)); | |
50 | ||
51 | static void | |
52 | type_print_base PARAMS ((struct type *, FILE *, int, int)); | |
53 | ||
54 | static void | |
f9b5584c | 55 | type_print_varspec_suffix PARAMS ((struct type *, FILE *, int, int, int)); |
2cd99985 PB |
56 | |
57 | static void | |
58 | type_print_varspec_prefix PARAMS ((struct type *, FILE *, int, int)); | |
59 | ||
60 | static void | |
61 | type_print_derivation_info PARAMS ((FILE *, struct type *)); | |
62 | ||
63 | static void | |
64 | type_print_method_args PARAMS ((struct type **, char *, char *, int, FILE *)); | |
65 | ||
66 | static void | |
67 | cplus_val_print PARAMS ((struct type *, char *, FILE *, int, int, | |
68 | enum val_prettyprint, struct type **)); | |
69 | ||
70 | static void | |
71 | val_print_fields PARAMS ((struct type *, char *, FILE *, int, int, | |
72 | enum val_prettyprint, struct type **)); | |
73 | ||
74 | static int | |
75 | is_vtbl_member PARAMS ((struct type *)); | |
76 | ||
77 | static int | |
78 | is_vtbl_ptr_type PARAMS ((struct type *)); | |
79 | ||
80 | static void | |
81 | print_hex_chars PARAMS ((FILE *, unsigned char *, unsigned)); | |
82 | ||
bd5635a1 RP |
83 | extern int demangle; /* whether to print C++ syms raw or source-form */ |
84 | ||
85 | /* Maximum number of chars to print for a string pointer value | |
86 | or vector contents, or UINT_MAX for no limit. */ | |
87 | ||
88 | static unsigned int print_max; | |
89 | ||
bd5635a1 RP |
90 | /* Default input and output radixes, and output format letter. */ |
91 | ||
92 | unsigned input_radix = 10; | |
93 | unsigned output_radix = 10; | |
94 | int output_format = 0; | |
95 | ||
bd5635a1 RP |
96 | /* Print repeat counts if there are more than this |
97 | many repetitions of an element in an array. */ | |
98 | #define REPEAT_COUNT_THRESHOLD 10 | |
0dce3774 JK |
99 | |
100 | /* Define a mess of print controls. */ | |
101 | ||
102 | int prettyprint; /* Controls pretty printing of structures */ | |
103 | int vtblprint; /* Controls printing of vtbl's */ | |
104 | int unionprint; /* Controls printing of nested unions. */ | |
105 | int arrayprint; /* Controls pretty printing of arrays. */ | |
106 | int addressprint; /* Controls pretty printing of addresses. */ | |
107 | int objectprint; /* Controls looking up an object's derived type | |
108 | using what we find in its vtables. */ | |
109 | ||
110 | struct obstack dont_print_obstack; | |
111 | ||
bd5635a1 RP |
112 | \f |
113 | /* Print the character string STRING, printing at most LENGTH characters. | |
114 | Printing stops early if the number hits print_max; repeat counts | |
115 | are printed as appropriate. Print ellipses at the end if we | |
116 | had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */ | |
117 | ||
2cd99985 | 118 | static void |
bd5635a1 RP |
119 | print_string (stream, string, length, force_ellipses) |
120 | FILE *stream; | |
121 | char *string; | |
122 | unsigned int length; | |
123 | int force_ellipses; | |
124 | { | |
125 | register unsigned int i; | |
126 | unsigned int things_printed = 0; | |
127 | int in_quotes = 0; | |
128 | int need_comma = 0; | |
129 | extern int inspect_it; | |
130 | ||
131 | if (length == 0) | |
132 | { | |
133 | fputs_filtered ("\"\"", stdout); | |
134 | return; | |
135 | } | |
136 | ||
137 | for (i = 0; i < length && things_printed < print_max; ++i) | |
138 | { | |
139 | /* Position of the character we are examining | |
140 | to see whether it is repeated. */ | |
141 | unsigned int rep1; | |
142 | /* Number of repetitions we have detected so far. */ | |
143 | unsigned int reps; | |
144 | ||
145 | QUIT; | |
146 | ||
147 | if (need_comma) | |
148 | { | |
149 | fputs_filtered (", ", stream); | |
150 | need_comma = 0; | |
151 | } | |
152 | ||
153 | rep1 = i + 1; | |
154 | reps = 1; | |
155 | while (rep1 < length && string[rep1] == string[i]) | |
156 | { | |
157 | ++rep1; | |
158 | ++reps; | |
159 | } | |
160 | ||
161 | if (reps > REPEAT_COUNT_THRESHOLD) | |
162 | { | |
163 | if (in_quotes) | |
164 | { | |
165 | if (inspect_it) | |
166 | fputs_filtered ("\\\", ", stream); | |
167 | else | |
168 | fputs_filtered ("\", ", stream); | |
169 | in_quotes = 0; | |
170 | } | |
171 | fputs_filtered ("'", stream); | |
172 | printchar (string[i], stream, '\''); | |
173 | fprintf_filtered (stream, "' <repeats %u times>", reps); | |
174 | i = rep1 - 1; | |
175 | things_printed += REPEAT_COUNT_THRESHOLD; | |
176 | need_comma = 1; | |
177 | } | |
178 | else | |
179 | { | |
180 | if (!in_quotes) | |
181 | { | |
182 | if (inspect_it) | |
183 | fputs_filtered ("\\\"", stream); | |
184 | else | |
185 | fputs_filtered ("\"", stream); | |
186 | in_quotes = 1; | |
187 | } | |
188 | printchar (string[i], stream, '"'); | |
189 | ++things_printed; | |
190 | } | |
191 | } | |
192 | ||
193 | /* Terminate the quotes if necessary. */ | |
194 | if (in_quotes) | |
195 | { | |
196 | if (inspect_it) | |
197 | fputs_filtered ("\\\"", stream); | |
198 | else | |
199 | fputs_filtered ("\"", stream); | |
200 | } | |
201 | ||
202 | if (force_ellipses || i < length) | |
203 | fputs_filtered ("...", stream); | |
204 | } | |
205 | ||
206 | /* Print a floating point value of type TYPE, pointed to in GDB by VALADDR, | |
207 | on STREAM. */ | |
208 | ||
209 | void | |
210 | print_floating (valaddr, type, stream) | |
211 | char *valaddr; | |
212 | struct type *type; | |
213 | FILE *stream; | |
214 | { | |
215 | double doub; | |
216 | int inv; | |
217 | unsigned len = TYPE_LENGTH (type); | |
218 | ||
219 | #if defined (IEEE_FLOAT) | |
220 | ||
221 | /* Check for NaN's. Note that this code does not depend on us being | |
222 | on an IEEE conforming system. It only depends on the target | |
223 | machine using IEEE representation. This means (a) | |
224 | cross-debugging works right, and (2) IEEE_FLOAT can (and should) | |
225 | be defined for systems like the 68881, which uses IEEE | |
226 | representation, but is not IEEE conforming. */ | |
227 | ||
228 | { | |
229 | long low, high; | |
230 | /* Is the sign bit 0? */ | |
231 | int nonnegative; | |
232 | /* Is it is a NaN (i.e. the exponent is all ones and | |
233 | the fraction is nonzero)? */ | |
234 | int is_nan; | |
235 | ||
236 | if (len == sizeof (float)) | |
237 | { | |
238 | /* It's single precision. */ | |
239 | bcopy (valaddr, &low, sizeof (low)); | |
240 | /* target -> host. */ | |
241 | SWAP_TARGET_AND_HOST (&low, sizeof (float)); | |
242 | nonnegative = low >= 0; | |
243 | is_nan = ((((low >> 23) & 0xFF) == 0xFF) | |
244 | && 0 != (low & 0x7FFFFF)); | |
245 | low &= 0x7fffff; | |
246 | high = 0; | |
247 | } | |
248 | else | |
249 | { | |
250 | /* It's double precision. Get the high and low words. */ | |
251 | ||
252 | #if TARGET_BYTE_ORDER == BIG_ENDIAN | |
253 | bcopy (valaddr+4, &low, sizeof (low)); | |
254 | bcopy (valaddr+0, &high, sizeof (high)); | |
255 | #else | |
256 | bcopy (valaddr+0, &low, sizeof (low)); | |
257 | bcopy (valaddr+4, &high, sizeof (high)); | |
258 | #endif | |
259 | SWAP_TARGET_AND_HOST (&low, sizeof (low)); | |
260 | SWAP_TARGET_AND_HOST (&high, sizeof (high)); | |
261 | nonnegative = high >= 0; | |
262 | is_nan = (((high >> 20) & 0x7ff) == 0x7ff | |
263 | && ! ((((high & 0xfffff) == 0)) && (low == 0))); | |
264 | high &= 0xfffff; | |
265 | } | |
266 | ||
267 | if (is_nan) | |
268 | { | |
269 | /* The meaning of the sign and fraction is not defined by IEEE. | |
270 | But the user might know what they mean. For example, they | |
271 | (in an implementation-defined manner) distinguish between | |
272 | signaling and quiet NaN's. */ | |
273 | if (high) | |
274 | fprintf_filtered (stream, "-NaN(0x%lx%.8lx)" + nonnegative, | |
275 | high, low); | |
276 | else | |
277 | fprintf_filtered (stream, "-NaN(0x%lx)" + nonnegative, low); | |
278 | return; | |
279 | } | |
280 | } | |
281 | #endif /* IEEE_FLOAT. */ | |
282 | ||
283 | doub = unpack_double (type, valaddr, &inv); | |
284 | if (inv) | |
285 | fprintf_filtered (stream, "<invalid float value>"); | |
286 | else | |
be3bc7ad | 287 | fprintf_filtered (stream, len <= sizeof(float) ? "%.9g" : "%.17g", doub); |
bd5635a1 RP |
288 | } |
289 | ||
290 | /* VALADDR points to an integer of LEN bytes. Print it in hex on stream. */ | |
291 | static void | |
292 | print_hex_chars (stream, valaddr, len) | |
293 | FILE *stream; | |
294 | unsigned char *valaddr; | |
295 | unsigned len; | |
296 | { | |
297 | unsigned char *p; | |
298 | ||
299 | fprintf_filtered (stream, "0x"); | |
300 | #if TARGET_BYTE_ORDER == BIG_ENDIAN | |
301 | for (p = valaddr; | |
302 | p < valaddr + len; | |
303 | p++) | |
304 | #else /* Little endian. */ | |
305 | for (p = valaddr + len - 1; | |
306 | p >= valaddr; | |
307 | p--) | |
308 | #endif | |
309 | { | |
310 | fprintf_filtered (stream, "%02x", *p); | |
311 | } | |
312 | } | |
313 | \f | |
314 | /* Print the value VAL in C-ish syntax on stream STREAM. | |
315 | FORMAT is a format-letter, or 0 for print in natural format of data type. | |
316 | If the object printed is a string pointer, returns | |
317 | the number of string bytes printed. */ | |
318 | ||
319 | int | |
320 | value_print (val, stream, format, pretty) | |
321 | value val; | |
322 | FILE *stream; | |
2cd99985 | 323 | int format; |
bd5635a1 RP |
324 | enum val_prettyprint pretty; |
325 | { | |
326 | register unsigned int i, n, typelen; | |
327 | ||
328 | if (val == 0) | |
329 | { | |
330 | printf_filtered ("<address of value unknown>"); | |
331 | return 0; | |
332 | } | |
333 | if (VALUE_OPTIMIZED_OUT (val)) | |
334 | { | |
335 | printf_filtered ("<value optimized out>"); | |
336 | return 0; | |
337 | } | |
aec4cb91 | 338 | |
bd5635a1 RP |
339 | /* A "repeated" value really contains several values in a row. |
340 | They are made by the @ operator. | |
341 | Print such values as if they were arrays. */ | |
342 | ||
343 | else if (VALUE_REPEATED (val)) | |
344 | { | |
345 | n = VALUE_REPETITIONS (val); | |
346 | typelen = TYPE_LENGTH (VALUE_TYPE (val)); | |
347 | fprintf_filtered (stream, "{"); | |
348 | /* Print arrays of characters using string syntax. */ | |
349 | if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT | |
350 | && format == 0) | |
351 | print_string (stream, VALUE_CONTENTS (val), n, 0); | |
352 | else | |
353 | { | |
354 | unsigned int things_printed = 0; | |
355 | ||
356 | for (i = 0; i < n && things_printed < print_max; i++) | |
357 | { | |
358 | /* Position of the array element we are examining to see | |
359 | whether it is repeated. */ | |
360 | unsigned int rep1; | |
361 | /* Number of repetitions we have detected so far. */ | |
362 | unsigned int reps; | |
363 | ||
364 | if (i != 0) | |
365 | fprintf_filtered (stream, ", "); | |
366 | wrap_here (""); | |
367 | ||
368 | rep1 = i + 1; | |
369 | reps = 1; | |
370 | while (rep1 < n | |
371 | && !bcmp (VALUE_CONTENTS (val) + typelen * i, | |
372 | VALUE_CONTENTS (val) + typelen * rep1, typelen)) | |
373 | { | |
374 | ++reps; | |
375 | ++rep1; | |
376 | } | |
377 | ||
378 | if (reps > REPEAT_COUNT_THRESHOLD) | |
379 | { | |
380 | val_print (VALUE_TYPE (val), | |
381 | VALUE_CONTENTS (val) + typelen * i, | |
382 | VALUE_ADDRESS (val) + typelen * i, | |
383 | stream, format, 1, 0, pretty); | |
384 | fprintf (stream, " <repeats %u times>", reps); | |
385 | i = rep1 - 1; | |
386 | things_printed += REPEAT_COUNT_THRESHOLD; | |
387 | } | |
388 | else | |
389 | { | |
390 | val_print (VALUE_TYPE (val), | |
391 | VALUE_CONTENTS (val) + typelen * i, | |
392 | VALUE_ADDRESS (val) + typelen * i, | |
393 | stream, format, 1, 0, pretty); | |
394 | things_printed++; | |
395 | } | |
396 | } | |
397 | if (i < n) | |
398 | fprintf_filtered (stream, "..."); | |
399 | } | |
400 | fprintf_filtered (stream, "}"); | |
401 | return n * typelen; | |
402 | } | |
403 | else | |
404 | { | |
0dce3774 JK |
405 | struct type *type = VALUE_TYPE (val); |
406 | ||
bd5635a1 RP |
407 | /* If it is a pointer, indicate what it points to. |
408 | ||
409 | Print type also if it is a reference. | |
410 | ||
411 | C++: if it is a member pointer, we will take care | |
412 | of that when we print it. */ | |
0dce3774 JK |
413 | if (TYPE_CODE (type) == TYPE_CODE_PTR |
414 | || TYPE_CODE (type) == TYPE_CODE_REF) | |
bd5635a1 RP |
415 | { |
416 | /* Hack: remove (char *) for char strings. Their | |
417 | type is indicated by the quoted string anyway. */ | |
0dce3774 JK |
418 | if (TYPE_CODE (type) == TYPE_CODE_PTR |
419 | && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) == sizeof(char) | |
420 | && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_INT | |
421 | && !TYPE_UNSIGNED (TYPE_TARGET_TYPE (type))) | |
bd5635a1 RP |
422 | { |
423 | /* Print nothing */ | |
424 | } | |
425 | else | |
426 | { | |
427 | fprintf_filtered (stream, "("); | |
0dce3774 | 428 | type_print (type, "", stream, -1); |
bd5635a1 RP |
429 | fprintf_filtered (stream, ") "); |
430 | } | |
431 | } | |
0dce3774 | 432 | return val_print (type, VALUE_CONTENTS (val), |
bd5635a1 RP |
433 | VALUE_ADDRESS (val), stream, format, 1, 0, pretty); |
434 | } | |
435 | } | |
436 | ||
437 | /* Return truth value for assertion that TYPE is of the type | |
438 | "pointer to virtual function". */ | |
439 | static int | |
440 | is_vtbl_ptr_type(type) | |
441 | struct type *type; | |
442 | { | |
abefb1f1 | 443 | char *typename = type_name_no_tag (type); |
bd5635a1 | 444 | static const char vtbl_ptr_name[] = |
bcccec8c | 445 | { CPLUS_MARKER,'v','t','b','l','_','p','t','r','_','t','y','p','e', 0 }; |
bd5635a1 RP |
446 | |
447 | return (typename != NULL && !strcmp(typename, vtbl_ptr_name)); | |
448 | } | |
449 | ||
450 | /* Return truth value for the assertion that TYPE is of the type | |
451 | "pointer to virtual function table". */ | |
452 | static int | |
453 | is_vtbl_member(type) | |
454 | struct type *type; | |
455 | { | |
0dce3774 JK |
456 | if (TYPE_CODE (type) == TYPE_CODE_PTR) |
457 | type = TYPE_TARGET_TYPE (type); | |
458 | else | |
459 | return 0; | |
460 | ||
461 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY | |
462 | && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT) | |
bd5635a1 | 463 | /* Virtual functions tables are full of pointers to virtual functions. */ |
0dce3774 | 464 | return is_vtbl_ptr_type (TYPE_TARGET_TYPE (type)); |
bd5635a1 RP |
465 | return 0; |
466 | } | |
467 | \f | |
bd5635a1 RP |
468 | /* Mutually recursive subroutines of cplus_val_print and val_print to print out |
469 | a structure's fields: val_print_fields and cplus_val_print. | |
470 | ||
471 | TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the | |
472 | same meanings as in cplus_val_print and val_print. | |
473 | ||
474 | DONT_PRINT is an array of baseclass types that we | |
475 | should not print, or zero if called from top level. */ | |
2cd99985 | 476 | |
bd5635a1 RP |
477 | static void |
478 | val_print_fields (type, valaddr, stream, format, recurse, pretty, dont_print) | |
479 | struct type *type; | |
480 | char *valaddr; | |
481 | FILE *stream; | |
2cd99985 | 482 | int format; |
bd5635a1 RP |
483 | int recurse; |
484 | enum val_prettyprint pretty; | |
485 | struct type **dont_print; | |
486 | { | |
487 | int i, len, n_baseclasses; | |
488 | ||
be3bc7ad JG |
489 | check_stub_type (type); |
490 | ||
bd5635a1 RP |
491 | fprintf_filtered (stream, "{"); |
492 | len = TYPE_NFIELDS (type); | |
493 | n_baseclasses = TYPE_N_BASECLASSES (type); | |
494 | ||
495 | /* Print out baseclasses such that we don't print | |
496 | duplicates of virtual baseclasses. */ | |
497 | if (n_baseclasses > 0) | |
498 | cplus_val_print (type, valaddr, stream, format, recurse+1, pretty, dont_print); | |
499 | ||
500 | if (!len && n_baseclasses == 1) | |
501 | fprintf_filtered (stream, "<No data fields>"); | |
502 | else | |
503 | { | |
504 | extern int inspect_it; | |
505 | int fields_seen = 0; | |
506 | ||
507 | for (i = n_baseclasses; i < len; i++) | |
508 | { | |
509 | /* Check if static field */ | |
510 | if (TYPE_FIELD_STATIC (type, i)) | |
511 | continue; | |
512 | if (fields_seen) | |
513 | fprintf_filtered (stream, ", "); | |
514 | else if (n_baseclasses > 0) | |
515 | { | |
516 | fprintf_filtered (stream, "\n"); | |
517 | print_spaces_filtered (2 + 2 * recurse, stream); | |
518 | fputs_filtered ("members of ", stream); | |
519 | fputs_filtered (type_name_no_tag (type), stream); | |
520 | fputs_filtered (": ", stream); | |
521 | } | |
522 | fields_seen = 1; | |
523 | ||
524 | if (pretty) | |
525 | { | |
526 | fprintf_filtered (stream, "\n"); | |
527 | print_spaces_filtered (2 + 2 * recurse, stream); | |
528 | } | |
529 | else | |
530 | { | |
531 | wrap_here (n_spaces (2 + 2 * recurse)); | |
532 | } | |
533 | if (inspect_it) | |
534 | { | |
535 | if (TYPE_CODE (TYPE_FIELD_TYPE (type, i)) == TYPE_CODE_PTR) | |
536 | fputs_filtered ("\"( ptr \"", stream); | |
537 | else | |
538 | fputs_filtered ("\"( nodef \"", stream); | |
539 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
540 | fputs_filtered ("\" \"", stream); | |
541 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
542 | fputs_filtered ("\") \"", stream); | |
543 | } | |
544 | else | |
545 | { | |
546 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
547 | fputs_filtered (" = ", stream); | |
548 | } | |
549 | if (TYPE_FIELD_PACKED (type, i)) | |
550 | { | |
f266e564 | 551 | value v; |
bd5635a1 | 552 | |
f266e564 JK |
553 | /* Bitfields require special handling, especially due to byte |
554 | order problems. */ | |
bee3c1a1 | 555 | v = value_from_longest (TYPE_FIELD_TYPE (type, i), |
f266e564 | 556 | unpack_field_as_long (type, valaddr, i)); |
bd5635a1 | 557 | |
f266e564 | 558 | val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0, |
bd5635a1 RP |
559 | stream, format, 0, recurse + 1, pretty); |
560 | } | |
561 | else | |
562 | { | |
563 | val_print (TYPE_FIELD_TYPE (type, i), | |
564 | valaddr + TYPE_FIELD_BITPOS (type, i) / 8, | |
565 | 0, stream, format, 0, recurse + 1, pretty); | |
566 | } | |
567 | } | |
568 | if (pretty) | |
569 | { | |
570 | fprintf_filtered (stream, "\n"); | |
571 | print_spaces_filtered (2 * recurse, stream); | |
572 | } | |
573 | } | |
574 | fprintf_filtered (stream, "}"); | |
575 | } | |
576 | ||
577 | /* Special val_print routine to avoid printing multiple copies of virtual | |
578 | baseclasses. */ | |
579 | ||
580 | static void | |
581 | cplus_val_print (type, valaddr, stream, format, recurse, pretty, dont_print) | |
582 | struct type *type; | |
583 | char *valaddr; | |
584 | FILE *stream; | |
2cd99985 | 585 | int format; |
bd5635a1 RP |
586 | int recurse; |
587 | enum val_prettyprint pretty; | |
588 | struct type **dont_print; | |
589 | { | |
590 | struct obstack tmp_obstack; | |
591 | struct type **last_dont_print | |
592 | = (struct type **)obstack_next_free (&dont_print_obstack); | |
593 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); | |
594 | ||
595 | if (dont_print == 0) | |
596 | { | |
597 | /* If we're at top level, carve out a completely fresh | |
598 | chunk of the obstack and use that until this particular | |
599 | invocation returns. */ | |
600 | tmp_obstack = dont_print_obstack; | |
601 | /* Bump up the high-water mark. Now alpha is omega. */ | |
602 | obstack_finish (&dont_print_obstack); | |
603 | } | |
604 | ||
605 | for (i = 0; i < n_baseclasses; i++) | |
606 | { | |
607 | char *baddr; | |
0dce3774 | 608 | int err; |
bd5635a1 RP |
609 | |
610 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
611 | { | |
612 | struct type **first_dont_print | |
613 | = (struct type **)obstack_base (&dont_print_obstack); | |
614 | ||
615 | int j = (struct type **)obstack_next_free (&dont_print_obstack) | |
616 | - first_dont_print; | |
617 | ||
618 | while (--j >= 0) | |
619 | if (TYPE_BASECLASS (type, i) == first_dont_print[j]) | |
620 | goto flush_it; | |
621 | ||
622 | obstack_ptr_grow (&dont_print_obstack, TYPE_BASECLASS (type, i)); | |
623 | } | |
624 | ||
0dce3774 JK |
625 | baddr = baseclass_addr (type, i, valaddr, 0, &err); |
626 | if (err == 0 && baddr == 0) | |
bd5635a1 RP |
627 | error ("could not find virtual baseclass `%s'\n", |
628 | type_name_no_tag (TYPE_BASECLASS (type, i))); | |
629 | ||
630 | fprintf_filtered (stream, "\n"); | |
631 | if (pretty) | |
632 | print_spaces_filtered (2 + 2 * recurse, stream); | |
633 | fputs_filtered ("<", stream); | |
634 | fputs_filtered (type_name_no_tag (TYPE_BASECLASS (type, i)), stream); | |
635 | fputs_filtered ("> = ", stream); | |
0dce3774 JK |
636 | if (err != 0) |
637 | fprintf_filtered (stream, "<invalid address 0x%x>", baddr); | |
638 | else | |
639 | val_print_fields (TYPE_BASECLASS (type, i), baddr, stream, format, | |
640 | recurse, pretty, | |
641 | (struct type **)obstack_base (&dont_print_obstack)); | |
bd5635a1 RP |
642 | flush_it: |
643 | ; | |
644 | } | |
645 | ||
646 | if (dont_print == 0) | |
647 | { | |
648 | /* Free the space used to deal with the printing | |
649 | of this type from top level. */ | |
650 | obstack_free (&dont_print_obstack, last_dont_print); | |
651 | /* Reset watermark so that we can continue protecting | |
652 | ourselves from whatever we were protecting ourselves. */ | |
653 | dont_print_obstack = tmp_obstack; | |
654 | } | |
655 | } | |
656 | ||
4a11eef2 FF |
657 | static void |
658 | print_class_member (valaddr, domain, stream, prefix) | |
659 | char *valaddr; | |
660 | struct type *domain; | |
661 | FILE *stream; | |
662 | char *prefix; | |
663 | { | |
664 | ||
665 | /* VAL is a byte offset into the structure type DOMAIN. | |
666 | Find the name of the field for that offset and | |
667 | print it. */ | |
668 | int extra = 0; | |
669 | int bits = 0; | |
670 | register unsigned int i; | |
671 | unsigned len = TYPE_NFIELDS (domain); | |
672 | /* @@ Make VAL into bit offset */ | |
673 | LONGEST val = unpack_long (builtin_type_int, valaddr) << 3; | |
674 | for (i = TYPE_N_BASECLASSES (domain); i < len; i++) | |
675 | { | |
676 | int bitpos = TYPE_FIELD_BITPOS (domain, i); | |
677 | QUIT; | |
678 | if (val == bitpos) | |
679 | break; | |
680 | if (val < bitpos && i != 0) | |
681 | { | |
682 | /* Somehow pointing into a field. */ | |
683 | i -= 1; | |
684 | extra = (val - TYPE_FIELD_BITPOS (domain, i)); | |
685 | if (extra & 0x7) | |
686 | bits = 1; | |
687 | else | |
688 | extra >>= 3; | |
689 | break; | |
690 | } | |
691 | } | |
692 | if (i < len) | |
693 | { | |
694 | char *name; | |
695 | fprintf_filtered (stream, prefix); | |
696 | name = type_name_no_tag (domain); | |
697 | if (name) | |
698 | fputs_filtered (name, stream); | |
699 | else | |
700 | type_print_base (domain, stream, 0, 0); | |
701 | fprintf_filtered (stream, "::"); | |
702 | fputs_filtered (TYPE_FIELD_NAME (domain, i), stream); | |
703 | if (extra) | |
704 | fprintf_filtered (stream, " + %d bytes", extra); | |
705 | if (bits) | |
706 | fprintf_filtered (stream, " (offset in bits)"); | |
707 | } | |
708 | else | |
709 | fprintf_filtered (stream, "%d", val >> 3); | |
710 | } | |
711 | ||
bd5635a1 RP |
712 | /* Print data of type TYPE located at VALADDR (within GDB), |
713 | which came from the inferior at address ADDRESS, | |
714 | onto stdio stream STREAM according to FORMAT | |
715 | (a letter or 0 for natural format). The data at VALADDR | |
716 | is in target byte order. | |
717 | ||
718 | If the data are a string pointer, returns the number of | |
719 | sting characters printed. | |
720 | ||
721 | if DEREF_REF is nonzero, then dereference references, | |
722 | otherwise just print them like pointers. | |
723 | ||
724 | The PRETTY parameter controls prettyprinting. */ | |
725 | ||
726 | int | |
2cd99985 | 727 | val_print (type, valaddr, address, stream, format, deref_ref, recurse, pretty) |
bd5635a1 RP |
728 | struct type *type; |
729 | char *valaddr; | |
730 | CORE_ADDR address; | |
731 | FILE *stream; | |
2cd99985 | 732 | int format; |
bd5635a1 RP |
733 | int deref_ref; |
734 | int recurse; | |
735 | enum val_prettyprint pretty; | |
736 | { | |
737 | register unsigned int i; | |
738 | unsigned len; | |
739 | struct type *elttype; | |
740 | unsigned eltlen; | |
741 | LONGEST val; | |
742 | unsigned char c; | |
743 | ||
744 | if (pretty == Val_pretty_default) | |
745 | { | |
746 | pretty = prettyprint ? Val_prettyprint : Val_no_prettyprint; | |
747 | } | |
748 | ||
749 | QUIT; | |
750 | ||
751 | check_stub_type (type); | |
752 | ||
753 | if (TYPE_FLAGS (type) & TYPE_FLAG_STUB) | |
754 | { | |
755 | fprintf_filtered (stream, "<unknown struct>"); | |
756 | fflush (stream); | |
757 | return 0; | |
758 | } | |
759 | ||
760 | switch (TYPE_CODE (type)) | |
761 | { | |
762 | case TYPE_CODE_ARRAY: | |
2a5ec41d | 763 | if (TYPE_LENGTH (type) > 0 |
bd5635a1 RP |
764 | && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0) |
765 | { | |
766 | elttype = TYPE_TARGET_TYPE (type); | |
767 | eltlen = TYPE_LENGTH (elttype); | |
768 | len = TYPE_LENGTH (type) / eltlen; | |
769 | if (arrayprint) | |
770 | print_spaces_filtered (2 + 2 * recurse, stream); | |
771 | fprintf_filtered (stream, "{"); | |
772 | /* For an array of chars, print with string syntax. */ | |
773 | if (eltlen == 1 && TYPE_CODE (elttype) == TYPE_CODE_INT | |
774 | && (format == 0 || format == 's') ) | |
775 | print_string (stream, valaddr, len, 0); | |
776 | else | |
777 | { | |
778 | unsigned int things_printed = 0; | |
779 | ||
0dce3774 JK |
780 | /* If this is a virtual function table, print the 0th |
781 | entry specially, and the rest of the members normally. */ | |
782 | if (is_vtbl_ptr_type (elttype)) | |
783 | { | |
784 | fprintf_filtered (stream, "%d vtable entries", len-1); | |
785 | i = 1; | |
786 | } | |
787 | else | |
788 | i = 0; | |
789 | ||
790 | for (; i < len && things_printed < print_max; i++) | |
bd5635a1 RP |
791 | { |
792 | /* Position of the array element we are examining to see | |
793 | whether it is repeated. */ | |
794 | unsigned int rep1; | |
795 | /* Number of repetitions we have detected so far. */ | |
796 | unsigned int reps; | |
797 | ||
798 | if (i != 0) | |
799 | if (arrayprint) | |
800 | { | |
801 | fprintf_filtered (stream, ",\n"); | |
802 | print_spaces_filtered (2 + 2 * recurse, stream); | |
803 | } | |
804 | else | |
805 | fprintf_filtered (stream, ", "); | |
806 | wrap_here (n_spaces (2 + 2 * recurse)); | |
807 | ||
808 | rep1 = i + 1; | |
809 | reps = 1; | |
810 | while (rep1 < len | |
811 | && !bcmp (valaddr + i * eltlen, | |
812 | valaddr + rep1 * eltlen, eltlen)) | |
813 | { | |
814 | ++reps; | |
815 | ++rep1; | |
816 | } | |
817 | ||
818 | if (reps > REPEAT_COUNT_THRESHOLD) | |
819 | { | |
820 | val_print (elttype, valaddr + i * eltlen, | |
821 | 0, stream, format, deref_ref, | |
822 | recurse + 1, pretty); | |
823 | fprintf_filtered (stream, " <repeats %u times>", reps); | |
824 | i = rep1 - 1; | |
825 | things_printed += REPEAT_COUNT_THRESHOLD; | |
826 | } | |
827 | else | |
828 | { | |
829 | val_print (elttype, valaddr + i * eltlen, | |
830 | 0, stream, format, deref_ref, | |
831 | recurse + 1, pretty); | |
832 | things_printed++; | |
833 | } | |
834 | } | |
835 | if (i < len) | |
836 | fprintf_filtered (stream, "..."); | |
837 | } | |
838 | fprintf_filtered (stream, "}"); | |
839 | break; | |
840 | } | |
841 | /* Array of unspecified length: treat like pointer to first elt. */ | |
842 | valaddr = (char *) &address; | |
843 | ||
844 | case TYPE_CODE_PTR: | |
845 | if (format && format != 's') | |
846 | { | |
847 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
848 | break; | |
849 | } | |
850 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD) | |
851 | { | |
852 | struct type *domain = TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)); | |
853 | struct fn_field *f; | |
854 | int j, len2; | |
855 | char *kind = ""; | |
e1ce8aa5 | 856 | CORE_ADDR addr; |
bd5635a1 | 857 | |
e1ce8aa5 JK |
858 | addr = unpack_pointer (lookup_pointer_type (builtin_type_void), |
859 | valaddr); | |
2a5ec41d | 860 | if (addr < 128) /* FIXME! What is this 128? */ |
bd5635a1 RP |
861 | { |
862 | len = TYPE_NFN_FIELDS (domain); | |
863 | for (i = 0; i < len; i++) | |
864 | { | |
865 | f = TYPE_FN_FIELDLIST1 (domain, i); | |
866 | len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i); | |
867 | ||
868 | for (j = 0; j < len2; j++) | |
869 | { | |
870 | QUIT; | |
e1ce8aa5 | 871 | if (TYPE_FN_FIELD_VOFFSET (f, j) == addr) |
bd5635a1 RP |
872 | { |
873 | kind = "virtual"; | |
874 | goto common; | |
875 | } | |
876 | } | |
877 | } | |
878 | } | |
879 | else | |
880 | { | |
e1ce8aa5 | 881 | struct symbol *sym = find_pc_function (addr); |
bd5635a1 RP |
882 | if (sym == 0) |
883 | error ("invalid pointer to member function"); | |
884 | len = TYPE_NFN_FIELDS (domain); | |
885 | for (i = 0; i < len; i++) | |
886 | { | |
887 | f = TYPE_FN_FIELDLIST1 (domain, i); | |
888 | len2 = TYPE_FN_FIELDLIST_LENGTH (domain, i); | |
889 | ||
890 | for (j = 0; j < len2; j++) | |
891 | { | |
892 | QUIT; | |
893 | if (!strcmp (SYMBOL_NAME (sym), TYPE_FN_FIELD_PHYSNAME (f, j))) | |
894 | goto common; | |
895 | } | |
896 | } | |
897 | } | |
898 | common: | |
899 | if (i < len) | |
900 | { | |
901 | fprintf_filtered (stream, "&"); | |
902 | type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (f, j), stream, 0, 0); | |
903 | fprintf (stream, kind); | |
904 | if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_' | |
905 | && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER) | |
906 | type_print_method_args | |
907 | (TYPE_FN_FIELD_ARGS (f, j) + 1, "~", | |
908 | TYPE_FN_FIELDLIST_NAME (domain, i), 0, stream); | |
909 | else | |
910 | type_print_method_args | |
911 | (TYPE_FN_FIELD_ARGS (f, j), "", | |
912 | TYPE_FN_FIELDLIST_NAME (domain, i), 0, stream); | |
913 | break; | |
914 | } | |
915 | fprintf_filtered (stream, "("); | |
916 | type_print (type, "", stream, -1); | |
e1ce8aa5 | 917 | fprintf_filtered (stream, ") %d", (int) addr >> 3); |
bd5635a1 RP |
918 | } |
919 | else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER) | |
920 | { | |
4a11eef2 FF |
921 | print_class_member (valaddr, |
922 | TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)), | |
923 | stream, "&"); | |
bd5635a1 RP |
924 | } |
925 | else | |
926 | { | |
e1ce8aa5 | 927 | CORE_ADDR addr = unpack_pointer (type, valaddr); |
bd5635a1 RP |
928 | elttype = TYPE_TARGET_TYPE (type); |
929 | ||
930 | if (TYPE_CODE (elttype) == TYPE_CODE_FUNC) | |
931 | { | |
932 | /* Try to print what function it points to. */ | |
933 | print_address_demangle (addr, stream, demangle); | |
934 | /* Return value is irrelevant except for string pointers. */ | |
935 | return 0; | |
936 | } | |
937 | ||
938 | if (addressprint && format != 's') | |
939 | fprintf_filtered (stream, "0x%x", addr); | |
940 | ||
941 | /* For a pointer to char or unsigned char, | |
942 | also print the string pointed to, unless pointer is null. */ | |
943 | i = 0; /* Number of characters printed. */ | |
944 | if (TYPE_LENGTH (elttype) == 1 | |
945 | && TYPE_CODE (elttype) == TYPE_CODE_INT | |
946 | && (format == 0 || format == 's') | |
947 | && addr != 0 | |
948 | /* If print_max is UINT_MAX, the alloca below will fail. | |
949 | In that case don't try to print the string. */ | |
950 | && print_max < UINT_MAX) | |
951 | { | |
952 | int first_addr_err = 0; | |
953 | int errcode = 0; | |
954 | ||
955 | /* Get first character. */ | |
956 | errcode = target_read_memory (addr, (char *)&c, 1); | |
957 | if (errcode != 0) | |
958 | { | |
959 | /* First address out of bounds. */ | |
960 | first_addr_err = 1; | |
961 | } | |
962 | else | |
963 | { | |
964 | /* A real string. */ | |
965 | char *string = (char *) alloca (print_max); | |
966 | ||
967 | /* If the loop ends by us hitting print_max characters, | |
968 | we need to have elipses at the end. */ | |
969 | int force_ellipses = 1; | |
970 | ||
971 | /* This loop always fetches print_max characters, even | |
972 | though print_string might want to print more or fewer | |
973 | (with repeated characters). This is so that | |
974 | we don't spend forever fetching if we print | |
975 | a long string consisting of the same character | |
976 | repeated. Also so we can do it all in one memory | |
977 | operation, which is faster. However, this will be | |
978 | slower if print_max is set high, e.g. if you set | |
979 | print_max to 1000, not only will it take a long | |
980 | time to fetch short strings, but if you are near | |
2cd99985 | 981 | the end of the address space, it might not work. */ |
bd5635a1 RP |
982 | QUIT; |
983 | errcode = target_read_memory (addr, string, print_max); | |
2cd99985 PB |
984 | if (errcode != 0) |
985 | { | |
986 | /* Try reading just one character. If that succeeds, | |
987 | assume we hit the end of the address space, but | |
988 | the initial part of the string is probably safe. */ | |
989 | char x[1]; | |
990 | errcode = target_read_memory (addr, x, 1); | |
991 | } | |
bd5635a1 RP |
992 | if (errcode != 0) |
993 | force_ellipses = 0; | |
994 | else | |
995 | for (i = 0; i < print_max; i++) | |
996 | if (string[i] == '\0') | |
997 | { | |
998 | force_ellipses = 0; | |
999 | break; | |
1000 | } | |
1001 | QUIT; | |
1002 | ||
1003 | if (addressprint) | |
1004 | fputs_filtered (" ", stream); | |
1005 | print_string (stream, string, i, force_ellipses); | |
1006 | } | |
1007 | ||
1008 | if (errcode != 0) | |
1009 | { | |
1010 | if (errcode == EIO) | |
1011 | { | |
1012 | fprintf_filtered (stream, | |
1013 | (" <Address 0x%x out of bounds>" | |
1014 | + first_addr_err), | |
1015 | addr + i); | |
1016 | } | |
1017 | else | |
1018 | { | |
4ace50a5 FF |
1019 | error ("Error reading memory address 0x%x: %s.", |
1020 | addr + i, safe_strerror (errcode)); | |
bd5635a1 RP |
1021 | } |
1022 | } | |
1023 | ||
1024 | fflush (stream); | |
1025 | } | |
1026 | else /* print vtbl's nicely */ | |
1027 | if (is_vtbl_member(type)) | |
1028 | { | |
e1ce8aa5 | 1029 | CORE_ADDR vt_address = unpack_pointer (type, valaddr); |
bd5635a1 | 1030 | |
2cd99985 PB |
1031 | struct minimal_symbol *msymbol = |
1032 | lookup_minimal_symbol_by_pc (vt_address); | |
1033 | if ((msymbol != NULL) && (vt_address == msymbol -> address)) | |
bd5635a1 RP |
1034 | { |
1035 | fputs_filtered (" <", stream); | |
8f793aa5 FF |
1036 | fputs_demangled (msymbol -> name, stream, |
1037 | DMGL_ANSI | DMGL_PARAMS); | |
bd5635a1 RP |
1038 | fputs_filtered (">", stream); |
1039 | } | |
1040 | if (vtblprint) | |
1041 | { | |
e1ce8aa5 | 1042 | value vt_val; |
bd5635a1 | 1043 | |
e1ce8aa5 JK |
1044 | vt_val = value_at (TYPE_TARGET_TYPE (type), vt_address); |
1045 | val_print (VALUE_TYPE (vt_val), VALUE_CONTENTS (vt_val), | |
1046 | VALUE_ADDRESS (vt_val), stream, format, | |
bd5635a1 RP |
1047 | deref_ref, recurse + 1, pretty); |
1048 | if (pretty) | |
1049 | { | |
1050 | fprintf_filtered (stream, "\n"); | |
1051 | print_spaces_filtered (2 + 2 * recurse, stream); | |
1052 | } | |
1053 | } | |
1054 | } | |
1055 | ||
1056 | /* Return number of characters printed, plus one for the | |
1057 | terminating null if we have "reached the end". */ | |
1058 | return i + (print_max && i != print_max); | |
1059 | } | |
1060 | break; | |
1061 | ||
1062 | case TYPE_CODE_MEMBER: | |
1063 | error ("not implemented: member type in val_print"); | |
1064 | break; | |
1065 | ||
1066 | case TYPE_CODE_REF: | |
4a11eef2 FF |
1067 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_MEMBER) |
1068 | { | |
1069 | print_class_member (valaddr, | |
1070 | TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (type)), | |
1071 | stream, ""); | |
1072 | break; | |
1073 | } | |
bd5635a1 RP |
1074 | if (addressprint) |
1075 | { | |
1076 | fprintf_filtered (stream, "@0x%lx", | |
1077 | unpack_long (builtin_type_int, valaddr)); | |
1078 | if (deref_ref) | |
1079 | fputs_filtered (": ", stream); | |
1080 | } | |
1081 | /* De-reference the reference. */ | |
1082 | if (deref_ref) | |
1083 | { | |
1084 | if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF) | |
1085 | { | |
e1ce8aa5 JK |
1086 | value deref_val = |
1087 | value_at | |
1088 | (TYPE_TARGET_TYPE (type), | |
1089 | unpack_pointer (lookup_pointer_type (builtin_type_void), | |
1090 | valaddr)); | |
1091 | val_print (VALUE_TYPE (deref_val), VALUE_CONTENTS (deref_val), | |
1092 | VALUE_ADDRESS (deref_val), stream, format, | |
bd5635a1 RP |
1093 | deref_ref, recurse + 1, pretty); |
1094 | } | |
1095 | else | |
1096 | fputs_filtered ("???", stream); | |
1097 | } | |
1098 | break; | |
1099 | ||
1100 | case TYPE_CODE_UNION: | |
1101 | if (recurse && !unionprint) | |
1102 | { | |
1103 | fprintf_filtered (stream, "{...}"); | |
1104 | break; | |
1105 | } | |
1106 | /* Fall through. */ | |
1107 | case TYPE_CODE_STRUCT: | |
1108 | if (vtblprint && is_vtbl_ptr_type(type)) | |
1109 | { | |
1110 | /* Print the unmangled name if desired. */ | |
1111 | print_address_demangle(*((int *) (valaddr + /* FIXME bytesex */ | |
1112 | TYPE_FIELD_BITPOS (type, VTBL_FNADDR_OFFSET) / 8)), | |
1113 | stream, demangle); | |
1114 | break; | |
1115 | } | |
1116 | val_print_fields (type, valaddr, stream, format, recurse, pretty, 0); | |
1117 | break; | |
1118 | ||
1119 | case TYPE_CODE_ENUM: | |
1120 | if (format) | |
1121 | { | |
1122 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
1123 | break; | |
1124 | } | |
1125 | len = TYPE_NFIELDS (type); | |
1126 | val = unpack_long (builtin_type_int, valaddr); | |
1127 | for (i = 0; i < len; i++) | |
1128 | { | |
1129 | QUIT; | |
1130 | if (val == TYPE_FIELD_BITPOS (type, i)) | |
1131 | break; | |
1132 | } | |
1133 | if (i < len) | |
1134 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
1135 | else | |
e1ce8aa5 JK |
1136 | #ifdef LONG_LONG |
1137 | fprintf_filtered (stream, "%lld", val); | |
1138 | #else | |
1139 | fprintf_filtered (stream, "%ld", val); | |
1140 | #endif | |
bd5635a1 RP |
1141 | break; |
1142 | ||
1143 | case TYPE_CODE_FUNC: | |
1144 | if (format) | |
1145 | { | |
1146 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
1147 | break; | |
1148 | } | |
36b9d39c JG |
1149 | /* FIXME, we should consider, at least for ANSI C language, eliminating |
1150 | the distinction made between FUNCs and POINTERs to FUNCs. */ | |
bd5635a1 RP |
1151 | fprintf_filtered (stream, "{"); |
1152 | type_print (type, "", stream, -1); | |
1153 | fprintf_filtered (stream, "} "); | |
36b9d39c JG |
1154 | /* Try to print what function it points to, and its address. */ |
1155 | print_address_demangle (address, stream, demangle); | |
bd5635a1 RP |
1156 | break; |
1157 | ||
1158 | case TYPE_CODE_INT: | |
1159 | if (format || output_format) | |
1160 | { | |
1161 | print_scalar_formatted (valaddr, type, | |
1162 | format? format: output_format, | |
1163 | 0, stream); | |
1164 | break; | |
1165 | } | |
1166 | if (TYPE_LENGTH (type) > sizeof (LONGEST)) | |
1167 | { | |
1168 | if (TYPE_UNSIGNED (type)) | |
1169 | { | |
1170 | /* First figure out whether the number in fact has zeros | |
1171 | in all its bytes more significant than least significant | |
1172 | sizeof (LONGEST) ones. */ | |
1173 | char *p; | |
1174 | /* Pointer to first (i.e. lowest address) nonzero character. */ | |
1175 | char *first_addr; | |
e1ce8aa5 | 1176 | len = TYPE_LENGTH (type); |
bd5635a1 RP |
1177 | |
1178 | #if TARGET_BYTE_ORDER == BIG_ENDIAN | |
1179 | for (p = valaddr; | |
1180 | len > sizeof (LONGEST) | |
1181 | && p < valaddr + TYPE_LENGTH (type); | |
1182 | p++) | |
1183 | #else /* Little endian. */ | |
1184 | first_addr = valaddr; | |
1185 | for (p = valaddr + TYPE_LENGTH (type); | |
1186 | len > sizeof (LONGEST) && p >= valaddr; | |
1187 | p--) | |
1188 | #endif /* Little endian. */ | |
1189 | { | |
1190 | if (*p == 0) | |
1191 | len--; | |
1192 | else | |
1193 | break; | |
1194 | } | |
1195 | #if TARGET_BYTE_ORDER == BIG_ENDIAN | |
1196 | first_addr = p; | |
1197 | #endif | |
1198 | ||
1199 | if (len <= sizeof (LONGEST)) | |
1200 | { | |
1201 | /* We can print it in decimal. */ | |
1202 | fprintf_filtered | |
1203 | (stream, | |
1204 | #if defined (LONG_LONG) | |
1205 | "%llu", | |
1206 | #else | |
1207 | "%lu", | |
1208 | #endif | |
1209 | unpack_long (BUILTIN_TYPE_LONGEST, first_addr)); | |
1210 | } | |
1211 | else | |
1212 | { | |
1213 | /* It is big, so print it in hex. */ | |
1214 | print_hex_chars (stream, (unsigned char *)first_addr, len); | |
1215 | } | |
1216 | } | |
1217 | else | |
1218 | { | |
1219 | /* Signed. One could assume two's complement (a reasonable | |
1220 | assumption, I think) and do better than this. */ | |
1221 | print_hex_chars (stream, (unsigned char *)valaddr, | |
1222 | TYPE_LENGTH (type)); | |
1223 | } | |
1224 | break; | |
1225 | } | |
1226 | #ifdef PRINT_TYPELESS_INTEGER | |
1227 | PRINT_TYPELESS_INTEGER (stream, type, unpack_long (type, valaddr)); | |
1228 | #else | |
1229 | #ifndef LONG_LONG | |
1230 | fprintf_filtered (stream, | |
1231 | TYPE_UNSIGNED (type) ? "%u" : "%d", | |
1232 | unpack_long (type, valaddr)); | |
1233 | #else | |
1234 | fprintf_filtered (stream, | |
1235 | TYPE_UNSIGNED (type) ? "%llu" : "%lld", | |
1236 | unpack_long (type, valaddr)); | |
1237 | #endif | |
1238 | #endif | |
1239 | ||
1240 | if (TYPE_LENGTH (type) == 1) | |
1241 | { | |
1242 | fprintf_filtered (stream, " '"); | |
1243 | printchar ((unsigned char) unpack_long (type, valaddr), | |
1244 | stream, '\''); | |
1245 | fprintf_filtered (stream, "'"); | |
1246 | } | |
1247 | break; | |
1248 | ||
1249 | case TYPE_CODE_FLT: | |
1250 | if (format) | |
1251 | print_scalar_formatted (valaddr, type, format, 0, stream); | |
1252 | else | |
1253 | print_floating (valaddr, type, stream); | |
1254 | break; | |
1255 | ||
1256 | case TYPE_CODE_VOID: | |
1257 | fprintf_filtered (stream, "void"); | |
1258 | break; | |
1259 | ||
1260 | case TYPE_CODE_UNDEF: | |
1261 | /* This happens (without TYPE_FLAG_STUB set) on systems which don't use | |
1262 | dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar" | |
1263 | and no complete type for struct foo in that file. */ | |
1264 | fprintf_filtered (stream, "<unknown struct>"); | |
1265 | break; | |
1266 | ||
1267 | case TYPE_CODE_ERROR: | |
1268 | fprintf_filtered (stream, "?"); | |
1269 | break; | |
1270 | ||
e2aab031 FF |
1271 | case TYPE_CODE_RANGE: |
1272 | /* FIXME, we should not ever have to print one of these yet. */ | |
1273 | fprintf_filtered (stream, "<range type>"); | |
1274 | break; | |
1275 | ||
bd5635a1 RP |
1276 | default: |
1277 | error ("Invalid type code in symbol table."); | |
1278 | } | |
1279 | fflush (stream); | |
1280 | return 0; | |
1281 | } | |
1282 | \f | |
be3bc7ad JG |
1283 | /* Print a description of a type in the format of a |
1284 | typedef for the current language. | |
1285 | NEW is the new name for a type TYPE. */ | |
1286 | void | |
1287 | typedef_print (type, new, stream) | |
1288 | struct type *type; | |
1289 | struct symbol *new; | |
1290 | FILE *stream; | |
1291 | { | |
1292 | switch (current_language->la_language) | |
1293 | { | |
1294 | #ifdef _LANG_c | |
1295 | case language_c: | |
7d9884b9 | 1296 | case language_cplus: |
be3bc7ad JG |
1297 | fprintf_filtered(stream, "typedef "); |
1298 | type_print(type,"",stream,0); | |
1299 | if(TYPE_NAME ((SYMBOL_TYPE (new))) == 0 | |
1300 | || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (new))), | |
1301 | SYMBOL_NAME (new))) | |
1302 | fprintf_filtered(stream, " %s", SYMBOL_NAME(new)); | |
1303 | break; | |
1304 | #endif | |
1305 | #ifdef _LANG_m2 | |
1306 | case language_m2: | |
1307 | fprintf_filtered(stream, "TYPE "); | |
1308 | if(!TYPE_NAME(SYMBOL_TYPE(new)) || | |
1309 | strcmp (TYPE_NAME(SYMBOL_TYPE(new)), | |
1310 | SYMBOL_NAME(new))) | |
1311 | fprintf_filtered(stream, "%s = ", SYMBOL_NAME(new)); | |
1312 | else | |
1313 | fprintf_filtered(stream, "<builtin> = "); | |
1314 | type_print(type,"",stream,0); | |
1315 | break; | |
1316 | #endif | |
1317 | default: | |
1318 | error("Language not supported."); | |
1319 | } | |
1320 | fprintf_filtered(stream, ";\n"); | |
1321 | } | |
1322 | ||
1323 | ||
bd5635a1 RP |
1324 | /* Print a description of a type TYPE |
1325 | in the form of a declaration of a variable named VARSTRING. | |
1326 | (VARSTRING is demangled if necessary.) | |
1327 | Output goes to STREAM (via stdio). | |
1328 | If SHOW is positive, we show the contents of the outermost level | |
1329 | of structure even if there is a type name that could be used instead. | |
1330 | If SHOW is negative, we never show the details of elements' types. */ | |
1331 | ||
1332 | void | |
1333 | type_print (type, varstring, stream, show) | |
1334 | struct type *type; | |
1335 | char *varstring; | |
1336 | FILE *stream; | |
1337 | int show; | |
1338 | { | |
1339 | type_print_1 (type, varstring, stream, show, 0); | |
1340 | } | |
1341 | ||
1342 | /* LEVEL is the depth to indent lines by. */ | |
1343 | ||
1344 | void | |
1345 | type_print_1 (type, varstring, stream, show, level) | |
1346 | struct type *type; | |
1347 | char *varstring; | |
1348 | FILE *stream; | |
1349 | int show; | |
1350 | int level; | |
1351 | { | |
1352 | register enum type_code code; | |
f9b5584c FF |
1353 | char *demangled; |
1354 | ||
bd5635a1 RP |
1355 | type_print_base (type, stream, show, level); |
1356 | code = TYPE_CODE (type); | |
1357 | if ((varstring && *varstring) | |
1358 | || | |
1359 | /* Need a space if going to print stars or brackets; | |
1360 | but not if we will print just a type name. */ | |
1361 | ((show > 0 || TYPE_NAME (type) == 0) | |
1362 | && | |
1363 | (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC | |
1364 | || code == TYPE_CODE_METHOD | |
1365 | || code == TYPE_CODE_ARRAY | |
1366 | || code == TYPE_CODE_MEMBER | |
1367 | || code == TYPE_CODE_REF))) | |
1368 | fprintf_filtered (stream, " "); | |
1369 | type_print_varspec_prefix (type, stream, show, 0); | |
f9b5584c FF |
1370 | if (demangle) |
1371 | { | |
1372 | demangled = cplus_demangle (varstring, DMGL_ANSI | DMGL_PARAMS); | |
1373 | } | |
1374 | if ((demangled != NULL) && (code == TYPE_CODE_FUNC)) | |
1375 | { | |
1376 | /* For demangled function names, we have the arglist as part | |
1377 | of the name, so don't print an additional pair of ()'s */ | |
1378 | fputs_filtered (demangled, stream); | |
1379 | type_print_varspec_suffix (type, stream, show, 0, 1); | |
1380 | free (demangled); | |
1381 | } | |
1382 | else | |
1383 | { | |
1384 | fputs_filtered (varstring, stream); | |
1385 | type_print_varspec_suffix (type, stream, show, 0, 0); | |
1386 | } | |
bd5635a1 RP |
1387 | } |
1388 | ||
1389 | /* Print the method arguments ARGS to the file STREAM. */ | |
1390 | static void | |
1391 | type_print_method_args (args, prefix, varstring, staticp, stream) | |
1392 | struct type **args; | |
1393 | char *prefix, *varstring; | |
1394 | int staticp; | |
1395 | FILE *stream; | |
1396 | { | |
1397 | int i; | |
1398 | ||
8f793aa5 FF |
1399 | fputs_demangled (prefix, stream, DMGL_ANSI | DMGL_PARAMS); |
1400 | fputs_demangled (varstring, stream, DMGL_ANSI | DMGL_PARAMS); | |
bd5635a1 RP |
1401 | fputs_filtered (" (", stream); |
1402 | if (args && args[!staticp] && args[!staticp]->code != TYPE_CODE_VOID) | |
1403 | { | |
1404 | i = !staticp; /* skip the class variable */ | |
1405 | while (1) | |
1406 | { | |
1407 | type_print (args[i++], "", stream, 0); | |
1408 | if (!args[i]) | |
1409 | { | |
1410 | fprintf_filtered (stream, " ..."); | |
1411 | break; | |
1412 | } | |
1413 | else if (args[i]->code != TYPE_CODE_VOID) | |
1414 | { | |
1415 | fprintf_filtered (stream, ", "); | |
1416 | } | |
1417 | else break; | |
1418 | } | |
1419 | } | |
1420 | fprintf_filtered (stream, ")"); | |
1421 | } | |
1422 | ||
1423 | /* If TYPE is a derived type, then print out derivation | |
1424 | information. Print out all layers of the type heirarchy | |
1425 | until we encounter one with multiple inheritance. | |
1426 | At that point, print out that ply, and return. */ | |
1427 | static void | |
1428 | type_print_derivation_info (stream, type) | |
1429 | FILE *stream; | |
1430 | struct type *type; | |
1431 | { | |
1432 | char *name; | |
1433 | int i, n_baseclasses = TYPE_N_BASECLASSES (type); | |
1434 | struct type *basetype = 0; | |
1435 | ||
1436 | while (type && n_baseclasses > 0) | |
1437 | { | |
1438 | /* Not actually sure about this one -- Bryan. */ | |
1439 | check_stub_type (type); | |
1440 | ||
1441 | fprintf_filtered (stream, ": "); | |
1442 | for (i = 0; ;) | |
1443 | { | |
1444 | basetype = TYPE_BASECLASS (type, i); | |
1445 | if (name = type_name_no_tag (basetype)) | |
1446 | { | |
1447 | fprintf_filtered (stream, "%s%s ", | |
1448 | BASETYPE_VIA_PUBLIC(type, i) ? "public" : "private", | |
1449 | BASETYPE_VIA_VIRTUAL(type, i) ? " virtual" : ""); | |
1450 | fputs_filtered (name, stream); | |
1451 | } | |
1452 | i++; | |
1453 | if (i >= n_baseclasses) | |
1454 | break; | |
1455 | fprintf_filtered (stream, ", "); | |
1456 | } | |
1457 | ||
1458 | fprintf_filtered (stream, " "); | |
1459 | if (n_baseclasses != 1) | |
1460 | break; | |
1461 | n_baseclasses = TYPE_N_BASECLASSES (basetype); | |
1462 | type = basetype; | |
1463 | } | |
1464 | } | |
1465 | ||
1466 | /* Print any asterisks or open-parentheses needed before the | |
1467 | variable name (to describe its type). | |
1468 | ||
1469 | On outermost call, pass 0 for PASSED_A_PTR. | |
1470 | On outermost call, SHOW > 0 means should ignore | |
1471 | any typename for TYPE and show its details. | |
1472 | SHOW is always zero on recursive calls. */ | |
1473 | ||
1474 | static void | |
1475 | type_print_varspec_prefix (type, stream, show, passed_a_ptr) | |
1476 | struct type *type; | |
1477 | FILE *stream; | |
1478 | int show; | |
1479 | int passed_a_ptr; | |
1480 | { | |
4a11eef2 | 1481 | char *name; |
bd5635a1 RP |
1482 | if (type == 0) |
1483 | return; | |
1484 | ||
1485 | if (TYPE_NAME (type) && show <= 0) | |
1486 | return; | |
1487 | ||
1488 | QUIT; | |
1489 | ||
1490 | switch (TYPE_CODE (type)) | |
1491 | { | |
1492 | case TYPE_CODE_PTR: | |
1493 | type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1); | |
1494 | fprintf_filtered (stream, "*"); | |
1495 | break; | |
1496 | ||
1497 | case TYPE_CODE_MEMBER: | |
1498 | if (passed_a_ptr) | |
1499 | fprintf_filtered (stream, "("); | |
1500 | type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, | |
1501 | 0); | |
1502 | fprintf_filtered (stream, " "); | |
4a11eef2 FF |
1503 | name = type_name_no_tag (TYPE_DOMAIN_TYPE (type)); |
1504 | if (name) | |
1505 | fputs_filtered (name, stream); | |
1506 | else | |
1507 | type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, passed_a_ptr); | |
bd5635a1 RP |
1508 | fprintf_filtered (stream, "::"); |
1509 | break; | |
1510 | ||
1511 | case TYPE_CODE_METHOD: | |
1512 | if (passed_a_ptr) | |
1513 | fprintf (stream, "("); | |
1514 | type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, | |
1515 | 0); | |
0dce3774 JK |
1516 | if (passed_a_ptr) |
1517 | { | |
1518 | fprintf_filtered (stream, " "); | |
1519 | type_print_base (TYPE_DOMAIN_TYPE (type), stream, 0, | |
1520 | passed_a_ptr); | |
1521 | fprintf_filtered (stream, "::"); | |
1522 | } | |
bd5635a1 RP |
1523 | break; |
1524 | ||
1525 | case TYPE_CODE_REF: | |
1526 | type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1); | |
1527 | fprintf_filtered (stream, "&"); | |
1528 | break; | |
1529 | ||
1530 | case TYPE_CODE_FUNC: | |
1531 | type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, | |
1532 | 0); | |
1533 | if (passed_a_ptr) | |
1534 | fprintf_filtered (stream, "("); | |
1535 | break; | |
1536 | ||
1537 | case TYPE_CODE_ARRAY: | |
1538 | type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, | |
1539 | 0); | |
1540 | if (passed_a_ptr) | |
1541 | fprintf_filtered (stream, "("); | |
2a5ec41d | 1542 | break; |
bd5635a1 RP |
1543 | |
1544 | case TYPE_CODE_UNDEF: | |
1545 | case TYPE_CODE_STRUCT: | |
1546 | case TYPE_CODE_UNION: | |
1547 | case TYPE_CODE_ENUM: | |
1548 | case TYPE_CODE_INT: | |
1549 | case TYPE_CODE_FLT: | |
1550 | case TYPE_CODE_VOID: | |
1551 | case TYPE_CODE_ERROR: | |
7d9884b9 JG |
1552 | case TYPE_CODE_CHAR: |
1553 | case TYPE_CODE_BOOL: | |
bd5635a1 RP |
1554 | /* These types need no prefix. They are listed here so that |
1555 | gcc -Wall will reveal any types that haven't been handled. */ | |
1556 | break; | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | /* Print any array sizes, function arguments or close parentheses | |
1561 | needed after the variable name (to describe its type). | |
1562 | Args work like type_print_varspec_prefix. */ | |
1563 | ||
1564 | static void | |
f9b5584c | 1565 | type_print_varspec_suffix (type, stream, show, passed_a_ptr, demangled_args) |
bd5635a1 RP |
1566 | struct type *type; |
1567 | FILE *stream; | |
1568 | int show; | |
1569 | int passed_a_ptr; | |
f9b5584c | 1570 | int demangled_args; |
bd5635a1 RP |
1571 | { |
1572 | if (type == 0) | |
1573 | return; | |
1574 | ||
1575 | if (TYPE_NAME (type) && show <= 0) | |
1576 | return; | |
1577 | ||
1578 | QUIT; | |
1579 | ||
1580 | switch (TYPE_CODE (type)) | |
1581 | { | |
1582 | case TYPE_CODE_ARRAY: | |
1583 | if (passed_a_ptr) | |
1584 | fprintf_filtered (stream, ")"); | |
1585 | ||
1586 | fprintf_filtered (stream, "["); | |
bee3c1a1 JG |
1587 | if (TYPE_LENGTH (type) > 0 |
1588 | && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0) | |
bd5635a1 RP |
1589 | fprintf_filtered (stream, "%d", |
1590 | (TYPE_LENGTH (type) | |
1591 | / TYPE_LENGTH (TYPE_TARGET_TYPE (type)))); | |
1592 | fprintf_filtered (stream, "]"); | |
1593 | ||
1594 | type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, | |
f9b5584c | 1595 | 0, 0); |
bd5635a1 RP |
1596 | break; |
1597 | ||
1598 | case TYPE_CODE_MEMBER: | |
1599 | if (passed_a_ptr) | |
1600 | fprintf_filtered (stream, ")"); | |
f9b5584c | 1601 | type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0); |
bd5635a1 RP |
1602 | break; |
1603 | ||
1604 | case TYPE_CODE_METHOD: | |
1605 | if (passed_a_ptr) | |
1606 | fprintf_filtered (stream, ")"); | |
f9b5584c | 1607 | type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 0, 0); |
bd5635a1 RP |
1608 | if (passed_a_ptr) |
1609 | { | |
1610 | int i; | |
1611 | struct type **args = TYPE_ARG_TYPES (type); | |
1612 | ||
1613 | fprintf_filtered (stream, "("); | |
1614 | if (args[1] == 0) | |
1615 | fprintf_filtered (stream, "..."); | |
1616 | else for (i = 1; args[i] != 0 && args[i]->code != TYPE_CODE_VOID; i++) | |
1617 | { | |
1618 | type_print_1 (args[i], "", stream, -1, 0); | |
1619 | if (args[i+1] == 0) | |
1620 | fprintf_filtered (stream, "..."); | |
8ffd75c8 | 1621 | else if (args[i+1]->code != TYPE_CODE_VOID) { |
bd5635a1 | 1622 | fprintf_filtered (stream, ","); |
8ffd75c8 JG |
1623 | wrap_here (" "); |
1624 | } | |
bd5635a1 RP |
1625 | } |
1626 | fprintf_filtered (stream, ")"); | |
1627 | } | |
1628 | break; | |
1629 | ||
1630 | case TYPE_CODE_PTR: | |
1631 | case TYPE_CODE_REF: | |
f9b5584c | 1632 | type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0); |
bd5635a1 RP |
1633 | break; |
1634 | ||
1635 | case TYPE_CODE_FUNC: | |
1636 | type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, | |
f9b5584c | 1637 | passed_a_ptr, 0); |
bd5635a1 RP |
1638 | if (passed_a_ptr) |
1639 | fprintf_filtered (stream, ")"); | |
f9b5584c FF |
1640 | if (!demangled_args) |
1641 | fprintf_filtered (stream, "()"); | |
bd5635a1 RP |
1642 | break; |
1643 | ||
1644 | case TYPE_CODE_UNDEF: | |
1645 | case TYPE_CODE_STRUCT: | |
1646 | case TYPE_CODE_UNION: | |
1647 | case TYPE_CODE_ENUM: | |
1648 | case TYPE_CODE_INT: | |
1649 | case TYPE_CODE_FLT: | |
1650 | case TYPE_CODE_VOID: | |
1651 | case TYPE_CODE_ERROR: | |
7d9884b9 JG |
1652 | case TYPE_CODE_CHAR: |
1653 | case TYPE_CODE_BOOL: | |
bd5635a1 RP |
1654 | /* These types do not need a suffix. They are listed so that |
1655 | gcc -Wall will report types that may not have been considered. */ | |
1656 | break; | |
1657 | } | |
1658 | } | |
1659 | ||
1660 | /* Print the name of the type (or the ultimate pointer target, | |
1661 | function value or array element), or the description of a | |
1662 | structure or union. | |
1663 | ||
1664 | SHOW nonzero means don't print this type as just its name; | |
1665 | show its real definition even if it has a name. | |
1666 | SHOW zero means print just typename or struct tag if there is one | |
1667 | SHOW negative means abbreviate structure elements. | |
1668 | SHOW is decremented for printing of structure elements. | |
1669 | ||
1670 | LEVEL is the depth to indent by. | |
1671 | We increase it for some recursive calls. */ | |
1672 | ||
1673 | static void | |
1674 | type_print_base (type, stream, show, level) | |
1675 | struct type *type; | |
1676 | FILE *stream; | |
1677 | int show; | |
1678 | int level; | |
1679 | { | |
1680 | char *name; | |
1681 | register int i; | |
1682 | register int len; | |
1683 | register int lastval; | |
1684 | ||
1685 | QUIT; | |
1686 | ||
8ffd75c8 | 1687 | wrap_here (" "); |
4ace50a5 | 1688 | if (type == NULL) |
bd5635a1 | 1689 | { |
4ace50a5 | 1690 | fputs_filtered ("<type unknown>", stream); |
bd5635a1 RP |
1691 | return; |
1692 | } | |
1693 | ||
4ace50a5 FF |
1694 | /* When SHOW is zero or less, and there is a valid type name, then always |
1695 | just print the type name directly from the type. */ | |
4a11eef2 | 1696 | |
4ace50a5 | 1697 | if ((show <= 0) && (TYPE_NAME (type) != NULL)) |
bd5635a1 RP |
1698 | { |
1699 | fputs_filtered (TYPE_NAME (type), stream); | |
1700 | return; | |
1701 | } | |
1702 | ||
1703 | switch (TYPE_CODE (type)) | |
1704 | { | |
1705 | case TYPE_CODE_ARRAY: | |
1706 | case TYPE_CODE_PTR: | |
1707 | case TYPE_CODE_MEMBER: | |
1708 | case TYPE_CODE_REF: | |
1709 | case TYPE_CODE_FUNC: | |
1710 | case TYPE_CODE_METHOD: | |
1711 | type_print_base (TYPE_TARGET_TYPE (type), stream, show, level); | |
1712 | break; | |
1713 | ||
1714 | case TYPE_CODE_STRUCT: | |
1715 | fprintf_filtered (stream, "struct "); | |
1716 | goto struct_union; | |
1717 | ||
1718 | case TYPE_CODE_UNION: | |
1719 | fprintf_filtered (stream, "union "); | |
1720 | struct_union: | |
1721 | if (name = type_name_no_tag (type)) | |
1722 | { | |
1723 | fputs_filtered (name, stream); | |
1724 | fputs_filtered (" ", stream); | |
8ffd75c8 | 1725 | wrap_here (" "); |
bd5635a1 RP |
1726 | } |
1727 | if (show < 0) | |
1728 | fprintf_filtered (stream, "{...}"); | |
1729 | else | |
1730 | { | |
1731 | check_stub_type (type); | |
1732 | ||
1733 | type_print_derivation_info (stream, type); | |
1734 | ||
1735 | fprintf_filtered (stream, "{"); | |
1736 | len = TYPE_NFIELDS (type); | |
1737 | if (len) | |
1738 | fprintf_filtered (stream, "\n"); | |
1739 | else | |
1740 | { | |
1741 | if (TYPE_FLAGS (type) & TYPE_FLAG_STUB) | |
1742 | fprintf_filtered (stream, "<incomplete type>\n"); | |
1743 | else | |
1744 | fprintf_filtered (stream, "<no data fields>\n"); | |
1745 | } | |
1746 | ||
1747 | /* If there is a base class for this type, | |
1748 | do not print the field that it occupies. */ | |
1749 | for (i = TYPE_N_BASECLASSES (type); i < len; i++) | |
1750 | { | |
1751 | QUIT; | |
1752 | /* Don't print out virtual function table. */ | |
1753 | if ((TYPE_FIELD_NAME (type, i))[5] == CPLUS_MARKER && | |
1754 | !strncmp (TYPE_FIELD_NAME (type, i), "_vptr", 5)) | |
1755 | continue; | |
1756 | ||
1757 | print_spaces_filtered (level + 4, stream); | |
1758 | if (TYPE_FIELD_STATIC (type, i)) | |
1759 | { | |
1760 | fprintf_filtered (stream, "static "); | |
1761 | } | |
1762 | type_print_1 (TYPE_FIELD_TYPE (type, i), | |
1763 | TYPE_FIELD_NAME (type, i), | |
1764 | stream, show - 1, level + 4); | |
1765 | if (!TYPE_FIELD_STATIC (type, i) | |
1766 | && TYPE_FIELD_PACKED (type, i)) | |
1767 | { | |
1768 | /* It is a bitfield. This code does not attempt | |
1769 | to look at the bitpos and reconstruct filler, | |
1770 | unnamed fields. This would lead to misleading | |
1771 | results if the compiler does not put out fields | |
1772 | for such things (I don't know what it does). */ | |
1773 | fprintf_filtered (stream, " : %d", | |
1774 | TYPE_FIELD_BITSIZE (type, i)); | |
1775 | } | |
1776 | fprintf_filtered (stream, ";\n"); | |
1777 | } | |
1778 | ||
1779 | /* C++: print out the methods */ | |
1780 | len = TYPE_NFN_FIELDS (type); | |
1781 | if (len) fprintf_filtered (stream, "\n"); | |
1782 | for (i = 0; i < len; i++) | |
1783 | { | |
1784 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i); | |
1785 | int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i); | |
24b2fbdc | 1786 | char *method_name = TYPE_FN_FIELDLIST_NAME (type, i); |
2cd99985 | 1787 | int is_constructor = name && strcmp(method_name, name) == 0; |
bd5635a1 RP |
1788 | for (j = 0; j < len2; j++) |
1789 | { | |
1790 | QUIT; | |
1791 | print_spaces_filtered (level + 4, stream); | |
1792 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) | |
1793 | fprintf_filtered (stream, "virtual "); | |
1794 | else if (TYPE_FN_FIELD_STATIC_P (f, j)) | |
1795 | fprintf_filtered (stream, "static "); | |
aec4cb91 MT |
1796 | if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0) |
1797 | { | |
1798 | /* Keep GDB from crashing here. */ | |
1799 | fprintf (stream, "<undefined type> %s;\n", | |
1800 | TYPE_FN_FIELD_PHYSNAME (f, j)); | |
1801 | break; | |
1802 | } | |
24b2fbdc PB |
1803 | else if (!is_constructor) |
1804 | { | |
1805 | type_print (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)), | |
1806 | "", stream, 0); | |
1807 | fputs_filtered (" ", stream); | |
1808 | } | |
1809 | if (TYPE_FN_FIELD_STUB (f, j)) | |
bd5635a1 RP |
1810 | { |
1811 | /* Build something we can demangle. */ | |
bcccec8c | 1812 | char *mangled_name = gdb_mangle_name (type, i, j); |
8f793aa5 FF |
1813 | char *demangled_name = |
1814 | cplus_demangle (mangled_name, | |
1815 | DMGL_ANSI | DMGL_PARAMS); | |
bd5635a1 | 1816 | if (demangled_name == 0) |
24b2fbdc | 1817 | fprintf_filtered (stream, "<badly mangled name %s>", |
bd5635a1 RP |
1818 | mangled_name); |
1819 | else | |
1820 | { | |
24b2fbdc | 1821 | fprintf_filtered (stream, "%s", |
bd5635a1 RP |
1822 | strchr (demangled_name, ':') + 2); |
1823 | free (demangled_name); | |
1824 | } | |
1825 | free (mangled_name); | |
1826 | } | |
1827 | else if (TYPE_FN_FIELD_PHYSNAME (f, j)[0] == '_' | |
1828 | && TYPE_FN_FIELD_PHYSNAME (f, j)[1] == CPLUS_MARKER) | |
1829 | type_print_method_args | |
1830 | (TYPE_FN_FIELD_ARGS (f, j) + 1, "~", | |
24b2fbdc | 1831 | method_name, 0, stream); |
bd5635a1 RP |
1832 | else |
1833 | type_print_method_args | |
1834 | (TYPE_FN_FIELD_ARGS (f, j), "", | |
24b2fbdc | 1835 | method_name, |
bd5635a1 RP |
1836 | TYPE_FN_FIELD_STATIC_P (f, j), stream); |
1837 | ||
1838 | fprintf_filtered (stream, ";\n"); | |
1839 | } | |
1840 | } | |
1841 | ||
1842 | print_spaces_filtered (level, stream); | |
1843 | fprintf_filtered (stream, "}"); | |
1844 | } | |
1845 | break; | |
1846 | ||
1847 | case TYPE_CODE_ENUM: | |
1848 | fprintf_filtered (stream, "enum "); | |
1849 | if (name = type_name_no_tag (type)) | |
1850 | { | |
1851 | fputs_filtered (name, stream); | |
1852 | fputs_filtered (" ", stream); | |
1853 | } | |
8ffd75c8 | 1854 | wrap_here (" "); |
bd5635a1 RP |
1855 | if (show < 0) |
1856 | fprintf_filtered (stream, "{...}"); | |
1857 | else | |
1858 | { | |
1859 | fprintf_filtered (stream, "{"); | |
1860 | len = TYPE_NFIELDS (type); | |
1861 | lastval = 0; | |
1862 | for (i = 0; i < len; i++) | |
1863 | { | |
1864 | QUIT; | |
1865 | if (i) fprintf_filtered (stream, ", "); | |
8ffd75c8 | 1866 | wrap_here (" "); |
bd5635a1 RP |
1867 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); |
1868 | if (lastval != TYPE_FIELD_BITPOS (type, i)) | |
1869 | { | |
1870 | fprintf_filtered (stream, " = %d", TYPE_FIELD_BITPOS (type, i)); | |
1871 | lastval = TYPE_FIELD_BITPOS (type, i); | |
1872 | } | |
1873 | lastval++; | |
1874 | } | |
1875 | fprintf_filtered (stream, "}"); | |
1876 | } | |
1877 | break; | |
1878 | ||
bd5635a1 RP |
1879 | case TYPE_CODE_VOID: |
1880 | fprintf_filtered (stream, "void"); | |
1881 | break; | |
1882 | ||
e2aab031 FF |
1883 | case TYPE_CODE_UNDEF: |
1884 | fprintf_filtered (stream, "struct <unknown>"); | |
bd5635a1 RP |
1885 | break; |
1886 | ||
1887 | case TYPE_CODE_ERROR: | |
1888 | fprintf_filtered (stream, "<unknown type>"); | |
1889 | break; | |
1890 | ||
e2aab031 FF |
1891 | case TYPE_CODE_RANGE: |
1892 | /* This should not occur */ | |
1893 | fprintf_filtered (stream, "<range type>"); | |
1894 | break; | |
1895 | ||
bd5635a1 | 1896 | default: |
4ace50a5 FF |
1897 | /* Handle types not explicitly handled by the other cases, |
1898 | such as fundamental types. For these, just print whatever | |
1899 | the type name is, as recorded in the type itself. If there | |
1900 | is no type name, then complain. */ | |
1901 | if (TYPE_NAME (type) != NULL) | |
1902 | { | |
1903 | fputs_filtered (TYPE_NAME (type), stream); | |
1904 | } | |
1905 | else | |
1906 | { | |
1907 | error ("Invalid type code (%d) in symbol table.", TYPE_CODE (type)); | |
1908 | } | |
1909 | break; | |
bd5635a1 RP |
1910 | } |
1911 | } | |
1912 | \f | |
e1ce8aa5 | 1913 | #if 0 |
bd5635a1 RP |
1914 | /* Validate an input or output radix setting, and make sure the user |
1915 | knows what they really did here. Radix setting is confusing, e.g. | |
1916 | setting the input radix to "10" never changes it! */ | |
1917 | ||
e1ce8aa5 | 1918 | /* ARGSUSED */ |
bd5635a1 RP |
1919 | static void |
1920 | set_input_radix (args, from_tty, c) | |
1921 | char *args; | |
1922 | int from_tty; | |
1923 | struct cmd_list_element *c; | |
1924 | { | |
1925 | unsigned radix = *(unsigned *)c->var; | |
1926 | ||
1927 | if (from_tty) | |
1928 | printf_filtered ("Input radix set to decimal %d, hex %x, octal %o\n", | |
1929 | radix, radix, radix); | |
1930 | } | |
e1ce8aa5 | 1931 | #endif |
bd5635a1 | 1932 | |
e1ce8aa5 | 1933 | /* ARGSUSED */ |
bd5635a1 RP |
1934 | static void |
1935 | set_output_radix (args, from_tty, c) | |
1936 | char *args; | |
1937 | int from_tty; | |
1938 | struct cmd_list_element *c; | |
1939 | { | |
1940 | unsigned radix = *(unsigned *)c->var; | |
1941 | ||
1942 | if (from_tty) | |
1943 | printf_filtered ("Output radix set to decimal %d, hex %x, octal %o\n", | |
1944 | radix, radix, radix); | |
1945 | ||
1946 | /* FIXME, we really should be able to validate the setting BEFORE | |
1947 | it takes effect. */ | |
1948 | switch (radix) | |
1949 | { | |
1950 | case 16: | |
1951 | output_format = 'x'; | |
1952 | break; | |
1953 | case 10: | |
1954 | output_format = 0; | |
1955 | break; | |
1956 | case 8: | |
1957 | output_format = 'o'; /* octal */ | |
1958 | break; | |
1959 | default: | |
1960 | output_format = 0; | |
1961 | error ("Unsupported radix ``decimal %d''; using decimal output", | |
1962 | radix); | |
1963 | } | |
1964 | } | |
1965 | ||
1966 | /* Both at once */ | |
1967 | static void | |
1968 | set_radix (arg, from_tty, c) | |
1969 | char *arg; | |
1970 | int from_tty; | |
1971 | struct cmd_list_element *c; | |
1972 | { | |
1973 | unsigned radix = *(unsigned *)c->var; | |
1974 | ||
1975 | if (from_tty) | |
1976 | printf_filtered ("Radix set to decimal %d, hex %x, octal %o\n", | |
1977 | radix, radix, radix); | |
1978 | ||
1979 | input_radix = radix; | |
1980 | output_radix = radix; | |
1981 | ||
1982 | set_output_radix (arg, 0, c); | |
1983 | } | |
1984 | \f | |
f266e564 JK |
1985 | struct cmd_list_element *setprintlist = NULL; |
1986 | struct cmd_list_element *showprintlist = NULL; | |
1987 | ||
1988 | /*ARGSUSED*/ | |
1989 | static void | |
1990 | set_print (arg, from_tty) | |
1991 | char *arg; | |
1992 | int from_tty; | |
1993 | { | |
1994 | printf ( | |
1995 | "\"set print\" must be followed by the name of a print subcommand.\n"); | |
1996 | help_list (setprintlist, "set print ", -1, stdout); | |
1997 | } | |
1998 | ||
1999 | /*ARGSUSED*/ | |
2000 | static void | |
2001 | show_print (args, from_tty) | |
2002 | char *args; | |
2003 | int from_tty; | |
2004 | { | |
2005 | cmd_show_list (showprintlist, from_tty, ""); | |
2006 | } | |
2007 | \f | |
bd5635a1 RP |
2008 | void |
2009 | _initialize_valprint () | |
2010 | { | |
2011 | struct cmd_list_element *c; | |
2012 | ||
f266e564 JK |
2013 | add_prefix_cmd ("print", no_class, set_print, |
2014 | "Generic command for setting how things print.", | |
2015 | &setprintlist, "set print ", 0, &setlist); | |
36b9d39c JG |
2016 | add_alias_cmd ("p", "print", no_class, 1, &setlist); |
2017 | add_alias_cmd ("pr", "print", no_class, 1, &setlist); /* prefer set print | |
2018 | to set prompt */ | |
f266e564 JK |
2019 | add_prefix_cmd ("print", no_class, show_print, |
2020 | "Generic command for showing print settings.", | |
2021 | &showprintlist, "show print ", 0, &showlist); | |
36b9d39c JG |
2022 | add_alias_cmd ("p", "print", no_class, 1, &showlist); |
2023 | add_alias_cmd ("pr", "print", no_class, 1, &showlist); | |
f266e564 | 2024 | |
bd5635a1 | 2025 | add_show_from_set |
f266e564 | 2026 | (add_set_cmd ("elements", no_class, var_uinteger, (char *)&print_max, |
bd5635a1 | 2027 | "Set limit on string chars or array elements to print.\n\ |
f266e564 JK |
2028 | \"set print elements 0\" causes there to be no limit.", |
2029 | &setprintlist), | |
2030 | &showprintlist); | |
bd5635a1 RP |
2031 | |
2032 | add_show_from_set | |
f266e564 | 2033 | (add_set_cmd ("pretty", class_support, var_boolean, (char *)&prettyprint, |
bd5635a1 | 2034 | "Set prettyprinting of structures.", |
f266e564 JK |
2035 | &setprintlist), |
2036 | &showprintlist); | |
bd5635a1 RP |
2037 | |
2038 | add_show_from_set | |
f266e564 | 2039 | (add_set_cmd ("union", class_support, var_boolean, (char *)&unionprint, |
bd5635a1 | 2040 | "Set printing of unions interior to structures.", |
f266e564 JK |
2041 | &setprintlist), |
2042 | &showprintlist); | |
bd5635a1 RP |
2043 | |
2044 | add_show_from_set | |
f266e564 | 2045 | (add_set_cmd ("vtbl", class_support, var_boolean, (char *)&vtblprint, |
bd5635a1 | 2046 | "Set printing of C++ virtual function tables.", |
f266e564 JK |
2047 | &setprintlist), |
2048 | &showprintlist); | |
bd5635a1 RP |
2049 | |
2050 | add_show_from_set | |
f266e564 | 2051 | (add_set_cmd ("array", class_support, var_boolean, (char *)&arrayprint, |
bd5635a1 | 2052 | "Set prettyprinting of arrays.", |
f266e564 JK |
2053 | &setprintlist), |
2054 | &showprintlist); | |
bd5635a1 | 2055 | |
0dce3774 JK |
2056 | add_show_from_set |
2057 | (add_set_cmd ("object", class_support, var_boolean, (char *)&objectprint, | |
2058 | "Set printing of object's derived type based on vtable info.", | |
2059 | &setprintlist), | |
2060 | &showprintlist); | |
2061 | ||
bd5635a1 | 2062 | add_show_from_set |
f266e564 | 2063 | (add_set_cmd ("address", class_support, var_boolean, (char *)&addressprint, |
bd5635a1 | 2064 | "Set printing of addresses.", |
f266e564 JK |
2065 | &setprintlist), |
2066 | &showprintlist); | |
bd5635a1 RP |
2067 | |
2068 | #if 0 | |
2069 | /* The "show radix" cmd isn't good enough to show two separate values. | |
2070 | The rest of the code works, but the show part is confusing, so don't | |
2071 | let them be set separately 'til we work out "show". */ | |
2072 | c = add_set_cmd ("input-radix", class_support, var_uinteger, | |
2073 | (char *)&input_radix, | |
2074 | "Set default input radix for entering numbers.", | |
2075 | &setlist); | |
2076 | add_show_from_set (c, &showlist); | |
2077 | c->function = set_input_radix; | |
2078 | ||
2079 | c = add_set_cmd ("output-radix", class_support, var_uinteger, | |
2080 | (char *)&output_radix, | |
2081 | "Set default output radix for printing of values.", | |
2082 | &setlist); | |
2083 | add_show_from_set (c, &showlist); | |
2084 | c->function = set_output_radix; | |
2085 | #endif | |
2086 | ||
2087 | c = add_set_cmd ("radix", class_support, var_uinteger, | |
2088 | (char *)&output_radix, | |
2089 | "Set default input and output number radix.", | |
2090 | &setlist); | |
2091 | add_show_from_set (c, &showlist); | |
2cd99985 | 2092 | c->function.sfunc = set_radix; |
bd5635a1 RP |
2093 | |
2094 | /* Give people the defaults which they are used to. */ | |
2095 | prettyprint = 0; | |
2096 | unionprint = 1; | |
2097 | vtblprint = 0; | |
2098 | arrayprint = 0; | |
2099 | addressprint = 1; | |
0dce3774 | 2100 | objectprint = 0; |
bd5635a1 RP |
2101 | |
2102 | print_max = 200; | |
2103 | ||
bd5635a1 RP |
2104 | obstack_begin (&dont_print_obstack, 32 * sizeof (struct type *)); |
2105 | } |