]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Language independent support for printing types for GDB, the GNU debugger. |
1bac305b | 2 | |
3666a048 | 3 | Copyright (C) 1986-2021 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "defs.h" | |
04ea0df1 | 21 | #include "gdb_obstack.h" |
c906108c SS |
22 | #include "bfd.h" /* Binary File Description */ |
23 | #include "symtab.h" | |
24 | #include "gdbtypes.h" | |
25 | #include "expression.h" | |
26 | #include "value.h" | |
27 | #include "gdbcore.h" | |
28 | #include "command.h" | |
29 | #include "gdbcmd.h" | |
30 | #include "target.h" | |
31 | #include "language.h" | |
015a42b4 | 32 | #include "cp-abi.h" |
b9362cc7 | 33 | #include "typeprint.h" |
79a45b7d | 34 | #include "valprint.h" |
53342f27 TT |
35 | #include <ctype.h> |
36 | #include "cli/cli-utils.h" | |
6dddc817 | 37 | #include "extension.h" |
4fc5d43e | 38 | #include "completer.h" |
7f6aba03 | 39 | #include "cli/cli-style.h" |
c906108c | 40 | |
79d43c61 TT |
41 | const struct type_print_options type_print_raw_options = |
42 | { | |
53342f27 TT |
43 | 1, /* raw */ |
44 | 1, /* print_methods */ | |
bd69fc68 | 45 | 1, /* print_typedefs */ |
7c161838 | 46 | 0, /* print_offsets */ |
fbb46296 | 47 | 0, /* print_in_hex */ |
883fd55a | 48 | 0, /* print_nested_type_limit */ |
18a9fc12 TT |
49 | NULL, /* local_typedefs */ |
50 | NULL, /* global_table */ | |
51 | NULL /* global_printers */ | |
79d43c61 TT |
52 | }; |
53 | ||
54 | /* The default flags for 'ptype' and 'whatis'. */ | |
55 | ||
56 | static struct type_print_options default_ptype_flags = | |
57 | { | |
53342f27 TT |
58 | 0, /* raw */ |
59 | 1, /* print_methods */ | |
bd69fc68 | 60 | 1, /* print_typedefs */ |
7c161838 | 61 | 0, /* print_offsets */ |
fbb46296 | 62 | 0, /* print_in_hex */ |
883fd55a | 63 | 0, /* print_nested_type_limit */ |
18a9fc12 TT |
64 | NULL, /* local_typedefs */ |
65 | NULL, /* global_table */ | |
66 | NULL /* global_printers */ | |
79d43c61 | 67 | }; |
5c6ce71d | 68 | |
53342f27 TT |
69 | \f |
70 | ||
e0c547d1 TT |
71 | /* See typeprint.h. */ |
72 | ||
fbb46296 | 73 | const int print_offset_data::indentation = 27; |
e0c547d1 | 74 | |
fbb46296 LS |
75 | /* See typeprint.h. */ |
76 | ||
77 | print_offset_data::print_offset_data (const struct type_print_options *flags) | |
78 | { | |
79 | if (flags != nullptr) | |
80 | print_in_hex = flags->print_in_hex; | |
81 | } | |
e0c547d1 TT |
82 | |
83 | /* See typeprint.h. */ | |
84 | ||
85 | void | |
86 | print_offset_data::maybe_print_hole (struct ui_file *stream, | |
87 | unsigned int bitpos, | |
88 | const char *for_what) | |
89 | { | |
90 | /* We check for END_BITPOS > 0 because there is a specific | |
91 | scenario when END_BITPOS can be zero and BITPOS can be > | |
92 | 0: when we are dealing with a struct/class with a virtual method. | |
93 | Because of the vtable, the first field of the struct/class will | |
94 | have an offset of sizeof (void *) (the size of the vtable). If | |
95 | we do not check for END_BITPOS > 0 here, GDB will report | |
96 | a hole before the first field, which is not accurate. */ | |
97 | if (end_bitpos > 0 && end_bitpos < bitpos) | |
98 | { | |
99 | /* If END_BITPOS is smaller than the current type's | |
100 | bitpos, it means there's a hole in the struct, so we report | |
101 | it here. */ | |
102 | unsigned int hole = bitpos - end_bitpos; | |
103 | unsigned int hole_byte = hole / TARGET_CHAR_BIT; | |
104 | unsigned int hole_bit = hole % TARGET_CHAR_BIT; | |
105 | ||
106 | if (hole_bit > 0) | |
fbb46296 | 107 | fprintf_filtered (stream, "/* XXX %2u-bit %-7s */\n", hole_bit, |
e0c547d1 TT |
108 | for_what); |
109 | ||
110 | if (hole_byte > 0) | |
fbb46296 | 111 | fprintf_filtered (stream, "/* XXX %2u-byte %-7s */\n", hole_byte, |
e0c547d1 TT |
112 | for_what); |
113 | } | |
114 | } | |
115 | ||
116 | /* See typeprint.h. */ | |
117 | ||
118 | void | |
119 | print_offset_data::update (struct type *type, unsigned int field_idx, | |
120 | struct ui_file *stream) | |
121 | { | |
ceacbf6e | 122 | if (field_is_static (&type->field (field_idx))) |
e0c547d1 TT |
123 | { |
124 | print_spaces_filtered (indentation, stream); | |
125 | return; | |
126 | } | |
127 | ||
940da03e | 128 | struct type *ftype = check_typedef (type->field (field_idx).type ()); |
78134374 | 129 | if (type->code () == TYPE_CODE_UNION) |
e0c547d1 TT |
130 | { |
131 | /* Since union fields don't have the concept of offsets, we just | |
132 | print their sizes. */ | |
fbb46296 LS |
133 | fprintf_filtered (stream, "/* %6s */", |
134 | (print_in_hex ? | |
135 | hex_string_custom (TYPE_LENGTH (ftype), 4) : | |
136 | pulongest (TYPE_LENGTH (ftype)))); | |
e0c547d1 TT |
137 | return; |
138 | } | |
139 | ||
140 | unsigned int bitpos = TYPE_FIELD_BITPOS (type, field_idx); | |
141 | unsigned int fieldsize_byte = TYPE_LENGTH (ftype); | |
142 | unsigned int fieldsize_bit = fieldsize_byte * TARGET_CHAR_BIT; | |
143 | ||
144 | maybe_print_hole (stream, bitpos, "hole"); | |
145 | ||
9d3421af TT |
146 | if (TYPE_FIELD_PACKED (type, field_idx) |
147 | || offset_bitpos % TARGET_CHAR_BIT != 0) | |
e0c547d1 | 148 | { |
9d3421af TT |
149 | /* We're dealing with a bitfield. Print the bit offset. */ |
150 | fieldsize_bit = TYPE_FIELD_BITSIZE (type, field_idx); | |
e0c547d1 | 151 | |
9d3421af TT |
152 | unsigned real_bitpos = bitpos + offset_bitpos; |
153 | ||
fbb46296 LS |
154 | fprintf_filtered (stream, |
155 | (print_in_hex ? "/* 0x%04x: 0x%x" : "/* %6u:%2u "), | |
156 | real_bitpos / TARGET_CHAR_BIT, | |
9d3421af | 157 | real_bitpos % TARGET_CHAR_BIT); |
e0c547d1 TT |
158 | } |
159 | else | |
160 | { | |
161 | /* The position of the field, relative to the beginning of the | |
162 | struct. */ | |
fbb46296 | 163 | fprintf_filtered (stream, (print_in_hex ? "/* 0x%04x" : "/* %6u"), |
e0c547d1 TT |
164 | (bitpos + offset_bitpos) / TARGET_CHAR_BIT); |
165 | ||
fbb46296 | 166 | fprintf_filtered (stream, " "); |
e0c547d1 TT |
167 | } |
168 | ||
fbb46296 LS |
169 | fprintf_filtered (stream, (print_in_hex ? " | 0x%04x */" : " | %6u */"), |
170 | fieldsize_byte); | |
e0c547d1 TT |
171 | |
172 | end_bitpos = bitpos + fieldsize_bit; | |
173 | } | |
174 | ||
175 | /* See typeprint.h. */ | |
176 | ||
177 | void | |
178 | print_offset_data::finish (struct type *type, int level, | |
179 | struct ui_file *stream) | |
180 | { | |
181 | unsigned int bitpos = TYPE_LENGTH (type) * TARGET_CHAR_BIT; | |
182 | maybe_print_hole (stream, bitpos, "padding"); | |
183 | ||
184 | fputs_filtered ("\n", stream); | |
185 | print_spaces_filtered (level + 4 + print_offset_data::indentation, stream); | |
cc1defb1 KS |
186 | fprintf_filtered (stream, "/* total size (bytes): %4s */\n", |
187 | pulongest (TYPE_LENGTH (type))); | |
e0c547d1 TT |
188 | } |
189 | ||
190 | \f | |
191 | ||
bd69fc68 TT |
192 | /* A hash function for a typedef_field. */ |
193 | ||
194 | static hashval_t | |
195 | hash_typedef_field (const void *p) | |
196 | { | |
883fd55a | 197 | const struct decl_field *tf = (const struct decl_field *) p; |
bd69fc68 TT |
198 | struct type *t = check_typedef (tf->type); |
199 | ||
200 | return htab_hash_string (TYPE_SAFE_NAME (t)); | |
201 | } | |
202 | ||
203 | /* An equality function for a typedef field. */ | |
204 | ||
205 | static int | |
206 | eq_typedef_field (const void *a, const void *b) | |
207 | { | |
883fd55a KS |
208 | const struct decl_field *tfa = (const struct decl_field *) a; |
209 | const struct decl_field *tfb = (const struct decl_field *) b; | |
bd69fc68 TT |
210 | |
211 | return types_equal (tfa->type, tfb->type); | |
212 | } | |
213 | ||
c819b2c0 | 214 | /* See typeprint.h. */ |
bd69fc68 TT |
215 | |
216 | void | |
c819b2c0 | 217 | typedef_hash_table::recursively_update (struct type *t) |
bd69fc68 TT |
218 | { |
219 | int i; | |
220 | ||
bd69fc68 TT |
221 | for (i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (t); ++i) |
222 | { | |
883fd55a | 223 | struct decl_field *tdef = &TYPE_TYPEDEF_FIELD (t, i); |
bd69fc68 TT |
224 | void **slot; |
225 | ||
fa9b1164 | 226 | slot = htab_find_slot (m_table.get (), tdef, INSERT); |
bd69fc68 TT |
227 | /* Only add a given typedef name once. Really this shouldn't |
228 | happen; but it is safe enough to do the updates breadth-first | |
229 | and thus use the most specific typedef. */ | |
230 | if (*slot == NULL) | |
231 | *slot = tdef; | |
232 | } | |
233 | ||
234 | /* Recurse into superclasses. */ | |
235 | for (i = 0; i < TYPE_N_BASECLASSES (t); ++i) | |
c819b2c0 | 236 | recursively_update (TYPE_BASECLASS (t, i)); |
bd69fc68 TT |
237 | } |
238 | ||
c819b2c0 | 239 | /* See typeprint.h. */ |
bd69fc68 TT |
240 | |
241 | void | |
c819b2c0 | 242 | typedef_hash_table::add_template_parameters (struct type *t) |
bd69fc68 TT |
243 | { |
244 | int i; | |
245 | ||
bd69fc68 TT |
246 | for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (t); ++i) |
247 | { | |
883fd55a | 248 | struct decl_field *tf; |
bd69fc68 TT |
249 | void **slot; |
250 | ||
251 | /* We only want type-valued template parameters in the hash. */ | |
252 | if (SYMBOL_CLASS (TYPE_TEMPLATE_ARGUMENT (t, i)) != LOC_TYPEDEF) | |
253 | continue; | |
254 | ||
c819b2c0 | 255 | tf = XOBNEW (&m_storage, struct decl_field); |
987012b8 | 256 | tf->name = TYPE_TEMPLATE_ARGUMENT (t, i)->linkage_name (); |
bd69fc68 TT |
257 | tf->type = SYMBOL_TYPE (TYPE_TEMPLATE_ARGUMENT (t, i)); |
258 | ||
fa9b1164 | 259 | slot = htab_find_slot (m_table.get (), tf, INSERT); |
bd69fc68 TT |
260 | if (*slot == NULL) |
261 | *slot = tf; | |
262 | } | |
263 | } | |
264 | ||
c819b2c0 | 265 | /* See typeprint.h. */ |
bd69fc68 | 266 | |
c819b2c0 | 267 | typedef_hash_table::typedef_hash_table () |
fa9b1164 TT |
268 | : m_table (htab_create_alloc (10, hash_typedef_field, eq_typedef_field, |
269 | NULL, xcalloc, xfree)) | |
bd69fc68 | 270 | { |
bd69fc68 TT |
271 | } |
272 | ||
c819b2c0 | 273 | /* Helper function for typedef_hash_table::copy. */ |
bd69fc68 TT |
274 | |
275 | static int | |
276 | copy_typedef_hash_element (void **slot, void *nt) | |
277 | { | |
19ba03f4 | 278 | htab_t new_table = (htab_t) nt; |
bd69fc68 TT |
279 | void **new_slot; |
280 | ||
281 | new_slot = htab_find_slot (new_table, *slot, INSERT); | |
282 | if (*new_slot == NULL) | |
283 | *new_slot = *slot; | |
284 | ||
285 | return 1; | |
286 | } | |
287 | ||
c819b2c0 | 288 | /* See typeprint.h. */ |
18a9fc12 | 289 | |
c819b2c0 | 290 | typedef_hash_table::typedef_hash_table (const typedef_hash_table &table) |
18a9fc12 | 291 | { |
fa9b1164 TT |
292 | m_table.reset (htab_create_alloc (10, hash_typedef_field, eq_typedef_field, |
293 | NULL, xcalloc, xfree)); | |
294 | htab_traverse_noresize (table.m_table.get (), copy_typedef_hash_element, | |
295 | m_table.get ()); | |
18a9fc12 TT |
296 | } |
297 | ||
298 | /* Look up the type T in the global typedef hash. If it is found, | |
299 | return the typedef name. If it is not found, apply the | |
6dddc817 | 300 | type-printers, if any, given by start_script_type_printers and return the |
18a9fc12 TT |
301 | result. A NULL return means that the name was not found. */ |
302 | ||
c819b2c0 TT |
303 | const char * |
304 | typedef_hash_table::find_global_typedef (const struct type_print_options *flags, | |
305 | struct type *t) | |
18a9fc12 TT |
306 | { |
307 | char *applied; | |
308 | void **slot; | |
883fd55a | 309 | struct decl_field tf, *new_tf; |
18a9fc12 TT |
310 | |
311 | if (flags->global_typedefs == NULL) | |
312 | return NULL; | |
313 | ||
314 | tf.name = NULL; | |
315 | tf.type = t; | |
316 | ||
fa9b1164 | 317 | slot = htab_find_slot (flags->global_typedefs->m_table.get (), &tf, INSERT); |
18a9fc12 TT |
318 | if (*slot != NULL) |
319 | { | |
883fd55a | 320 | new_tf = (struct decl_field *) *slot; |
18a9fc12 TT |
321 | return new_tf->name; |
322 | } | |
323 | ||
9b94fcf1 DE |
324 | /* Put an entry into the hash table now, in case |
325 | apply_ext_lang_type_printers recurses. */ | |
c819b2c0 | 326 | new_tf = XOBNEW (&flags->global_typedefs->m_storage, struct decl_field); |
18a9fc12 TT |
327 | new_tf->name = NULL; |
328 | new_tf->type = t; | |
329 | ||
330 | *slot = new_tf; | |
331 | ||
6dddc817 | 332 | applied = apply_ext_lang_type_printers (flags->global_printers, t); |
18a9fc12 TT |
333 | |
334 | if (applied != NULL) | |
335 | { | |
021887d8 TT |
336 | new_tf->name = obstack_strdup (&flags->global_typedefs->m_storage, |
337 | applied); | |
18a9fc12 TT |
338 | xfree (applied); |
339 | } | |
340 | ||
341 | return new_tf->name; | |
342 | } | |
343 | ||
c819b2c0 | 344 | /* See typeprint.h. */ |
bd69fc68 TT |
345 | |
346 | const char * | |
c819b2c0 TT |
347 | typedef_hash_table::find_typedef (const struct type_print_options *flags, |
348 | struct type *t) | |
bd69fc68 | 349 | { |
18a9fc12 TT |
350 | if (flags->local_typedefs != NULL) |
351 | { | |
883fd55a | 352 | struct decl_field tf, *found; |
bd69fc68 | 353 | |
18a9fc12 TT |
354 | tf.name = NULL; |
355 | tf.type = t; | |
fa9b1164 TT |
356 | htab_t table = flags->local_typedefs->m_table.get (); |
357 | found = (struct decl_field *) htab_find (table, &tf); | |
bd69fc68 | 358 | |
18a9fc12 TT |
359 | if (found != NULL) |
360 | return found->name; | |
361 | } | |
bd69fc68 | 362 | |
18a9fc12 | 363 | return find_global_typedef (flags, t); |
bd69fc68 TT |
364 | } |
365 | ||
366 | \f | |
367 | ||
a5238fbc PM |
368 | /* Print a description of a type in the format of a |
369 | typedef for the current language. | |
c378eb4e | 370 | NEW is the new name for a type TYPE. */ |
a5238fbc PM |
371 | |
372 | void | |
fe978cb0 | 373 | typedef_print (struct type *type, struct symbol *newobj, struct ui_file *stream) |
a5238fbc | 374 | { |
d3b67c56 | 375 | current_language->print_typedef (type, newobj, stream); |
5c6ce71d TT |
376 | } |
377 | ||
c906108c SS |
378 | /* Print a description of a type TYPE in the form of a declaration of a |
379 | variable named VARSTRING. (VARSTRING is demangled if necessary.) | |
380 | Output goes to STREAM (via stdio). | |
381 | If SHOW is positive, we show the contents of the outermost level | |
382 | of structure even if there is a type name that could be used instead. | |
383 | If SHOW is negative, we never show the details of elements' types. */ | |
384 | ||
385 | void | |
0d5cff50 | 386 | type_print (struct type *type, const char *varstring, struct ui_file *stream, |
fba45db2 | 387 | int show) |
c906108c | 388 | { |
79d43c61 | 389 | LA_PRINT_TYPE (type, varstring, stream, show, 0, &default_ptype_flags); |
c906108c SS |
390 | } |
391 | ||
ae6a3a4c TJB |
392 | /* Print TYPE to a string, returning it. The caller is responsible for |
393 | freeing the string. */ | |
394 | ||
2f408ecb | 395 | std::string |
ae6a3a4c TJB |
396 | type_to_string (struct type *type) |
397 | { | |
a70b8144 | 398 | try |
ae6a3a4c | 399 | { |
d7e74731 PA |
400 | string_file stb; |
401 | ||
402 | type_print (type, "", &stb, -1); | |
403 | return std::move (stb.string ()); | |
ae6a3a4c | 404 | } |
230d2906 | 405 | catch (const gdb_exception &except) |
492d29ea | 406 | { |
492d29ea | 407 | } |
ae6a3a4c | 408 | |
d7e74731 | 409 | return {}; |
ae6a3a4c TJB |
410 | } |
411 | ||
7022349d PA |
412 | /* See typeprint.h. */ |
413 | ||
414 | void | |
415 | type_print_unknown_return_type (struct ui_file *stream) | |
416 | { | |
7f6aba03 TT |
417 | fprintf_styled (stream, metadata_style.style (), |
418 | _("<unknown return type>")); | |
7022349d PA |
419 | } |
420 | ||
46a4882b PA |
421 | /* See typeprint.h. */ |
422 | ||
423 | void | |
424 | error_unknown_type (const char *sym_print_name) | |
425 | { | |
426 | error (_("'%s' has unknown type; cast it to its declared type"), | |
427 | sym_print_name); | |
428 | } | |
429 | ||
c906108c SS |
430 | /* Print type of EXP, or last thing in value history if EXP == NULL. |
431 | show is passed to type_print. */ | |
432 | ||
433 | static void | |
0b39b52e | 434 | whatis_exp (const char *exp, int show) |
c906108c | 435 | { |
3d6d86c6 | 436 | struct value *val; |
c5aa993b | 437 | struct type *real_type = NULL; |
070ad9f0 | 438 | struct type *type; |
c906108c | 439 | int full = 0; |
6b850546 | 440 | LONGEST top = -1; |
c906108c | 441 | int using_enc = 0; |
79a45b7d | 442 | struct value_print_options opts; |
53342f27 | 443 | struct type_print_options flags = default_ptype_flags; |
c906108c SS |
444 | |
445 | if (exp) | |
446 | { | |
53342f27 TT |
447 | if (*exp == '/') |
448 | { | |
449 | int seen_one = 0; | |
450 | ||
451 | for (++exp; *exp && !isspace (*exp); ++exp) | |
452 | { | |
453 | switch (*exp) | |
454 | { | |
455 | case 'r': | |
456 | flags.raw = 1; | |
457 | break; | |
458 | case 'm': | |
459 | flags.print_methods = 0; | |
460 | break; | |
461 | case 'M': | |
462 | flags.print_methods = 1; | |
463 | break; | |
464 | case 't': | |
465 | flags.print_typedefs = 0; | |
466 | break; | |
467 | case 'T': | |
468 | flags.print_typedefs = 1; | |
469 | break; | |
7c161838 SDJ |
470 | case 'o': |
471 | { | |
472 | /* Filter out languages which don't implement the | |
473 | feature. */ | |
46afe196 SDJ |
474 | if (show > 0 |
475 | && (current_language->la_language == language_c | |
a33ccfc7 TT |
476 | || current_language->la_language == language_cplus |
477 | || current_language->la_language == language_rust)) | |
7c161838 SDJ |
478 | { |
479 | flags.print_offsets = 1; | |
480 | flags.print_typedefs = 0; | |
481 | flags.print_methods = 0; | |
482 | } | |
483 | break; | |
484 | } | |
fbb46296 LS |
485 | case 'x': |
486 | flags.print_in_hex = 1; | |
487 | break; | |
488 | case 'd': | |
489 | flags.print_in_hex = 0; | |
490 | break; | |
53342f27 TT |
491 | default: |
492 | error (_("unrecognized flag '%c'"), *exp); | |
493 | } | |
494 | seen_one = 1; | |
495 | } | |
496 | ||
497 | if (!*exp && !seen_one) | |
498 | error (_("flag expected")); | |
499 | if (!isspace (*exp)) | |
500 | error (_("expected space after format")); | |
501 | exp = skip_spaces (exp); | |
502 | } | |
503 | ||
4d01a485 | 504 | expression_up expr = parse_expression (exp); |
c973d0aa PA |
505 | |
506 | /* The behavior of "whatis" depends on whether the user | |
507 | expression names a type directly, or a language expression | |
508 | (including variable names). If the former, then "whatis" | |
509 | strips one level of typedefs, only. If an expression, | |
510 | "whatis" prints the type of the expression without stripping | |
511 | any typedef level. "ptype" always strips all levels of | |
512 | typedefs. */ | |
a6f3c8a1 TT |
513 | val = evaluate_type (expr.get ()); |
514 | type = value_type (val); | |
515 | ||
2adab65c | 516 | if (show == -1 && expr->first_opcode () == OP_TYPE) |
c973d0aa PA |
517 | { |
518 | /* The user expression names a type directly. */ | |
c973d0aa PA |
519 | |
520 | /* If this is a typedef, then find its immediate target. | |
521 | Use check_typedef to resolve stubs, but ignore its result | |
522 | because we do not want to dig past all typedefs. */ | |
523 | check_typedef (type); | |
78134374 | 524 | if (type->code () == TYPE_CODE_TYPEDEF) |
c973d0aa | 525 | type = TYPE_TARGET_TYPE (type); |
5c319bb2 PA |
526 | |
527 | /* If the expression is actually a type, then there's no | |
528 | value to fetch the dynamic type from. */ | |
529 | val = NULL; | |
c973d0aa | 530 | } |
c906108c SS |
531 | } |
532 | else | |
c973d0aa PA |
533 | { |
534 | val = access_value_history (0); | |
535 | type = value_type (val); | |
536 | } | |
070ad9f0 | 537 | |
79a45b7d | 538 | get_user_print_options (&opts); |
5c319bb2 | 539 | if (val != NULL && opts.objectprint) |
070ad9f0 | 540 | { |
78134374 SM |
541 | if (((type->code () == TYPE_CODE_PTR) || TYPE_IS_REFERENCE (type)) |
542 | && (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_STRUCT)) | |
dda83cd7 | 543 | real_type = value_rtti_indirect_type (val, &full, &top, &using_enc); |
78134374 | 544 | else if (type->code () == TYPE_CODE_STRUCT) |
41808ebe | 545 | real_type = value_rtti_type (val, &full, &top, &using_enc); |
070ad9f0 | 546 | } |
c906108c | 547 | |
7c161838 | 548 | if (flags.print_offsets |
78134374 SM |
549 | && (type->code () == TYPE_CODE_STRUCT |
550 | || type->code () == TYPE_CODE_UNION)) | |
fbb46296 | 551 | fprintf_filtered (gdb_stdout, "/* offset | size */ "); |
7c161838 | 552 | |
c906108c SS |
553 | printf_filtered ("type = "); |
554 | ||
c819b2c0 TT |
555 | std::unique_ptr<typedef_hash_table> table_holder; |
556 | std::unique_ptr<ext_lang_type_printers> printer_holder; | |
18a9fc12 | 557 | if (!flags.raw) |
c819b2c0 TT |
558 | { |
559 | table_holder.reset (new typedef_hash_table); | |
560 | flags.global_typedefs = table_holder.get (); | |
561 | ||
562 | printer_holder.reset (new ext_lang_type_printers); | |
563 | flags.global_printers = printer_holder.get (); | |
564 | } | |
18a9fc12 | 565 | |
070ad9f0 DB |
566 | if (real_type) |
567 | { | |
568 | printf_filtered ("/* real type = "); | |
569 | type_print (real_type, "", gdb_stdout, -1); | |
570 | if (! full) | |
dda83cd7 | 571 | printf_filtered (" (incomplete object)"); |
070ad9f0 DB |
572 | printf_filtered (" */\n"); |
573 | } | |
c906108c | 574 | |
53342f27 | 575 | LA_PRINT_TYPE (type, "", gdb_stdout, show, 0, &flags); |
c906108c | 576 | printf_filtered ("\n"); |
c906108c SS |
577 | } |
578 | ||
c906108c | 579 | static void |
0b39b52e | 580 | whatis_command (const char *exp, int from_tty) |
c906108c SS |
581 | { |
582 | /* Most of the time users do not want to see all the fields | |
583 | in a structure. If they do they can use the "ptype" command. | |
584 | Hence the "-1" below. */ | |
585 | whatis_exp (exp, -1); | |
586 | } | |
587 | ||
c906108c SS |
588 | /* TYPENAME is either the name of a type, or an expression. */ |
589 | ||
c906108c | 590 | static void |
0b39b52e | 591 | ptype_command (const char *type_name, int from_tty) |
c906108c | 592 | { |
fe978cb0 | 593 | whatis_exp (type_name, 1); |
c906108c SS |
594 | } |
595 | ||
596 | /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM. | |
597 | Used to print data from type structures in a specified type. For example, | |
598 | array bounds may be characters or booleans in some languages, and this | |
599 | allows the ranges to be printed in their "natural" form rather than as | |
600 | decimal integer values. | |
601 | ||
602 | FIXME: This is here simply because only the type printing routines | |
603 | currently use it, and it wasn't clear if it really belonged somewhere | |
604 | else (like printcmd.c). There are a lot of other gdb routines that do | |
605 | something similar, but they are generally concerned with printing values | |
41808ebe | 606 | that come from the inferior in target byte order and target size. */ |
c906108c SS |
607 | |
608 | void | |
fba45db2 | 609 | print_type_scalar (struct type *type, LONGEST val, struct ui_file *stream) |
c906108c SS |
610 | { |
611 | unsigned int i; | |
612 | unsigned len; | |
613 | ||
f168693b | 614 | type = check_typedef (type); |
c906108c | 615 | |
78134374 | 616 | switch (type->code ()) |
c906108c SS |
617 | { |
618 | ||
619 | case TYPE_CODE_ENUM: | |
1f704f76 | 620 | len = type->num_fields (); |
c906108c SS |
621 | for (i = 0; i < len; i++) |
622 | { | |
14e75d8e | 623 | if (TYPE_FIELD_ENUMVAL (type, i) == val) |
c906108c SS |
624 | { |
625 | break; | |
626 | } | |
627 | } | |
628 | if (i < len) | |
629 | { | |
630 | fputs_filtered (TYPE_FIELD_NAME (type, i), stream); | |
631 | } | |
632 | else | |
633 | { | |
634 | print_longest (stream, 'd', 0, val); | |
635 | } | |
636 | break; | |
637 | ||
638 | case TYPE_CODE_INT: | |
c6d940a9 | 639 | print_longest (stream, type->is_unsigned () ? 'u' : 'd', 0, val); |
c906108c SS |
640 | break; |
641 | ||
642 | case TYPE_CODE_CHAR: | |
6c7a06a3 | 643 | LA_PRINT_CHAR ((unsigned char) val, type, stream); |
c906108c SS |
644 | break; |
645 | ||
646 | case TYPE_CODE_BOOL: | |
647 | fprintf_filtered (stream, val ? "TRUE" : "FALSE"); | |
648 | break; | |
649 | ||
650 | case TYPE_CODE_RANGE: | |
651 | print_type_scalar (TYPE_TARGET_TYPE (type), val, stream); | |
652 | return; | |
653 | ||
4afa9fd9 JB |
654 | case TYPE_CODE_FIXED_POINT: |
655 | print_type_fixed_point (type, stream); | |
656 | break; | |
657 | ||
c906108c SS |
658 | case TYPE_CODE_UNDEF: |
659 | case TYPE_CODE_PTR: | |
660 | case TYPE_CODE_ARRAY: | |
661 | case TYPE_CODE_STRUCT: | |
662 | case TYPE_CODE_UNION: | |
663 | case TYPE_CODE_FUNC: | |
664 | case TYPE_CODE_FLT: | |
665 | case TYPE_CODE_VOID: | |
666 | case TYPE_CODE_SET: | |
667 | case TYPE_CODE_STRING: | |
668 | case TYPE_CODE_ERROR: | |
0d5de010 DJ |
669 | case TYPE_CODE_MEMBERPTR: |
670 | case TYPE_CODE_METHODPTR: | |
c906108c SS |
671 | case TYPE_CODE_METHOD: |
672 | case TYPE_CODE_REF: | |
aa006118 | 673 | case TYPE_CODE_RVALUE_REF: |
5c4e30ca | 674 | case TYPE_CODE_NAMESPACE: |
8a3fe4f8 | 675 | error (_("internal error: unhandled type in print_type_scalar")); |
c906108c SS |
676 | break; |
677 | ||
678 | default: | |
8a3fe4f8 | 679 | error (_("Invalid type code in symbol table.")); |
c906108c | 680 | } |
c906108c SS |
681 | } |
682 | ||
0c9150e4 JB |
683 | /* See typeprint.h. */ |
684 | ||
685 | void | |
686 | print_type_fixed_point (struct type *type, struct ui_file *stream) | |
687 | { | |
e6fcee3a | 688 | std::string small_img = type->fixed_point_scaling_factor ().str (); |
0c9150e4 JB |
689 | |
690 | fprintf_filtered (stream, "%s-byte fixed point (small = %s)", | |
987b6703 | 691 | pulongest (TYPE_LENGTH (type)), small_img.c_str ()); |
0c9150e4 JB |
692 | } |
693 | ||
c906108c SS |
694 | /* Dump details of a type specified either directly or indirectly. |
695 | Uses the same sort of type lookup mechanism as ptype_command() | |
41808ebe | 696 | and whatis_command(). */ |
c906108c SS |
697 | |
698 | void | |
58971144 | 699 | maintenance_print_type (const char *type_name, int from_tty) |
c906108c | 700 | { |
fe978cb0 | 701 | if (type_name != NULL) |
c5aa993b | 702 | { |
4d01a485 | 703 | expression_up expr = parse_expression (type_name); |
a6f3c8a1 TT |
704 | struct value *val = evaluate_type (expr.get ()); |
705 | struct type *type = value_type (val); | |
706 | ||
707 | if (type != nullptr) | |
708 | recursive_dump_type (type, 0); | |
c5aa993b | 709 | } |
c906108c | 710 | } |
c906108c | 711 | \f |
c5aa993b | 712 | |
53342f27 TT |
713 | struct cmd_list_element *setprinttypelist; |
714 | ||
715 | struct cmd_list_element *showprinttypelist; | |
716 | ||
491144b5 | 717 | static bool print_methods = true; |
53342f27 TT |
718 | |
719 | static void | |
eb4c3f4a TT |
720 | set_print_type_methods (const char *args, |
721 | int from_tty, struct cmd_list_element *c) | |
53342f27 TT |
722 | { |
723 | default_ptype_flags.print_methods = print_methods; | |
724 | } | |
725 | ||
726 | static void | |
727 | show_print_type_methods (struct ui_file *file, int from_tty, | |
728 | struct cmd_list_element *c, const char *value) | |
729 | { | |
730 | fprintf_filtered (file, _("Printing of methods defined in a class in %s\n"), | |
731 | value); | |
732 | } | |
733 | ||
491144b5 | 734 | static bool print_typedefs = true; |
53342f27 TT |
735 | |
736 | static void | |
eb4c3f4a TT |
737 | set_print_type_typedefs (const char *args, |
738 | int from_tty, struct cmd_list_element *c) | |
53342f27 TT |
739 | { |
740 | default_ptype_flags.print_typedefs = print_typedefs; | |
741 | } | |
742 | ||
743 | static void | |
744 | show_print_type_typedefs (struct ui_file *file, int from_tty, | |
745 | struct cmd_list_element *c, const char *value) | |
746 | { | |
747 | fprintf_filtered (file, _("Printing of typedefs defined in a class in %s\n"), | |
748 | value); | |
749 | } | |
750 | ||
883fd55a KS |
751 | /* Limit on the number of nested type definitions to print or -1 to print |
752 | all nested type definitions in a class. By default, we do not print | |
753 | nested definitions. */ | |
754 | ||
755 | static int print_nested_type_limit = 0; | |
756 | ||
757 | /* Set how many nested type definitions should be printed by the type | |
758 | printer. */ | |
759 | ||
760 | static void | |
761 | set_print_type_nested_types (const char *args, int from_tty, | |
762 | struct cmd_list_element *c) | |
763 | { | |
764 | default_ptype_flags.print_nested_type_limit = print_nested_type_limit; | |
765 | } | |
766 | ||
767 | /* Show how many nested type definitions the type printer will print. */ | |
768 | ||
769 | static void | |
770 | show_print_type_nested_types (struct ui_file *file, int from_tty, | |
771 | struct cmd_list_element *c, const char *value) | |
772 | { | |
773 | if (*value == '0') | |
774 | { | |
775 | fprintf_filtered (file, | |
776 | _("Will not print nested types defined in a class\n")); | |
777 | } | |
778 | else | |
779 | { | |
780 | fprintf_filtered (file, | |
781 | _("Will print %s nested types defined in a class\n"), | |
782 | value); | |
783 | } | |
784 | } | |
785 | ||
fbb46296 LS |
786 | /* When printing structs, offsets and sizes of members can be displayed using |
787 | decimal notation or hexadecimal notation. By default, Decimal notation is | |
788 | used. */ | |
789 | ||
790 | static bool print_offsets_and_sizes_in_hex = false; | |
791 | ||
792 | /* Set the flags that instructs if sizes and offsets of struct members are | |
793 | displayed in hexadecimal or decimal notation. */ | |
794 | ||
795 | static void | |
796 | set_print_offsets_and_sizes_in_hex (const char *args, | |
797 | int from_tty, struct cmd_list_element *c) | |
798 | { | |
799 | default_ptype_flags.print_in_hex = print_offsets_and_sizes_in_hex; | |
800 | } | |
801 | ||
802 | /* Display whether struct members sizes and offsets are printed | |
803 | using decimal or hexadecimal notation. */ | |
804 | ||
805 | static void | |
806 | show_print_offsets_and_sizes_in_hex (struct ui_file *file, int from_tty, | |
807 | struct cmd_list_element *c, | |
808 | const char *value) | |
809 | { | |
810 | fprintf_filtered (file, _("\ | |
811 | Display of struct members offsets and sizes in hexadecimal is %s\n"), | |
812 | value); | |
813 | } | |
814 | ||
6c265988 | 815 | void _initialize_typeprint (); |
c906108c | 816 | void |
6c265988 | 817 | _initialize_typeprint () |
c906108c | 818 | { |
4fc5d43e TT |
819 | struct cmd_list_element *c; |
820 | ||
821 | c = add_com ("ptype", class_vars, ptype_command, _("\ | |
1bedd215 | 822 | Print definition of type TYPE.\n\ |
a9375afe DE |
823 | Usage: ptype[/FLAGS] TYPE | EXPRESSION\n\ |
824 | Argument may be any type (for example a type name defined by typedef,\n\ | |
825 | or \"struct STRUCT-TAG\" or \"class CLASS-NAME\" or \"union UNION-TAG\"\n\ | |
826 | or \"enum ENUM-TAG\") or an expression.\n\ | |
11081198 | 827 | The selected stack frame's lexical context is used to look up the name.\n\ |
53342f27 TT |
828 | Contrary to \"whatis\", \"ptype\" always unrolls any typedefs.\n\ |
829 | \n\ | |
830 | Available FLAGS are:\n\ | |
831 | /r print in \"raw\" form; do not substitute typedefs\n\ | |
832 | /m do not print methods defined in a class\n\ | |
833 | /M print methods defined in a class\n\ | |
834 | /t do not print typedefs defined in a class\n\ | |
7c161838 | 835 | /T print typedefs defined in a class\n\ |
fbb46296 LS |
836 | /o print offsets and sizes of fields in a struct (like pahole)\n\ |
837 | /x use hexadecimal notation when displaying sizes and offsets\n\ | |
838 | of struct members\n\ | |
839 | /d use decimal notation when displaying sizes and offsets\n\ | |
840 | of struct members ")); | |
4fc5d43e | 841 | set_cmd_completer (c, expression_completer); |
c906108c | 842 | |
4fc5d43e TT |
843 | c = add_com ("whatis", class_vars, whatis_command, |
844 | _("Print data type of expression EXP.\n\ | |
11081198 | 845 | Only one level of typedefs is unrolled. See also \"ptype\".")); |
4fc5d43e | 846 | set_cmd_completer (c, expression_completer); |
53342f27 | 847 | |
0743fc83 TT |
848 | add_show_prefix_cmd ("type", no_class, |
849 | _("Generic command for showing type-printing settings."), | |
2f822da5 | 850 | &showprinttypelist, 0, &showprintlist); |
0743fc83 TT |
851 | add_basic_prefix_cmd ("type", no_class, |
852 | _("Generic command for setting how types print."), | |
2f822da5 | 853 | &setprinttypelist, 0, &setprintlist); |
53342f27 TT |
854 | |
855 | add_setshow_boolean_cmd ("methods", no_class, &print_methods, | |
856 | _("\ | |
857 | Set printing of methods defined in classes."), _("\ | |
858 | Show printing of methods defined in classes."), NULL, | |
859 | set_print_type_methods, | |
860 | show_print_type_methods, | |
861 | &setprinttypelist, &showprinttypelist); | |
862 | add_setshow_boolean_cmd ("typedefs", no_class, &print_typedefs, | |
863 | _("\ | |
864 | Set printing of typedefs defined in classes."), _("\ | |
865 | Show printing of typedefs defined in classes."), NULL, | |
866 | set_print_type_typedefs, | |
867 | show_print_type_typedefs, | |
868 | &setprinttypelist, &showprinttypelist); | |
883fd55a KS |
869 | |
870 | add_setshow_zuinteger_unlimited_cmd ("nested-type-limit", no_class, | |
871 | &print_nested_type_limit, | |
872 | _("\ | |
873 | Set the number of recursive nested type definitions to print \ | |
874 | (\"unlimited\" or -1 to show all)."), _("\ | |
875 | Show the number of recursive nested type definitions to print."), NULL, | |
876 | set_print_type_nested_types, | |
877 | show_print_type_nested_types, | |
878 | &setprinttypelist, &showprinttypelist); | |
fbb46296 LS |
879 | |
880 | add_setshow_boolean_cmd ("hex", no_class, &print_offsets_and_sizes_in_hex, | |
881 | _("\ | |
882 | Set printing of struct members sizes and offsets using hex notation."), _("\ | |
883 | Show whether sizes and offsets of struct members are printed using hex \ | |
884 | notation."), nullptr, set_print_offsets_and_sizes_in_hex, | |
885 | show_print_offsets_and_sizes_in_hex, | |
886 | &setprinttypelist, &showprinttypelist); | |
c906108c | 887 | } |
3f2f83dd KB |
888 | |
889 | /* Print <not allocated> status to stream STREAM. */ | |
890 | ||
891 | void | |
892 | val_print_not_allocated (struct ui_file *stream) | |
893 | { | |
7f6aba03 | 894 | fprintf_styled (stream, metadata_style.style (), _("<not allocated>")); |
3f2f83dd KB |
895 | } |
896 | ||
897 | /* Print <not associated> status to stream STREAM. */ | |
898 | ||
899 | void | |
900 | val_print_not_associated (struct ui_file *stream) | |
901 | { | |
7f6aba03 | 902 | fprintf_styled (stream, metadata_style.style (), _("<not associated>")); |
3f2f83dd | 903 | } |