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