]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/printcmd.c
Fix some value comments
[thirdparty/binutils-gdb.git] / gdb / printcmd.c
CommitLineData
c906108c 1/* Print values for GNU debugger GDB.
e2ad119d 2
213516ef 3 Copyright (C) 1986-2023 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"
c906108c
SS
21#include "frame.h"
22#include "symtab.h"
23#include "gdbtypes.h"
24#include "value.h"
25#include "language.h"
1f6f6e21 26#include "c-lang.h"
c906108c
SS
27#include "expression.h"
28#include "gdbcore.h"
29#include "gdbcmd.h"
30#include "target.h"
31#include "breakpoint.h"
32#include "demangle.h"
50f182aa 33#include "gdb-demangle.h"
c906108c
SS
34#include "valprint.h"
35#include "annotate.h"
c5aa993b
JM
36#include "symfile.h" /* for overlay functions */
37#include "objfiles.h" /* ditto */
c94fdfd0 38#include "completer.h" /* for completion functions */
8b93c638 39#include "ui-out.h"
fe898f56 40#include "block.h"
92bf2b80 41#include "disasm.h"
f69fdf9b 42#include "target-float.h"
76727919 43#include "observable.h"
a3247a22 44#include "solist.h"
a3247a22 45#include "parser-defs.h"
6c7a06a3 46#include "charset.h"
704e9165 47#include "arch-utils.h"
e9cafbcc 48#include "cli/cli-utils.h"
7d8062de 49#include "cli/cli-option.h"
01770bbd 50#include "cli/cli-script.h"
80ae2043 51#include "cli/cli-style.h"
268a13a5 52#include "gdbsupport/format.h"
05cba821 53#include "source.h"
268a13a5
TT
54#include "gdbsupport/byte-vector.h"
55#include "gdbsupport/gdb_optional.h"
037d7135 56#include "safe-ctype.h"
48136e00
LM
57#include "gdbsupport/rsp-low.h"
58
59/* Chain containing all defined memory-tag subcommands. */
60
61static struct cmd_list_element *memory_tag_list;
c906108c 62
c906108c
SS
63/* Last specified output format. */
64
a6bac58e 65static char last_format = 0;
c906108c
SS
66
67/* Last specified examination size. 'b', 'h', 'w' or `q'. */
68
69static char last_size = 'w';
70
9be2ae8f
TT
71/* Last specified count for the 'x' command. */
72
73static int last_count;
74
bef382e6
LM
75/* Last specified tag-printing option. */
76
77static bool last_print_tags = false;
78
5d3729b5 79/* Default address to examine next, and associated architecture. */
c906108c 80
5d3729b5 81static struct gdbarch *next_gdbarch;
c906108c
SS
82static CORE_ADDR next_address;
83
a4642986
MR
84/* Number of delay instructions following current disassembled insn. */
85
86static int branch_delay_insns;
87
c906108c
SS
88/* Last address examined. */
89
90static CORE_ADDR last_examine_address;
91
92/* Contents of last address examined.
93 This is not valid past the end of the `x' command! */
94
9b558729 95static value_ref_ptr last_examine_value;
c906108c
SS
96
97/* Largest offset between a symbolic value and an address, that will be
98 printed as `0x1234 <symbol+offset>'. */
99
100static unsigned int max_symbolic_offset = UINT_MAX;
920d2a44
AC
101static void
102show_max_symbolic_offset (struct ui_file *file, int from_tty,
103 struct cmd_list_element *c, const char *value)
104{
6cb06a8c
TT
105 gdb_printf (file,
106 _("The largest offset that will be "
107 "printed in <symbol+1234> form is %s.\n"),
108 value);
920d2a44 109}
c906108c
SS
110
111/* Append the source filename and linenumber of the symbol when
112 printing a symbolic value as `<symbol at filename:linenum>' if set. */
491144b5 113static bool print_symbol_filename = false;
920d2a44
AC
114static void
115show_print_symbol_filename (struct ui_file *file, int from_tty,
116 struct cmd_list_element *c, const char *value)
117{
6cb06a8c
TT
118 gdb_printf (file, _("Printing of source filename and "
119 "line number with <symbol> is %s.\n"),
120 value);
920d2a44 121}
c906108c
SS
122
123/* Number of auto-display expression currently being displayed.
9d8fa392 124 So that we can disable it if we get a signal within it.
c906108c
SS
125 -1 when not doing one. */
126
5a18e302 127static int current_display_number;
c906108c 128
8be4b118
TT
129/* Last allocated display number. */
130
131static int display_number;
132
c906108c 133struct display
c5aa993b 134 {
8be4b118
TT
135 display (const char *exp_string_, expression_up &&exp_,
136 const struct format_data &format_, struct program_space *pspace_,
137 const struct block *block_)
138 : exp_string (exp_string_),
139 exp (std::move (exp_)),
140 number (++display_number),
141 format (format_),
142 pspace (pspace_),
143 block (block_),
144 enabled_p (true)
145 {
146 }
6c95b8df 147
fa8a61dc 148 /* The expression as the user typed it. */
8be4b118 149 std::string exp_string;
6c95b8df 150
c5aa993b 151 /* Expression to be evaluated and displayed. */
4d01a485 152 expression_up exp;
6c95b8df 153
c5aa993b
JM
154 /* Item number of this auto-display item. */
155 int number;
6c95b8df 156
c5aa993b
JM
157 /* Display format specified. */
158 struct format_data format;
6c95b8df
PA
159
160 /* Program space associated with `block'. */
161 struct program_space *pspace;
162
0df8b418 163 /* Innermost block required by this expression when evaluated. */
270140bd 164 const struct block *block;
6c95b8df 165
0df8b418 166 /* Status of this display (enabled or disabled). */
8be4b118 167 bool enabled_p;
c5aa993b 168 };
c906108c 169
8be4b118
TT
170/* Expressions whose values should be displayed automatically each
171 time the program stops. */
c906108c 172
8be4b118 173static std::vector<std::unique_ptr<struct display>> all_displays;
c9174737 174
0df8b418 175/* Prototypes for local functions. */
c906108c 176
a14ed312 177static void do_one_display (struct display *);
c906108c 178\f
c5aa993b 179
c906108c
SS
180/* Decode a format specification. *STRING_PTR should point to it.
181 OFORMAT and OSIZE are used as defaults for the format and size
182 if none are given in the format specification.
183 If OSIZE is zero, then the size field of the returned value
184 should be set only if a size is explicitly specified by the
185 user.
186 The structure returned describes all the data
187 found in the specification. In addition, *STRING_PTR is advanced
188 past the specification and past all whitespace following it. */
189
190static struct format_data
6f937416 191decode_format (const char **string_ptr, int oformat, int osize)
c906108c
SS
192{
193 struct format_data val;
6f937416 194 const char *p = *string_ptr;
c906108c
SS
195
196 val.format = '?';
197 val.size = '?';
198 val.count = 1;
a6bac58e 199 val.raw = 0;
bef382e6 200 val.print_tags = false;
c906108c 201
bb556f1f
TK
202 if (*p == '-')
203 {
204 val.count = -1;
205 p++;
206 }
c906108c 207 if (*p >= '0' && *p <= '9')
bb556f1f 208 val.count *= atoi (p);
c5aa993b
JM
209 while (*p >= '0' && *p <= '9')
210 p++;
c906108c
SS
211
212 /* Now process size or format letters that follow. */
213
214 while (1)
215 {
216 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
217 val.size = *p++;
a6bac58e
TT
218 else if (*p == 'r')
219 {
220 val.raw = 1;
221 p++;
222 }
bef382e6
LM
223 else if (*p == 'm')
224 {
225 val.print_tags = true;
226 p++;
227 }
c906108c
SS
228 else if (*p >= 'a' && *p <= 'z')
229 val.format = *p++;
230 else
231 break;
232 }
233
2f433492 234 *string_ptr = skip_spaces (p);
c906108c
SS
235
236 /* Set defaults for format and size if not specified. */
237 if (val.format == '?')
238 {
239 if (val.size == '?')
240 {
241 /* Neither has been specified. */
242 val.format = oformat;
243 val.size = osize;
244 }
245 else
246 /* If a size is specified, any format makes a reasonable
247 default except 'i'. */
248 val.format = oformat == 'i' ? 'x' : oformat;
249 }
250 else if (val.size == '?')
251 switch (val.format)
252 {
253 case 'a':
5d3729b5
UW
254 /* Pick the appropriate size for an address. This is deferred
255 until do_examine when we know the actual architecture to use.
256 A special size value of 'a' is used to indicate this case. */
257 val.size = osize ? 'a' : osize;
c906108c
SS
258 break;
259 case 'f':
260 /* Floating point has to be word or giantword. */
261 if (osize == 'w' || osize == 'g')
262 val.size = osize;
263 else
264 /* Default it to giantword if the last used size is not
265 appropriate. */
266 val.size = osize ? 'g' : osize;
267 break;
268 case 'c':
269 /* Characters default to one byte. */
270 val.size = osize ? 'b' : osize;
271 break;
9a22f0d0 272 case 's':
3e43a32a
MS
273 /* Display strings with byte size chars unless explicitly
274 specified. */
9a22f0d0
PM
275 val.size = '\0';
276 break;
277
c906108c
SS
278 default:
279 /* The default is the size most recently specified. */
280 val.size = osize;
281 }
282
283 return val;
284}
285\f
79a45b7d 286/* Print value VAL on stream according to OPTIONS.
c906108c 287 Do not end with a newline.
c906108c 288 SIZE is the letter for the size of datum being printed.
ea37ba09
DJ
289 This is used to pad hex numbers so they line up. SIZE is 0
290 for print / output and set for examine. */
c906108c
SS
291
292static void
79a45b7d
TT
293print_formatted (struct value *val, int size,
294 const struct value_print_options *options,
fba45db2 295 struct ui_file *stream)
c906108c 296{
d0c97917 297 struct type *type = check_typedef (val->type ());
df86565b 298 int len = type->length ();
c906108c 299
736355f2 300 if (val->lval () == lval_memory)
9feb2d07 301 next_address = val->address () + len;
c906108c 302
ea37ba09 303 if (size)
c906108c 304 {
79a45b7d 305 switch (options->format)
ea37ba09
DJ
306 {
307 case 's':
6c7a06a3 308 {
d0c97917 309 struct type *elttype = val->type ();
ad3bbd48 310
9feb2d07 311 next_address = (val->address ()
09ca9e2e 312 + val_print_string (elttype, NULL,
9feb2d07 313 val->address (), -1,
9a22f0d0 314 stream, options) * len);
6c7a06a3 315 }
ea37ba09 316 return;
c906108c 317
ea37ba09
DJ
318 case 'i':
319 /* We often wrap here if there are long symbolic names. */
1285ce86 320 stream->wrap_here (4);
9feb2d07 321 next_address = (val->address ()
8ee511af 322 + gdb_print_insn (type->arch (),
9feb2d07 323 val->address (), stream,
ea37ba09
DJ
324 &branch_delay_insns));
325 return;
326 }
c906108c 327 }
ea37ba09 328
79a45b7d 329 if (options->format == 0 || options->format == 's'
3d87245c 330 || type->code () == TYPE_CODE_VOID
78134374
SM
331 || type->code () == TYPE_CODE_REF
332 || type->code () == TYPE_CODE_ARRAY
333 || type->code () == TYPE_CODE_STRING
334 || type->code () == TYPE_CODE_STRUCT
335 || type->code () == TYPE_CODE_UNION
336 || type->code () == TYPE_CODE_NAMESPACE)
79a45b7d 337 value_print (val, stream, options);
ea37ba09 338 else
b021a221
MS
339 /* User specified format, so don't look to the type to tell us
340 what to do. */
4dba70ee 341 value_print_scalar_formatted (val, options, size, stream);
c906108c
SS
342}
343
b806fb9a
UW
344/* Return builtin floating point type of same length as TYPE.
345 If no such type is found, return TYPE itself. */
346static struct type *
50810684 347float_type_from_length (struct type *type)
b806fb9a 348{
8ee511af 349 struct gdbarch *gdbarch = type->arch ();
b806fb9a 350 const struct builtin_type *builtin = builtin_type (gdbarch);
b806fb9a 351
df86565b 352 if (type->length () == builtin->builtin_float->length ())
b806fb9a 353 type = builtin->builtin_float;
df86565b 354 else if (type->length () == builtin->builtin_double->length ())
b806fb9a 355 type = builtin->builtin_double;
df86565b 356 else if (type->length () == builtin->builtin_long_double->length ())
b806fb9a
UW
357 type = builtin->builtin_long_double;
358
359 return type;
360}
361
c906108c 362/* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
ab2188aa
PA
363 according to OPTIONS and SIZE on STREAM. Formats s and i are not
364 supported at this level. */
c906108c
SS
365
366void
7c543f7b 367print_scalar_formatted (const gdb_byte *valaddr, struct type *type,
79a45b7d
TT
368 const struct value_print_options *options,
369 int size, struct ui_file *stream)
c906108c 370{
8ee511af 371 struct gdbarch *gdbarch = type->arch ();
df86565b 372 unsigned int len = type->length ();
34877895 373 enum bfd_endian byte_order = type_byte_order (type);
c906108c 374
ab2188aa
PA
375 /* String printing should go through val_print_scalar_formatted. */
376 gdb_assert (options->format != 's');
ea37ba09 377
ef166cf4 378 /* If the value is a pointer, and pointers and addresses are not the
d0aee0c4 379 same, then at this point, the value's length (in target bytes) is
df86565b 380 gdbarch_addr_bit/TARGET_CHAR_BIT, not type->length (). */
78134374 381 if (type->code () == TYPE_CODE_PTR)
69feb676 382 len = gdbarch_addr_bit (gdbarch) / TARGET_CHAR_BIT;
ef166cf4 383
c906108c
SS
384 /* If we are printing it as unsigned, truncate it in case it is actually
385 a negative signed value (e.g. "print/u (short)-1" should print 65535
386 (if shorts are 16 bits) instead of 4294967295). */
d9109c80 387 if (options->format != 'c'
c6d940a9 388 && (options->format != 'd' || type->is_unsigned ()))
c906108c 389 {
df86565b
SM
390 if (len < type->length () && byte_order == BFD_ENDIAN_BIG)
391 valaddr += type->length () - len;
c906108c
SS
392 }
393
20a5fcbd
TT
394 /* Allow LEN == 0, and in this case, don't assume that VALADDR is
395 valid. */
396 const gdb_byte zero = 0;
397 if (len == 0)
398 {
399 len = 1;
400 valaddr = &zero;
401 }
402
d9109c80 403 if (size != 0 && (options->format == 'x' || options->format == 't'))
c906108c 404 {
d9109c80
TT
405 /* Truncate to fit. */
406 unsigned newlen;
407 switch (size)
c906108c 408 {
d9109c80
TT
409 case 'b':
410 newlen = 1;
411 break;
412 case 'h':
413 newlen = 2;
414 break;
415 case 'w':
416 newlen = 4;
417 break;
418 case 'g':
419 newlen = 8;
420 break;
421 default:
422 error (_("Undefined output size \"%c\"."), size);
c906108c 423 }
d9109c80
TT
424 if (newlen < len && byte_order == BFD_ENDIAN_BIG)
425 valaddr += len - newlen;
426 len = newlen;
427 }
c906108c 428
56262a93 429 /* Biased range types and sub-word scalar types must be handled
20a5fcbd 430 here; the value is correctly computed by unpack_long. */
4e962e74
TT
431 gdb::byte_vector converted_bytes;
432 /* Some cases below will unpack the value again. In the biased
433 range case, we want to avoid this, so we store the unpacked value
434 here for possible use later. */
435 gdb::optional<LONGEST> val_long;
56262a93 436 if ((is_fixed_point_type (type)
4e962e74
TT
437 && (options->format == 'o'
438 || options->format == 'x'
439 || options->format == 't'
440 || options->format == 'z'
441 || options->format == 'd'
442 || options->format == 'u'))
20a5fcbd
TT
443 || (type->code () == TYPE_CODE_RANGE && type->bounds ()->bias != 0)
444 || type->bit_size_differs_p ())
d9109c80 445 {
4e962e74 446 val_long.emplace (unpack_long (type, valaddr));
df86565b
SM
447 converted_bytes.resize (type->length ());
448 store_signed_integer (converted_bytes.data (), type->length (),
4e962e74
TT
449 byte_order, *val_long);
450 valaddr = converted_bytes.data ();
d9109c80 451 }
c906108c 452
fdf0cbc2
UW
453 /* Printing a non-float type as 'f' will interpret the data as if it were
454 of a floating-point type of the same length, if that exists. Otherwise,
455 the data is printed as integer. */
456 char format = options->format;
78134374 457 if (format == 'f' && type->code () != TYPE_CODE_FLT)
fdf0cbc2
UW
458 {
459 type = float_type_from_length (type);
78134374 460 if (type->code () != TYPE_CODE_FLT)
dda83cd7 461 format = 0;
fdf0cbc2
UW
462 }
463
464 switch (format)
d9109c80
TT
465 {
466 case 'o':
467 print_octal_chars (stream, valaddr, len, byte_order);
468 break;
d6382fff
TT
469 case 'd':
470 print_decimal_chars (stream, valaddr, len, true, byte_order);
471 break;
c906108c 472 case 'u':
d9109c80 473 print_decimal_chars (stream, valaddr, len, false, byte_order);
c906108c 474 break;
d9109c80 475 case 0:
78134374 476 if (type->code () != TYPE_CODE_FLT)
d9109c80 477 {
c6d940a9 478 print_decimal_chars (stream, valaddr, len, !type->is_unsigned (),
d9109c80
TT
479 byte_order);
480 break;
481 }
482 /* FALLTHROUGH */
483 case 'f':
d9109c80 484 print_floating (valaddr, type, stream);
c906108c
SS
485 break;
486
d9109c80 487 case 't':
21a527df 488 print_binary_chars (stream, valaddr, len, byte_order, size > 0, options);
d9109c80
TT
489 break;
490 case 'x':
491 print_hex_chars (stream, valaddr, len, byte_order, size > 0);
492 break;
493 case 'z':
494 print_hex_chars (stream, valaddr, len, byte_order, true);
c906108c 495 break;
c906108c 496 case 'c':
79a45b7d
TT
497 {
498 struct value_print_options opts = *options;
69feb676 499
4e962e74
TT
500 if (!val_long.has_value ())
501 val_long.emplace (unpack_long (type, valaddr));
d9109c80 502
ad3bbd48 503 opts.format = 0;
c6d940a9 504 if (type->is_unsigned ())
69feb676 505 type = builtin_type (gdbarch)->builtin_true_unsigned_char;
24b21115 506 else
69feb676
UW
507 type = builtin_type (gdbarch)->builtin_true_char;
508
4e962e74 509 value_print (value_from_longest (type, *val_long), stream, &opts);
79a45b7d 510 }
c906108c
SS
511 break;
512
d9109c80 513 case 'a':
c906108c 514 {
4e962e74
TT
515 if (!val_long.has_value ())
516 val_long.emplace (unpack_long (type, valaddr));
517 print_address (gdbarch, *val_long, stream);
c906108c
SS
518 }
519 break;
520
521 default:
fdf0cbc2 522 error (_("Undefined output format \"%c\"."), format);
c906108c
SS
523 }
524}
525
526/* Specify default address for `x' command.
675dcf4f 527 The `info lines' command uses this. */
c906108c
SS
528
529void
8b9b9e1a 530set_next_address (struct gdbarch *gdbarch, CORE_ADDR addr)
c906108c 531{
8b9b9e1a
UW
532 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
533
5d3729b5 534 next_gdbarch = gdbarch;
c906108c
SS
535 next_address = addr;
536
537 /* Make address available to the user as $_. */
538 set_internalvar (lookup_internalvar ("_"),
8b9b9e1a 539 value_from_pointer (ptr_type, addr));
c906108c
SS
540}
541
542/* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
543 after LEADIN. Print nothing if no symbolic name is found nearby.
544 Optionally also print source file and line number, if available.
545 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
546 or to interpret it as a possible C++ name and convert it back to source
547 form. However note that DO_DEMANGLE can be overridden by the specific
9cb709b6
TT
548 settings of the demangle and asm_demangle variables. Returns
549 non-zero if anything was printed; zero otherwise. */
c906108c 550
9cb709b6 551int
22e722e1
DJ
552print_address_symbolic (struct gdbarch *gdbarch, CORE_ADDR addr,
553 struct ui_file *stream,
a121b7c1 554 int do_demangle, const char *leadin)
dfcd3bfb 555{
c7110220 556 std::string name, filename;
dfcd3bfb
JM
557 int unmapped = 0;
558 int offset = 0;
559 int line = 0;
560
2dc80cf8 561 if (build_address_symbolic (gdbarch, addr, do_demangle, false, &name,
dda83cd7 562 &offset, &filename, &line, &unmapped))
c7110220 563 return 0;
dfcd3bfb 564
0426ad51 565 gdb_puts (leadin, stream);
dfcd3bfb 566 if (unmapped)
0426ad51 567 gdb_puts ("<*", stream);
dfcd3bfb 568 else
0426ad51 569 gdb_puts ("<", stream);
af79b68d 570 fputs_styled (name.c_str (), function_name_style.style (), stream);
dfcd3bfb 571 if (offset != 0)
6cb06a8c 572 gdb_printf (stream, "%+d", offset);
dfcd3bfb
JM
573
574 /* Append source filename and line number if desired. Give specific
575 line # of this addr, if we have it; else line # of the nearest symbol. */
c7110220 576 if (print_symbol_filename && !filename.empty ())
dfcd3bfb 577 {
0426ad51 578 gdb_puts (line == -1 ? " in " : " at ", stream);
af79b68d 579 fputs_styled (filename.c_str (), file_name_style.style (), stream);
dfcd3bfb 580 if (line != -1)
6cb06a8c 581 gdb_printf (stream, ":%d", line);
dfcd3bfb
JM
582 }
583 if (unmapped)
0426ad51 584 gdb_puts ("*>", stream);
dfcd3bfb 585 else
0426ad51 586 gdb_puts (">", stream);
dfcd3bfb 587
9cb709b6 588 return 1;
dfcd3bfb
JM
589}
590
c7110220
TT
591/* See valprint.h. */
592
dfcd3bfb 593int
22e722e1
DJ
594build_address_symbolic (struct gdbarch *gdbarch,
595 CORE_ADDR addr, /* IN */
2dc80cf8
KB
596 bool do_demangle, /* IN */
597 bool prefer_sym_over_minsym, /* IN */
c7110220 598 std::string *name, /* OUT */
dfcd3bfb 599 int *offset, /* OUT */
c7110220 600 std::string *filename, /* OUT */
dfcd3bfb
JM
601 int *line, /* OUT */
602 int *unmapped) /* OUT */
c906108c 603{
77e371c0 604 struct bound_minimal_symbol msymbol;
c906108c 605 struct symbol *symbol;
c906108c 606 CORE_ADDR name_location = 0;
714835d5 607 struct obj_section *section = NULL;
0d5cff50 608 const char *name_temp = "";
dfcd3bfb 609
89c83b10 610 /* Let's say it is mapped (not unmapped). */
dfcd3bfb 611 *unmapped = 0;
c906108c 612
dfcd3bfb 613 /* Determine if the address is in an overlay, and whether it is
675dcf4f 614 mapped. */
c906108c
SS
615 if (overlay_debugging)
616 {
617 section = find_pc_overlay (addr);
618 if (pc_in_unmapped_range (addr, section))
619 {
dfcd3bfb 620 *unmapped = 1;
c906108c
SS
621 addr = overlay_mapped_address (addr, section);
622 }
623 }
624
2dc80cf8
KB
625 /* Try to find the address in both the symbol table and the minsyms.
626 In most cases, we'll prefer to use the symbol instead of the
627 minsym. However, there are cases (see below) where we'll choose
628 to use the minsym instead. */
c906108c
SS
629
630 /* This is defective in the sense that it only finds text symbols. So
631 really this is kind of pointless--we should make sure that the
632 minimal symbols have everything we need (by changing that we could
633 save some memory, but for many debug format--ELF/DWARF or
634 anything/stabs--it would be inconvenient to eliminate those minimal
635 symbols anyway). */
77e371c0 636 msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
c906108c
SS
637 symbol = find_pc_sect_function (addr, section);
638
639 if (symbol)
640 {
22e722e1
DJ
641 /* If this is a function (i.e. a code address), strip out any
642 non-address bits. For instance, display a pointer to the
643 first instruction of a Thumb function as <function>; the
644 second instruction will be <function+2>, even though the
645 pointer is <function+3>. This matches the ISA behavior. */
646 addr = gdbarch_addr_bits_remove (gdbarch, addr);
647
6395b628 648 name_location = symbol->value_block ()->entry_pc ();
406fc7fb 649 if (do_demangle || asm_demangle)
987012b8 650 name_temp = symbol->print_name ();
c906108c 651 else
987012b8 652 name_temp = symbol->linkage_name ();
c906108c
SS
653 }
654
77e371c0 655 if (msymbol.minsym != NULL
5bbfd12d
SM
656 && msymbol.minsym->has_size ()
657 && msymbol.minsym->size () == 0
60f62e2b
SM
658 && msymbol.minsym->type () != mst_text
659 && msymbol.minsym->type () != mst_text_gnu_ifunc
660 && msymbol.minsym->type () != mst_file_text)
77e371c0 661 msymbol.minsym = NULL;
9cb709b6 662
77e371c0 663 if (msymbol.minsym != NULL)
c906108c 664 {
2dc80cf8
KB
665 /* Use the minsym if no symbol is found.
666
667 Additionally, use the minsym instead of a (found) symbol if
668 the following conditions all hold:
669 1) The prefer_sym_over_minsym flag is false.
670 2) The minsym address is identical to that of the address under
671 consideration.
672 3) The symbol address is not identical to that of the address
673 under consideration. */
674 if (symbol == NULL ||
dda83cd7 675 (!prefer_sym_over_minsym
4aeddc50 676 && msymbol.value_address () == addr
2dc80cf8 677 && name_location != addr))
c906108c 678 {
fe8400b4
WN
679 /* If this is a function (i.e. a code address), strip out any
680 non-address bits. For instance, display a pointer to the
681 first instruction of a Thumb function as <function>; the
682 second instruction will be <function+2>, even though the
683 pointer is <function+3>. This matches the ISA behavior. */
60f62e2b
SM
684 if (msymbol.minsym->type () == mst_text
685 || msymbol.minsym->type () == mst_text_gnu_ifunc
686 || msymbol.minsym->type () == mst_file_text
687 || msymbol.minsym->type () == mst_solib_trampoline)
fe8400b4
WN
688 addr = gdbarch_addr_bits_remove (gdbarch, addr);
689
c906108c 690 symbol = 0;
4aeddc50 691 name_location = msymbol.value_address ();
406fc7fb 692 if (do_demangle || asm_demangle)
c9d95fa3 693 name_temp = msymbol.minsym->print_name ();
c906108c 694 else
c9d95fa3 695 name_temp = msymbol.minsym->linkage_name ();
c906108c
SS
696 }
697 }
77e371c0 698 if (symbol == NULL && msymbol.minsym == NULL)
dfcd3bfb 699 return 1;
c906108c 700
c906108c
SS
701 /* If the nearest symbol is too far away, don't print anything symbolic. */
702
703 /* For when CORE_ADDR is larger than unsigned int, we do math in
704 CORE_ADDR. But when we detect unsigned wraparound in the
705 CORE_ADDR math, we ignore this test and print the offset,
706 because addr+max_symbolic_offset has wrapped through the end
707 of the address space back to the beginning, giving bogus comparison. */
708 if (addr > name_location + max_symbolic_offset
709 && name_location + max_symbolic_offset > name_location)
dfcd3bfb 710 return 1;
c906108c 711
a1530dc7 712 *offset = (LONGEST) addr - name_location;
dfcd3bfb 713
c7110220 714 *name = name_temp;
c906108c 715
c906108c
SS
716 if (print_symbol_filename)
717 {
718 struct symtab_and_line sal;
719
720 sal = find_pc_sect_line (addr, section, 0);
721
722 if (sal.symtab)
dfcd3bfb 723 {
c7110220 724 *filename = symtab_to_filename_for_display (sal.symtab);
dfcd3bfb
JM
725 *line = sal.line;
726 }
c906108c 727 }
dfcd3bfb 728 return 0;
c906108c
SS
729}
730
c906108c
SS
731
732/* Print address ADDR symbolically on STREAM.
733 First print it as a number. Then perhaps print
734 <SYMBOL + OFFSET> after the number. */
735
736void
5af949e3
UW
737print_address (struct gdbarch *gdbarch,
738 CORE_ADDR addr, struct ui_file *stream)
c906108c 739{
35fb8261 740 fputs_styled (paddress (gdbarch, addr), address_style.style (), stream);
22e722e1 741 print_address_symbolic (gdbarch, addr, stream, asm_demangle, " ");
c906108c
SS
742}
743
2b28d209
PP
744/* Return a prefix for instruction address:
745 "=> " for current instruction, else " ". */
746
747const char *
748pc_prefix (CORE_ADDR addr)
749{
750 if (has_stack_frames ())
751 {
bd2b40ac 752 frame_info_ptr frame;
2b28d209
PP
753 CORE_ADDR pc;
754
755 frame = get_selected_frame (NULL);
ce406537 756 if (get_frame_pc_if_available (frame, &pc) && pc == addr)
2b28d209
PP
757 return "=> ";
758 }
759 return " ";
760}
761
c906108c
SS
762/* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
763 controls whether to print the symbolic name "raw" or demangled.
9cb709b6 764 Return non-zero if anything was printed; zero otherwise. */
c906108c 765
9cb709b6 766int
edf0c1b7
TT
767print_address_demangle (const struct value_print_options *opts,
768 struct gdbarch *gdbarch, CORE_ADDR addr,
5af949e3 769 struct ui_file *stream, int do_demangle)
c906108c 770{
1d51a733 771 if (opts->addressprint)
c906108c 772 {
35fb8261 773 fputs_styled (paddress (gdbarch, addr), address_style.style (), stream);
22e722e1 774 print_address_symbolic (gdbarch, addr, stream, do_demangle, " ");
c906108c
SS
775 }
776 else
777 {
9cb709b6 778 return print_address_symbolic (gdbarch, addr, stream, do_demangle, "");
c906108c 779 }
9cb709b6 780 return 1;
c906108c
SS
781}
782\f
783
bb556f1f
TK
784/* Find the address of the instruction that is INST_COUNT instructions before
785 the instruction at ADDR.
786 Since some architectures have variable-length instructions, we can't just
787 simply subtract INST_COUNT * INSN_LEN from ADDR. Instead, we use line
788 number information to locate the nearest known instruction boundary,
789 and disassemble forward from there. If we go out of the symbol range
790 during disassembling, we return the lowest address we've got so far and
791 set the number of instructions read to INST_READ. */
792
793static CORE_ADDR
794find_instruction_backward (struct gdbarch *gdbarch, CORE_ADDR addr,
dda83cd7 795 int inst_count, int *inst_read)
bb556f1f
TK
796{
797 /* The vector PCS is used to store instruction addresses within
798 a pc range. */
799 CORE_ADDR loop_start, loop_end, p;
52d214d3 800 std::vector<CORE_ADDR> pcs;
bb556f1f 801 struct symtab_and_line sal;
bb556f1f
TK
802
803 *inst_read = 0;
804 loop_start = loop_end = addr;
805
806 /* In each iteration of the outer loop, we get a pc range that ends before
807 LOOP_START, then we count and store every instruction address of the range
808 iterated in the loop.
809 If the number of instructions counted reaches INST_COUNT, return the
810 stored address that is located INST_COUNT instructions back from ADDR.
811 If INST_COUNT is not reached, we subtract the number of counted
812 instructions from INST_COUNT, and go to the next iteration. */
813 do
814 {
52d214d3 815 pcs.clear ();
bb556f1f
TK
816 sal = find_pc_sect_line (loop_start, NULL, 1);
817 if (sal.line <= 0)
dda83cd7
SM
818 {
819 /* We reach here when line info is not available. In this case,
820 we print a message and just exit the loop. The return value
821 is calculated after the loop. */
6cb06a8c
TT
822 gdb_printf (_("No line number information available "
823 "for address "));
1285ce86 824 gdb_stdout->wrap_here (2);
dda83cd7 825 print_address (gdbarch, loop_start - 1, gdb_stdout);
6cb06a8c 826 gdb_printf ("\n");
dda83cd7
SM
827 break;
828 }
bb556f1f
TK
829
830 loop_end = loop_start;
831 loop_start = sal.pc;
832
833 /* This loop pushes instruction addresses in the range from
dda83cd7 834 LOOP_START to LOOP_END. */
bb556f1f 835 for (p = loop_start; p < loop_end;)
dda83cd7 836 {
52d214d3 837 pcs.push_back (p);
dda83cd7
SM
838 p += gdb_insn_length (gdbarch, p);
839 }
bb556f1f 840
52d214d3
TT
841 inst_count -= pcs.size ();
842 *inst_read += pcs.size ();
bb556f1f
TK
843 }
844 while (inst_count > 0);
845
846 /* After the loop, the vector PCS has instruction addresses of the last
847 source line we processed, and INST_COUNT has a negative value.
848 We return the address at the index of -INST_COUNT in the vector for
849 the reason below.
850 Let's assume the following instruction addresses and run 'x/-4i 0x400e'.
851 Line X of File
dda83cd7
SM
852 0x4000
853 0x4001
854 0x4005
bb556f1f 855 Line Y of File
dda83cd7
SM
856 0x4009
857 0x400c
bb556f1f 858 => 0x400e
dda83cd7 859 0x4011
bb556f1f
TK
860 find_instruction_backward is called with INST_COUNT = 4 and expected to
861 return 0x4001. When we reach here, INST_COUNT is set to -1 because
862 it was subtracted by 2 (from Line Y) and 3 (from Line X). The value
863 4001 is located at the index 1 of the last iterated line (= Line X),
864 which is simply calculated by -INST_COUNT.
865 The case when the length of PCS is 0 means that we reached an area for
866 which line info is not available. In such case, we return LOOP_START,
867 which was the lowest instruction address that had line info. */
52d214d3 868 p = pcs.size () > 0 ? pcs[-inst_count] : loop_start;
bb556f1f
TK
869
870 /* INST_READ includes all instruction addresses in a pc range. Need to
871 exclude the beginning part up to the address we're returning. That
872 is, exclude {0x4000} in the example above. */
873 if (inst_count < 0)
874 *inst_read += inst_count;
875
bb556f1f
TK
876 return p;
877}
878
879/* Backward read LEN bytes of target memory from address MEMADDR + LEN,
880 placing the results in GDB's memory from MYADDR + LEN. Returns
881 a count of the bytes actually read. */
882
883static int
884read_memory_backward (struct gdbarch *gdbarch,
dda83cd7 885 CORE_ADDR memaddr, gdb_byte *myaddr, int len)
bb556f1f
TK
886{
887 int errcode;
888 int nread; /* Number of bytes actually read. */
889
890 /* First try a complete read. */
891 errcode = target_read_memory (memaddr, myaddr, len);
892 if (errcode == 0)
893 {
894 /* Got it all. */
895 nread = len;
896 }
897 else
898 {
899 /* Loop, reading one byte at a time until we get as much as we can. */
900 memaddr += len;
901 myaddr += len;
902 for (nread = 0; nread < len; ++nread)
dda83cd7
SM
903 {
904 errcode = target_read_memory (--memaddr, --myaddr, 1);
905 if (errcode != 0)
906 {
907 /* The read was unsuccessful, so exit the loop. */
6cb06a8c
TT
908 gdb_printf (_("Cannot access memory at address %s\n"),
909 paddress (gdbarch, memaddr));
dda83cd7
SM
910 break;
911 }
912 }
bb556f1f
TK
913 }
914 return nread;
915}
916
917/* Returns true if X (which is LEN bytes wide) is the number zero. */
918
919static int
920integer_is_zero (const gdb_byte *x, int len)
921{
922 int i = 0;
923
924 while (i < len && x[i] == 0)
925 ++i;
926 return (i == len);
927}
928
929/* Find the start address of a string in which ADDR is included.
930 Basically we search for '\0' and return the next address,
931 but if OPTIONS->PRINT_MAX is smaller than the length of a string,
932 we stop searching and return the address to print characters as many as
933 PRINT_MAX from the string. */
934
935static CORE_ADDR
936find_string_backward (struct gdbarch *gdbarch,
dda83cd7
SM
937 CORE_ADDR addr, int count, int char_size,
938 const struct value_print_options *options,
939 int *strings_counted)
bb556f1f
TK
940{
941 const int chunk_size = 0x20;
bb556f1f
TK
942 int read_error = 0;
943 int chars_read = 0;
944 int chars_to_read = chunk_size;
945 int chars_counted = 0;
946 int count_original = count;
947 CORE_ADDR string_start_addr = addr;
948
949 gdb_assert (char_size == 1 || char_size == 2 || char_size == 4);
26fcd5d7 950 gdb::byte_vector buffer (chars_to_read * char_size);
bb556f1f
TK
951 while (count > 0 && read_error == 0)
952 {
953 int i;
954
955 addr -= chars_to_read * char_size;
26fcd5d7 956 chars_read = read_memory_backward (gdbarch, addr, buffer.data (),
dda83cd7 957 chars_to_read * char_size);
bb556f1f
TK
958 chars_read /= char_size;
959 read_error = (chars_read == chars_to_read) ? 0 : 1;
76b58849 960 unsigned int print_max_chars = get_print_max_chars (options);
bb556f1f
TK
961 /* Searching for '\0' from the end of buffer in backward direction. */
962 for (i = 0; i < chars_read && count > 0 ; ++i, ++chars_counted)
dda83cd7
SM
963 {
964 int offset = (chars_to_read - i - 1) * char_size;
965
966 if (integer_is_zero (&buffer[offset], char_size)
76b58849 967 || chars_counted == print_max_chars)
dda83cd7 968 {
76b58849
AB
969 /* Found '\0' or reached `print_max_chars'. As OFFSET
970 is the offset to '\0', we add CHAR_SIZE to return
971 the start address of a string. */
dda83cd7
SM
972 --count;
973 string_start_addr = addr + offset + char_size;
974 chars_counted = 0;
975 }
976 }
bb556f1f
TK
977 }
978
979 /* Update STRINGS_COUNTED with the actual number of loaded strings. */
980 *strings_counted = count_original - count;
981
982 if (read_error != 0)
983 {
984 /* In error case, STRING_START_ADDR is pointing to the string that
dda83cd7 985 was last successfully loaded. Rewind the partially loaded string. */
bb556f1f
TK
986 string_start_addr -= chars_counted * char_size;
987 }
988
bb556f1f
TK
989 return string_start_addr;
990}
991
c906108c
SS
992/* Examine data at address ADDR in format FMT.
993 Fetch it from memory and print on gdb_stdout. */
994
995static void
5d3729b5 996do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
c906108c 997{
52f0bd74
AC
998 char format = 0;
999 char size;
1000 int count = 1;
c906108c 1001 struct type *val_type = NULL;
52f0bd74
AC
1002 int i;
1003 int maxelts;
79a45b7d 1004 struct value_print_options opts;
bb556f1f
TK
1005 int need_to_update_next_address = 0;
1006 CORE_ADDR addr_rewound = 0;
c906108c
SS
1007
1008 format = fmt.format;
1009 size = fmt.size;
1010 count = fmt.count;
5d3729b5 1011 next_gdbarch = gdbarch;
c906108c 1012 next_address = addr;
c906108c 1013
9a22f0d0
PM
1014 /* Instruction format implies fetch single bytes
1015 regardless of the specified size.
1016 The case of strings is handled in decode_format, only explicit
1017 size operator are not changed to 'b'. */
1018 if (format == 'i')
c906108c
SS
1019 size = 'b';
1020
5d3729b5
UW
1021 if (size == 'a')
1022 {
1023 /* Pick the appropriate size for an address. */
1024 if (gdbarch_ptr_bit (next_gdbarch) == 64)
1025 size = 'g';
1026 else if (gdbarch_ptr_bit (next_gdbarch) == 32)
1027 size = 'w';
1028 else if (gdbarch_ptr_bit (next_gdbarch) == 16)
1029 size = 'h';
1030 else
1031 /* Bad value for gdbarch_ptr_bit. */
f34652de 1032 internal_error (_("failed internal consistency check"));
5d3729b5
UW
1033 }
1034
1035 if (size == 'b')
df4df182 1036 val_type = builtin_type (next_gdbarch)->builtin_int8;
c906108c 1037 else if (size == 'h')
df4df182 1038 val_type = builtin_type (next_gdbarch)->builtin_int16;
c906108c 1039 else if (size == 'w')
df4df182 1040 val_type = builtin_type (next_gdbarch)->builtin_int32;
c906108c 1041 else if (size == 'g')
df4df182 1042 val_type = builtin_type (next_gdbarch)->builtin_int64;
c906108c 1043
9a22f0d0
PM
1044 if (format == 's')
1045 {
1046 struct type *char_type = NULL;
ad3bbd48 1047
9a22f0d0
PM
1048 /* Search for "char16_t" or "char32_t" types or fall back to 8-bit char
1049 if type is not found. */
1050 if (size == 'h')
1051 char_type = builtin_type (next_gdbarch)->builtin_char16;
1052 else if (size == 'w')
1053 char_type = builtin_type (next_gdbarch)->builtin_char32;
1054 if (char_type)
dda83cd7 1055 val_type = char_type;
9a22f0d0 1056 else
dda83cd7 1057 {
9a22f0d0 1058 if (size != '\0' && size != 'b')
0df8b418
MS
1059 warning (_("Unable to display strings with "
1060 "size '%c', using 'b' instead."), size);
9a22f0d0
PM
1061 size = 'b';
1062 val_type = builtin_type (next_gdbarch)->builtin_int8;
dda83cd7 1063 }
9a22f0d0
PM
1064 }
1065
c906108c
SS
1066 maxelts = 8;
1067 if (size == 'w')
1068 maxelts = 4;
1069 if (size == 'g')
1070 maxelts = 2;
1071 if (format == 's' || format == 'i')
1072 maxelts = 1;
1073
79a45b7d
TT
1074 get_formatted_print_options (&opts, format);
1075
bb556f1f
TK
1076 if (count < 0)
1077 {
1078 /* This is the negative repeat count case.
dda83cd7
SM
1079 We rewind the address based on the given repeat count and format,
1080 then examine memory from there in forward direction. */
bb556f1f
TK
1081
1082 count = -count;
1083 if (format == 'i')
dda83cd7
SM
1084 {
1085 next_address = find_instruction_backward (gdbarch, addr, count,
1086 &count);
1087 }
bb556f1f 1088 else if (format == 's')
dda83cd7
SM
1089 {
1090 next_address = find_string_backward (gdbarch, addr, count,
df86565b 1091 val_type->length (),
dda83cd7
SM
1092 &opts, &count);
1093 }
bb556f1f 1094 else
dda83cd7 1095 {
df86565b 1096 next_address = addr - count * val_type->length ();
dda83cd7 1097 }
bb556f1f
TK
1098
1099 /* The following call to print_formatted updates next_address in every
dda83cd7
SM
1100 iteration. In backward case, we store the start address here
1101 and update next_address with it before exiting the function. */
bb556f1f 1102 addr_rewound = (format == 's'
df86565b 1103 ? next_address - val_type->length ()
dda83cd7 1104 : next_address);
bb556f1f
TK
1105 need_to_update_next_address = 1;
1106 }
1107
bef382e6
LM
1108 /* Whether we need to print the memory tag information for the current
1109 address range. */
1110 bool print_range_tag = true;
1111 uint32_t gsize = gdbarch_memtag_granule_size (gdbarch);
1112
c906108c
SS
1113 /* Print as many objects as specified in COUNT, at most maxelts per line,
1114 with the address of the next one at the start of each line. */
1115
1116 while (count > 0)
1117 {
1118 QUIT;
bef382e6
LM
1119
1120 CORE_ADDR tag_laddr = 0, tag_haddr = 0;
1121
1122 /* Print the memory tag information if requested. */
1123 if (fmt.print_tags && print_range_tag
1124 && target_supports_memory_tagging ())
1125 {
1126 tag_laddr = align_down (next_address, gsize);
1127 tag_haddr = align_down (next_address + gsize, gsize);
1128
1129 struct value *v_addr
1130 = value_from_ulongest (builtin_type (gdbarch)->builtin_data_ptr,
1131 tag_laddr);
1132
1133 if (gdbarch_tagged_address_p (target_gdbarch (), v_addr))
1134 {
1135 /* Fetch the allocation tag. */
1136 struct value *tag
1137 = gdbarch_get_memtag (gdbarch, v_addr, memtag_type::allocation);
1138 std::string atag
1139 = gdbarch_memtag_to_string (gdbarch, tag);
1140
1141 if (!atag.empty ())
1142 {
6cb06a8c
TT
1143 gdb_printf (_("<Allocation Tag %s for range [%s,%s)>\n"),
1144 atag.c_str (),
1145 paddress (gdbarch, tag_laddr),
1146 paddress (gdbarch, tag_haddr));
bef382e6
LM
1147 }
1148 }
1149 print_range_tag = false;
1150 }
1151
2b28d209 1152 if (format == 'i')
0426ad51 1153 gdb_puts (pc_prefix (next_address));
5af949e3 1154 print_address (next_gdbarch, next_address, gdb_stdout);
6cb06a8c 1155 gdb_printf (":");
c906108c
SS
1156 for (i = maxelts;
1157 i > 0 && count > 0;
1158 i--, count--)
1159 {
6cb06a8c 1160 gdb_printf ("\t");
c906108c
SS
1161 /* Note that print_formatted sets next_address for the next
1162 object. */
1163 last_examine_address = next_address;
1164
c906108c 1165 /* The value to be displayed is not fetched greedily.
5d51a2db
MR
1166 Instead, to avoid the possibility of a fetched value not
1167 being used, its retrieval is delayed until the print code
c5aa993b
JM
1168 uses it. When examining an instruction stream, the
1169 disassembler will perform its own memory fetch using just
1170 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
1171 the disassembler be modified so that LAST_EXAMINE_VALUE
1172 is left with the byte sequence from the last complete
0df8b418 1173 instruction fetched from memory? */
9b558729
TT
1174 last_examine_value
1175 = release_value (value_at_lazy (val_type, next_address));
c906108c 1176
9b558729 1177 print_formatted (last_examine_value.get (), size, &opts, gdb_stdout);
a4642986
MR
1178
1179 /* Display any branch delay slots following the final insn. */
1180 if (format == 'i' && count == 1)
1181 count += branch_delay_insns;
bef382e6
LM
1182
1183 /* Update the tag range based on the current address being
1184 processed. */
1185 if (tag_haddr <= next_address)
1186 print_range_tag = true;
c906108c 1187 }
6cb06a8c 1188 gdb_printf ("\n");
c906108c 1189 }
bb556f1f
TK
1190
1191 if (need_to_update_next_address)
1192 next_address = addr_rewound;
c906108c
SS
1193}
1194\f
1195static void
8d89f51a 1196validate_format (struct format_data fmt, const char *cmdname)
c906108c
SS
1197{
1198 if (fmt.size != 0)
8a3fe4f8 1199 error (_("Size letters are meaningless in \"%s\" command."), cmdname);
c906108c 1200 if (fmt.count != 1)
8a3fe4f8 1201 error (_("Item count other than 1 is meaningless in \"%s\" command."),
c906108c 1202 cmdname);
ea37ba09 1203 if (fmt.format == 'i')
8a3fe4f8 1204 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
c906108c
SS
1205 fmt.format, cmdname);
1206}
1207
7d8062de 1208/* Parse print command format string into *OPTS and update *EXPP.
1c88ceb1
JK
1209 CMDNAME should name the current command. */
1210
1211void
1212print_command_parse_format (const char **expp, const char *cmdname,
7d8062de 1213 value_print_options *opts)
1c88ceb1
JK
1214{
1215 const char *exp = *expp;
1216
d8edc8b7
PW
1217 /* opts->raw value might already have been set by 'set print raw-values'
1218 or by using 'print -raw-values'.
1219 So, do not set opts->raw to 0, only set it to 1 if /r is given. */
1c88ceb1
JK
1220 if (exp && *exp == '/')
1221 {
7d8062de
PA
1222 format_data fmt;
1223
1c88ceb1 1224 exp++;
7d8062de
PA
1225 fmt = decode_format (&exp, last_format, 0);
1226 validate_format (fmt, cmdname);
1227 last_format = fmt.format;
1228
1229 opts->format = fmt.format;
d8edc8b7 1230 opts->raw = opts->raw || fmt.raw;
1c88ceb1
JK
1231 }
1232 else
1233 {
7d8062de 1234 opts->format = 0;
1c88ceb1
JK
1235 }
1236
1237 *expp = exp;
1238}
1239
7d8062de 1240/* See valprint.h. */
1c88ceb1
JK
1241
1242void
7d8062de 1243print_value (value *val, const value_print_options &opts)
1c88ceb1 1244{
a0c07915
AB
1245 /* This setting allows large arrays to be printed by limiting the
1246 number of elements that are loaded into GDB's memory; we only
1247 need to load as many array elements as we plan to print. */
1248 scoped_array_length_limiting limit_large_arrays (opts.print_max);
1249
0d0f488e 1250 int histindex = val->record_latest ();
1c88ceb1 1251
d0c97917 1252 annotate_value_history_begin (histindex, val->type ());
1c88ceb1 1253
6cb06a8c 1254 gdb_printf ("$%d = ", histindex);
1c88ceb1
JK
1255
1256 annotate_value_history_value ();
1257
7d8062de 1258 print_formatted (val, 0, &opts, gdb_stdout);
6cb06a8c 1259 gdb_printf ("\n");
1c88ceb1
JK
1260
1261 annotate_value_history_end ();
1262}
1263
bef382e6
LM
1264/* Returns true if memory tags should be validated. False otherwise. */
1265
1266static bool
1267should_validate_memtags (struct value *value)
1268{
d0c97917 1269 gdb_assert (value != nullptr && value->type () != nullptr);
bef382e6 1270
05558223
LM
1271 if (!target_supports_memory_tagging ())
1272 return false;
bef382e6 1273
d0c97917 1274 enum type_code code = value->type ()->code ();
05558223
LM
1275
1276 /* Skip non-address values. */
1277 if (code != TYPE_CODE_PTR
d0c97917 1278 && !TYPE_IS_REFERENCE (value->type ()))
05558223
LM
1279 return false;
1280
1281 /* OK, we have an address value. Check we have a complete value we
1282 can extract. */
d00664db
TT
1283 if (value->optimized_out ()
1284 || !value->entirely_available ())
05558223
LM
1285 return false;
1286
1287 /* We do. Check whether it includes any tags. */
1288 return gdbarch_tagged_address_p (target_gdbarch (), value);
bef382e6
LM
1289}
1290
48136e00 1291/* Helper for parsing arguments for print_command_1. */
c906108c 1292
48136e00
LM
1293static struct value *
1294process_print_command_args (const char *args, value_print_options *print_opts,
1295 bool voidprint)
c906108c 1296{
48136e00 1297 get_user_print_options (print_opts);
7d8062de 1298 /* Override global settings with explicit options, if any. */
48136e00 1299 auto group = make_value_print_options_def_group (print_opts);
7d8062de
PA
1300 gdb::option::process_options
1301 (&args, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group);
1302
48136e00 1303 print_command_parse_format (&args, "print", print_opts);
c906108c 1304
7d8062de 1305 const char *exp = args;
c906108c 1306
7d8062de 1307 if (exp != nullptr && *exp)
c906108c 1308 {
a0c07915
AB
1309 /* This setting allows large arrays to be printed by limiting the
1310 number of elements that are loaded into GDB's memory; we only
1311 need to load as many array elements as we plan to print. */
1312 scoped_array_length_limiting limit_large_arrays (print_opts->print_max);
1313
8fc48b79
TT
1314 /* VOIDPRINT is true to indicate that we do want to print a void
1315 value, so invert it for parse_expression. */
1316 expression_up expr = parse_expression (exp, nullptr, !voidprint);
48136e00 1317 return evaluate_expression (expr.get ());
c906108c 1318 }
48136e00
LM
1319
1320 return access_value_history (0);
1321}
1322
1323/* Implementation of the "print" and "call" commands. */
1324
1325static void
1326print_command_1 (const char *args, int voidprint)
1327{
1328 value_print_options print_opts;
1329
1330 struct value *val = process_print_command_args (args, &print_opts, voidprint);
c906108c 1331
d0c97917
TT
1332 if (voidprint || (val && val->type () &&
1333 val->type ()->code () != TYPE_CODE_VOID))
bef382e6
LM
1334 {
1335 /* If memory tagging validation is on, check if the tag is valid. */
05558223 1336 if (print_opts.memory_tag_violations)
bef382e6 1337 {
05558223
LM
1338 try
1339 {
1340 if (should_validate_memtags (val)
1341 && !gdbarch_memtag_matches_p (target_gdbarch (), val))
1342 {
1343 /* Fetch the logical tag. */
1344 struct value *tag
1345 = gdbarch_get_memtag (target_gdbarch (), val,
1346 memtag_type::logical);
1347 std::string ltag
1348 = gdbarch_memtag_to_string (target_gdbarch (), tag);
1349
1350 /* Fetch the allocation tag. */
1351 tag = gdbarch_get_memtag (target_gdbarch (), val,
1352 memtag_type::allocation);
1353 std::string atag
1354 = gdbarch_memtag_to_string (target_gdbarch (), tag);
1355
6cb06a8c
TT
1356 gdb_printf (_("Logical tag (%s) does not match the "
1357 "allocation tag (%s).\n"),
1358 ltag.c_str (), atag.c_str ());
05558223
LM
1359 }
1360 }
1361 catch (gdb_exception_error &ex)
1362 {
1363 if (ex.error == TARGET_CLOSE_ERROR)
1364 throw;
1365
6cb06a8c
TT
1366 gdb_printf (gdb_stderr,
1367 _("Could not validate memory tag: %s\n"),
1368 ex.message->c_str ());
05558223 1369 }
bef382e6 1370 }
05558223 1371
bef382e6
LM
1372 print_value (val, print_opts);
1373 }
7d8062de
PA
1374}
1375
037d7135
AB
1376/* Called from command completion function to skip over /FMT
1377 specifications, allowing the rest of the line to be completed. Returns
1378 true if the /FMT is at the end of the current line and there is nothing
1379 left to complete, otherwise false is returned.
1380
1381 In either case *ARGS can be updated to point after any part of /FMT that
1382 is present.
1383
1384 This function is designed so that trying to complete '/' will offer no
1385 completions, the user needs to insert the format specification
1386 themselves. Trying to complete '/FMT' (where FMT is any non-empty set
1387 of alpha-numeric characters) will cause readline to insert a single
1388 space, setting the user up to enter the expression. */
1389
1390static bool
1391skip_over_slash_fmt (completion_tracker &tracker, const char **args)
1392{
1393 const char *text = *args;
1394
1395 if (text[0] == '/')
1396 {
1397 bool in_fmt;
1398 tracker.set_use_custom_word_point (true);
1399
3df8c6af 1400 if (text[1] == '\0')
037d7135 1401 {
3df8c6af
AB
1402 /* The user tried to complete after typing just the '/' character
1403 of the /FMT string. Step the completer past the '/', but we
1404 don't offer any completions. */
1405 in_fmt = true;
1406 ++text;
1407 }
1408 else
1409 {
1410 /* The user has typed some characters after the '/', we assume
1411 this is a complete /FMT string, first skip over it. */
b3ff61f8 1412 text = skip_to_space (text);
037d7135
AB
1413
1414 if (*text == '\0')
1415 {
3df8c6af
AB
1416 /* We're at the end of the input string. The user has typed
1417 '/FMT' and asked for a completion. Push an empty
1418 completion string, this will cause readline to insert a
1419 space so the user now has '/FMT '. */
037d7135
AB
1420 in_fmt = true;
1421 tracker.add_completion (make_unique_xstrdup (text));
1422 }
1423 else
1424 {
3df8c6af
AB
1425 /* The user has already typed things after the /FMT, skip the
1426 whitespace and return false. Whoever called this function
1427 should then try to complete what comes next. */
037d7135 1428 in_fmt = false;
b3ff61f8 1429 text = skip_spaces (text);
037d7135
AB
1430 }
1431 }
037d7135
AB
1432
1433 tracker.advance_custom_word_point_by (text - *args);
1434 *args = text;
1435 return in_fmt;
1436 }
1437
1438 return false;
1439}
1440
7d8062de
PA
1441/* See valprint.h. */
1442
1443void
1444print_command_completer (struct cmd_list_element *ignore,
1445 completion_tracker &tracker,
1446 const char *text, const char * /*word*/)
1447{
1448 const auto group = make_value_print_options_def_group (nullptr);
1449 if (gdb::option::complete_options
1450 (tracker, &text, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group))
1451 return;
1452
037d7135
AB
1453 if (skip_over_slash_fmt (tracker, &text))
1454 return;
1455
7d8062de
PA
1456 const char *word = advance_to_expression_complete_word_point (tracker, text);
1457 expression_completer (ignore, tracker, text, word);
c906108c
SS
1458}
1459
c906108c 1460static void
0b39b52e 1461print_command (const char *exp, int from_tty)
c906108c 1462{
8fc48b79 1463 print_command_1 (exp, true);
c906108c
SS
1464}
1465
675dcf4f 1466/* Same as print, except it doesn't print void results. */
c906108c 1467static void
0b39b52e 1468call_command (const char *exp, int from_tty)
c906108c 1469{
8fc48b79 1470 print_command_1 (exp, false);
c906108c
SS
1471}
1472
6f937416
PA
1473/* Implementation of the "output" command. */
1474
6f937416 1475void
122b53ea 1476output_command (const char *exp, int from_tty)
c906108c 1477{
52f0bd74 1478 char format = 0;
3d6d86c6 1479 struct value *val;
c906108c 1480 struct format_data fmt;
79a45b7d 1481 struct value_print_options opts;
c906108c 1482
777ea8f1 1483 fmt.size = 0;
a6bac58e 1484 fmt.raw = 0;
777ea8f1 1485
c906108c
SS
1486 if (exp && *exp == '/')
1487 {
1488 exp++;
1489 fmt = decode_format (&exp, 0, 0);
1490 validate_format (fmt, "output");
1491 format = fmt.format;
1492 }
1493
4d01a485 1494 expression_up expr = parse_expression (exp);
c906108c 1495
4d01a485 1496 val = evaluate_expression (expr.get ());
c906108c 1497
d0c97917 1498 annotate_value_begin (val->type ());
c906108c 1499
79a45b7d 1500 get_formatted_print_options (&opts, format);
a6bac58e 1501 opts.raw = fmt.raw;
a0c07915
AB
1502
1503 /* This setting allows large arrays to be printed by limiting the
1504 number of elements that are loaded into GDB's memory; we only
1505 need to load as many array elements as we plan to print. */
1506 scoped_array_length_limiting limit_large_arrays (opts.print_max);
1507
79a45b7d 1508 print_formatted (val, fmt.size, &opts, gdb_stdout);
c906108c
SS
1509
1510 annotate_value_end ();
1511
2acceee2 1512 gdb_flush (gdb_stdout);
c906108c
SS
1513}
1514
c906108c 1515static void
981a3fb3 1516set_command (const char *exp, int from_tty)
c906108c 1517{
4d01a485 1518 expression_up expr = parse_expression (exp);
ad3bbd48 1519
d654ad96 1520 switch (expr->first_opcode ())
3dd93bf8
TT
1521 {
1522 case UNOP_PREINCREMENT:
1523 case UNOP_POSTINCREMENT:
1524 case UNOP_PREDECREMENT:
1525 case UNOP_POSTDECREMENT:
1526 case BINOP_ASSIGN:
1527 case BINOP_ASSIGN_MODIFY:
1528 case BINOP_COMMA:
1529 break;
1530 default:
1531 warning
1532 (_("Expression is not an assignment (and might have no effect)"));
1533 }
52b3699b 1534
4d01a485 1535 evaluate_expression (expr.get ());
c906108c
SS
1536}
1537
c906108c 1538static void
1d12d88f 1539info_symbol_command (const char *arg, int from_tty)
c906108c
SS
1540{
1541 struct minimal_symbol *msymbol;
c5aa993b 1542 struct obj_section *osect;
c5aa993b
JM
1543 CORE_ADDR addr, sect_addr;
1544 int matches = 0;
1545 unsigned int offset;
c906108c
SS
1546
1547 if (!arg)
e2e0b3e5 1548 error_no_arg (_("address"));
c906108c
SS
1549
1550 addr = parse_and_eval_address (arg);
2030c079 1551 for (objfile *objfile : current_program_space->objfiles ())
3b9d3ac2 1552 ALL_OBJFILE_OSECTIONS (objfile, osect)
c5aa993b 1553 {
3b9d3ac2
TT
1554 /* Only process each object file once, even if there's a separate
1555 debug file. */
1556 if (objfile->separate_debug_objfile_backlink)
1557 continue;
1558
1559 sect_addr = overlay_mapped_address (addr, osect);
1560
0c1bcd23 1561 if (osect->addr () <= sect_addr && sect_addr < osect->endaddr ()
3b9d3ac2
TT
1562 && (msymbol
1563 = lookup_minimal_symbol_by_pc_section (sect_addr,
1564 osect).minsym))
5178ed48 1565 {
3b9d3ac2
TT
1566 const char *obj_name, *mapped, *sec_name, *msym_name;
1567 const char *loc_string;
1568
1569 matches = 1;
4aeddc50 1570 offset = sect_addr - msymbol->value_address (objfile);
3b9d3ac2
TT
1571 mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
1572 sec_name = osect->the_bfd_section->name;
c9d95fa3 1573 msym_name = msymbol->print_name ();
3b9d3ac2
TT
1574
1575 /* Don't print the offset if it is zero.
1576 We assume there's no need to handle i18n of "sym + offset". */
1577 std::string string_holder;
1578 if (offset)
1579 {
1580 string_holder = string_printf ("%s + %u", msym_name, offset);
1581 loc_string = string_holder.c_str ();
1582 }
c14c28ba 1583 else
3b9d3ac2
TT
1584 loc_string = msym_name;
1585
1586 gdb_assert (osect->objfile && objfile_name (osect->objfile));
1587 obj_name = objfile_name (osect->objfile);
1588
deeafabb 1589 if (current_program_space->multi_objfile_p ())
3b9d3ac2
TT
1590 if (pc_in_unmapped_range (addr, osect))
1591 if (section_is_overlay (osect))
6cb06a8c
TT
1592 gdb_printf (_("%s in load address range of "
1593 "%s overlay section %s of %s\n"),
1594 loc_string, mapped, sec_name, obj_name);
3b9d3ac2 1595 else
6cb06a8c
TT
1596 gdb_printf (_("%s in load address range of "
1597 "section %s of %s\n"),
1598 loc_string, sec_name, obj_name);
3b9d3ac2
TT
1599 else
1600 if (section_is_overlay (osect))
6cb06a8c
TT
1601 gdb_printf (_("%s in %s overlay section %s of %s\n"),
1602 loc_string, mapped, sec_name, obj_name);
3b9d3ac2 1603 else
6cb06a8c
TT
1604 gdb_printf (_("%s in section %s of %s\n"),
1605 loc_string, sec_name, obj_name);
c14c28ba 1606 else
3b9d3ac2
TT
1607 if (pc_in_unmapped_range (addr, osect))
1608 if (section_is_overlay (osect))
6cb06a8c
TT
1609 gdb_printf (_("%s in load address range of %s overlay "
1610 "section %s\n"),
1611 loc_string, mapped, sec_name);
3b9d3ac2 1612 else
6cb06a8c 1613 gdb_printf
3b9d3ac2
TT
1614 (_("%s in load address range of section %s\n"),
1615 loc_string, sec_name);
1616 else
1617 if (section_is_overlay (osect))
6cb06a8c
TT
1618 gdb_printf (_("%s in %s overlay section %s\n"),
1619 loc_string, mapped, sec_name);
3b9d3ac2 1620 else
6cb06a8c
TT
1621 gdb_printf (_("%s in section %s\n"),
1622 loc_string, sec_name);
3b9d3ac2 1623 }
c5aa993b 1624 }
c906108c 1625 if (matches == 0)
6cb06a8c 1626 gdb_printf (_("No symbol matches %s.\n"), arg);
c906108c
SS
1627}
1628
c906108c 1629static void
1d12d88f 1630info_address_command (const char *exp, int from_tty)
c906108c 1631{
768a979c
UW
1632 struct gdbarch *gdbarch;
1633 int regno;
52f0bd74 1634 struct symbol *sym;
7c7b6655 1635 struct bound_minimal_symbol msymbol;
52f0bd74 1636 long val;
714835d5 1637 struct obj_section *section;
08922a10 1638 CORE_ADDR load_addr, context_pc = 0;
1993b719 1639 struct field_of_this_result is_a_field_of_this;
c906108c
SS
1640
1641 if (exp == 0)
8a3fe4f8 1642 error (_("Argument required."));
c906108c 1643
08922a10 1644 sym = lookup_symbol (exp, get_selected_block (&context_pc), VAR_DOMAIN,
d12307c1 1645 &is_a_field_of_this).symbol;
c906108c
SS
1646 if (sym == NULL)
1647 {
1993b719 1648 if (is_a_field_of_this.type != NULL)
c906108c 1649 {
6cb06a8c 1650 gdb_printf ("Symbol \"");
bed009b9
TT
1651 fprintf_symbol (gdb_stdout, exp,
1652 current_language->la_language, DMGL_ANSI);
6cb06a8c 1653 gdb_printf ("\" is a field of the local class variable ");
e2b23ee9 1654 if (current_language->la_language == language_objc)
6cb06a8c 1655 gdb_printf ("`self'\n"); /* ObjC equivalent of "this" */
e2b23ee9 1656 else
6cb06a8c 1657 gdb_printf ("`this'\n");
c906108c
SS
1658 return;
1659 }
1660
7c7b6655 1661 msymbol = lookup_bound_minimal_symbol (exp);
c906108c 1662
7c7b6655 1663 if (msymbol.minsym != NULL)
c906108c 1664 {
7c7b6655 1665 struct objfile *objfile = msymbol.objfile;
e27d198c 1666
08feed99 1667 gdbarch = objfile->arch ();
4aeddc50 1668 load_addr = msymbol.value_address ();
c906108c 1669
6cb06a8c 1670 gdb_printf ("Symbol \"");
bed009b9
TT
1671 fprintf_symbol (gdb_stdout, exp,
1672 current_language->la_language, DMGL_ANSI);
6cb06a8c 1673 gdb_printf ("\" is at ");
35fb8261
TT
1674 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1675 gdb_stdout);
6cb06a8c 1676 gdb_printf (" in a file compiled without debugging");
ebbc3a7d 1677 section = msymbol.minsym->obj_section (objfile);
c906108c
SS
1678 if (section_is_overlay (section))
1679 {
1680 load_addr = overlay_unmapped_address (load_addr, section);
6cb06a8c 1681 gdb_printf (",\n -- loaded at ");
35fb8261
TT
1682 fputs_styled (paddress (gdbarch, load_addr),
1683 address_style.style (),
1684 gdb_stdout);
6cb06a8c
TT
1685 gdb_printf (" in overlay section %s",
1686 section->the_bfd_section->name);
c906108c 1687 }
6cb06a8c 1688 gdb_printf (".\n");
c906108c
SS
1689 }
1690 else
8a3fe4f8 1691 error (_("No symbol \"%s\" in current context."), exp);
c906108c
SS
1692 return;
1693 }
1694
6cb06a8c 1695 gdb_printf ("Symbol \"");
0426ad51 1696 gdb_puts (sym->print_name ());
6cb06a8c 1697 gdb_printf ("\" is ");
4aeddc50 1698 val = sym->value_longest ();
7b3ecc75 1699 if (sym->is_objfile_owned ())
e19b2d94 1700 section = sym->obj_section (sym->objfile ());
1994afbf
DE
1701 else
1702 section = NULL;
bcd6845e 1703 gdbarch = sym->arch ();
c906108c 1704
24d6c2a0
TT
1705 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
1706 {
1707 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, context_pc,
1708 gdb_stdout);
6cb06a8c 1709 gdb_printf (".\n");
24d6c2a0
TT
1710 return;
1711 }
1712
66d7f48f 1713 switch (sym->aclass ())
c906108c
SS
1714 {
1715 case LOC_CONST:
1716 case LOC_CONST_BYTES:
6cb06a8c 1717 gdb_printf ("constant");
c906108c
SS
1718 break;
1719
1720 case LOC_LABEL:
6cb06a8c 1721 gdb_printf ("a label at address ");
4aeddc50 1722 load_addr = sym->value_address ();
35fb8261
TT
1723 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1724 gdb_stdout);
c906108c
SS
1725 if (section_is_overlay (section))
1726 {
1727 load_addr = overlay_unmapped_address (load_addr, section);
6cb06a8c 1728 gdb_printf (",\n -- loaded at ");
35fb8261
TT
1729 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1730 gdb_stdout);
6cb06a8c
TT
1731 gdb_printf (" in overlay section %s",
1732 section->the_bfd_section->name);
c906108c
SS
1733 }
1734 break;
1735
4c2df51b 1736 case LOC_COMPUTED:
557b4d76 1737 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
4c2df51b 1738
c906108c 1739 case LOC_REGISTER:
768a979c
UW
1740 /* GDBARCH is the architecture associated with the objfile the symbol
1741 is defined in; the target architecture may be different, and may
1742 provide additional registers. However, we do not know the target
1743 architecture at this point. We assume the objfile architecture
1744 will contain all the standard registers that occur in debug info
1745 in that objfile. */
1746 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1747
d9743061 1748 if (sym->is_argument ())
6cb06a8c
TT
1749 gdb_printf (_("an argument in register %s"),
1750 gdbarch_register_name (gdbarch, regno));
2a2d4dc3 1751 else
6cb06a8c
TT
1752 gdb_printf (_("a variable in register %s"),
1753 gdbarch_register_name (gdbarch, regno));
c906108c
SS
1754 break;
1755
1756 case LOC_STATIC:
6cb06a8c 1757 gdb_printf (_("static storage at address "));
4aeddc50 1758 load_addr = sym->value_address ();
35fb8261
TT
1759 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1760 gdb_stdout);
c906108c
SS
1761 if (section_is_overlay (section))
1762 {
1763 load_addr = overlay_unmapped_address (load_addr, section);
6cb06a8c 1764 gdb_printf (_(",\n -- loaded at "));
35fb8261
TT
1765 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1766 gdb_stdout);
6cb06a8c
TT
1767 gdb_printf (_(" in overlay section %s"),
1768 section->the_bfd_section->name);
c906108c
SS
1769 }
1770 break;
1771
c906108c 1772 case LOC_REGPARM_ADDR:
768a979c
UW
1773 /* Note comment at LOC_REGISTER. */
1774 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
6cb06a8c
TT
1775 gdb_printf (_("address of an argument in register %s"),
1776 gdbarch_register_name (gdbarch, regno));
c906108c
SS
1777 break;
1778
1779 case LOC_ARG:
6cb06a8c 1780 gdb_printf (_("an argument at offset %ld"), val);
c906108c
SS
1781 break;
1782
c906108c 1783 case LOC_LOCAL:
6cb06a8c 1784 gdb_printf (_("a local variable at frame offset %ld"), val);
c906108c
SS
1785 break;
1786
1787 case LOC_REF_ARG:
6cb06a8c 1788 gdb_printf (_("a reference argument at offset %ld"), val);
c906108c
SS
1789 break;
1790
c906108c 1791 case LOC_TYPEDEF:
6cb06a8c 1792 gdb_printf (_("a typedef"));
c906108c
SS
1793 break;
1794
1795 case LOC_BLOCK:
6cb06a8c 1796 gdb_printf (_("a function at address "));
6395b628 1797 load_addr = sym->value_block ()->entry_pc ();
35fb8261
TT
1798 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1799 gdb_stdout);
c906108c
SS
1800 if (section_is_overlay (section))
1801 {
1802 load_addr = overlay_unmapped_address (load_addr, section);
6cb06a8c 1803 gdb_printf (_(",\n -- loaded at "));
35fb8261
TT
1804 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1805 gdb_stdout);
6cb06a8c
TT
1806 gdb_printf (_(" in overlay section %s"),
1807 section->the_bfd_section->name);
c906108c
SS
1808 }
1809 break;
1810
1811 case LOC_UNRESOLVED:
1812 {
e27d198c 1813 struct bound_minimal_symbol msym;
c906108c 1814
987012b8 1815 msym = lookup_bound_minimal_symbol (sym->linkage_name ());
e27d198c 1816 if (msym.minsym == NULL)
6cb06a8c 1817 gdb_printf ("unresolved");
c906108c
SS
1818 else
1819 {
1db66e34 1820 section = msym.obj_section ();
e0740f77
JK
1821
1822 if (section
1823 && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
5382cfab 1824 {
4aeddc50 1825 load_addr = msym.minsym->value_raw_address ();
6cb06a8c
TT
1826 gdb_printf (_("a thread-local variable at offset %s "
1827 "in the thread-local storage for `%s'"),
1828 paddress (gdbarch, load_addr),
1829 objfile_name (section->objfile));
5382cfab 1830 }
e0740f77 1831 else
c906108c 1832 {
4aeddc50 1833 load_addr = msym.value_address ();
6cb06a8c 1834 gdb_printf (_("static storage at address "));
35fb8261
TT
1835 fputs_styled (paddress (gdbarch, load_addr),
1836 address_style.style (), gdb_stdout);
e0740f77
JK
1837 if (section_is_overlay (section))
1838 {
1839 load_addr = overlay_unmapped_address (load_addr, section);
6cb06a8c 1840 gdb_printf (_(",\n -- loaded at "));
35fb8261
TT
1841 fputs_styled (paddress (gdbarch, load_addr),
1842 address_style.style (),
1843 gdb_stdout);
6cb06a8c
TT
1844 gdb_printf (_(" in overlay section %s"),
1845 section->the_bfd_section->name);
e0740f77 1846 }
c906108c
SS
1847 }
1848 }
1849 }
1850 break;
1851
c906108c 1852 case LOC_OPTIMIZED_OUT:
6cb06a8c 1853 gdb_printf (_("optimized out"));
c906108c 1854 break;
c5aa993b 1855
c906108c 1856 default:
6cb06a8c 1857 gdb_printf (_("of unknown (botched) type"));
c906108c
SS
1858 break;
1859 }
6cb06a8c 1860 gdb_printf (".\n");
c906108c
SS
1861}
1862\f
675dcf4f
MK
1863
1864static void
0b39b52e 1865x_command (const char *exp, int from_tty)
c906108c 1866{
c906108c 1867 struct format_data fmt;
c906108c
SS
1868 struct value *val;
1869
a6bac58e 1870 fmt.format = last_format ? last_format : 'x';
bef382e6 1871 fmt.print_tags = last_print_tags;
c906108c
SS
1872 fmt.size = last_size;
1873 fmt.count = 1;
a6bac58e 1874 fmt.raw = 0;
c906108c 1875
9be2ae8f
TT
1876 /* If there is no expression and no format, use the most recent
1877 count. */
1878 if (exp == nullptr && last_count > 0)
1879 fmt.count = last_count;
1880
c906108c
SS
1881 if (exp && *exp == '/')
1882 {
6f937416
PA
1883 const char *tmp = exp + 1;
1884
1885 fmt = decode_format (&tmp, last_format, last_size);
1886 exp = (char *) tmp;
c906108c
SS
1887 }
1888
9be2ae8f
TT
1889 last_count = fmt.count;
1890
c906108c
SS
1891 /* If we have an expression, evaluate it and use it as the address. */
1892
1893 if (exp != 0 && *exp != 0)
1894 {
4d01a485 1895 expression_up expr = parse_expression (exp);
675dcf4f 1896 /* Cause expression not to be there any more if this command is
dda83cd7
SM
1897 repeated with Newline. But don't clobber a user-defined
1898 command's definition. */
c906108c 1899 if (from_tty)
85c4be7c 1900 set_repeat_arguments ("");
4d01a485 1901 val = evaluate_expression (expr.get ());
d0c97917 1902 if (TYPE_IS_REFERENCE (val->type ()))
e1c34c5d 1903 val = coerce_ref (val);
c906108c 1904 /* In rvalue contexts, such as this, functions are coerced into
dda83cd7 1905 pointers to functions. This makes "x/i main" work. */
d0c97917 1906 if (val->type ()->code () == TYPE_CODE_FUNC
736355f2 1907 && val->lval () == lval_memory)
9feb2d07 1908 next_address = val->address ();
c906108c 1909 else
1aa20aa8 1910 next_address = value_as_address (val);
5d3729b5
UW
1911
1912 next_gdbarch = expr->gdbarch;
c906108c
SS
1913 }
1914
5d3729b5
UW
1915 if (!next_gdbarch)
1916 error_no_arg (_("starting display address"));
1917
1918 do_examine (fmt, next_gdbarch, next_address);
c906108c 1919
675dcf4f 1920 /* If the examine succeeds, we remember its size and format for next
9a22f0d0
PM
1921 time. Set last_size to 'b' for strings. */
1922 if (fmt.format == 's')
1923 last_size = 'b';
1924 else
1925 last_size = fmt.size;
c906108c
SS
1926 last_format = fmt.format;
1927
bef382e6
LM
1928 /* Remember tag-printing setting. */
1929 last_print_tags = fmt.print_tags;
1930
0df8b418 1931 /* Set a couple of internal variables if appropriate. */
9b558729 1932 if (last_examine_value != nullptr)
c906108c
SS
1933 {
1934 /* Make last address examined available to the user as $_. Use
dda83cd7 1935 the correct pointer type. */
4478b372 1936 struct type *pointer_type
f28085df 1937 = lookup_pointer_type (last_examine_value->type ());
c906108c 1938 set_internalvar (lookup_internalvar ("_"),
4478b372
JB
1939 value_from_pointer (pointer_type,
1940 last_examine_address));
c5aa993b 1941
675dcf4f
MK
1942 /* Make contents of last address examined available to the user
1943 as $__. If the last value has not been fetched from memory
1944 then don't fetch it now; instead mark it by voiding the $__
1945 variable. */
f28085df 1946 if (last_examine_value->lazy ())
4fa62494 1947 clear_internalvar (lookup_internalvar ("__"));
c906108c 1948 else
9b558729 1949 set_internalvar (lookup_internalvar ("__"), last_examine_value.get ());
c906108c
SS
1950 }
1951}
037d7135
AB
1952
1953/* Command completion for the 'display' and 'x' commands. */
1954
1955static void
1956display_and_x_command_completer (struct cmd_list_element *ignore,
1957 completion_tracker &tracker,
1958 const char *text, const char * /*word*/)
1959{
1960 if (skip_over_slash_fmt (tracker, &text))
1961 return;
1962
1963 const char *word = advance_to_expression_complete_word_point (tracker, text);
1964 expression_completer (ignore, tracker, text, word);
1965}
1966
c906108c 1967\f
c5aa993b 1968
c906108c
SS
1969/* Add an expression to the auto-display chain.
1970 Specify the expression. */
1971
1972static void
0b39b52e 1973display_command (const char *arg, int from_tty)
c906108c
SS
1974{
1975 struct format_data fmt;
fe978cb0 1976 struct display *newobj;
6f937416 1977 const char *exp = arg;
c906108c 1978
7bd0be3a 1979 if (exp == 0)
c906108c 1980 {
7bd0be3a
AB
1981 do_displays ();
1982 return;
1983 }
c906108c 1984
7bd0be3a
AB
1985 if (*exp == '/')
1986 {
1987 exp++;
1988 fmt = decode_format (&exp, 0, 0);
1989 if (fmt.size && fmt.format == 0)
1990 fmt.format = 'x';
1991 if (fmt.format == 'i' || fmt.format == 's')
1992 fmt.size = 'b';
1993 }
1994 else
1995 {
1996 fmt.format = 0;
1997 fmt.size = 0;
1998 fmt.count = 0;
1999 fmt.raw = 0;
2000 }
c906108c 2001
699bd4cf
TT
2002 innermost_block_tracker tracker;
2003 expression_up expr = parse_expression (exp, &tracker);
c906108c 2004
8be4b118
TT
2005 newobj = new display (exp, std::move (expr), fmt,
2006 current_program_space, tracker.block ());
2007 all_displays.emplace_back (newobj);
c906108c 2008
7bd0be3a
AB
2009 if (from_tty)
2010 do_one_display (newobj);
c906108c 2011
7bd0be3a 2012 dont_repeat ();
c906108c
SS
2013}
2014
675dcf4f
MK
2015/* Clear out the display_chain. Done when new symtabs are loaded,
2016 since this invalidates the types stored in many expressions. */
c906108c
SS
2017
2018void
8be4b118 2019clear_displays ()
c906108c 2020{
8be4b118 2021 all_displays.clear ();
c906108c
SS
2022}
2023
3c3fe74c 2024/* Delete the auto-display DISPLAY. */
c906108c
SS
2025
2026static void
3c3fe74c 2027delete_display (struct display *display)
c906108c 2028{
3c3fe74c 2029 gdb_assert (display != NULL);
c906108c 2030
8be4b118
TT
2031 auto iter = std::find_if (all_displays.begin (),
2032 all_displays.end (),
2033 [=] (const std::unique_ptr<struct display> &item)
2034 {
2035 return item.get () == display;
2036 });
2037 gdb_assert (iter != all_displays.end ());
2038 all_displays.erase (iter);
c906108c
SS
2039}
2040
c9174737
PA
2041/* Call FUNCTION on each of the displays whose numbers are given in
2042 ARGS. DATA is passed unmodified to FUNCTION. */
c906108c
SS
2043
2044static void
77763700 2045map_display_numbers (const char *args,
8be4b118 2046 gdb::function_view<void (struct display *)> function)
c906108c 2047{
c9174737 2048 int num;
c906108c 2049
c9174737
PA
2050 if (args == NULL)
2051 error_no_arg (_("one or more display numbers"));
c906108c 2052
bfd28288 2053 number_or_range_parser parser (args);
c9174737 2054
bfd28288 2055 while (!parser.finished ())
c906108c 2056 {
bfd28288 2057 const char *p = parser.cur_tok ();
c906108c 2058
bfd28288 2059 num = parser.get_number ();
3c3fe74c
PA
2060 if (num == 0)
2061 warning (_("bad display number at or near '%s'"), p);
2062 else
2063 {
8be4b118
TT
2064 auto iter = std::find_if (all_displays.begin (),
2065 all_displays.end (),
2066 [=] (const std::unique_ptr<display> &item)
2067 {
2068 return item->number == num;
2069 });
2070 if (iter == all_displays.end ())
6cb06a8c 2071 gdb_printf (_("No display number %d.\n"), num);
3c3fe74c 2072 else
8be4b118 2073 function (iter->get ());
3c3fe74c 2074 }
c906108c 2075 }
c9174737
PA
2076}
2077
c9174737
PA
2078/* "undisplay" command. */
2079
2080static void
77763700 2081undisplay_command (const char *args, int from_tty)
c9174737 2082{
c9174737
PA
2083 if (args == NULL)
2084 {
2085 if (query (_("Delete all auto-display expressions? ")))
2086 clear_displays ();
2087 dont_repeat ();
2088 return;
2089 }
2090
8be4b118 2091 map_display_numbers (args, delete_display);
c906108c
SS
2092 dont_repeat ();
2093}
2094
2095/* Display a single auto-display.
2096 Do nothing if the display cannot be printed in the current context,
0df8b418 2097 or if the display is disabled. */
c906108c
SS
2098
2099static void
fba45db2 2100do_one_display (struct display *d)
c906108c
SS
2101{
2102 int within_current_scope;
2103
8be4b118 2104 if (!d->enabled_p)
c906108c
SS
2105 return;
2106
704e9165
UW
2107 /* The expression carries the architecture that was used at parse time.
2108 This is a problem if the expression depends on architecture features
2109 (e.g. register numbers), and the current architecture is now different.
2110 For example, a display statement like "display/i $pc" is expected to
2111 display the PC register of the current architecture, not the arch at
2112 the time the display command was given. Therefore, we re-parse the
2113 expression if the current architecture has changed. */
2114 if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
2115 {
4d01a485 2116 d->exp.reset ();
704e9165
UW
2117 d->block = NULL;
2118 }
2119
a3247a22
PP
2120 if (d->exp == NULL)
2121 {
ad3bbd48 2122
a70b8144 2123 try
a3247a22 2124 {
699bd4cf 2125 innermost_block_tracker tracker;
8be4b118 2126 d->exp = parse_expression (d->exp_string.c_str (), &tracker);
699bd4cf 2127 d->block = tracker.block ();
a3247a22 2128 }
b1ffd112 2129 catch (const gdb_exception_error &ex)
a3247a22
PP
2130 {
2131 /* Can't re-parse the expression. Disable this display item. */
8be4b118 2132 d->enabled_p = false;
a3247a22 2133 warning (_("Unable to display \"%s\": %s"),
8be4b118 2134 d->exp_string.c_str (), ex.what ());
a3247a22
PP
2135 return;
2136 }
2137 }
2138
c906108c 2139 if (d->block)
6c95b8df
PA
2140 {
2141 if (d->pspace == current_program_space)
0d191295
TT
2142 within_current_scope = d->block->contains (get_selected_block (0),
2143 true);
6c95b8df
PA
2144 else
2145 within_current_scope = 0;
2146 }
c906108c
SS
2147 else
2148 within_current_scope = 1;
2149 if (!within_current_scope)
2150 return;
2151
b7b633e9
TT
2152 scoped_restore save_display_number
2153 = make_scoped_restore (&current_display_number, d->number);
c906108c
SS
2154
2155 annotate_display_begin ();
6cb06a8c 2156 gdb_printf ("%d", d->number);
c906108c 2157 annotate_display_number_end ();
6cb06a8c 2158 gdb_printf (": ");
c906108c
SS
2159 if (d->format.size)
2160 {
c906108c
SS
2161
2162 annotate_display_format ();
2163
6cb06a8c 2164 gdb_printf ("x/");
c906108c 2165 if (d->format.count != 1)
6cb06a8c
TT
2166 gdb_printf ("%d", d->format.count);
2167 gdb_printf ("%c", d->format.format);
c906108c 2168 if (d->format.format != 'i' && d->format.format != 's')
6cb06a8c
TT
2169 gdb_printf ("%c", d->format.size);
2170 gdb_printf (" ");
c906108c
SS
2171
2172 annotate_display_expression ();
2173
0426ad51 2174 gdb_puts (d->exp_string.c_str ());
c906108c
SS
2175 annotate_display_expression_end ();
2176
6a2eb474 2177 if (d->format.count != 1 || d->format.format == 'i')
6cb06a8c 2178 gdb_printf ("\n");
c906108c 2179 else
6cb06a8c 2180 gdb_printf (" ");
c5aa993b 2181
c906108c
SS
2182 annotate_display_value ();
2183
a70b8144 2184 try
dda83cd7 2185 {
9d8fa392
PA
2186 struct value *val;
2187 CORE_ADDR addr;
2188
4d01a485 2189 val = evaluate_expression (d->exp.get ());
9d8fa392
PA
2190 addr = value_as_address (val);
2191 if (d->format.format == 'i')
2192 addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
2193 do_examine (d->format, d->exp->gdbarch, addr);
2194 }
230d2906 2195 catch (const gdb_exception_error &ex)
492d29ea 2196 {
6cb06a8c
TT
2197 gdb_printf (_("%p[<error: %s>%p]\n"),
2198 metadata_style.style ().ptr (), ex.what (),
2199 nullptr);
492d29ea 2200 }
c906108c
SS
2201 }
2202 else
2203 {
79a45b7d
TT
2204 struct value_print_options opts;
2205
c906108c
SS
2206 annotate_display_format ();
2207
2208 if (d->format.format)
6cb06a8c 2209 gdb_printf ("/%c ", d->format.format);
c906108c
SS
2210
2211 annotate_display_expression ();
2212
0426ad51 2213 gdb_puts (d->exp_string.c_str ());
c906108c
SS
2214 annotate_display_expression_end ();
2215
6cb06a8c 2216 gdb_printf (" = ");
c906108c
SS
2217
2218 annotate_display_expression ();
2219
79a45b7d 2220 get_formatted_print_options (&opts, d->format.format);
a6bac58e 2221 opts.raw = d->format.raw;
9d8fa392 2222
a70b8144 2223 try
dda83cd7 2224 {
9d8fa392
PA
2225 struct value *val;
2226
4d01a485 2227 val = evaluate_expression (d->exp.get ());
9d8fa392
PA
2228 print_formatted (val, d->format.size, &opts, gdb_stdout);
2229 }
230d2906 2230 catch (const gdb_exception_error &ex)
492d29ea 2231 {
7f6aba03
TT
2232 fprintf_styled (gdb_stdout, metadata_style.style (),
2233 _("<error: %s>"), ex.what ());
492d29ea 2234 }
492d29ea 2235
6cb06a8c 2236 gdb_printf ("\n");
c906108c
SS
2237 }
2238
2239 annotate_display_end ();
2240
2241 gdb_flush (gdb_stdout);
c906108c
SS
2242}
2243
2244/* Display all of the values on the auto-display chain which can be
2245 evaluated in the current scope. */
2246
2247void
fba45db2 2248do_displays (void)
c906108c 2249{
8be4b118
TT
2250 for (auto &d : all_displays)
2251 do_one_display (d.get ());
c906108c
SS
2252}
2253
2254/* Delete the auto-display which we were in the process of displaying.
2255 This is done when there is an error or a signal. */
2256
2257void
fba45db2 2258disable_display (int num)
c906108c 2259{
8be4b118 2260 for (auto &d : all_displays)
c906108c
SS
2261 if (d->number == num)
2262 {
8be4b118 2263 d->enabled_p = false;
c906108c
SS
2264 return;
2265 }
6cb06a8c 2266 gdb_printf (_("No display number %d.\n"), num);
c906108c 2267}
c5aa993b 2268
c906108c 2269void
fba45db2 2270disable_current_display (void)
c906108c
SS
2271{
2272 if (current_display_number >= 0)
2273 {
2274 disable_display (current_display_number);
6cb06a8c
TT
2275 gdb_printf (gdb_stderr,
2276 _("Disabling display %d to "
2277 "avoid infinite recursion.\n"),
2278 current_display_number);
c906108c
SS
2279 }
2280 current_display_number = -1;
2281}
2282
2283static void
1d12d88f 2284info_display_command (const char *ignore, int from_tty)
c906108c 2285{
8be4b118 2286 if (all_displays.empty ())
6cb06a8c 2287 gdb_printf (_("There are no auto-display expressions now.\n"));
c906108c 2288 else
6cb06a8c 2289 gdb_printf (_("Auto-display expressions now in effect:\n\
a3f17187 2290Num Enb Expression\n"));
c906108c 2291
8be4b118 2292 for (auto &d : all_displays)
c906108c 2293 {
6cb06a8c 2294 gdb_printf ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
c906108c 2295 if (d->format.size)
6cb06a8c
TT
2296 gdb_printf ("/%d%c%c ", d->format.count, d->format.size,
2297 d->format.format);
c906108c 2298 else if (d->format.format)
6cb06a8c 2299 gdb_printf ("/%c ", d->format.format);
0426ad51 2300 gdb_puts (d->exp_string.c_str ());
0d191295 2301 if (d->block && !d->block->contains (get_selected_block (0), true))
6cb06a8c
TT
2302 gdb_printf (_(" (cannot be evaluated in the current context)"));
2303 gdb_printf ("\n");
c906108c
SS
2304 }
2305}
2306
85102364 2307/* Implementation of both the "disable display" and "enable display"
c9174737
PA
2308 commands. ENABLE decides what to do. */
2309
2310static void
8be4b118 2311enable_disable_display_command (const char *args, int from_tty, bool enable)
c9174737
PA
2312{
2313 if (args == NULL)
c906108c 2314 {
8be4b118 2315 for (auto &d : all_displays)
c9174737
PA
2316 d->enabled_p = enable;
2317 return;
2318 }
c5aa993b 2319
8be4b118
TT
2320 map_display_numbers (args,
2321 [=] (struct display *d)
2322 {
2323 d->enabled_p = enable;
2324 });
c906108c
SS
2325}
2326
c9174737
PA
2327/* The "enable display" command. */
2328
c906108c 2329static void
77763700 2330enable_display_command (const char *args, int from_tty)
c906108c 2331{
8be4b118 2332 enable_disable_display_command (args, from_tty, true);
c9174737 2333}
c5aa993b 2334
c9174737 2335/* The "disable display" command. */
c906108c 2336
c9174737 2337static void
77763700 2338disable_display_command (const char *args, int from_tty)
c9174737 2339{
8be4b118 2340 enable_disable_display_command (args, from_tty, false);
c906108c 2341}
a3247a22 2342
a3247a22
PP
2343/* display_chain items point to blocks and expressions. Some expressions in
2344 turn may point to symbols.
2345 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
2346 obstack_free'd when a shared library is unloaded.
2347 Clear pointers that are about to become dangling.
2348 Both .exp and .block fields will be restored next time we need to display
2349 an item by re-parsing .exp_string field in the new execution context. */
2350
2351static void
63644780 2352clear_dangling_display_expressions (struct objfile *objfile)
a3247a22 2353{
63644780 2354 struct program_space *pspace;
a3247a22 2355
c0201579
JK
2356 /* With no symbol file we cannot have a block or expression from it. */
2357 if (objfile == NULL)
2358 return;
63644780 2359 pspace = objfile->pspace;
c0201579 2360 if (objfile->separate_debug_objfile_backlink)
63644780
NB
2361 {
2362 objfile = objfile->separate_debug_objfile_backlink;
2363 gdb_assert (objfile->pspace == pspace);
2364 }
c0201579 2365
8be4b118 2366 for (auto &d : all_displays)
a3247a22 2367 {
63644780 2368 if (d->pspace != pspace)
c0201579
JK
2369 continue;
2370
d6bc0792
TT
2371 struct objfile *bl_objf = nullptr;
2372 if (d->block != nullptr)
2373 {
46baa3c6 2374 bl_objf = d->block->objfile ();
d6bc0792
TT
2375 if (bl_objf->separate_debug_objfile_backlink != nullptr)
2376 bl_objf = bl_objf->separate_debug_objfile_backlink;
2377 }
2378
2379 if (bl_objf == objfile
aa9bd445 2380 || (d->exp != nullptr && d->exp->uses_objfile (objfile)))
d6bc0792
TT
2381 {
2382 d->exp.reset ();
2383 d->block = NULL;
2384 }
a3247a22
PP
2385 }
2386}
c906108c 2387\f
c5aa993b 2388
675dcf4f 2389/* Print the value in stack frame FRAME of a variable specified by a
aad95b57
TT
2390 struct symbol. NAME is the name to print; if NULL then VAR's print
2391 name will be used. STREAM is the ui_file on which to print the
2392 value. INDENT specifies the number of indent levels to print
8f043999
JK
2393 before printing the variable name.
2394
2395 This function invalidates FRAME. */
c906108c
SS
2396
2397void
aad95b57 2398print_variable_and_value (const char *name, struct symbol *var,
bd2b40ac 2399 frame_info_ptr frame,
aad95b57 2400 struct ui_file *stream, int indent)
c906108c 2401{
c906108c 2402
aad95b57 2403 if (!name)
987012b8 2404 name = var->print_name ();
aad95b57 2405
6cb06a8c
TT
2406 gdb_printf (stream, "%*s%ps = ", 2 * indent, "",
2407 styled_string (variable_name_style.style (), name));
80ae2043 2408
a70b8144 2409 try
0f6a939d
PM
2410 {
2411 struct value *val;
2412 struct value_print_options opts;
aad95b57 2413
63e43d3a
PMR
2414 /* READ_VAR_VALUE needs a block in order to deal with non-local
2415 references (i.e. to handle nested functions). In this context, we
2416 print variables that are local to this frame, so we can avoid passing
2417 a block to it. */
2418 val = read_var_value (var, NULL, frame);
0f6a939d 2419 get_user_print_options (&opts);
dad6b350 2420 opts.deref_ref = true;
644a2e75 2421 common_val_print_checked (val, stream, indent, &opts, current_language);
8f043999
JK
2422
2423 /* common_val_print invalidates FRAME when a pretty printer calls inferior
2424 function. */
2425 frame = NULL;
0f6a939d 2426 }
230d2906 2427 catch (const gdb_exception_error &except)
492d29ea 2428 {
7f6aba03
TT
2429 fprintf_styled (stream, metadata_style.style (),
2430 "<error reading variable %s (%s)>", name,
2431 except.what ());
492d29ea 2432 }
492d29ea 2433
6cb06a8c 2434 gdb_printf (stream, "\n");
c906108c
SS
2435}
2436
c2792f5a
DE
2437/* Subroutine of ui_printf to simplify it.
2438 Print VALUE to STREAM using FORMAT.
1f6f6e21
PW
2439 VALUE is a C-style string either on the target or
2440 in a GDB internal variable. */
c2792f5a
DE
2441
2442static void
2443printf_c_string (struct ui_file *stream, const char *format,
2444 struct value *value)
2445{
1f6f6e21 2446 const gdb_byte *str;
c2792f5a 2447
d0c97917 2448 if (value->type ()->code () != TYPE_CODE_PTR
736355f2 2449 && value->lval () == lval_internalvar
d0c97917 2450 && c_is_string_type_p (value->type ()))
3ae9ce5d 2451 {
d0c97917 2452 size_t len = value->type ()->length ();
c2792f5a 2453
1f6f6e21
PW
2454 /* Copy the internal var value to TEM_STR and append a terminating null
2455 character. This protects against corrupted C-style strings that lack
2456 the terminating null char. It also allows Ada-style strings (not
2457 null terminated) to be printed without problems. */
2458 gdb_byte *tem_str = (gdb_byte *) alloca (len + 1);
c2792f5a 2459
efaf1ae0 2460 memcpy (tem_str, value->contents ().data (), len);
1f6f6e21
PW
2461 tem_str [len] = 0;
2462 str = tem_str;
c2792f5a 2463 }
1f6f6e21
PW
2464 else
2465 {
2466 CORE_ADDR tem = value_as_address (value);;
2467
2468 if (tem == 0)
2469 {
2470 DIAGNOSTIC_PUSH
2471 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2472 gdb_printf (stream, format, "(null)");
1f6f6e21
PW
2473 DIAGNOSTIC_POP
2474 return;
2475 }
2476
2477 /* This is a %s argument. Find the length of the string. */
2478 size_t len;
2479
2480 for (len = 0;; len++)
2481 {
2482 gdb_byte c;
c2792f5a 2483
1f6f6e21
PW
2484 QUIT;
2485 read_memory (tem + len, &c, 1);
2486 if (c == 0)
2487 break;
2488 }
2489
2490 /* Copy the string contents into a string inside GDB. */
2491 gdb_byte *tem_str = (gdb_byte *) alloca (len + 1);
2492
2493 if (len != 0)
2494 read_memory (tem, tem_str, len);
2495 tem_str[len] = 0;
2496 str = tem_str;
2497 }
c2792f5a 2498
af39b1c2
SM
2499 DIAGNOSTIC_PUSH
2500 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2501 gdb_printf (stream, format, (char *) str);
af39b1c2 2502 DIAGNOSTIC_POP
c2792f5a
DE
2503}
2504
2505/* Subroutine of ui_printf to simplify it.
2506 Print VALUE to STREAM using FORMAT.
1f6f6e21
PW
2507 VALUE is a wide C-style string on the target or
2508 in a GDB internal variable. */
c2792f5a
DE
2509
2510static void
2511printf_wide_c_string (struct ui_file *stream, const char *format,
2512 struct value *value)
2513{
1f6f6e21
PW
2514 const gdb_byte *str;
2515 size_t len;
d0c97917 2516 struct gdbarch *gdbarch = value->type ()->arch ();
b858499d 2517 struct type *wctype = lookup_typename (current_language,
c2792f5a 2518 "wchar_t", NULL, 0);
df86565b 2519 int wcwidth = wctype->length ();
c2792f5a 2520
736355f2 2521 if (value->lval () == lval_internalvar
d0c97917 2522 && c_is_string_type_p (value->type ()))
3ae9ce5d 2523 {
efaf1ae0 2524 str = value->contents ().data ();
d0c97917 2525 len = value->type ()->length ();
3ae9ce5d 2526 }
1f6f6e21 2527 else
c2792f5a 2528 {
1f6f6e21 2529 CORE_ADDR tem = value_as_address (value);
c2792f5a 2530
1f6f6e21
PW
2531 if (tem == 0)
2532 {
2533 DIAGNOSTIC_PUSH
2534 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2535 gdb_printf (stream, format, "(null)");
1f6f6e21
PW
2536 DIAGNOSTIC_POP
2537 return;
2538 }
2539
2540 /* This is a %s argument. Find the length of the string. */
2541 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2542 gdb_byte *buf = (gdb_byte *) alloca (wcwidth);
2543
2544 for (len = 0;; len += wcwidth)
2545 {
2546 QUIT;
2547 read_memory (tem + len, buf, wcwidth);
2548 if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
2549 break;
2550 }
2551
2552 /* Copy the string contents into a string inside GDB. */
2553 gdb_byte *tem_str = (gdb_byte *) alloca (len + wcwidth);
2554
2555 if (len != 0)
2556 read_memory (tem, tem_str, len);
2557 memset (&tem_str[len], 0, wcwidth);
2558 str = tem_str;
2559 }
c2792f5a 2560
8268c778 2561 auto_obstack output;
c2792f5a
DE
2562
2563 convert_between_encodings (target_wide_charset (gdbarch),
2564 host_charset (),
1f6f6e21 2565 str, len, wcwidth,
c2792f5a
DE
2566 &output, translit_char);
2567 obstack_grow_str0 (&output, "");
2568
af39b1c2
SM
2569 DIAGNOSTIC_PUSH
2570 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2571 gdb_printf (stream, format, obstack_base (&output));
af39b1c2 2572 DIAGNOSTIC_POP
c2792f5a
DE
2573}
2574
2575/* Subroutine of ui_printf to simplify it.
16e812b2 2576 Print VALUE, a floating point value, to STREAM using FORMAT. */
c2792f5a
DE
2577
2578static void
16e812b2
UW
2579printf_floating (struct ui_file *stream, const char *format,
2580 struct value *value, enum argclass argclass)
c2792f5a 2581{
c2792f5a 2582 /* Parameter data. */
d0c97917 2583 struct type *param_type = value->type ();
8ee511af 2584 struct gdbarch *gdbarch = param_type->arch ();
c2792f5a 2585
16e812b2
UW
2586 /* Determine target type corresponding to the format string. */
2587 struct type *fmt_type;
2588 switch (argclass)
c2792f5a 2589 {
16e812b2
UW
2590 case double_arg:
2591 fmt_type = builtin_type (gdbarch)->builtin_double;
2592 break;
2593 case long_double_arg:
2594 fmt_type = builtin_type (gdbarch)->builtin_long_double;
2595 break;
2596 case dec32float_arg:
2597 fmt_type = builtin_type (gdbarch)->builtin_decfloat;
2598 break;
2599 case dec64float_arg:
2600 fmt_type = builtin_type (gdbarch)->builtin_decdouble;
2601 break;
2602 case dec128float_arg:
2603 fmt_type = builtin_type (gdbarch)->builtin_declong;
2604 break;
2605 default:
2606 gdb_assert_not_reached ("unexpected argument class");
c2792f5a
DE
2607 }
2608
16e812b2
UW
2609 /* To match the traditional GDB behavior, the conversion is
2610 done differently depending on the type of the parameter:
2611
2612 - if the parameter has floating-point type, it's value
2613 is converted to the target type;
2614
2615 - otherwise, if the parameter has a type that is of the
2616 same size as a built-in floating-point type, the value
2617 bytes are interpreted as if they were of that type, and
2618 then converted to the target type (this is not done for
2619 decimal floating-point argument classes);
2620
2621 - otherwise, if the source value has an integer value,
2622 it's value is converted to the target type;
c2792f5a 2623
16e812b2 2624 - otherwise, an error is raised.
c2792f5a 2625
16e812b2
UW
2626 In either case, the result of the conversion is a byte buffer
2627 formatted in the target format for the target type. */
2628
78134374 2629 if (fmt_type->code () == TYPE_CODE_FLT)
16e812b2
UW
2630 {
2631 param_type = float_type_from_length (param_type);
d0c97917 2632 if (param_type != value->type ())
50888e42 2633 value = value_from_contents (param_type,
efaf1ae0 2634 value->contents ().data ());
16e812b2
UW
2635 }
2636
2637 value = value_cast (fmt_type, value);
c2792f5a 2638
3b4b2f16 2639 /* Convert the value to a string and print it. */
f69fdf9b 2640 std::string str
efaf1ae0 2641 = target_float_to_string (value->contents ().data (), fmt_type, format);
0426ad51 2642 gdb_puts (str.c_str (), stream);
c2792f5a
DE
2643}
2644
2645/* Subroutine of ui_printf to simplify it.
2646 Print VALUE, a target pointer, to STREAM using FORMAT. */
2647
2648static void
2649printf_pointer (struct ui_file *stream, const char *format,
2650 struct value *value)
2651{
2652 /* We avoid the host's %p because pointers are too
2653 likely to be the wrong size. The only interesting
2654 modifier for %p is a width; extract that, and then
2655 handle %p as glibc would: %#x or a literal "(nil)". */
2656
2657 const char *p;
2658 char *fmt, *fmt_p;
2659#ifdef PRINTF_HAS_LONG_LONG
2660 long long val = value_as_long (value);
2661#else
2662 long val = value_as_long (value);
2663#endif
2664
224c3ddb 2665 fmt = (char *) alloca (strlen (format) + 5);
c2792f5a
DE
2666
2667 /* Copy up to the leading %. */
2668 p = format;
2669 fmt_p = fmt;
2670 while (*p)
2671 {
2672 int is_percent = (*p == '%');
2673
2674 *fmt_p++ = *p++;
2675 if (is_percent)
2676 {
2677 if (*p == '%')
2678 *fmt_p++ = *p++;
2679 else
2680 break;
2681 }
2682 }
2683
2684 if (val != 0)
2685 *fmt_p++ = '#';
2686
b8c2339b
TT
2687 /* Copy any width or flags. Only the "-" flag is valid for pointers
2688 -- see the format_pieces constructor. */
2689 while (*p == '-' || (*p >= '0' && *p < '9'))
c2792f5a
DE
2690 *fmt_p++ = *p++;
2691
2692 gdb_assert (*p == 'p' && *(p + 1) == '\0');
2693 if (val != 0)
2694 {
2695#ifdef PRINTF_HAS_LONG_LONG
2696 *fmt_p++ = 'l';
2697#endif
2698 *fmt_p++ = 'l';
2699 *fmt_p++ = 'x';
2700 *fmt_p++ = '\0';
af39b1c2
SM
2701 DIAGNOSTIC_PUSH
2702 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2703 gdb_printf (stream, fmt, val);
af39b1c2 2704 DIAGNOSTIC_POP
c2792f5a
DE
2705 }
2706 else
2707 {
2708 *fmt_p++ = 's';
2709 *fmt_p++ = '\0';
af39b1c2
SM
2710 DIAGNOSTIC_PUSH
2711 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2712 gdb_printf (stream, fmt, "(nil)");
af39b1c2 2713 DIAGNOSTIC_POP
c2792f5a
DE
2714 }
2715}
2716
a04b0428
JB
2717/* printf "printf format string" ARG to STREAM. */
2718
2719static void
bbc13ae3 2720ui_printf (const char *arg, struct ui_file *stream)
c906108c 2721{
bbc13ae3 2722 const char *s = arg;
8e481c3b 2723 std::vector<struct value *> val_args;
c906108c
SS
2724
2725 if (s == 0)
e2e0b3e5 2726 error_no_arg (_("format-control string and values to print"));
c906108c 2727
f1735a53 2728 s = skip_spaces (s);
c906108c 2729
675dcf4f 2730 /* A format string should follow, enveloped in double quotes. */
c906108c 2731 if (*s++ != '"')
8a3fe4f8 2732 error (_("Bad format string, missing '\"'."));
c906108c 2733
8e481c3b 2734 format_pieces fpieces (&s);
c906108c 2735
d3ce09f5
SS
2736 if (*s++ != '"')
2737 error (_("Bad format string, non-terminated '\"'."));
2738
f1735a53 2739 s = skip_spaces (s);
c906108c
SS
2740
2741 if (*s != ',' && *s != 0)
8a3fe4f8 2742 error (_("Invalid argument syntax"));
c906108c 2743
c5aa993b
JM
2744 if (*s == ',')
2745 s++;
f1735a53 2746 s = skip_spaces (s);
c906108c 2747
c906108c 2748 {
c906108c 2749 int nargs_wanted;
8e481c3b
TT
2750 int i;
2751 const char *current_substring;
c906108c 2752
c906108c 2753 nargs_wanted = 0;
8e481c3b
TT
2754 for (auto &&piece : fpieces)
2755 if (piece.argclass != literal_piece)
d3ce09f5 2756 ++nargs_wanted;
c906108c
SS
2757
2758 /* Now, parse all arguments and evaluate them.
2759 Store the VALUEs in VAL_ARGS. */
2760
2761 while (*s != '\0')
2762 {
bbc13ae3 2763 const char *s1;
ad3bbd48 2764
a04b0428 2765 s1 = s;
8e481c3b 2766 val_args.push_back (parse_to_comma_and_eval (&s1));
c5aa993b 2767
c906108c
SS
2768 s = s1;
2769 if (*s == ',')
2770 s++;
2771 }
c5aa993b 2772
8e481c3b 2773 if (val_args.size () != nargs_wanted)
8a3fe4f8 2774 error (_("Wrong number of arguments for specified format-string"));
c906108c
SS
2775
2776 /* Now actually print them. */
d3ce09f5 2777 i = 0;
8e481c3b 2778 for (auto &&piece : fpieces)
c906108c 2779 {
8e481c3b
TT
2780 current_substring = piece.string;
2781 switch (piece.argclass)
c906108c
SS
2782 {
2783 case string_arg:
c2792f5a 2784 printf_c_string (stream, current_substring, val_args[i]);
c906108c 2785 break;
6c7a06a3 2786 case wide_string_arg:
c2792f5a 2787 printf_wide_c_string (stream, current_substring, val_args[i]);
6c7a06a3
TT
2788 break;
2789 case wide_char_arg:
2790 {
d0c97917 2791 struct gdbarch *gdbarch = val_args[i]->type ()->arch ();
b858499d 2792 struct type *wctype = lookup_typename (current_language,
e6c014f2 2793 "wchar_t", NULL, 0);
6c7a06a3 2794 struct type *valtype;
6c7a06a3
TT
2795 const gdb_byte *bytes;
2796
d0c97917 2797 valtype = val_args[i]->type ();
df86565b 2798 if (valtype->length () != wctype->length ()
78134374 2799 || valtype->code () != TYPE_CODE_INT)
6c7a06a3
TT
2800 error (_("expected wchar_t argument for %%lc"));
2801
efaf1ae0 2802 bytes = val_args[i]->contents ().data ();
6c7a06a3 2803
8268c778 2804 auto_obstack output;
6c7a06a3 2805
f870a310 2806 convert_between_encodings (target_wide_charset (gdbarch),
6c7a06a3 2807 host_charset (),
df86565b
SM
2808 bytes, valtype->length (),
2809 valtype->length (),
6c7a06a3
TT
2810 &output, translit_char);
2811 obstack_grow_str0 (&output, "");
2812
af39b1c2
SM
2813 DIAGNOSTIC_PUSH
2814 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c
TT
2815 gdb_printf (stream, current_substring,
2816 obstack_base (&output));
af39b1c2 2817 DIAGNOSTIC_POP
6c7a06a3
TT
2818 }
2819 break;
c906108c 2820 case long_long_arg:
74a0d9f6 2821#ifdef PRINTF_HAS_LONG_LONG
c906108c
SS
2822 {
2823 long long val = value_as_long (val_args[i]);
ad3bbd48 2824
af39b1c2
SM
2825 DIAGNOSTIC_PUSH
2826 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2827 gdb_printf (stream, current_substring, val);
af39b1c2 2828 DIAGNOSTIC_POP
c906108c
SS
2829 break;
2830 }
2831#else
8a3fe4f8 2832 error (_("long long not supported in printf"));
c906108c
SS
2833#endif
2834 case int_arg:
2835 {
46e9880c 2836 int val = value_as_long (val_args[i]);
ad3bbd48 2837
af39b1c2
SM
2838 DIAGNOSTIC_PUSH
2839 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2840 gdb_printf (stream, current_substring, val);
af39b1c2 2841 DIAGNOSTIC_POP
46e9880c
DJ
2842 break;
2843 }
2844 case long_arg:
2845 {
c906108c 2846 long val = value_as_long (val_args[i]);
ad3bbd48 2847
e06f3d6e
AB
2848 DIAGNOSTIC_PUSH
2849 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2850 gdb_printf (stream, current_substring, val);
e06f3d6e
AB
2851 DIAGNOSTIC_POP
2852 break;
2853 }
2854 case size_t_arg:
2855 {
2856 size_t val = value_as_long (val_args[i]);
2857
af39b1c2
SM
2858 DIAGNOSTIC_PUSH
2859 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2860 gdb_printf (stream, current_substring, val);
af39b1c2 2861 DIAGNOSTIC_POP
c906108c
SS
2862 break;
2863 }
16e812b2
UW
2864 /* Handles floating-point values. */
2865 case double_arg:
2866 case long_double_arg:
2867 case dec32float_arg:
2868 case dec64float_arg:
2869 case dec128float_arg:
2870 printf_floating (stream, current_substring, val_args[i],
8e481c3b 2871 piece.argclass);
c2792f5a 2872 break;
2025a643 2873 case ptr_arg:
c2792f5a
DE
2874 printf_pointer (stream, current_substring, val_args[i]);
2875 break;
d3ce09f5
SS
2876 case literal_piece:
2877 /* Print a portion of the format string that has no
2878 directives. Note that this will not include any
2879 ordinary %-specs, but it might include "%%". That is
6cb06a8c 2880 why we use gdb_printf and not gdb_puts here.
d3ce09f5
SS
2881 Also, we pass a dummy argument because some platforms
2882 have modified GCC to include -Wformat-security by
2883 default, which will warn here if there is no
2884 argument. */
af39b1c2
SM
2885 DIAGNOSTIC_PUSH
2886 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2887 gdb_printf (stream, current_substring, 0);
af39b1c2 2888 DIAGNOSTIC_POP
d3ce09f5 2889 break;
675dcf4f 2890 default:
f34652de 2891 internal_error (_("failed internal consistency check"));
c906108c 2892 }
d3ce09f5 2893 /* Maybe advance to the next argument. */
8e481c3b 2894 if (piece.argclass != literal_piece)
d3ce09f5 2895 ++i;
c906108c 2896 }
c906108c 2897 }
c906108c 2898}
c906108c 2899
f1421989
HZ
2900/* Implement the "printf" command. */
2901
a04b0428 2902static void
0b39b52e 2903printf_command (const char *arg, int from_tty)
f1421989 2904{
a04b0428 2905 ui_printf (arg, gdb_stdout);
3cd52293 2906 gdb_stdout->reset_style ();
1285ce86 2907 gdb_stdout->wrap_here (0);
da5bd37e 2908 gdb_stdout->flush ();
f1421989
HZ
2909}
2910
2911/* Implement the "eval" command. */
2912
2913static void
0b39b52e 2914eval_command (const char *arg, int from_tty)
f1421989 2915{
d7e74731 2916 string_file stb;
f1421989 2917
d7e74731 2918 ui_printf (arg, &stb);
f1421989 2919
d7e74731 2920 std::string expanded = insert_user_defined_cmd_args (stb.c_str ());
01770bbd 2921
95a6b0a1 2922 execute_command (expanded.c_str (), from_tty);
f1421989
HZ
2923}
2924
48136e00
LM
2925/* Convenience function for error checking in memory-tag commands. */
2926
2927static void
2928show_addr_not_tagged (CORE_ADDR address)
2929{
2930 error (_("Address %s not in a region mapped with a memory tagging flag."),
2931 paddress (target_gdbarch (), address));
2932}
2933
2934/* Convenience function for error checking in memory-tag commands. */
2935
2936static void
2937show_memory_tagging_unsupported (void)
2938{
2939 error (_("Memory tagging not supported or disabled by the current"
2940 " architecture."));
2941}
2942
2943/* Implement the "memory-tag" prefix command. */
2944
2945static void
2946memory_tag_command (const char *arg, int from_tty)
2947{
2948 help_list (memory_tag_list, "memory-tag ", all_commands, gdb_stdout);
2949}
2950
2951/* Helper for print-logical-tag and print-allocation-tag. */
2952
2953static void
2954memory_tag_print_tag_command (const char *args, enum memtag_type tag_type)
2955{
2956 if (args == nullptr)
2957 error_no_arg (_("address or pointer"));
2958
2959 /* Parse args into a value. If the value is a pointer or an address,
2960 then fetch the logical or allocation tag. */
2961 value_print_options print_opts;
2962
2963 struct value *val = process_print_command_args (args, &print_opts, true);
2964
2965 /* If the address is not in a region memory mapped with a memory tagging
2966 flag, it is no use trying to access/manipulate its allocation tag.
2967
2968 It is OK to manipulate the logical tag though. */
2969 if (tag_type == memtag_type::allocation
2970 && !gdbarch_tagged_address_p (target_gdbarch (), val))
2971 show_addr_not_tagged (value_as_address (val));
2972
2973 struct value *tag_value
2974 = gdbarch_get_memtag (target_gdbarch (), val, tag_type);
2975 std::string tag = gdbarch_memtag_to_string (target_gdbarch (), tag_value);
2976
2977 if (tag.empty ())
6cb06a8c
TT
2978 gdb_printf (_("%s tag unavailable.\n"),
2979 tag_type
2980 == memtag_type::logical? "Logical" : "Allocation");
48136e00
LM
2981
2982 struct value *v_tag = process_print_command_args (tag.c_str (),
2983 &print_opts,
2984 true);
2985 print_opts.output_format = 'x';
2986 print_value (v_tag, print_opts);
2987}
2988
2989/* Implement the "memory-tag print-logical-tag" command. */
2990
2991static void
2992memory_tag_print_logical_tag_command (const char *args, int from_tty)
2993{
2994 if (!target_supports_memory_tagging ())
2995 show_memory_tagging_unsupported ();
2996
2997 memory_tag_print_tag_command (args, memtag_type::logical);
2998}
2999
3000/* Implement the "memory-tag print-allocation-tag" command. */
3001
3002static void
3003memory_tag_print_allocation_tag_command (const char *args, int from_tty)
3004{
3005 if (!target_supports_memory_tagging ())
3006 show_memory_tagging_unsupported ();
3007
3008 memory_tag_print_tag_command (args, memtag_type::allocation);
3009}
3010
3011/* Parse ARGS and extract ADDR and TAG.
3012 ARGS should have format <expression> <tag bytes>. */
3013
3014static void
3015parse_with_logical_tag_input (const char *args, struct value **val,
3016 gdb::byte_vector &tags,
3017 value_print_options *print_opts)
3018{
3019 /* Fetch the address. */
3020 std::string address_string = extract_string_maybe_quoted (&args);
3021
3022 /* Parse the address into a value. */
3023 *val = process_print_command_args (address_string.c_str (), print_opts,
3024 true);
3025
3026 /* Fetch the tag bytes. */
3027 std::string tag_string = extract_string_maybe_quoted (&args);
3028
3029 /* Validate the input. */
3030 if (address_string.empty () || tag_string.empty ())
3031 error (_("Missing arguments."));
3032
3033 if (tag_string.length () != 2)
3034 error (_("Error parsing tags argument. The tag should be 2 digits."));
3035
3036 tags = hex2bin (tag_string.c_str ());
3037}
3038
3039/* Implement the "memory-tag with-logical-tag" command. */
3040
3041static void
3042memory_tag_with_logical_tag_command (const char *args, int from_tty)
3043{
3044 if (!target_supports_memory_tagging ())
3045 show_memory_tagging_unsupported ();
3046
3047 if (args == nullptr)
3048 error_no_arg (_("<address> <tag>"));
3049
3050 gdb::byte_vector tags;
3051 struct value *val;
3052 value_print_options print_opts;
3053
3054 /* Parse the input. */
3055 parse_with_logical_tag_input (args, &val, tags, &print_opts);
3056
3057 /* Setting the logical tag is just a local operation that does not touch
3058 any memory from the target. Given an input value, we modify the value
3059 to include the appropriate tag.
3060
3061 For this reason we need to cast the argument value to a
3062 (void *) pointer. This is so we have the right type for the gdbarch
3063 hook to manipulate the value and insert the tag.
3064
3065 Otherwise, this would fail if, for example, GDB parsed the argument value
3066 into an int-sized value and the pointer value has a type of greater
3067 length. */
3068
3069 /* Cast to (void *). */
3070 val = value_cast (builtin_type (target_gdbarch ())->builtin_data_ptr,
3071 val);
3072
3073 /* Length doesn't matter for a logical tag. Pass 0. */
3074 if (!gdbarch_set_memtags (target_gdbarch (), val, 0, tags,
3075 memtag_type::logical))
6cb06a8c 3076 gdb_printf (_("Could not update the logical tag data.\n"));
48136e00
LM
3077 else
3078 {
3079 /* Always print it in hex format. */
3080 print_opts.output_format = 'x';
3081 print_value (val, print_opts);
3082 }
3083}
3084
3085/* Parse ARGS and extract ADDR, LENGTH and TAGS. */
3086
3087static void
3088parse_set_allocation_tag_input (const char *args, struct value **val,
3089 size_t *length, gdb::byte_vector &tags)
3090{
3091 /* Fetch the address. */
3092 std::string address_string = extract_string_maybe_quoted (&args);
3093
3094 /* Parse the address into a value. */
3095 value_print_options print_opts;
3096 *val = process_print_command_args (address_string.c_str (), &print_opts,
3097 true);
3098
3099 /* Fetch the length. */
3100 std::string length_string = extract_string_maybe_quoted (&args);
3101
3102 /* Fetch the tag bytes. */
3103 std::string tags_string = extract_string_maybe_quoted (&args);
3104
3105 /* Validate the input. */
3106 if (address_string.empty () || length_string.empty () || tags_string.empty ())
3107 error (_("Missing arguments."));
3108
3109 errno = 0;
3110 const char *trailer = nullptr;
3111 LONGEST parsed_length = strtoulst (length_string.c_str (), &trailer, 10);
3112
3113 if (errno != 0 || (trailer != nullptr && trailer[0] != '\0'))
3114 error (_("Error parsing length argument."));
3115
3116 if (parsed_length <= 0)
3117 error (_("Invalid zero or negative length."));
3118
3119 *length = parsed_length;
3120
3121 if (tags_string.length () % 2)
3122 error (_("Error parsing tags argument. Tags should be 2 digits per byte."));
3123
3124 tags = hex2bin (tags_string.c_str ());
3125
3126 /* If the address is not in a region memory mapped with a memory tagging
3127 flag, it is no use trying to access/manipulate its allocation tag. */
3128 if (!gdbarch_tagged_address_p (target_gdbarch (), *val))
3129 show_addr_not_tagged (value_as_address (*val));
3130}
3131
3132/* Implement the "memory-tag set-allocation-tag" command.
3133 ARGS should be in the format <address> <length> <tags>. */
3134
3135static void
3136memory_tag_set_allocation_tag_command (const char *args, int from_tty)
3137{
3138 if (!target_supports_memory_tagging ())
3139 show_memory_tagging_unsupported ();
3140
3141 if (args == nullptr)
3142 error_no_arg (_("<starting address> <length> <tag bytes>"));
3143
3144 gdb::byte_vector tags;
3145 size_t length = 0;
3146 struct value *val;
3147
3148 /* Parse the input. */
3149 parse_set_allocation_tag_input (args, &val, &length, tags);
3150
3151 if (!gdbarch_set_memtags (target_gdbarch (), val, length, tags,
3152 memtag_type::allocation))
6cb06a8c 3153 gdb_printf (_("Could not update the allocation tag(s).\n"));
48136e00 3154 else
6cb06a8c 3155 gdb_printf (_("Allocation tag(s) updated successfully.\n"));
48136e00
LM
3156}
3157
3158/* Implement the "memory-tag check" command. */
3159
3160static void
3161memory_tag_check_command (const char *args, int from_tty)
3162{
3163 if (!target_supports_memory_tagging ())
3164 show_memory_tagging_unsupported ();
3165
3166 if (args == nullptr)
3167 error (_("Argument required (address or pointer)"));
3168
3169 /* Parse the expression into a value. If the value is an address or
3170 pointer, then check its logical tag against the allocation tag. */
3171 value_print_options print_opts;
3172
3173 struct value *val = process_print_command_args (args, &print_opts, true);
3174
3175 /* If the address is not in a region memory mapped with a memory tagging
3176 flag, it is no use trying to access/manipulate its allocation tag. */
3177 if (!gdbarch_tagged_address_p (target_gdbarch (), val))
3178 show_addr_not_tagged (value_as_address (val));
3179
3180 CORE_ADDR addr = value_as_address (val);
3181
3182 /* Check if the tag is valid. */
3183 if (!gdbarch_memtag_matches_p (target_gdbarch (), val))
3184 {
3185 struct value *tag
3186 = gdbarch_get_memtag (target_gdbarch (), val, memtag_type::logical);
3187 std::string ltag
3188 = gdbarch_memtag_to_string (target_gdbarch (), tag);
3189
3190 tag = gdbarch_get_memtag (target_gdbarch (), val,
3191 memtag_type::allocation);
3192 std::string atag
3193 = gdbarch_memtag_to_string (target_gdbarch (), tag);
3194
6cb06a8c
TT
3195 gdb_printf (_("Logical tag (%s) does not match"
3196 " the allocation tag (%s) for address %s.\n"),
3197 ltag.c_str (), atag.c_str (),
3198 paddress (target_gdbarch (), addr));
48136e00
LM
3199 }
3200 else
3201 {
3202 struct value *tag
3203 = gdbarch_get_memtag (target_gdbarch (), val, memtag_type::logical);
3204 std::string ltag
3205 = gdbarch_memtag_to_string (target_gdbarch (), tag);
3206
6cb06a8c
TT
3207 gdb_printf (_("Memory tags for address %s match (%s).\n"),
3208 paddress (target_gdbarch (), addr), ltag.c_str ());
48136e00
LM
3209 }
3210}
3211
6c265988 3212void _initialize_printcmd ();
c906108c 3213void
6c265988 3214_initialize_printcmd ()
c906108c 3215{
c94fdfd0
EZ
3216 struct cmd_list_element *c;
3217
c906108c
SS
3218 current_display_number = -1;
3219
c90e7d63
SM
3220 gdb::observers::free_objfile.attach (clear_dangling_display_expressions,
3221 "printcmd");
a3247a22 3222
11db9430 3223 add_info ("address", info_address_command,
188e1fa9
TT
3224 _("Describe where symbol SYM is stored.\n\
3225Usage: info address SYM"));
c906108c 3226
11db9430 3227 add_info ("symbol", info_symbol_command, _("\
1bedd215 3228Describe what symbol is at location ADDR.\n\
188e1fa9 3229Usage: info symbol ADDR\n\
1bedd215 3230Only for symbols with fixed locations (global or static scope)."));
c906108c 3231
037d7135 3232 c = add_com ("x", class_vars, x_command, _("\
1bedd215 3233Examine memory: x/FMT ADDRESS.\n\
c906108c
SS
3234ADDRESS is an expression for the memory address to examine.\n\
3235FMT is a repeat count followed by a format letter and a size letter.\n\
3236Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
6fbe845e
AB
3237 t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
3238 and z(hex, zero padded on the left).\n\
1bedd215 3239Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
c906108c 3240The specified number of objects of the specified size are printed\n\
bb556f1f
TK
3241according to the format. If a negative number is specified, memory is\n\
3242examined backward from the address.\n\n\
c906108c
SS
3243Defaults for format and size letters are those previously used.\n\
3244Default count is 1. Default address is following last thing printed\n\
1bedd215 3245with this command or \"print\"."));
037d7135 3246 set_cmd_completer_handle_brkchars (c, display_and_x_command_completer);
c906108c 3247
11db9430 3248 add_info ("display", info_display_command, _("\
188e1fa9
TT
3249Expressions to display when program stops, with code numbers.\n\
3250Usage: info display"));
c906108c 3251
1a966eab
AC
3252 add_cmd ("undisplay", class_vars, undisplay_command, _("\
3253Cancel some expressions to be displayed when program stops.\n\
188e1fa9 3254Usage: undisplay [NUM]...\n\
c906108c
SS
3255Arguments are the code numbers of the expressions to stop displaying.\n\
3256No argument means cancel all automatic-display expressions.\n\
3257\"delete display\" has the same effect as this command.\n\
1a966eab 3258Do \"info display\" to see current list of code numbers."),
c5aa993b 3259 &cmdlist);
c906108c 3260
037d7135 3261 c = add_com ("display", class_vars, display_command, _("\
1bedd215 3262Print value of expression EXP each time the program stops.\n\
188e1fa9 3263Usage: display[/FMT] EXP\n\
c906108c
SS
3264/FMT may be used before EXP as in the \"print\" command.\n\
3265/FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
3266as in the \"x\" command, and then EXP is used to get the address to examine\n\
3267and examining is done as in the \"x\" command.\n\n\
3268With no argument, display all currently requested auto-display expressions.\n\
1bedd215 3269Use \"undisplay\" to cancel display requests previously made."));
037d7135 3270 set_cmd_completer_handle_brkchars (c, display_and_x_command_completer);
c906108c 3271
c9174737 3272 add_cmd ("display", class_vars, enable_display_command, _("\
1a966eab 3273Enable some expressions to be displayed when program stops.\n\
188e1fa9 3274Usage: enable display [NUM]...\n\
c906108c
SS
3275Arguments are the code numbers of the expressions to resume displaying.\n\
3276No argument means enable all automatic-display expressions.\n\
1a966eab 3277Do \"info display\" to see current list of code numbers."), &enablelist);
c906108c 3278
1a966eab
AC
3279 add_cmd ("display", class_vars, disable_display_command, _("\
3280Disable some expressions to be displayed when program stops.\n\
188e1fa9 3281Usage: disable display [NUM]...\n\
c906108c
SS
3282Arguments are the code numbers of the expressions to stop displaying.\n\
3283No argument means disable all automatic-display expressions.\n\
1a966eab 3284Do \"info display\" to see current list of code numbers."), &disablelist);
c906108c 3285
1a966eab
AC
3286 add_cmd ("display", class_vars, undisplay_command, _("\
3287Cancel some expressions to be displayed when program stops.\n\
188e1fa9 3288Usage: delete display [NUM]...\n\
c906108c
SS
3289Arguments are the code numbers of the expressions to stop displaying.\n\
3290No argument means cancel all automatic-display expressions.\n\
1a966eab 3291Do \"info display\" to see current list of code numbers."), &deletelist);
c906108c 3292
1bedd215 3293 add_com ("printf", class_vars, printf_command, _("\
80ae639d 3294Formatted printing, like the C \"printf\" function.\n\
188e1fa9 3295Usage: printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
80ae639d 3296This supports most C printf format specifications, like %s, %d, etc."));
c906108c 3297
1bedd215
AC
3298 add_com ("output", class_vars, output_command, _("\
3299Like \"print\" but don't put in value history and don't print newline.\n\
188e1fa9 3300Usage: output EXP\n\
1bedd215 3301This is useful in user-defined commands."));
c906108c 3302
1bedd215 3303 add_prefix_cmd ("set", class_vars, set_command, _("\
590042fc 3304Evaluate expression EXP and assign result to variable VAR.\n\
188e1fa9
TT
3305Usage: set VAR = EXP\n\
3306This uses assignment syntax appropriate for the current language\n\
3307(VAR = EXP or VAR := EXP for example).\n\
3308VAR may be a debugger \"convenience\" variable (names starting\n\
c906108c 3309with $), a register (a few standard names starting with $), or an actual\n\
1bedd215
AC
3310variable in the program being debugged. EXP is any valid expression.\n\
3311Use \"set variable\" for variables with names identical to set subcommands.\n\
3312\n\
3313With a subcommand, this command modifies parts of the gdb environment.\n\
3314You can see these environment settings with the \"show\" command."),
2f822da5 3315 &setlist, 1, &cmdlist);
c906108c 3316
0df8b418 3317 /* "call" is the same as "set", but handy for dbx users to call fns. */
1bedd215
AC
3318 c = add_com ("call", class_vars, call_command, _("\
3319Call a function in the program.\n\
188e1fa9 3320Usage: call EXP\n\
c906108c
SS
3321The argument is the function name and arguments, in the notation of the\n\
3322current working language. The result is printed and saved in the value\n\
1bedd215 3323history, if it is not void."));
7d8062de 3324 set_cmd_completer_handle_brkchars (c, print_command_completer);
c906108c 3325
5e84b7ee
SM
3326 cmd_list_element *set_variable_cmd
3327 = add_cmd ("variable", class_vars, set_command, _("\
590042fc 3328Evaluate expression EXP and assign result to variable VAR.\n\
188e1fa9
TT
3329Usage: set variable VAR = EXP\n\
3330This uses assignment syntax appropriate for the current language\n\
3331(VAR = EXP or VAR := EXP for example).\n\
3332VAR may be a debugger \"convenience\" variable (names starting\n\
c906108c
SS
3333with $), a register (a few standard names starting with $), or an actual\n\
3334variable in the program being debugged. EXP is any valid expression.\n\
1a966eab 3335This may usually be abbreviated to simply \"set\"."),
5e84b7ee
SM
3336 &setlist);
3337 add_alias_cmd ("var", set_variable_cmd, class_vars, 0, &setlist);
c906108c 3338
7d8062de
PA
3339 const auto print_opts = make_value_print_options_def_group (nullptr);
3340
8abfcabc 3341 static const std::string print_help = gdb::option::build_help (_("\
1bedd215 3342Print value of expression EXP.\n\
7d8062de
PA
3343Usage: print [[OPTION]... --] [/FMT] [EXP]\n\
3344\n\
3345Options:\n\
590042fc
PW
3346%OPTIONS%\n\
3347\n\
7d8062de
PA
3348Note: because this command accepts arbitrary expressions, if you\n\
3349specify any command option, you must use a double dash (\"--\")\n\
3350to mark the end of option processing. E.g.: \"print -o -- myobj\".\n\
3351\n\
c906108c
SS
3352Variables accessible are those of the lexical environment of the selected\n\
3353stack frame, plus all those whose scope is global or an entire file.\n\
3354\n\
3355$NUM gets previous value number NUM. $ and $$ are the last two values.\n\
3356$$NUM refers to NUM'th value back from the last one.\n\
1bedd215
AC
3357Names starting with $ refer to registers (with the values they would have\n\
3358if the program were to return to the stack frame now selected, restoring\n\
c906108c
SS
3359all registers saved by frames farther in) or else to debugger\n\
3360\"convenience\" variables (any such name not a known register).\n\
1bedd215
AC
3361Use assignment expressions to give values to convenience variables.\n\
3362\n\
c906108c
SS
3363{TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
3364@ is a binary operator for treating consecutive data objects\n\
3365anywhere in memory as an array. FOO@NUM gives an array whose first\n\
3366element is FOO, whose second element is stored in the space following\n\
3367where FOO is stored, etc. FOO must be an expression whose value\n\
1bedd215
AC
3368resides in memory.\n\
3369\n\
c906108c 3370EXP may be preceded with /FMT, where FMT is a format letter\n\
7d8062de
PA
3371but no count or size letter (see \"x\" command)."),
3372 print_opts);
3373
3947f654
SM
3374 cmd_list_element *print_cmd
3375 = add_com ("print", class_vars, print_command, print_help.c_str ());
3376 set_cmd_completer_handle_brkchars (print_cmd, print_command_completer);
3377 add_com_alias ("p", print_cmd, class_vars, 1);
3378 add_com_alias ("inspect", print_cmd, class_vars, 1);
c906108c 3379
35096d9d
AC
3380 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
3381 &max_symbolic_offset, _("\
188e1fa9
TT
3382Set the largest offset that will be printed in <SYMBOL+1234> form."), _("\
3383Show the largest offset that will be printed in <SYMBOL+1234> form."), _("\
f81d1120
PA
3384Tell GDB to only display the symbolic form of an address if the\n\
3385offset between the closest earlier symbol and the address is less than\n\
3386the specified maximum offset. The default is \"unlimited\", which tells GDB\n\
3387to always print the symbolic form of an address if any symbol precedes\n\
3388it. Zero is equivalent to \"unlimited\"."),
35096d9d 3389 NULL,
920d2a44 3390 show_max_symbolic_offset,
35096d9d 3391 &setprintlist, &showprintlist);
5bf193a2
AC
3392 add_setshow_boolean_cmd ("symbol-filename", no_class,
3393 &print_symbol_filename, _("\
188e1fa9
TT
3394Set printing of source filename and line number with <SYMBOL>."), _("\
3395Show printing of source filename and line number with <SYMBOL>."), NULL,
5bf193a2 3396 NULL,
920d2a44 3397 show_print_symbol_filename,
5bf193a2 3398 &setprintlist, &showprintlist);
f1421989
HZ
3399
3400 add_com ("eval", no_class, eval_command, _("\
188e1fa9
TT
3401Construct a GDB command and then evaluate it.\n\
3402Usage: eval \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
3403Convert the arguments to a string as \"printf\" would, but then\n\
3404treat this string as a command line, and evaluate it."));
48136e00
LM
3405
3406 /* Memory tagging commands. */
3407 add_prefix_cmd ("memory-tag", class_vars, memory_tag_command, _("\
3408Generic command for printing and manipulating memory tag properties."),
2f822da5 3409 &memory_tag_list, 0, &cmdlist);
48136e00
LM
3410 add_cmd ("print-logical-tag", class_vars,
3411 memory_tag_print_logical_tag_command,
3412 ("Print the logical tag from POINTER.\n\
3413Usage: memory-tag print-logical-tag <POINTER>.\n\
3414<POINTER> is an expression that evaluates to a pointer.\n\
3415Print the logical tag contained in POINTER. The tag interpretation is\n\
3416architecture-specific."),
3417 &memory_tag_list);
3418 add_cmd ("print-allocation-tag", class_vars,
3419 memory_tag_print_allocation_tag_command,
3420 _("Print the allocation tag for ADDRESS.\n\
3421Usage: memory-tag print-allocation-tag <ADDRESS>.\n\
3422<ADDRESS> is an expression that evaluates to a memory address.\n\
3423Print the allocation tag associated with the memory address ADDRESS.\n\
3424The tag interpretation is architecture-specific."),
3425 &memory_tag_list);
3426 add_cmd ("with-logical-tag", class_vars, memory_tag_with_logical_tag_command,
3427 _("Print a POINTER with a specific logical TAG.\n\
3428Usage: memory-tag with-logical-tag <POINTER> <TAG>\n\
3429<POINTER> is an expression that evaluates to a pointer.\n\
3430<TAG> is a sequence of hex bytes that is interpreted by the architecture\n\
3431as a single memory tag."),
3432 &memory_tag_list);
3433 add_cmd ("set-allocation-tag", class_vars,
3434 memory_tag_set_allocation_tag_command,
3435 _("Set the allocation tag(s) for a memory range.\n\
3436Usage: memory-tag set-allocation-tag <ADDRESS> <LENGTH> <TAG_BYTES>\n\
3437<ADDRESS> is an expression that evaluates to a memory address\n\
3438<LENGTH> is the number of bytes that is added to <ADDRESS> to calculate\n\
3439the memory range.\n\
3440<TAG_BYTES> is a sequence of hex bytes that is interpreted by the\n\
3441architecture as one or more memory tags.\n\
3442Sets the tags of the memory range [ADDRESS, ADDRESS + LENGTH)\n\
3443to TAG_BYTES.\n\
3444\n\
3445If the number of tags is greater than or equal to the number of tag granules\n\
3446in the [ADDRESS, ADDRESS + LENGTH) range, only the tags up to the\n\
3447number of tag granules are updated.\n\
3448\n\
3449If the number of tags is less than the number of tag granules, then the\n\
3450command is a fill operation. The TAG_BYTES are interpreted as a pattern\n\
3451that gets repeated until the number of tag granules in the memory range\n\
3452[ADDRESS, ADDRESS + LENGTH) is updated."),
3453 &memory_tag_list);
3454 add_cmd ("check", class_vars, memory_tag_check_command,
3455 _("Validate a pointer's logical tag against the allocation tag.\n\
3456Usage: memory-tag check <POINTER>\n\
3457<POINTER> is an expression that evaluates to a pointer\n\
3458Fetch the logical and allocation tags for POINTER and compare them\n\
3459for equality. If the tags do not match, print additional information about\n\
3460the tag mismatch."),
3461 &memory_tag_list);
c906108c 3462}