]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/printcmd.c
Unify gdb printf functions
[thirdparty/binutils-gdb.git] / gdb / printcmd.c
CommitLineData
c906108c 1/* Print values for GNU debugger GDB.
e2ad119d 2
4a94e368 3 Copyright (C) 1986-2022 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{
df407dfe 297 struct type *type = check_typedef (value_type (val));
c906108c
SS
298 int len = TYPE_LENGTH (type);
299
300 if (VALUE_LVAL (val) == lval_memory)
42ae5230 301 next_address = value_address (val) + len;
c906108c 302
ea37ba09 303 if (size)
c906108c 304 {
79a45b7d 305 switch (options->format)
ea37ba09
DJ
306 {
307 case 's':
6c7a06a3
TT
308 {
309 struct type *elttype = value_type (val);
ad3bbd48 310
42ae5230 311 next_address = (value_address (val)
09ca9e2e 312 + val_print_string (elttype, NULL,
42ae5230 313 value_address (val), -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);
42ae5230 321 next_address = (value_address (val)
8ee511af 322 + gdb_print_insn (type->arch (),
13274fc3 323 value_address (val), 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
744a8059 352 if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_float))
b806fb9a 353 type = builtin->builtin_float;
744a8059 354 else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_double))
b806fb9a 355 type = builtin->builtin_double;
744a8059 356 else if (TYPE_LENGTH (type) == TYPE_LENGTH (builtin->builtin_long_double))
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 ();
c906108c 372 unsigned int len = TYPE_LENGTH (type);
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
17a912b6 380 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
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 {
d9109c80
TT
390 if (len < TYPE_LENGTH (type) && byte_order == BFD_ENDIAN_BIG)
391 valaddr += TYPE_LENGTH (type) - 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
TT
446 val_long.emplace (unpack_long (type, valaddr));
447 converted_bytes.resize (TYPE_LENGTH (type));
448 store_signed_integer (converted_bytes.data (), TYPE_LENGTH (type),
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
TT
487 case 't':
488 print_binary_chars (stream, valaddr, len, byte_order, size > 0);
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
2b1ffcfd 648 name_location = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (symbol));
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
TT
655 if (msymbol.minsym != NULL
656 && MSYMBOL_HAS_SIZE (msymbol.minsym)
657 && MSYMBOL_SIZE (msymbol.minsym) == 0
658 && MSYMBOL_TYPE (msymbol.minsym) != mst_text
659 && MSYMBOL_TYPE (msymbol.minsym) != mst_text_gnu_ifunc
660 && MSYMBOL_TYPE (msymbol.minsym) != mst_file_text)
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
2dc80cf8
KB
676 && BMSYMBOL_VALUE_ADDRESS (msymbol) == addr
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. */
77e371c0
TT
684 if (MSYMBOL_TYPE (msymbol.minsym) == mst_text
685 || MSYMBOL_TYPE (msymbol.minsym) == mst_text_gnu_ifunc
686 || MSYMBOL_TYPE (msymbol.minsym) == mst_file_text
687 || MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
fe8400b4
WN
688 addr = gdbarch_addr_bits_remove (gdbarch, addr);
689
c906108c 690 symbol = 0;
77e371c0 691 name_location = BMSYMBOL_VALUE_ADDRESS (msymbol);
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 {
752 struct frame_info *frame;
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;
960 /* Searching for '\0' from the end of buffer in backward direction. */
961 for (i = 0; i < chars_read && count > 0 ; ++i, ++chars_counted)
dda83cd7
SM
962 {
963 int offset = (chars_to_read - i - 1) * char_size;
964
965 if (integer_is_zero (&buffer[offset], char_size)
966 || chars_counted == options->print_max)
967 {
968 /* Found '\0' or reached print_max. As OFFSET is the offset to
969 '\0', we add CHAR_SIZE to return the start address of
970 a string. */
971 --count;
972 string_start_addr = addr + offset + char_size;
973 chars_counted = 0;
974 }
975 }
bb556f1f
TK
976 }
977
978 /* Update STRINGS_COUNTED with the actual number of loaded strings. */
979 *strings_counted = count_original - count;
980
981 if (read_error != 0)
982 {
983 /* In error case, STRING_START_ADDR is pointing to the string that
dda83cd7 984 was last successfully loaded. Rewind the partially loaded string. */
bb556f1f
TK
985 string_start_addr -= chars_counted * char_size;
986 }
987
bb556f1f
TK
988 return string_start_addr;
989}
990
c906108c
SS
991/* Examine data at address ADDR in format FMT.
992 Fetch it from memory and print on gdb_stdout. */
993
994static void
5d3729b5 995do_examine (struct format_data fmt, struct gdbarch *gdbarch, CORE_ADDR addr)
c906108c 996{
52f0bd74
AC
997 char format = 0;
998 char size;
999 int count = 1;
c906108c 1000 struct type *val_type = NULL;
52f0bd74
AC
1001 int i;
1002 int maxelts;
79a45b7d 1003 struct value_print_options opts;
bb556f1f
TK
1004 int need_to_update_next_address = 0;
1005 CORE_ADDR addr_rewound = 0;
c906108c
SS
1006
1007 format = fmt.format;
1008 size = fmt.size;
1009 count = fmt.count;
5d3729b5 1010 next_gdbarch = gdbarch;
c906108c 1011 next_address = addr;
c906108c 1012
9a22f0d0
PM
1013 /* Instruction format implies fetch single bytes
1014 regardless of the specified size.
1015 The case of strings is handled in decode_format, only explicit
1016 size operator are not changed to 'b'. */
1017 if (format == 'i')
c906108c
SS
1018 size = 'b';
1019
5d3729b5
UW
1020 if (size == 'a')
1021 {
1022 /* Pick the appropriate size for an address. */
1023 if (gdbarch_ptr_bit (next_gdbarch) == 64)
1024 size = 'g';
1025 else if (gdbarch_ptr_bit (next_gdbarch) == 32)
1026 size = 'w';
1027 else if (gdbarch_ptr_bit (next_gdbarch) == 16)
1028 size = 'h';
1029 else
1030 /* Bad value for gdbarch_ptr_bit. */
1031 internal_error (__FILE__, __LINE__,
1032 _("failed internal consistency check"));
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,
1091 TYPE_LENGTH (val_type),
1092 &opts, &count);
1093 }
bb556f1f 1094 else
dda83cd7
SM
1095 {
1096 next_address = addr - count * TYPE_LENGTH (val_type);
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'
dda83cd7
SM
1103 ? next_address - TYPE_LENGTH (val_type)
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{
1c88ceb1
JK
1245 int histindex = record_latest_value (val);
1246
1247 annotate_value_history_begin (histindex, value_type (val));
1248
6cb06a8c 1249 gdb_printf ("$%d = ", histindex);
1c88ceb1
JK
1250
1251 annotate_value_history_value ();
1252
7d8062de 1253 print_formatted (val, 0, &opts, gdb_stdout);
6cb06a8c 1254 gdb_printf ("\n");
1c88ceb1
JK
1255
1256 annotate_value_history_end ();
1257}
1258
bef382e6
LM
1259/* Returns true if memory tags should be validated. False otherwise. */
1260
1261static bool
1262should_validate_memtags (struct value *value)
1263{
05558223 1264 gdb_assert (value != nullptr && value_type (value) != nullptr);
bef382e6 1265
05558223
LM
1266 if (!target_supports_memory_tagging ())
1267 return false;
bef382e6 1268
05558223
LM
1269 enum type_code code = value_type (value)->code ();
1270
1271 /* Skip non-address values. */
1272 if (code != TYPE_CODE_PTR
1273 && !TYPE_IS_REFERENCE (value_type (value)))
1274 return false;
1275
1276 /* OK, we have an address value. Check we have a complete value we
1277 can extract. */
1278 if (value_optimized_out (value)
1279 || !value_entirely_available (value))
1280 return false;
1281
1282 /* We do. Check whether it includes any tags. */
1283 return gdbarch_tagged_address_p (target_gdbarch (), value);
bef382e6
LM
1284}
1285
48136e00 1286/* Helper for parsing arguments for print_command_1. */
c906108c 1287
48136e00
LM
1288static struct value *
1289process_print_command_args (const char *args, value_print_options *print_opts,
1290 bool voidprint)
c906108c 1291{
48136e00 1292 get_user_print_options (print_opts);
7d8062de 1293 /* Override global settings with explicit options, if any. */
48136e00 1294 auto group = make_value_print_options_def_group (print_opts);
7d8062de
PA
1295 gdb::option::process_options
1296 (&args, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group);
1297
48136e00 1298 print_command_parse_format (&args, "print", print_opts);
c906108c 1299
7d8062de 1300 const char *exp = args;
c906108c 1301
7d8062de 1302 if (exp != nullptr && *exp)
c906108c 1303 {
8fc48b79
TT
1304 /* VOIDPRINT is true to indicate that we do want to print a void
1305 value, so invert it for parse_expression. */
1306 expression_up expr = parse_expression (exp, nullptr, !voidprint);
48136e00 1307 return evaluate_expression (expr.get ());
c906108c 1308 }
48136e00
LM
1309
1310 return access_value_history (0);
1311}
1312
1313/* Implementation of the "print" and "call" commands. */
1314
1315static void
1316print_command_1 (const char *args, int voidprint)
1317{
1318 value_print_options print_opts;
1319
1320 struct value *val = process_print_command_args (args, &print_opts, voidprint);
c906108c 1321
df407dfe 1322 if (voidprint || (val && value_type (val) &&
78134374 1323 value_type (val)->code () != TYPE_CODE_VOID))
bef382e6
LM
1324 {
1325 /* If memory tagging validation is on, check if the tag is valid. */
05558223 1326 if (print_opts.memory_tag_violations)
bef382e6 1327 {
05558223
LM
1328 try
1329 {
1330 if (should_validate_memtags (val)
1331 && !gdbarch_memtag_matches_p (target_gdbarch (), val))
1332 {
1333 /* Fetch the logical tag. */
1334 struct value *tag
1335 = gdbarch_get_memtag (target_gdbarch (), val,
1336 memtag_type::logical);
1337 std::string ltag
1338 = gdbarch_memtag_to_string (target_gdbarch (), tag);
1339
1340 /* Fetch the allocation tag. */
1341 tag = gdbarch_get_memtag (target_gdbarch (), val,
1342 memtag_type::allocation);
1343 std::string atag
1344 = gdbarch_memtag_to_string (target_gdbarch (), tag);
1345
6cb06a8c
TT
1346 gdb_printf (_("Logical tag (%s) does not match the "
1347 "allocation tag (%s).\n"),
1348 ltag.c_str (), atag.c_str ());
05558223
LM
1349 }
1350 }
1351 catch (gdb_exception_error &ex)
1352 {
1353 if (ex.error == TARGET_CLOSE_ERROR)
1354 throw;
1355
6cb06a8c
TT
1356 gdb_printf (gdb_stderr,
1357 _("Could not validate memory tag: %s\n"),
1358 ex.message->c_str ());
05558223 1359 }
bef382e6 1360 }
05558223 1361
bef382e6
LM
1362 print_value (val, print_opts);
1363 }
7d8062de
PA
1364}
1365
037d7135
AB
1366/* Called from command completion function to skip over /FMT
1367 specifications, allowing the rest of the line to be completed. Returns
1368 true if the /FMT is at the end of the current line and there is nothing
1369 left to complete, otherwise false is returned.
1370
1371 In either case *ARGS can be updated to point after any part of /FMT that
1372 is present.
1373
1374 This function is designed so that trying to complete '/' will offer no
1375 completions, the user needs to insert the format specification
1376 themselves. Trying to complete '/FMT' (where FMT is any non-empty set
1377 of alpha-numeric characters) will cause readline to insert a single
1378 space, setting the user up to enter the expression. */
1379
1380static bool
1381skip_over_slash_fmt (completion_tracker &tracker, const char **args)
1382{
1383 const char *text = *args;
1384
1385 if (text[0] == '/')
1386 {
1387 bool in_fmt;
1388 tracker.set_use_custom_word_point (true);
1389
3df8c6af 1390 if (text[1] == '\0')
037d7135 1391 {
3df8c6af
AB
1392 /* The user tried to complete after typing just the '/' character
1393 of the /FMT string. Step the completer past the '/', but we
1394 don't offer any completions. */
1395 in_fmt = true;
1396 ++text;
1397 }
1398 else
1399 {
1400 /* The user has typed some characters after the '/', we assume
1401 this is a complete /FMT string, first skip over it. */
b3ff61f8 1402 text = skip_to_space (text);
037d7135
AB
1403
1404 if (*text == '\0')
1405 {
3df8c6af
AB
1406 /* We're at the end of the input string. The user has typed
1407 '/FMT' and asked for a completion. Push an empty
1408 completion string, this will cause readline to insert a
1409 space so the user now has '/FMT '. */
037d7135
AB
1410 in_fmt = true;
1411 tracker.add_completion (make_unique_xstrdup (text));
1412 }
1413 else
1414 {
3df8c6af
AB
1415 /* The user has already typed things after the /FMT, skip the
1416 whitespace and return false. Whoever called this function
1417 should then try to complete what comes next. */
037d7135 1418 in_fmt = false;
b3ff61f8 1419 text = skip_spaces (text);
037d7135
AB
1420 }
1421 }
037d7135
AB
1422
1423 tracker.advance_custom_word_point_by (text - *args);
1424 *args = text;
1425 return in_fmt;
1426 }
1427
1428 return false;
1429}
1430
7d8062de
PA
1431/* See valprint.h. */
1432
1433void
1434print_command_completer (struct cmd_list_element *ignore,
1435 completion_tracker &tracker,
1436 const char *text, const char * /*word*/)
1437{
1438 const auto group = make_value_print_options_def_group (nullptr);
1439 if (gdb::option::complete_options
1440 (tracker, &text, gdb::option::PROCESS_OPTIONS_REQUIRE_DELIMITER, group))
1441 return;
1442
037d7135
AB
1443 if (skip_over_slash_fmt (tracker, &text))
1444 return;
1445
7d8062de
PA
1446 const char *word = advance_to_expression_complete_word_point (tracker, text);
1447 expression_completer (ignore, tracker, text, word);
c906108c
SS
1448}
1449
c906108c 1450static void
0b39b52e 1451print_command (const char *exp, int from_tty)
c906108c 1452{
8fc48b79 1453 print_command_1 (exp, true);
c906108c
SS
1454}
1455
675dcf4f 1456/* Same as print, except it doesn't print void results. */
c906108c 1457static void
0b39b52e 1458call_command (const char *exp, int from_tty)
c906108c 1459{
8fc48b79 1460 print_command_1 (exp, false);
c906108c
SS
1461}
1462
6f937416
PA
1463/* Implementation of the "output" command. */
1464
6f937416 1465void
122b53ea 1466output_command (const char *exp, int from_tty)
c906108c 1467{
52f0bd74 1468 char format = 0;
3d6d86c6 1469 struct value *val;
c906108c 1470 struct format_data fmt;
79a45b7d 1471 struct value_print_options opts;
c906108c 1472
777ea8f1 1473 fmt.size = 0;
a6bac58e 1474 fmt.raw = 0;
777ea8f1 1475
c906108c
SS
1476 if (exp && *exp == '/')
1477 {
1478 exp++;
1479 fmt = decode_format (&exp, 0, 0);
1480 validate_format (fmt, "output");
1481 format = fmt.format;
1482 }
1483
4d01a485 1484 expression_up expr = parse_expression (exp);
c906108c 1485
4d01a485 1486 val = evaluate_expression (expr.get ());
c906108c 1487
df407dfe 1488 annotate_value_begin (value_type (val));
c906108c 1489
79a45b7d 1490 get_formatted_print_options (&opts, format);
a6bac58e 1491 opts.raw = fmt.raw;
79a45b7d 1492 print_formatted (val, fmt.size, &opts, gdb_stdout);
c906108c
SS
1493
1494 annotate_value_end ();
1495
1285ce86 1496 gdb_stdout->wrap_here (0);
2acceee2 1497 gdb_flush (gdb_stdout);
c906108c
SS
1498}
1499
c906108c 1500static void
981a3fb3 1501set_command (const char *exp, int from_tty)
c906108c 1502{
4d01a485 1503 expression_up expr = parse_expression (exp);
ad3bbd48 1504
3dd93bf8
TT
1505 switch (expr->op->opcode ())
1506 {
1507 case UNOP_PREINCREMENT:
1508 case UNOP_POSTINCREMENT:
1509 case UNOP_PREDECREMENT:
1510 case UNOP_POSTDECREMENT:
1511 case BINOP_ASSIGN:
1512 case BINOP_ASSIGN_MODIFY:
1513 case BINOP_COMMA:
1514 break;
1515 default:
1516 warning
1517 (_("Expression is not an assignment (and might have no effect)"));
1518 }
52b3699b 1519
4d01a485 1520 evaluate_expression (expr.get ());
c906108c
SS
1521}
1522
c906108c 1523static void
1d12d88f 1524info_symbol_command (const char *arg, int from_tty)
c906108c
SS
1525{
1526 struct minimal_symbol *msymbol;
c5aa993b 1527 struct obj_section *osect;
c5aa993b
JM
1528 CORE_ADDR addr, sect_addr;
1529 int matches = 0;
1530 unsigned int offset;
c906108c
SS
1531
1532 if (!arg)
e2e0b3e5 1533 error_no_arg (_("address"));
c906108c
SS
1534
1535 addr = parse_and_eval_address (arg);
2030c079 1536 for (objfile *objfile : current_program_space->objfiles ())
3b9d3ac2 1537 ALL_OBJFILE_OSECTIONS (objfile, osect)
c5aa993b 1538 {
3b9d3ac2
TT
1539 /* Only process each object file once, even if there's a separate
1540 debug file. */
1541 if (objfile->separate_debug_objfile_backlink)
1542 continue;
1543
1544 sect_addr = overlay_mapped_address (addr, osect);
1545
0c1bcd23 1546 if (osect->addr () <= sect_addr && sect_addr < osect->endaddr ()
3b9d3ac2
TT
1547 && (msymbol
1548 = lookup_minimal_symbol_by_pc_section (sect_addr,
1549 osect).minsym))
5178ed48 1550 {
3b9d3ac2
TT
1551 const char *obj_name, *mapped, *sec_name, *msym_name;
1552 const char *loc_string;
1553
1554 matches = 1;
1555 offset = sect_addr - MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
1556 mapped = section_is_mapped (osect) ? _("mapped") : _("unmapped");
1557 sec_name = osect->the_bfd_section->name;
c9d95fa3 1558 msym_name = msymbol->print_name ();
3b9d3ac2
TT
1559
1560 /* Don't print the offset if it is zero.
1561 We assume there's no need to handle i18n of "sym + offset". */
1562 std::string string_holder;
1563 if (offset)
1564 {
1565 string_holder = string_printf ("%s + %u", msym_name, offset);
1566 loc_string = string_holder.c_str ();
1567 }
c14c28ba 1568 else
3b9d3ac2
TT
1569 loc_string = msym_name;
1570
1571 gdb_assert (osect->objfile && objfile_name (osect->objfile));
1572 obj_name = objfile_name (osect->objfile);
1573
deeafabb 1574 if (current_program_space->multi_objfile_p ())
3b9d3ac2
TT
1575 if (pc_in_unmapped_range (addr, osect))
1576 if (section_is_overlay (osect))
6cb06a8c
TT
1577 gdb_printf (_("%s in load address range of "
1578 "%s overlay section %s of %s\n"),
1579 loc_string, mapped, sec_name, obj_name);
3b9d3ac2 1580 else
6cb06a8c
TT
1581 gdb_printf (_("%s in load address range of "
1582 "section %s of %s\n"),
1583 loc_string, sec_name, obj_name);
3b9d3ac2
TT
1584 else
1585 if (section_is_overlay (osect))
6cb06a8c
TT
1586 gdb_printf (_("%s in %s overlay section %s of %s\n"),
1587 loc_string, mapped, sec_name, obj_name);
3b9d3ac2 1588 else
6cb06a8c
TT
1589 gdb_printf (_("%s in section %s of %s\n"),
1590 loc_string, sec_name, obj_name);
c14c28ba 1591 else
3b9d3ac2
TT
1592 if (pc_in_unmapped_range (addr, osect))
1593 if (section_is_overlay (osect))
6cb06a8c
TT
1594 gdb_printf (_("%s in load address range of %s overlay "
1595 "section %s\n"),
1596 loc_string, mapped, sec_name);
3b9d3ac2 1597 else
6cb06a8c 1598 gdb_printf
3b9d3ac2
TT
1599 (_("%s in load address range of section %s\n"),
1600 loc_string, sec_name);
1601 else
1602 if (section_is_overlay (osect))
6cb06a8c
TT
1603 gdb_printf (_("%s in %s overlay section %s\n"),
1604 loc_string, mapped, sec_name);
3b9d3ac2 1605 else
6cb06a8c
TT
1606 gdb_printf (_("%s in section %s\n"),
1607 loc_string, sec_name);
3b9d3ac2 1608 }
c5aa993b 1609 }
c906108c 1610 if (matches == 0)
6cb06a8c 1611 gdb_printf (_("No symbol matches %s.\n"), arg);
c906108c
SS
1612}
1613
c906108c 1614static void
1d12d88f 1615info_address_command (const char *exp, int from_tty)
c906108c 1616{
768a979c
UW
1617 struct gdbarch *gdbarch;
1618 int regno;
52f0bd74 1619 struct symbol *sym;
7c7b6655 1620 struct bound_minimal_symbol msymbol;
52f0bd74 1621 long val;
714835d5 1622 struct obj_section *section;
08922a10 1623 CORE_ADDR load_addr, context_pc = 0;
1993b719 1624 struct field_of_this_result is_a_field_of_this;
c906108c
SS
1625
1626 if (exp == 0)
8a3fe4f8 1627 error (_("Argument required."));
c906108c 1628
08922a10 1629 sym = lookup_symbol (exp, get_selected_block (&context_pc), VAR_DOMAIN,
d12307c1 1630 &is_a_field_of_this).symbol;
c906108c
SS
1631 if (sym == NULL)
1632 {
1993b719 1633 if (is_a_field_of_this.type != NULL)
c906108c 1634 {
6cb06a8c 1635 gdb_printf ("Symbol \"");
c906108c
SS
1636 fprintf_symbol_filtered (gdb_stdout, exp,
1637 current_language->la_language, DMGL_ANSI);
6cb06a8c 1638 gdb_printf ("\" is a field of the local class variable ");
e2b23ee9 1639 if (current_language->la_language == language_objc)
6cb06a8c 1640 gdb_printf ("`self'\n"); /* ObjC equivalent of "this" */
e2b23ee9 1641 else
6cb06a8c 1642 gdb_printf ("`this'\n");
c906108c
SS
1643 return;
1644 }
1645
7c7b6655 1646 msymbol = lookup_bound_minimal_symbol (exp);
c906108c 1647
7c7b6655 1648 if (msymbol.minsym != NULL)
c906108c 1649 {
7c7b6655 1650 struct objfile *objfile = msymbol.objfile;
e27d198c 1651
08feed99 1652 gdbarch = objfile->arch ();
77e371c0 1653 load_addr = BMSYMBOL_VALUE_ADDRESS (msymbol);
c906108c 1654
6cb06a8c 1655 gdb_printf ("Symbol \"");
c906108c
SS
1656 fprintf_symbol_filtered (gdb_stdout, exp,
1657 current_language->la_language, DMGL_ANSI);
6cb06a8c 1658 gdb_printf ("\" is at ");
35fb8261
TT
1659 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1660 gdb_stdout);
6cb06a8c 1661 gdb_printf (" in a file compiled without debugging");
ebbc3a7d 1662 section = msymbol.minsym->obj_section (objfile);
c906108c
SS
1663 if (section_is_overlay (section))
1664 {
1665 load_addr = overlay_unmapped_address (load_addr, section);
6cb06a8c 1666 gdb_printf (",\n -- loaded at ");
35fb8261
TT
1667 fputs_styled (paddress (gdbarch, load_addr),
1668 address_style.style (),
1669 gdb_stdout);
6cb06a8c
TT
1670 gdb_printf (" in overlay section %s",
1671 section->the_bfd_section->name);
c906108c 1672 }
6cb06a8c 1673 gdb_printf (".\n");
c906108c
SS
1674 }
1675 else
8a3fe4f8 1676 error (_("No symbol \"%s\" in current context."), exp);
c906108c
SS
1677 return;
1678 }
1679
6cb06a8c 1680 gdb_printf ("Symbol \"");
0426ad51 1681 gdb_puts (sym->print_name ());
6cb06a8c 1682 gdb_printf ("\" is ");
c5aa993b 1683 val = SYMBOL_VALUE (sym);
7b3ecc75 1684 if (sym->is_objfile_owned ())
ebbc3a7d 1685 section = sym->obj_section (symbol_objfile (sym));
1994afbf
DE
1686 else
1687 section = NULL;
08be3fe3 1688 gdbarch = symbol_arch (sym);
c906108c 1689
24d6c2a0
TT
1690 if (SYMBOL_COMPUTED_OPS (sym) != NULL)
1691 {
1692 SYMBOL_COMPUTED_OPS (sym)->describe_location (sym, context_pc,
1693 gdb_stdout);
6cb06a8c 1694 gdb_printf (".\n");
24d6c2a0
TT
1695 return;
1696 }
1697
66d7f48f 1698 switch (sym->aclass ())
c906108c
SS
1699 {
1700 case LOC_CONST:
1701 case LOC_CONST_BYTES:
6cb06a8c 1702 gdb_printf ("constant");
c906108c
SS
1703 break;
1704
1705 case LOC_LABEL:
6cb06a8c 1706 gdb_printf ("a label at address ");
5af949e3 1707 load_addr = SYMBOL_VALUE_ADDRESS (sym);
35fb8261
TT
1708 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1709 gdb_stdout);
c906108c
SS
1710 if (section_is_overlay (section))
1711 {
1712 load_addr = overlay_unmapped_address (load_addr, section);
6cb06a8c 1713 gdb_printf (",\n -- loaded at ");
35fb8261
TT
1714 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1715 gdb_stdout);
6cb06a8c
TT
1716 gdb_printf (" in overlay section %s",
1717 section->the_bfd_section->name);
c906108c
SS
1718 }
1719 break;
1720
4c2df51b 1721 case LOC_COMPUTED:
557b4d76 1722 gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
4c2df51b 1723
c906108c 1724 case LOC_REGISTER:
768a979c
UW
1725 /* GDBARCH is the architecture associated with the objfile the symbol
1726 is defined in; the target architecture may be different, and may
1727 provide additional registers. However, we do not know the target
1728 architecture at this point. We assume the objfile architecture
1729 will contain all the standard registers that occur in debug info
1730 in that objfile. */
1731 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
1732
d9743061 1733 if (sym->is_argument ())
6cb06a8c
TT
1734 gdb_printf (_("an argument in register %s"),
1735 gdbarch_register_name (gdbarch, regno));
2a2d4dc3 1736 else
6cb06a8c
TT
1737 gdb_printf (_("a variable in register %s"),
1738 gdbarch_register_name (gdbarch, regno));
c906108c
SS
1739 break;
1740
1741 case LOC_STATIC:
6cb06a8c 1742 gdb_printf (_("static storage at address "));
5af949e3 1743 load_addr = SYMBOL_VALUE_ADDRESS (sym);
35fb8261
TT
1744 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1745 gdb_stdout);
c906108c
SS
1746 if (section_is_overlay (section))
1747 {
1748 load_addr = overlay_unmapped_address (load_addr, section);
6cb06a8c 1749 gdb_printf (_(",\n -- loaded at "));
35fb8261
TT
1750 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1751 gdb_stdout);
6cb06a8c
TT
1752 gdb_printf (_(" in overlay section %s"),
1753 section->the_bfd_section->name);
c906108c
SS
1754 }
1755 break;
1756
c906108c 1757 case LOC_REGPARM_ADDR:
768a979c
UW
1758 /* Note comment at LOC_REGISTER. */
1759 regno = SYMBOL_REGISTER_OPS (sym)->register_number (sym, gdbarch);
6cb06a8c
TT
1760 gdb_printf (_("address of an argument in register %s"),
1761 gdbarch_register_name (gdbarch, regno));
c906108c
SS
1762 break;
1763
1764 case LOC_ARG:
6cb06a8c 1765 gdb_printf (_("an argument at offset %ld"), val);
c906108c
SS
1766 break;
1767
c906108c 1768 case LOC_LOCAL:
6cb06a8c 1769 gdb_printf (_("a local variable at frame offset %ld"), val);
c906108c
SS
1770 break;
1771
1772 case LOC_REF_ARG:
6cb06a8c 1773 gdb_printf (_("a reference argument at offset %ld"), val);
c906108c
SS
1774 break;
1775
c906108c 1776 case LOC_TYPEDEF:
6cb06a8c 1777 gdb_printf (_("a typedef"));
c906108c
SS
1778 break;
1779
1780 case LOC_BLOCK:
6cb06a8c 1781 gdb_printf (_("a function at address "));
2b1ffcfd 1782 load_addr = BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym));
35fb8261
TT
1783 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1784 gdb_stdout);
c906108c
SS
1785 if (section_is_overlay (section))
1786 {
1787 load_addr = overlay_unmapped_address (load_addr, section);
6cb06a8c 1788 gdb_printf (_(",\n -- loaded at "));
35fb8261
TT
1789 fputs_styled (paddress (gdbarch, load_addr), address_style.style (),
1790 gdb_stdout);
6cb06a8c
TT
1791 gdb_printf (_(" in overlay section %s"),
1792 section->the_bfd_section->name);
c906108c
SS
1793 }
1794 break;
1795
1796 case LOC_UNRESOLVED:
1797 {
e27d198c 1798 struct bound_minimal_symbol msym;
c906108c 1799
987012b8 1800 msym = lookup_bound_minimal_symbol (sym->linkage_name ());
e27d198c 1801 if (msym.minsym == NULL)
6cb06a8c 1802 gdb_printf ("unresolved");
c906108c
SS
1803 else
1804 {
1db66e34 1805 section = msym.obj_section ();
e0740f77
JK
1806
1807 if (section
1808 && (section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
5382cfab
PW
1809 {
1810 load_addr = MSYMBOL_VALUE_RAW_ADDRESS (msym.minsym);
6cb06a8c
TT
1811 gdb_printf (_("a thread-local variable at offset %s "
1812 "in the thread-local storage for `%s'"),
1813 paddress (gdbarch, load_addr),
1814 objfile_name (section->objfile));
5382cfab 1815 }
e0740f77 1816 else
c906108c 1817 {
5382cfab 1818 load_addr = BMSYMBOL_VALUE_ADDRESS (msym);
6cb06a8c 1819 gdb_printf (_("static storage at address "));
35fb8261
TT
1820 fputs_styled (paddress (gdbarch, load_addr),
1821 address_style.style (), gdb_stdout);
e0740f77
JK
1822 if (section_is_overlay (section))
1823 {
1824 load_addr = overlay_unmapped_address (load_addr, section);
6cb06a8c 1825 gdb_printf (_(",\n -- loaded at "));
35fb8261
TT
1826 fputs_styled (paddress (gdbarch, load_addr),
1827 address_style.style (),
1828 gdb_stdout);
6cb06a8c
TT
1829 gdb_printf (_(" in overlay section %s"),
1830 section->the_bfd_section->name);
e0740f77 1831 }
c906108c
SS
1832 }
1833 }
1834 }
1835 break;
1836
c906108c 1837 case LOC_OPTIMIZED_OUT:
6cb06a8c 1838 gdb_printf (_("optimized out"));
c906108c 1839 break;
c5aa993b 1840
c906108c 1841 default:
6cb06a8c 1842 gdb_printf (_("of unknown (botched) type"));
c906108c
SS
1843 break;
1844 }
6cb06a8c 1845 gdb_printf (".\n");
c906108c
SS
1846}
1847\f
675dcf4f
MK
1848
1849static void
0b39b52e 1850x_command (const char *exp, int from_tty)
c906108c 1851{
c906108c 1852 struct format_data fmt;
c906108c
SS
1853 struct value *val;
1854
a6bac58e 1855 fmt.format = last_format ? last_format : 'x';
bef382e6 1856 fmt.print_tags = last_print_tags;
c906108c
SS
1857 fmt.size = last_size;
1858 fmt.count = 1;
a6bac58e 1859 fmt.raw = 0;
c906108c 1860
9be2ae8f
TT
1861 /* If there is no expression and no format, use the most recent
1862 count. */
1863 if (exp == nullptr && last_count > 0)
1864 fmt.count = last_count;
1865
c906108c
SS
1866 if (exp && *exp == '/')
1867 {
6f937416
PA
1868 const char *tmp = exp + 1;
1869
1870 fmt = decode_format (&tmp, last_format, last_size);
1871 exp = (char *) tmp;
c906108c
SS
1872 }
1873
9be2ae8f
TT
1874 last_count = fmt.count;
1875
c906108c
SS
1876 /* If we have an expression, evaluate it and use it as the address. */
1877
1878 if (exp != 0 && *exp != 0)
1879 {
4d01a485 1880 expression_up expr = parse_expression (exp);
675dcf4f 1881 /* Cause expression not to be there any more if this command is
dda83cd7
SM
1882 repeated with Newline. But don't clobber a user-defined
1883 command's definition. */
c906108c 1884 if (from_tty)
85c4be7c 1885 set_repeat_arguments ("");
4d01a485 1886 val = evaluate_expression (expr.get ());
aa006118 1887 if (TYPE_IS_REFERENCE (value_type (val)))
e1c34c5d 1888 val = coerce_ref (val);
c906108c 1889 /* In rvalue contexts, such as this, functions are coerced into
dda83cd7 1890 pointers to functions. This makes "x/i main" work. */
78134374 1891 if (value_type (val)->code () == TYPE_CODE_FUNC
c5aa993b 1892 && VALUE_LVAL (val) == lval_memory)
42ae5230 1893 next_address = value_address (val);
c906108c 1894 else
1aa20aa8 1895 next_address = value_as_address (val);
5d3729b5
UW
1896
1897 next_gdbarch = expr->gdbarch;
c906108c
SS
1898 }
1899
5d3729b5
UW
1900 if (!next_gdbarch)
1901 error_no_arg (_("starting display address"));
1902
1903 do_examine (fmt, next_gdbarch, next_address);
c906108c 1904
675dcf4f 1905 /* If the examine succeeds, we remember its size and format for next
9a22f0d0
PM
1906 time. Set last_size to 'b' for strings. */
1907 if (fmt.format == 's')
1908 last_size = 'b';
1909 else
1910 last_size = fmt.size;
c906108c
SS
1911 last_format = fmt.format;
1912
bef382e6
LM
1913 /* Remember tag-printing setting. */
1914 last_print_tags = fmt.print_tags;
1915
0df8b418 1916 /* Set a couple of internal variables if appropriate. */
9b558729 1917 if (last_examine_value != nullptr)
c906108c
SS
1918 {
1919 /* Make last address examined available to the user as $_. Use
dda83cd7 1920 the correct pointer type. */
4478b372 1921 struct type *pointer_type
9b558729 1922 = lookup_pointer_type (value_type (last_examine_value.get ()));
c906108c 1923 set_internalvar (lookup_internalvar ("_"),
4478b372
JB
1924 value_from_pointer (pointer_type,
1925 last_examine_address));
c5aa993b 1926
675dcf4f
MK
1927 /* Make contents of last address examined available to the user
1928 as $__. If the last value has not been fetched from memory
1929 then don't fetch it now; instead mark it by voiding the $__
1930 variable. */
9b558729 1931 if (value_lazy (last_examine_value.get ()))
4fa62494 1932 clear_internalvar (lookup_internalvar ("__"));
c906108c 1933 else
9b558729 1934 set_internalvar (lookup_internalvar ("__"), last_examine_value.get ());
c906108c
SS
1935 }
1936}
037d7135
AB
1937
1938/* Command completion for the 'display' and 'x' commands. */
1939
1940static void
1941display_and_x_command_completer (struct cmd_list_element *ignore,
1942 completion_tracker &tracker,
1943 const char *text, const char * /*word*/)
1944{
1945 if (skip_over_slash_fmt (tracker, &text))
1946 return;
1947
1948 const char *word = advance_to_expression_complete_word_point (tracker, text);
1949 expression_completer (ignore, tracker, text, word);
1950}
1951
c906108c 1952\f
c5aa993b 1953
c906108c
SS
1954/* Add an expression to the auto-display chain.
1955 Specify the expression. */
1956
1957static void
0b39b52e 1958display_command (const char *arg, int from_tty)
c906108c
SS
1959{
1960 struct format_data fmt;
fe978cb0 1961 struct display *newobj;
6f937416 1962 const char *exp = arg;
c906108c 1963
7bd0be3a 1964 if (exp == 0)
c906108c 1965 {
7bd0be3a
AB
1966 do_displays ();
1967 return;
1968 }
c906108c 1969
7bd0be3a
AB
1970 if (*exp == '/')
1971 {
1972 exp++;
1973 fmt = decode_format (&exp, 0, 0);
1974 if (fmt.size && fmt.format == 0)
1975 fmt.format = 'x';
1976 if (fmt.format == 'i' || fmt.format == 's')
1977 fmt.size = 'b';
1978 }
1979 else
1980 {
1981 fmt.format = 0;
1982 fmt.size = 0;
1983 fmt.count = 0;
1984 fmt.raw = 0;
1985 }
c906108c 1986
699bd4cf
TT
1987 innermost_block_tracker tracker;
1988 expression_up expr = parse_expression (exp, &tracker);
c906108c 1989
8be4b118
TT
1990 newobj = new display (exp, std::move (expr), fmt,
1991 current_program_space, tracker.block ());
1992 all_displays.emplace_back (newobj);
c906108c 1993
7bd0be3a
AB
1994 if (from_tty)
1995 do_one_display (newobj);
c906108c 1996
7bd0be3a 1997 dont_repeat ();
c906108c
SS
1998}
1999
675dcf4f
MK
2000/* Clear out the display_chain. Done when new symtabs are loaded,
2001 since this invalidates the types stored in many expressions. */
c906108c
SS
2002
2003void
8be4b118 2004clear_displays ()
c906108c 2005{
8be4b118 2006 all_displays.clear ();
c906108c
SS
2007}
2008
3c3fe74c 2009/* Delete the auto-display DISPLAY. */
c906108c
SS
2010
2011static void
3c3fe74c 2012delete_display (struct display *display)
c906108c 2013{
3c3fe74c 2014 gdb_assert (display != NULL);
c906108c 2015
8be4b118
TT
2016 auto iter = std::find_if (all_displays.begin (),
2017 all_displays.end (),
2018 [=] (const std::unique_ptr<struct display> &item)
2019 {
2020 return item.get () == display;
2021 });
2022 gdb_assert (iter != all_displays.end ());
2023 all_displays.erase (iter);
c906108c
SS
2024}
2025
c9174737
PA
2026/* Call FUNCTION on each of the displays whose numbers are given in
2027 ARGS. DATA is passed unmodified to FUNCTION. */
c906108c
SS
2028
2029static void
77763700 2030map_display_numbers (const char *args,
8be4b118 2031 gdb::function_view<void (struct display *)> function)
c906108c 2032{
c9174737 2033 int num;
c906108c 2034
c9174737
PA
2035 if (args == NULL)
2036 error_no_arg (_("one or more display numbers"));
c906108c 2037
bfd28288 2038 number_or_range_parser parser (args);
c9174737 2039
bfd28288 2040 while (!parser.finished ())
c906108c 2041 {
bfd28288 2042 const char *p = parser.cur_tok ();
c906108c 2043
bfd28288 2044 num = parser.get_number ();
3c3fe74c
PA
2045 if (num == 0)
2046 warning (_("bad display number at or near '%s'"), p);
2047 else
2048 {
8be4b118
TT
2049 auto iter = std::find_if (all_displays.begin (),
2050 all_displays.end (),
2051 [=] (const std::unique_ptr<display> &item)
2052 {
2053 return item->number == num;
2054 });
2055 if (iter == all_displays.end ())
6cb06a8c 2056 gdb_printf (_("No display number %d.\n"), num);
3c3fe74c 2057 else
8be4b118 2058 function (iter->get ());
3c3fe74c 2059 }
c906108c 2060 }
c9174737
PA
2061}
2062
c9174737
PA
2063/* "undisplay" command. */
2064
2065static void
77763700 2066undisplay_command (const char *args, int from_tty)
c9174737 2067{
c9174737
PA
2068 if (args == NULL)
2069 {
2070 if (query (_("Delete all auto-display expressions? ")))
2071 clear_displays ();
2072 dont_repeat ();
2073 return;
2074 }
2075
8be4b118 2076 map_display_numbers (args, delete_display);
c906108c
SS
2077 dont_repeat ();
2078}
2079
2080/* Display a single auto-display.
2081 Do nothing if the display cannot be printed in the current context,
0df8b418 2082 or if the display is disabled. */
c906108c
SS
2083
2084static void
fba45db2 2085do_one_display (struct display *d)
c906108c
SS
2086{
2087 int within_current_scope;
2088
8be4b118 2089 if (!d->enabled_p)
c906108c
SS
2090 return;
2091
704e9165
UW
2092 /* The expression carries the architecture that was used at parse time.
2093 This is a problem if the expression depends on architecture features
2094 (e.g. register numbers), and the current architecture is now different.
2095 For example, a display statement like "display/i $pc" is expected to
2096 display the PC register of the current architecture, not the arch at
2097 the time the display command was given. Therefore, we re-parse the
2098 expression if the current architecture has changed. */
2099 if (d->exp != NULL && d->exp->gdbarch != get_current_arch ())
2100 {
4d01a485 2101 d->exp.reset ();
704e9165
UW
2102 d->block = NULL;
2103 }
2104
a3247a22
PP
2105 if (d->exp == NULL)
2106 {
ad3bbd48 2107
a70b8144 2108 try
a3247a22 2109 {
699bd4cf 2110 innermost_block_tracker tracker;
8be4b118 2111 d->exp = parse_expression (d->exp_string.c_str (), &tracker);
699bd4cf 2112 d->block = tracker.block ();
a3247a22 2113 }
230d2906 2114 catch (const gdb_exception &ex)
a3247a22
PP
2115 {
2116 /* Can't re-parse the expression. Disable this display item. */
8be4b118 2117 d->enabled_p = false;
a3247a22 2118 warning (_("Unable to display \"%s\": %s"),
8be4b118 2119 d->exp_string.c_str (), ex.what ());
a3247a22
PP
2120 return;
2121 }
2122 }
2123
c906108c 2124 if (d->block)
6c95b8df
PA
2125 {
2126 if (d->pspace == current_program_space)
f21c2bd7
TT
2127 within_current_scope = contained_in (get_selected_block (0), d->block,
2128 true);
6c95b8df
PA
2129 else
2130 within_current_scope = 0;
2131 }
c906108c
SS
2132 else
2133 within_current_scope = 1;
2134 if (!within_current_scope)
2135 return;
2136
b7b633e9
TT
2137 scoped_restore save_display_number
2138 = make_scoped_restore (&current_display_number, d->number);
c906108c
SS
2139
2140 annotate_display_begin ();
6cb06a8c 2141 gdb_printf ("%d", d->number);
c906108c 2142 annotate_display_number_end ();
6cb06a8c 2143 gdb_printf (": ");
c906108c
SS
2144 if (d->format.size)
2145 {
c906108c
SS
2146
2147 annotate_display_format ();
2148
6cb06a8c 2149 gdb_printf ("x/");
c906108c 2150 if (d->format.count != 1)
6cb06a8c
TT
2151 gdb_printf ("%d", d->format.count);
2152 gdb_printf ("%c", d->format.format);
c906108c 2153 if (d->format.format != 'i' && d->format.format != 's')
6cb06a8c
TT
2154 gdb_printf ("%c", d->format.size);
2155 gdb_printf (" ");
c906108c
SS
2156
2157 annotate_display_expression ();
2158
0426ad51 2159 gdb_puts (d->exp_string.c_str ());
c906108c
SS
2160 annotate_display_expression_end ();
2161
6a2eb474 2162 if (d->format.count != 1 || d->format.format == 'i')
6cb06a8c 2163 gdb_printf ("\n");
c906108c 2164 else
6cb06a8c 2165 gdb_printf (" ");
c5aa993b 2166
c906108c
SS
2167 annotate_display_value ();
2168
a70b8144 2169 try
dda83cd7 2170 {
9d8fa392
PA
2171 struct value *val;
2172 CORE_ADDR addr;
2173
4d01a485 2174 val = evaluate_expression (d->exp.get ());
9d8fa392
PA
2175 addr = value_as_address (val);
2176 if (d->format.format == 'i')
2177 addr = gdbarch_addr_bits_remove (d->exp->gdbarch, addr);
2178 do_examine (d->format, d->exp->gdbarch, addr);
2179 }
230d2906 2180 catch (const gdb_exception_error &ex)
492d29ea 2181 {
6cb06a8c
TT
2182 gdb_printf (_("%p[<error: %s>%p]\n"),
2183 metadata_style.style ().ptr (), ex.what (),
2184 nullptr);
492d29ea 2185 }
c906108c
SS
2186 }
2187 else
2188 {
79a45b7d
TT
2189 struct value_print_options opts;
2190
c906108c
SS
2191 annotate_display_format ();
2192
2193 if (d->format.format)
6cb06a8c 2194 gdb_printf ("/%c ", d->format.format);
c906108c
SS
2195
2196 annotate_display_expression ();
2197
0426ad51 2198 gdb_puts (d->exp_string.c_str ());
c906108c
SS
2199 annotate_display_expression_end ();
2200
6cb06a8c 2201 gdb_printf (" = ");
c906108c
SS
2202
2203 annotate_display_expression ();
2204
79a45b7d 2205 get_formatted_print_options (&opts, d->format.format);
a6bac58e 2206 opts.raw = d->format.raw;
9d8fa392 2207
a70b8144 2208 try
dda83cd7 2209 {
9d8fa392
PA
2210 struct value *val;
2211
4d01a485 2212 val = evaluate_expression (d->exp.get ());
9d8fa392
PA
2213 print_formatted (val, d->format.size, &opts, gdb_stdout);
2214 }
230d2906 2215 catch (const gdb_exception_error &ex)
492d29ea 2216 {
7f6aba03
TT
2217 fprintf_styled (gdb_stdout, metadata_style.style (),
2218 _("<error: %s>"), ex.what ());
492d29ea 2219 }
492d29ea 2220
6cb06a8c 2221 gdb_printf ("\n");
c906108c
SS
2222 }
2223
2224 annotate_display_end ();
2225
2226 gdb_flush (gdb_stdout);
c906108c
SS
2227}
2228
2229/* Display all of the values on the auto-display chain which can be
2230 evaluated in the current scope. */
2231
2232void
fba45db2 2233do_displays (void)
c906108c 2234{
8be4b118
TT
2235 for (auto &d : all_displays)
2236 do_one_display (d.get ());
c906108c
SS
2237}
2238
2239/* Delete the auto-display which we were in the process of displaying.
2240 This is done when there is an error or a signal. */
2241
2242void
fba45db2 2243disable_display (int num)
c906108c 2244{
8be4b118 2245 for (auto &d : all_displays)
c906108c
SS
2246 if (d->number == num)
2247 {
8be4b118 2248 d->enabled_p = false;
c906108c
SS
2249 return;
2250 }
6cb06a8c 2251 gdb_printf (_("No display number %d.\n"), num);
c906108c 2252}
c5aa993b 2253
c906108c 2254void
fba45db2 2255disable_current_display (void)
c906108c
SS
2256{
2257 if (current_display_number >= 0)
2258 {
2259 disable_display (current_display_number);
6cb06a8c
TT
2260 gdb_printf (gdb_stderr,
2261 _("Disabling display %d to "
2262 "avoid infinite recursion.\n"),
2263 current_display_number);
c906108c
SS
2264 }
2265 current_display_number = -1;
2266}
2267
2268static void
1d12d88f 2269info_display_command (const char *ignore, int from_tty)
c906108c 2270{
8be4b118 2271 if (all_displays.empty ())
6cb06a8c 2272 gdb_printf (_("There are no auto-display expressions now.\n"));
c906108c 2273 else
6cb06a8c 2274 gdb_printf (_("Auto-display expressions now in effect:\n\
a3f17187 2275Num Enb Expression\n"));
c906108c 2276
8be4b118 2277 for (auto &d : all_displays)
c906108c 2278 {
6cb06a8c 2279 gdb_printf ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
c906108c 2280 if (d->format.size)
6cb06a8c
TT
2281 gdb_printf ("/%d%c%c ", d->format.count, d->format.size,
2282 d->format.format);
c906108c 2283 else if (d->format.format)
6cb06a8c 2284 gdb_printf ("/%c ", d->format.format);
0426ad51 2285 gdb_puts (d->exp_string.c_str ());
f21c2bd7 2286 if (d->block && !contained_in (get_selected_block (0), d->block, true))
6cb06a8c
TT
2287 gdb_printf (_(" (cannot be evaluated in the current context)"));
2288 gdb_printf ("\n");
c906108c
SS
2289 }
2290}
2291
85102364 2292/* Implementation of both the "disable display" and "enable display"
c9174737
PA
2293 commands. ENABLE decides what to do. */
2294
2295static void
8be4b118 2296enable_disable_display_command (const char *args, int from_tty, bool enable)
c9174737
PA
2297{
2298 if (args == NULL)
c906108c 2299 {
8be4b118 2300 for (auto &d : all_displays)
c9174737
PA
2301 d->enabled_p = enable;
2302 return;
2303 }
c5aa993b 2304
8be4b118
TT
2305 map_display_numbers (args,
2306 [=] (struct display *d)
2307 {
2308 d->enabled_p = enable;
2309 });
c906108c
SS
2310}
2311
c9174737
PA
2312/* The "enable display" command. */
2313
c906108c 2314static void
77763700 2315enable_display_command (const char *args, int from_tty)
c906108c 2316{
8be4b118 2317 enable_disable_display_command (args, from_tty, true);
c9174737 2318}
c5aa993b 2319
c9174737 2320/* The "disable display" command. */
c906108c 2321
c9174737 2322static void
77763700 2323disable_display_command (const char *args, int from_tty)
c9174737 2324{
8be4b118 2325 enable_disable_display_command (args, from_tty, false);
c906108c 2326}
a3247a22 2327
a3247a22
PP
2328/* display_chain items point to blocks and expressions. Some expressions in
2329 turn may point to symbols.
2330 Both symbols and blocks are obstack_alloc'd on objfile_stack, and are
2331 obstack_free'd when a shared library is unloaded.
2332 Clear pointers that are about to become dangling.
2333 Both .exp and .block fields will be restored next time we need to display
2334 an item by re-parsing .exp_string field in the new execution context. */
2335
2336static void
63644780 2337clear_dangling_display_expressions (struct objfile *objfile)
a3247a22 2338{
63644780 2339 struct program_space *pspace;
a3247a22 2340
c0201579
JK
2341 /* With no symbol file we cannot have a block or expression from it. */
2342 if (objfile == NULL)
2343 return;
63644780 2344 pspace = objfile->pspace;
c0201579 2345 if (objfile->separate_debug_objfile_backlink)
63644780
NB
2346 {
2347 objfile = objfile->separate_debug_objfile_backlink;
2348 gdb_assert (objfile->pspace == pspace);
2349 }
c0201579 2350
8be4b118 2351 for (auto &d : all_displays)
a3247a22 2352 {
63644780 2353 if (d->pspace != pspace)
c0201579
JK
2354 continue;
2355
d6bc0792
TT
2356 struct objfile *bl_objf = nullptr;
2357 if (d->block != nullptr)
2358 {
2359 bl_objf = block_objfile (d->block);
2360 if (bl_objf->separate_debug_objfile_backlink != nullptr)
2361 bl_objf = bl_objf->separate_debug_objfile_backlink;
2362 }
2363
2364 if (bl_objf == objfile
4d01a485 2365 || (d->exp != NULL && exp_uses_objfile (d->exp.get (), objfile)))
d6bc0792
TT
2366 {
2367 d->exp.reset ();
2368 d->block = NULL;
2369 }
a3247a22
PP
2370 }
2371}
c906108c 2372\f
c5aa993b 2373
675dcf4f 2374/* Print the value in stack frame FRAME of a variable specified by a
aad95b57
TT
2375 struct symbol. NAME is the name to print; if NULL then VAR's print
2376 name will be used. STREAM is the ui_file on which to print the
2377 value. INDENT specifies the number of indent levels to print
8f043999
JK
2378 before printing the variable name.
2379
2380 This function invalidates FRAME. */
c906108c
SS
2381
2382void
aad95b57
TT
2383print_variable_and_value (const char *name, struct symbol *var,
2384 struct frame_info *frame,
2385 struct ui_file *stream, int indent)
c906108c 2386{
c906108c 2387
aad95b57 2388 if (!name)
987012b8 2389 name = var->print_name ();
aad95b57 2390
6cb06a8c
TT
2391 gdb_printf (stream, "%*s%ps = ", 2 * indent, "",
2392 styled_string (variable_name_style.style (), name));
80ae2043 2393
a70b8144 2394 try
0f6a939d
PM
2395 {
2396 struct value *val;
2397 struct value_print_options opts;
aad95b57 2398
63e43d3a
PMR
2399 /* READ_VAR_VALUE needs a block in order to deal with non-local
2400 references (i.e. to handle nested functions). In this context, we
2401 print variables that are local to this frame, so we can avoid passing
2402 a block to it. */
2403 val = read_var_value (var, NULL, frame);
0f6a939d 2404 get_user_print_options (&opts);
3343315b 2405 opts.deref_ref = 1;
644a2e75 2406 common_val_print_checked (val, stream, indent, &opts, current_language);
8f043999
JK
2407
2408 /* common_val_print invalidates FRAME when a pretty printer calls inferior
2409 function. */
2410 frame = NULL;
0f6a939d 2411 }
230d2906 2412 catch (const gdb_exception_error &except)
492d29ea 2413 {
7f6aba03
TT
2414 fprintf_styled (stream, metadata_style.style (),
2415 "<error reading variable %s (%s)>", name,
2416 except.what ());
492d29ea 2417 }
492d29ea 2418
6cb06a8c 2419 gdb_printf (stream, "\n");
c906108c
SS
2420}
2421
c2792f5a
DE
2422/* Subroutine of ui_printf to simplify it.
2423 Print VALUE to STREAM using FORMAT.
1f6f6e21
PW
2424 VALUE is a C-style string either on the target or
2425 in a GDB internal variable. */
c2792f5a
DE
2426
2427static void
2428printf_c_string (struct ui_file *stream, const char *format,
2429 struct value *value)
2430{
1f6f6e21 2431 const gdb_byte *str;
c2792f5a 2432
78134374 2433 if (value_type (value)->code () != TYPE_CODE_PTR
7b973adc 2434 && VALUE_LVAL (value) == lval_internalvar
1f6f6e21 2435 && c_is_string_type_p (value_type (value)))
3ae9ce5d 2436 {
1f6f6e21 2437 size_t len = TYPE_LENGTH (value_type (value));
c2792f5a 2438
1f6f6e21
PW
2439 /* Copy the internal var value to TEM_STR and append a terminating null
2440 character. This protects against corrupted C-style strings that lack
2441 the terminating null char. It also allows Ada-style strings (not
2442 null terminated) to be printed without problems. */
2443 gdb_byte *tem_str = (gdb_byte *) alloca (len + 1);
c2792f5a 2444
50888e42 2445 memcpy (tem_str, value_contents (value).data (), len);
1f6f6e21
PW
2446 tem_str [len] = 0;
2447 str = tem_str;
c2792f5a 2448 }
1f6f6e21
PW
2449 else
2450 {
2451 CORE_ADDR tem = value_as_address (value);;
2452
2453 if (tem == 0)
2454 {
2455 DIAGNOSTIC_PUSH
2456 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2457 gdb_printf (stream, format, "(null)");
1f6f6e21
PW
2458 DIAGNOSTIC_POP
2459 return;
2460 }
2461
2462 /* This is a %s argument. Find the length of the string. */
2463 size_t len;
2464
2465 for (len = 0;; len++)
2466 {
2467 gdb_byte c;
c2792f5a 2468
1f6f6e21
PW
2469 QUIT;
2470 read_memory (tem + len, &c, 1);
2471 if (c == 0)
2472 break;
2473 }
2474
2475 /* Copy the string contents into a string inside GDB. */
2476 gdb_byte *tem_str = (gdb_byte *) alloca (len + 1);
2477
2478 if (len != 0)
2479 read_memory (tem, tem_str, len);
2480 tem_str[len] = 0;
2481 str = tem_str;
2482 }
c2792f5a 2483
af39b1c2
SM
2484 DIAGNOSTIC_PUSH
2485 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2486 gdb_printf (stream, format, (char *) str);
af39b1c2 2487 DIAGNOSTIC_POP
c2792f5a
DE
2488}
2489
2490/* Subroutine of ui_printf to simplify it.
2491 Print VALUE to STREAM using FORMAT.
1f6f6e21
PW
2492 VALUE is a wide C-style string on the target or
2493 in a GDB internal variable. */
c2792f5a
DE
2494
2495static void
2496printf_wide_c_string (struct ui_file *stream, const char *format,
2497 struct value *value)
2498{
1f6f6e21
PW
2499 const gdb_byte *str;
2500 size_t len;
8ee511af 2501 struct gdbarch *gdbarch = value_type (value)->arch ();
b858499d 2502 struct type *wctype = lookup_typename (current_language,
c2792f5a
DE
2503 "wchar_t", NULL, 0);
2504 int wcwidth = TYPE_LENGTH (wctype);
c2792f5a 2505
1f6f6e21
PW
2506 if (VALUE_LVAL (value) == lval_internalvar
2507 && c_is_string_type_p (value_type (value)))
3ae9ce5d 2508 {
50888e42 2509 str = value_contents (value).data ();
1f6f6e21 2510 len = TYPE_LENGTH (value_type (value));
3ae9ce5d 2511 }
1f6f6e21 2512 else
c2792f5a 2513 {
1f6f6e21 2514 CORE_ADDR tem = value_as_address (value);
c2792f5a 2515
1f6f6e21
PW
2516 if (tem == 0)
2517 {
2518 DIAGNOSTIC_PUSH
2519 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2520 gdb_printf (stream, format, "(null)");
1f6f6e21
PW
2521 DIAGNOSTIC_POP
2522 return;
2523 }
2524
2525 /* This is a %s argument. Find the length of the string. */
2526 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2527 gdb_byte *buf = (gdb_byte *) alloca (wcwidth);
2528
2529 for (len = 0;; len += wcwidth)
2530 {
2531 QUIT;
2532 read_memory (tem + len, buf, wcwidth);
2533 if (extract_unsigned_integer (buf, wcwidth, byte_order) == 0)
2534 break;
2535 }
2536
2537 /* Copy the string contents into a string inside GDB. */
2538 gdb_byte *tem_str = (gdb_byte *) alloca (len + wcwidth);
2539
2540 if (len != 0)
2541 read_memory (tem, tem_str, len);
2542 memset (&tem_str[len], 0, wcwidth);
2543 str = tem_str;
2544 }
c2792f5a 2545
8268c778 2546 auto_obstack output;
c2792f5a
DE
2547
2548 convert_between_encodings (target_wide_charset (gdbarch),
2549 host_charset (),
1f6f6e21 2550 str, len, wcwidth,
c2792f5a
DE
2551 &output, translit_char);
2552 obstack_grow_str0 (&output, "");
2553
af39b1c2
SM
2554 DIAGNOSTIC_PUSH
2555 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2556 gdb_printf (stream, format, obstack_base (&output));
af39b1c2 2557 DIAGNOSTIC_POP
c2792f5a
DE
2558}
2559
2560/* Subroutine of ui_printf to simplify it.
16e812b2 2561 Print VALUE, a floating point value, to STREAM using FORMAT. */
c2792f5a
DE
2562
2563static void
16e812b2
UW
2564printf_floating (struct ui_file *stream, const char *format,
2565 struct value *value, enum argclass argclass)
c2792f5a 2566{
c2792f5a
DE
2567 /* Parameter data. */
2568 struct type *param_type = value_type (value);
8ee511af 2569 struct gdbarch *gdbarch = param_type->arch ();
c2792f5a 2570
16e812b2
UW
2571 /* Determine target type corresponding to the format string. */
2572 struct type *fmt_type;
2573 switch (argclass)
c2792f5a 2574 {
16e812b2
UW
2575 case double_arg:
2576 fmt_type = builtin_type (gdbarch)->builtin_double;
2577 break;
2578 case long_double_arg:
2579 fmt_type = builtin_type (gdbarch)->builtin_long_double;
2580 break;
2581 case dec32float_arg:
2582 fmt_type = builtin_type (gdbarch)->builtin_decfloat;
2583 break;
2584 case dec64float_arg:
2585 fmt_type = builtin_type (gdbarch)->builtin_decdouble;
2586 break;
2587 case dec128float_arg:
2588 fmt_type = builtin_type (gdbarch)->builtin_declong;
2589 break;
2590 default:
2591 gdb_assert_not_reached ("unexpected argument class");
c2792f5a
DE
2592 }
2593
16e812b2
UW
2594 /* To match the traditional GDB behavior, the conversion is
2595 done differently depending on the type of the parameter:
2596
2597 - if the parameter has floating-point type, it's value
2598 is converted to the target type;
2599
2600 - otherwise, if the parameter has a type that is of the
2601 same size as a built-in floating-point type, the value
2602 bytes are interpreted as if they were of that type, and
2603 then converted to the target type (this is not done for
2604 decimal floating-point argument classes);
2605
2606 - otherwise, if the source value has an integer value,
2607 it's value is converted to the target type;
c2792f5a 2608
16e812b2 2609 - otherwise, an error is raised.
c2792f5a 2610
16e812b2
UW
2611 In either case, the result of the conversion is a byte buffer
2612 formatted in the target format for the target type. */
2613
78134374 2614 if (fmt_type->code () == TYPE_CODE_FLT)
16e812b2
UW
2615 {
2616 param_type = float_type_from_length (param_type);
2617 if (param_type != value_type (value))
50888e42
SM
2618 value = value_from_contents (param_type,
2619 value_contents (value).data ());
16e812b2
UW
2620 }
2621
2622 value = value_cast (fmt_type, value);
c2792f5a 2623
3b4b2f16 2624 /* Convert the value to a string and print it. */
f69fdf9b 2625 std::string str
50888e42 2626 = target_float_to_string (value_contents (value).data (), fmt_type, format);
0426ad51 2627 gdb_puts (str.c_str (), stream);
c2792f5a
DE
2628}
2629
2630/* Subroutine of ui_printf to simplify it.
2631 Print VALUE, a target pointer, to STREAM using FORMAT. */
2632
2633static void
2634printf_pointer (struct ui_file *stream, const char *format,
2635 struct value *value)
2636{
2637 /* We avoid the host's %p because pointers are too
2638 likely to be the wrong size. The only interesting
2639 modifier for %p is a width; extract that, and then
2640 handle %p as glibc would: %#x or a literal "(nil)". */
2641
2642 const char *p;
2643 char *fmt, *fmt_p;
2644#ifdef PRINTF_HAS_LONG_LONG
2645 long long val = value_as_long (value);
2646#else
2647 long val = value_as_long (value);
2648#endif
2649
224c3ddb 2650 fmt = (char *) alloca (strlen (format) + 5);
c2792f5a
DE
2651
2652 /* Copy up to the leading %. */
2653 p = format;
2654 fmt_p = fmt;
2655 while (*p)
2656 {
2657 int is_percent = (*p == '%');
2658
2659 *fmt_p++ = *p++;
2660 if (is_percent)
2661 {
2662 if (*p == '%')
2663 *fmt_p++ = *p++;
2664 else
2665 break;
2666 }
2667 }
2668
2669 if (val != 0)
2670 *fmt_p++ = '#';
2671
b8c2339b
TT
2672 /* Copy any width or flags. Only the "-" flag is valid for pointers
2673 -- see the format_pieces constructor. */
2674 while (*p == '-' || (*p >= '0' && *p < '9'))
c2792f5a
DE
2675 *fmt_p++ = *p++;
2676
2677 gdb_assert (*p == 'p' && *(p + 1) == '\0');
2678 if (val != 0)
2679 {
2680#ifdef PRINTF_HAS_LONG_LONG
2681 *fmt_p++ = 'l';
2682#endif
2683 *fmt_p++ = 'l';
2684 *fmt_p++ = 'x';
2685 *fmt_p++ = '\0';
af39b1c2
SM
2686 DIAGNOSTIC_PUSH
2687 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2688 gdb_printf (stream, fmt, val);
af39b1c2 2689 DIAGNOSTIC_POP
c2792f5a
DE
2690 }
2691 else
2692 {
2693 *fmt_p++ = 's';
2694 *fmt_p++ = '\0';
af39b1c2
SM
2695 DIAGNOSTIC_PUSH
2696 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2697 gdb_printf (stream, fmt, "(nil)");
af39b1c2 2698 DIAGNOSTIC_POP
c2792f5a
DE
2699 }
2700}
2701
a04b0428
JB
2702/* printf "printf format string" ARG to STREAM. */
2703
2704static void
bbc13ae3 2705ui_printf (const char *arg, struct ui_file *stream)
c906108c 2706{
bbc13ae3 2707 const char *s = arg;
8e481c3b 2708 std::vector<struct value *> val_args;
c906108c
SS
2709
2710 if (s == 0)
e2e0b3e5 2711 error_no_arg (_("format-control string and values to print"));
c906108c 2712
f1735a53 2713 s = skip_spaces (s);
c906108c 2714
675dcf4f 2715 /* A format string should follow, enveloped in double quotes. */
c906108c 2716 if (*s++ != '"')
8a3fe4f8 2717 error (_("Bad format string, missing '\"'."));
c906108c 2718
8e481c3b 2719 format_pieces fpieces (&s);
c906108c 2720
d3ce09f5
SS
2721 if (*s++ != '"')
2722 error (_("Bad format string, non-terminated '\"'."));
2723
f1735a53 2724 s = skip_spaces (s);
c906108c
SS
2725
2726 if (*s != ',' && *s != 0)
8a3fe4f8 2727 error (_("Invalid argument syntax"));
c906108c 2728
c5aa993b
JM
2729 if (*s == ',')
2730 s++;
f1735a53 2731 s = skip_spaces (s);
c906108c 2732
c906108c 2733 {
c906108c 2734 int nargs_wanted;
8e481c3b
TT
2735 int i;
2736 const char *current_substring;
c906108c 2737
c906108c 2738 nargs_wanted = 0;
8e481c3b
TT
2739 for (auto &&piece : fpieces)
2740 if (piece.argclass != literal_piece)
d3ce09f5 2741 ++nargs_wanted;
c906108c
SS
2742
2743 /* Now, parse all arguments and evaluate them.
2744 Store the VALUEs in VAL_ARGS. */
2745
2746 while (*s != '\0')
2747 {
bbc13ae3 2748 const char *s1;
ad3bbd48 2749
a04b0428 2750 s1 = s;
8e481c3b 2751 val_args.push_back (parse_to_comma_and_eval (&s1));
c5aa993b 2752
c906108c
SS
2753 s = s1;
2754 if (*s == ',')
2755 s++;
2756 }
c5aa993b 2757
8e481c3b 2758 if (val_args.size () != nargs_wanted)
8a3fe4f8 2759 error (_("Wrong number of arguments for specified format-string"));
c906108c
SS
2760
2761 /* Now actually print them. */
d3ce09f5 2762 i = 0;
8e481c3b 2763 for (auto &&piece : fpieces)
c906108c 2764 {
8e481c3b
TT
2765 current_substring = piece.string;
2766 switch (piece.argclass)
c906108c
SS
2767 {
2768 case string_arg:
c2792f5a 2769 printf_c_string (stream, current_substring, val_args[i]);
c906108c 2770 break;
6c7a06a3 2771 case wide_string_arg:
c2792f5a 2772 printf_wide_c_string (stream, current_substring, val_args[i]);
6c7a06a3
TT
2773 break;
2774 case wide_char_arg:
2775 {
8ee511af 2776 struct gdbarch *gdbarch = value_type (val_args[i])->arch ();
b858499d 2777 struct type *wctype = lookup_typename (current_language,
e6c014f2 2778 "wchar_t", NULL, 0);
6c7a06a3 2779 struct type *valtype;
6c7a06a3
TT
2780 const gdb_byte *bytes;
2781
2782 valtype = value_type (val_args[i]);
2783 if (TYPE_LENGTH (valtype) != TYPE_LENGTH (wctype)
78134374 2784 || valtype->code () != TYPE_CODE_INT)
6c7a06a3
TT
2785 error (_("expected wchar_t argument for %%lc"));
2786
50888e42 2787 bytes = value_contents (val_args[i]).data ();
6c7a06a3 2788
8268c778 2789 auto_obstack output;
6c7a06a3 2790
f870a310 2791 convert_between_encodings (target_wide_charset (gdbarch),
6c7a06a3
TT
2792 host_charset (),
2793 bytes, TYPE_LENGTH (valtype),
2794 TYPE_LENGTH (valtype),
2795 &output, translit_char);
2796 obstack_grow_str0 (&output, "");
2797
af39b1c2
SM
2798 DIAGNOSTIC_PUSH
2799 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c
TT
2800 gdb_printf (stream, current_substring,
2801 obstack_base (&output));
af39b1c2 2802 DIAGNOSTIC_POP
6c7a06a3
TT
2803 }
2804 break;
c906108c 2805 case long_long_arg:
74a0d9f6 2806#ifdef PRINTF_HAS_LONG_LONG
c906108c
SS
2807 {
2808 long long val = value_as_long (val_args[i]);
ad3bbd48 2809
af39b1c2
SM
2810 DIAGNOSTIC_PUSH
2811 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2812 gdb_printf (stream, current_substring, val);
af39b1c2 2813 DIAGNOSTIC_POP
c906108c
SS
2814 break;
2815 }
2816#else
8a3fe4f8 2817 error (_("long long not supported in printf"));
c906108c
SS
2818#endif
2819 case int_arg:
2820 {
46e9880c 2821 int val = value_as_long (val_args[i]);
ad3bbd48 2822
af39b1c2
SM
2823 DIAGNOSTIC_PUSH
2824 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2825 gdb_printf (stream, current_substring, val);
af39b1c2 2826 DIAGNOSTIC_POP
46e9880c
DJ
2827 break;
2828 }
2829 case long_arg:
2830 {
c906108c 2831 long val = value_as_long (val_args[i]);
ad3bbd48 2832
e06f3d6e
AB
2833 DIAGNOSTIC_PUSH
2834 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2835 gdb_printf (stream, current_substring, val);
e06f3d6e
AB
2836 DIAGNOSTIC_POP
2837 break;
2838 }
2839 case size_t_arg:
2840 {
2841 size_t val = value_as_long (val_args[i]);
2842
af39b1c2
SM
2843 DIAGNOSTIC_PUSH
2844 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2845 gdb_printf (stream, current_substring, val);
af39b1c2 2846 DIAGNOSTIC_POP
c906108c
SS
2847 break;
2848 }
16e812b2
UW
2849 /* Handles floating-point values. */
2850 case double_arg:
2851 case long_double_arg:
2852 case dec32float_arg:
2853 case dec64float_arg:
2854 case dec128float_arg:
2855 printf_floating (stream, current_substring, val_args[i],
8e481c3b 2856 piece.argclass);
c2792f5a 2857 break;
2025a643 2858 case ptr_arg:
c2792f5a
DE
2859 printf_pointer (stream, current_substring, val_args[i]);
2860 break;
d3ce09f5
SS
2861 case literal_piece:
2862 /* Print a portion of the format string that has no
2863 directives. Note that this will not include any
2864 ordinary %-specs, but it might include "%%". That is
6cb06a8c 2865 why we use gdb_printf and not gdb_puts here.
d3ce09f5
SS
2866 Also, we pass a dummy argument because some platforms
2867 have modified GCC to include -Wformat-security by
2868 default, which will warn here if there is no
2869 argument. */
af39b1c2
SM
2870 DIAGNOSTIC_PUSH
2871 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
6cb06a8c 2872 gdb_printf (stream, current_substring, 0);
af39b1c2 2873 DIAGNOSTIC_POP
d3ce09f5 2874 break;
675dcf4f
MK
2875 default:
2876 internal_error (__FILE__, __LINE__,
2025a643 2877 _("failed internal consistency check"));
c906108c 2878 }
d3ce09f5 2879 /* Maybe advance to the next argument. */
8e481c3b 2880 if (piece.argclass != literal_piece)
d3ce09f5 2881 ++i;
c906108c 2882 }
c906108c 2883 }
c906108c 2884}
c906108c 2885
f1421989
HZ
2886/* Implement the "printf" command. */
2887
a04b0428 2888static void
0b39b52e 2889printf_command (const char *arg, int from_tty)
f1421989 2890{
a04b0428 2891 ui_printf (arg, gdb_stdout);
3cd52293 2892 gdb_stdout->reset_style ();
1285ce86 2893 gdb_stdout->wrap_here (0);
da5bd37e 2894 gdb_stdout->flush ();
f1421989
HZ
2895}
2896
2897/* Implement the "eval" command. */
2898
2899static void
0b39b52e 2900eval_command (const char *arg, int from_tty)
f1421989 2901{
d7e74731 2902 string_file stb;
f1421989 2903
d7e74731 2904 ui_printf (arg, &stb);
f1421989 2905
d7e74731 2906 std::string expanded = insert_user_defined_cmd_args (stb.c_str ());
01770bbd 2907
95a6b0a1 2908 execute_command (expanded.c_str (), from_tty);
f1421989
HZ
2909}
2910
48136e00
LM
2911/* Convenience function for error checking in memory-tag commands. */
2912
2913static void
2914show_addr_not_tagged (CORE_ADDR address)
2915{
2916 error (_("Address %s not in a region mapped with a memory tagging flag."),
2917 paddress (target_gdbarch (), address));
2918}
2919
2920/* Convenience function for error checking in memory-tag commands. */
2921
2922static void
2923show_memory_tagging_unsupported (void)
2924{
2925 error (_("Memory tagging not supported or disabled by the current"
2926 " architecture."));
2927}
2928
2929/* Implement the "memory-tag" prefix command. */
2930
2931static void
2932memory_tag_command (const char *arg, int from_tty)
2933{
2934 help_list (memory_tag_list, "memory-tag ", all_commands, gdb_stdout);
2935}
2936
2937/* Helper for print-logical-tag and print-allocation-tag. */
2938
2939static void
2940memory_tag_print_tag_command (const char *args, enum memtag_type tag_type)
2941{
2942 if (args == nullptr)
2943 error_no_arg (_("address or pointer"));
2944
2945 /* Parse args into a value. If the value is a pointer or an address,
2946 then fetch the logical or allocation tag. */
2947 value_print_options print_opts;
2948
2949 struct value *val = process_print_command_args (args, &print_opts, true);
2950
2951 /* If the address is not in a region memory mapped with a memory tagging
2952 flag, it is no use trying to access/manipulate its allocation tag.
2953
2954 It is OK to manipulate the logical tag though. */
2955 if (tag_type == memtag_type::allocation
2956 && !gdbarch_tagged_address_p (target_gdbarch (), val))
2957 show_addr_not_tagged (value_as_address (val));
2958
2959 struct value *tag_value
2960 = gdbarch_get_memtag (target_gdbarch (), val, tag_type);
2961 std::string tag = gdbarch_memtag_to_string (target_gdbarch (), tag_value);
2962
2963 if (tag.empty ())
6cb06a8c
TT
2964 gdb_printf (_("%s tag unavailable.\n"),
2965 tag_type
2966 == memtag_type::logical? "Logical" : "Allocation");
48136e00
LM
2967
2968 struct value *v_tag = process_print_command_args (tag.c_str (),
2969 &print_opts,
2970 true);
2971 print_opts.output_format = 'x';
2972 print_value (v_tag, print_opts);
2973}
2974
2975/* Implement the "memory-tag print-logical-tag" command. */
2976
2977static void
2978memory_tag_print_logical_tag_command (const char *args, int from_tty)
2979{
2980 if (!target_supports_memory_tagging ())
2981 show_memory_tagging_unsupported ();
2982
2983 memory_tag_print_tag_command (args, memtag_type::logical);
2984}
2985
2986/* Implement the "memory-tag print-allocation-tag" command. */
2987
2988static void
2989memory_tag_print_allocation_tag_command (const char *args, int from_tty)
2990{
2991 if (!target_supports_memory_tagging ())
2992 show_memory_tagging_unsupported ();
2993
2994 memory_tag_print_tag_command (args, memtag_type::allocation);
2995}
2996
2997/* Parse ARGS and extract ADDR and TAG.
2998 ARGS should have format <expression> <tag bytes>. */
2999
3000static void
3001parse_with_logical_tag_input (const char *args, struct value **val,
3002 gdb::byte_vector &tags,
3003 value_print_options *print_opts)
3004{
3005 /* Fetch the address. */
3006 std::string address_string = extract_string_maybe_quoted (&args);
3007
3008 /* Parse the address into a value. */
3009 *val = process_print_command_args (address_string.c_str (), print_opts,
3010 true);
3011
3012 /* Fetch the tag bytes. */
3013 std::string tag_string = extract_string_maybe_quoted (&args);
3014
3015 /* Validate the input. */
3016 if (address_string.empty () || tag_string.empty ())
3017 error (_("Missing arguments."));
3018
3019 if (tag_string.length () != 2)
3020 error (_("Error parsing tags argument. The tag should be 2 digits."));
3021
3022 tags = hex2bin (tag_string.c_str ());
3023}
3024
3025/* Implement the "memory-tag with-logical-tag" command. */
3026
3027static void
3028memory_tag_with_logical_tag_command (const char *args, int from_tty)
3029{
3030 if (!target_supports_memory_tagging ())
3031 show_memory_tagging_unsupported ();
3032
3033 if (args == nullptr)
3034 error_no_arg (_("<address> <tag>"));
3035
3036 gdb::byte_vector tags;
3037 struct value *val;
3038 value_print_options print_opts;
3039
3040 /* Parse the input. */
3041 parse_with_logical_tag_input (args, &val, tags, &print_opts);
3042
3043 /* Setting the logical tag is just a local operation that does not touch
3044 any memory from the target. Given an input value, we modify the value
3045 to include the appropriate tag.
3046
3047 For this reason we need to cast the argument value to a
3048 (void *) pointer. This is so we have the right type for the gdbarch
3049 hook to manipulate the value and insert the tag.
3050
3051 Otherwise, this would fail if, for example, GDB parsed the argument value
3052 into an int-sized value and the pointer value has a type of greater
3053 length. */
3054
3055 /* Cast to (void *). */
3056 val = value_cast (builtin_type (target_gdbarch ())->builtin_data_ptr,
3057 val);
3058
3059 /* Length doesn't matter for a logical tag. Pass 0. */
3060 if (!gdbarch_set_memtags (target_gdbarch (), val, 0, tags,
3061 memtag_type::logical))
6cb06a8c 3062 gdb_printf (_("Could not update the logical tag data.\n"));
48136e00
LM
3063 else
3064 {
3065 /* Always print it in hex format. */
3066 print_opts.output_format = 'x';
3067 print_value (val, print_opts);
3068 }
3069}
3070
3071/* Parse ARGS and extract ADDR, LENGTH and TAGS. */
3072
3073static void
3074parse_set_allocation_tag_input (const char *args, struct value **val,
3075 size_t *length, gdb::byte_vector &tags)
3076{
3077 /* Fetch the address. */
3078 std::string address_string = extract_string_maybe_quoted (&args);
3079
3080 /* Parse the address into a value. */
3081 value_print_options print_opts;
3082 *val = process_print_command_args (address_string.c_str (), &print_opts,
3083 true);
3084
3085 /* Fetch the length. */
3086 std::string length_string = extract_string_maybe_quoted (&args);
3087
3088 /* Fetch the tag bytes. */
3089 std::string tags_string = extract_string_maybe_quoted (&args);
3090
3091 /* Validate the input. */
3092 if (address_string.empty () || length_string.empty () || tags_string.empty ())
3093 error (_("Missing arguments."));
3094
3095 errno = 0;
3096 const char *trailer = nullptr;
3097 LONGEST parsed_length = strtoulst (length_string.c_str (), &trailer, 10);
3098
3099 if (errno != 0 || (trailer != nullptr && trailer[0] != '\0'))
3100 error (_("Error parsing length argument."));
3101
3102 if (parsed_length <= 0)
3103 error (_("Invalid zero or negative length."));
3104
3105 *length = parsed_length;
3106
3107 if (tags_string.length () % 2)
3108 error (_("Error parsing tags argument. Tags should be 2 digits per byte."));
3109
3110 tags = hex2bin (tags_string.c_str ());
3111
3112 /* If the address is not in a region memory mapped with a memory tagging
3113 flag, it is no use trying to access/manipulate its allocation tag. */
3114 if (!gdbarch_tagged_address_p (target_gdbarch (), *val))
3115 show_addr_not_tagged (value_as_address (*val));
3116}
3117
3118/* Implement the "memory-tag set-allocation-tag" command.
3119 ARGS should be in the format <address> <length> <tags>. */
3120
3121static void
3122memory_tag_set_allocation_tag_command (const char *args, int from_tty)
3123{
3124 if (!target_supports_memory_tagging ())
3125 show_memory_tagging_unsupported ();
3126
3127 if (args == nullptr)
3128 error_no_arg (_("<starting address> <length> <tag bytes>"));
3129
3130 gdb::byte_vector tags;
3131 size_t length = 0;
3132 struct value *val;
3133
3134 /* Parse the input. */
3135 parse_set_allocation_tag_input (args, &val, &length, tags);
3136
3137 if (!gdbarch_set_memtags (target_gdbarch (), val, length, tags,
3138 memtag_type::allocation))
6cb06a8c 3139 gdb_printf (_("Could not update the allocation tag(s).\n"));
48136e00 3140 else
6cb06a8c 3141 gdb_printf (_("Allocation tag(s) updated successfully.\n"));
48136e00
LM
3142}
3143
3144/* Implement the "memory-tag check" command. */
3145
3146static void
3147memory_tag_check_command (const char *args, int from_tty)
3148{
3149 if (!target_supports_memory_tagging ())
3150 show_memory_tagging_unsupported ();
3151
3152 if (args == nullptr)
3153 error (_("Argument required (address or pointer)"));
3154
3155 /* Parse the expression into a value. If the value is an address or
3156 pointer, then check its logical tag against the allocation tag. */
3157 value_print_options print_opts;
3158
3159 struct value *val = process_print_command_args (args, &print_opts, true);
3160
3161 /* If the address is not in a region memory mapped with a memory tagging
3162 flag, it is no use trying to access/manipulate its allocation tag. */
3163 if (!gdbarch_tagged_address_p (target_gdbarch (), val))
3164 show_addr_not_tagged (value_as_address (val));
3165
3166 CORE_ADDR addr = value_as_address (val);
3167
3168 /* Check if the tag is valid. */
3169 if (!gdbarch_memtag_matches_p (target_gdbarch (), val))
3170 {
3171 struct value *tag
3172 = gdbarch_get_memtag (target_gdbarch (), val, memtag_type::logical);
3173 std::string ltag
3174 = gdbarch_memtag_to_string (target_gdbarch (), tag);
3175
3176 tag = gdbarch_get_memtag (target_gdbarch (), val,
3177 memtag_type::allocation);
3178 std::string atag
3179 = gdbarch_memtag_to_string (target_gdbarch (), tag);
3180
6cb06a8c
TT
3181 gdb_printf (_("Logical tag (%s) does not match"
3182 " the allocation tag (%s) for address %s.\n"),
3183 ltag.c_str (), atag.c_str (),
3184 paddress (target_gdbarch (), addr));
48136e00
LM
3185 }
3186 else
3187 {
3188 struct value *tag
3189 = gdbarch_get_memtag (target_gdbarch (), val, memtag_type::logical);
3190 std::string ltag
3191 = gdbarch_memtag_to_string (target_gdbarch (), tag);
3192
6cb06a8c
TT
3193 gdb_printf (_("Memory tags for address %s match (%s).\n"),
3194 paddress (target_gdbarch (), addr), ltag.c_str ());
48136e00
LM
3195 }
3196}
3197
6c265988 3198void _initialize_printcmd ();
c906108c 3199void
6c265988 3200_initialize_printcmd ()
c906108c 3201{
c94fdfd0
EZ
3202 struct cmd_list_element *c;
3203
c906108c
SS
3204 current_display_number = -1;
3205
c90e7d63
SM
3206 gdb::observers::free_objfile.attach (clear_dangling_display_expressions,
3207 "printcmd");
a3247a22 3208
11db9430 3209 add_info ("address", info_address_command,
188e1fa9
TT
3210 _("Describe where symbol SYM is stored.\n\
3211Usage: info address SYM"));
c906108c 3212
11db9430 3213 add_info ("symbol", info_symbol_command, _("\
1bedd215 3214Describe what symbol is at location ADDR.\n\
188e1fa9 3215Usage: info symbol ADDR\n\
1bedd215 3216Only for symbols with fixed locations (global or static scope)."));
c906108c 3217
037d7135 3218 c = add_com ("x", class_vars, x_command, _("\
1bedd215 3219Examine memory: x/FMT ADDRESS.\n\
c906108c
SS
3220ADDRESS is an expression for the memory address to examine.\n\
3221FMT is a repeat count followed by a format letter and a size letter.\n\
3222Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
6fbe845e
AB
3223 t(binary), f(float), a(address), i(instruction), c(char), s(string)\n\
3224 and z(hex, zero padded on the left).\n\
1bedd215 3225Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
c906108c 3226The specified number of objects of the specified size are printed\n\
bb556f1f
TK
3227according to the format. If a negative number is specified, memory is\n\
3228examined backward from the address.\n\n\
c906108c
SS
3229Defaults for format and size letters are those previously used.\n\
3230Default count is 1. Default address is following last thing printed\n\
1bedd215 3231with this command or \"print\"."));
037d7135 3232 set_cmd_completer_handle_brkchars (c, display_and_x_command_completer);
c906108c 3233
11db9430 3234 add_info ("display", info_display_command, _("\
188e1fa9
TT
3235Expressions to display when program stops, with code numbers.\n\
3236Usage: info display"));
c906108c 3237
1a966eab
AC
3238 add_cmd ("undisplay", class_vars, undisplay_command, _("\
3239Cancel some expressions to be displayed when program stops.\n\
188e1fa9 3240Usage: undisplay [NUM]...\n\
c906108c
SS
3241Arguments are the code numbers of the expressions to stop displaying.\n\
3242No argument means cancel all automatic-display expressions.\n\
3243\"delete display\" has the same effect as this command.\n\
1a966eab 3244Do \"info display\" to see current list of code numbers."),
c5aa993b 3245 &cmdlist);
c906108c 3246
037d7135 3247 c = add_com ("display", class_vars, display_command, _("\
1bedd215 3248Print value of expression EXP each time the program stops.\n\
188e1fa9 3249Usage: display[/FMT] EXP\n\
c906108c
SS
3250/FMT may be used before EXP as in the \"print\" command.\n\
3251/FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
3252as in the \"x\" command, and then EXP is used to get the address to examine\n\
3253and examining is done as in the \"x\" command.\n\n\
3254With no argument, display all currently requested auto-display expressions.\n\
1bedd215 3255Use \"undisplay\" to cancel display requests previously made."));
037d7135 3256 set_cmd_completer_handle_brkchars (c, display_and_x_command_completer);
c906108c 3257
c9174737 3258 add_cmd ("display", class_vars, enable_display_command, _("\
1a966eab 3259Enable some expressions to be displayed when program stops.\n\
188e1fa9 3260Usage: enable display [NUM]...\n\
c906108c
SS
3261Arguments are the code numbers of the expressions to resume displaying.\n\
3262No argument means enable all automatic-display expressions.\n\
1a966eab 3263Do \"info display\" to see current list of code numbers."), &enablelist);
c906108c 3264
1a966eab
AC
3265 add_cmd ("display", class_vars, disable_display_command, _("\
3266Disable some expressions to be displayed when program stops.\n\
188e1fa9 3267Usage: disable display [NUM]...\n\
c906108c
SS
3268Arguments are the code numbers of the expressions to stop displaying.\n\
3269No argument means disable all automatic-display expressions.\n\
1a966eab 3270Do \"info display\" to see current list of code numbers."), &disablelist);
c906108c 3271
1a966eab
AC
3272 add_cmd ("display", class_vars, undisplay_command, _("\
3273Cancel some expressions to be displayed when program stops.\n\
188e1fa9 3274Usage: delete display [NUM]...\n\
c906108c
SS
3275Arguments are the code numbers of the expressions to stop displaying.\n\
3276No argument means cancel all automatic-display expressions.\n\
1a966eab 3277Do \"info display\" to see current list of code numbers."), &deletelist);
c906108c 3278
1bedd215 3279 add_com ("printf", class_vars, printf_command, _("\
80ae639d 3280Formatted printing, like the C \"printf\" function.\n\
188e1fa9 3281Usage: printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
80ae639d 3282This supports most C printf format specifications, like %s, %d, etc."));
c906108c 3283
1bedd215
AC
3284 add_com ("output", class_vars, output_command, _("\
3285Like \"print\" but don't put in value history and don't print newline.\n\
188e1fa9 3286Usage: output EXP\n\
1bedd215 3287This is useful in user-defined commands."));
c906108c 3288
1bedd215 3289 add_prefix_cmd ("set", class_vars, set_command, _("\
590042fc 3290Evaluate expression EXP and assign result to variable VAR.\n\
188e1fa9
TT
3291Usage: set VAR = EXP\n\
3292This uses assignment syntax appropriate for the current language\n\
3293(VAR = EXP or VAR := EXP for example).\n\
3294VAR may be a debugger \"convenience\" variable (names starting\n\
c906108c 3295with $), a register (a few standard names starting with $), or an actual\n\
1bedd215
AC
3296variable in the program being debugged. EXP is any valid expression.\n\
3297Use \"set variable\" for variables with names identical to set subcommands.\n\
3298\n\
3299With a subcommand, this command modifies parts of the gdb environment.\n\
3300You can see these environment settings with the \"show\" command."),
2f822da5 3301 &setlist, 1, &cmdlist);
c906108c 3302 if (dbx_commands)
0b39b52e 3303 add_com ("assign", class_vars, set_command, _("\
590042fc 3304Evaluate expression EXP and assign result to variable VAR.\n\
188e1fa9
TT
3305Usage: assign 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\
c906108c 3312\nWith a subcommand, this command modifies parts of the gdb environment.\n\
1bedd215 3313You can see these environment settings with the \"show\" command."));
c906108c 3314
0df8b418 3315 /* "call" is the same as "set", but handy for dbx users to call fns. */
1bedd215
AC
3316 c = add_com ("call", class_vars, call_command, _("\
3317Call a function in the program.\n\
188e1fa9 3318Usage: call EXP\n\
c906108c
SS
3319The argument is the function name and arguments, in the notation of the\n\
3320current working language. The result is printed and saved in the value\n\
1bedd215 3321history, if it is not void."));
7d8062de 3322 set_cmd_completer_handle_brkchars (c, print_command_completer);
c906108c 3323
5e84b7ee
SM
3324 cmd_list_element *set_variable_cmd
3325 = add_cmd ("variable", class_vars, set_command, _("\
590042fc 3326Evaluate expression EXP and assign result to variable VAR.\n\
188e1fa9
TT
3327Usage: set variable VAR = EXP\n\
3328This uses assignment syntax appropriate for the current language\n\
3329(VAR = EXP or VAR := EXP for example).\n\
3330VAR may be a debugger \"convenience\" variable (names starting\n\
c906108c
SS
3331with $), a register (a few standard names starting with $), or an actual\n\
3332variable in the program being debugged. EXP is any valid expression.\n\
1a966eab 3333This may usually be abbreviated to simply \"set\"."),
5e84b7ee
SM
3334 &setlist);
3335 add_alias_cmd ("var", set_variable_cmd, class_vars, 0, &setlist);
c906108c 3336
7d8062de
PA
3337 const auto print_opts = make_value_print_options_def_group (nullptr);
3338
8abfcabc 3339 static const std::string print_help = gdb::option::build_help (_("\
1bedd215 3340Print value of expression EXP.\n\
7d8062de
PA
3341Usage: print [[OPTION]... --] [/FMT] [EXP]\n\
3342\n\
3343Options:\n\
590042fc
PW
3344%OPTIONS%\n\
3345\n\
7d8062de
PA
3346Note: because this command accepts arbitrary expressions, if you\n\
3347specify any command option, you must use a double dash (\"--\")\n\
3348to mark the end of option processing. E.g.: \"print -o -- myobj\".\n\
3349\n\
c906108c
SS
3350Variables accessible are those of the lexical environment of the selected\n\
3351stack frame, plus all those whose scope is global or an entire file.\n\
3352\n\
3353$NUM gets previous value number NUM. $ and $$ are the last two values.\n\
3354$$NUM refers to NUM'th value back from the last one.\n\
1bedd215
AC
3355Names starting with $ refer to registers (with the values they would have\n\
3356if the program were to return to the stack frame now selected, restoring\n\
c906108c
SS
3357all registers saved by frames farther in) or else to debugger\n\
3358\"convenience\" variables (any such name not a known register).\n\
1bedd215
AC
3359Use assignment expressions to give values to convenience variables.\n\
3360\n\
c906108c
SS
3361{TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
3362@ is a binary operator for treating consecutive data objects\n\
3363anywhere in memory as an array. FOO@NUM gives an array whose first\n\
3364element is FOO, whose second element is stored in the space following\n\
3365where FOO is stored, etc. FOO must be an expression whose value\n\
1bedd215
AC
3366resides in memory.\n\
3367\n\
c906108c 3368EXP may be preceded with /FMT, where FMT is a format letter\n\
7d8062de
PA
3369but no count or size letter (see \"x\" command)."),
3370 print_opts);
3371
3947f654
SM
3372 cmd_list_element *print_cmd
3373 = add_com ("print", class_vars, print_command, print_help.c_str ());
3374 set_cmd_completer_handle_brkchars (print_cmd, print_command_completer);
3375 add_com_alias ("p", print_cmd, class_vars, 1);
3376 add_com_alias ("inspect", print_cmd, class_vars, 1);
c906108c 3377
35096d9d
AC
3378 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
3379 &max_symbolic_offset, _("\
188e1fa9
TT
3380Set the largest offset that will be printed in <SYMBOL+1234> form."), _("\
3381Show the largest offset that will be printed in <SYMBOL+1234> form."), _("\
f81d1120
PA
3382Tell GDB to only display the symbolic form of an address if the\n\
3383offset between the closest earlier symbol and the address is less than\n\
3384the specified maximum offset. The default is \"unlimited\", which tells GDB\n\
3385to always print the symbolic form of an address if any symbol precedes\n\
3386it. Zero is equivalent to \"unlimited\"."),
35096d9d 3387 NULL,
920d2a44 3388 show_max_symbolic_offset,
35096d9d 3389 &setprintlist, &showprintlist);
5bf193a2
AC
3390 add_setshow_boolean_cmd ("symbol-filename", no_class,
3391 &print_symbol_filename, _("\
188e1fa9
TT
3392Set printing of source filename and line number with <SYMBOL>."), _("\
3393Show printing of source filename and line number with <SYMBOL>."), NULL,
5bf193a2 3394 NULL,
920d2a44 3395 show_print_symbol_filename,
5bf193a2 3396 &setprintlist, &showprintlist);
f1421989
HZ
3397
3398 add_com ("eval", no_class, eval_command, _("\
188e1fa9
TT
3399Construct a GDB command and then evaluate it.\n\
3400Usage: eval \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
3401Convert the arguments to a string as \"printf\" would, but then\n\
3402treat this string as a command line, and evaluate it."));
48136e00
LM
3403
3404 /* Memory tagging commands. */
3405 add_prefix_cmd ("memory-tag", class_vars, memory_tag_command, _("\
3406Generic command for printing and manipulating memory tag properties."),
2f822da5 3407 &memory_tag_list, 0, &cmdlist);
48136e00
LM
3408 add_cmd ("print-logical-tag", class_vars,
3409 memory_tag_print_logical_tag_command,
3410 ("Print the logical tag from POINTER.\n\
3411Usage: memory-tag print-logical-tag <POINTER>.\n\
3412<POINTER> is an expression that evaluates to a pointer.\n\
3413Print the logical tag contained in POINTER. The tag interpretation is\n\
3414architecture-specific."),
3415 &memory_tag_list);
3416 add_cmd ("print-allocation-tag", class_vars,
3417 memory_tag_print_allocation_tag_command,
3418 _("Print the allocation tag for ADDRESS.\n\
3419Usage: memory-tag print-allocation-tag <ADDRESS>.\n\
3420<ADDRESS> is an expression that evaluates to a memory address.\n\
3421Print the allocation tag associated with the memory address ADDRESS.\n\
3422The tag interpretation is architecture-specific."),
3423 &memory_tag_list);
3424 add_cmd ("with-logical-tag", class_vars, memory_tag_with_logical_tag_command,
3425 _("Print a POINTER with a specific logical TAG.\n\
3426Usage: memory-tag with-logical-tag <POINTER> <TAG>\n\
3427<POINTER> is an expression that evaluates to a pointer.\n\
3428<TAG> is a sequence of hex bytes that is interpreted by the architecture\n\
3429as a single memory tag."),
3430 &memory_tag_list);
3431 add_cmd ("set-allocation-tag", class_vars,
3432 memory_tag_set_allocation_tag_command,
3433 _("Set the allocation tag(s) for a memory range.\n\
3434Usage: memory-tag set-allocation-tag <ADDRESS> <LENGTH> <TAG_BYTES>\n\
3435<ADDRESS> is an expression that evaluates to a memory address\n\
3436<LENGTH> is the number of bytes that is added to <ADDRESS> to calculate\n\
3437the memory range.\n\
3438<TAG_BYTES> is a sequence of hex bytes that is interpreted by the\n\
3439architecture as one or more memory tags.\n\
3440Sets the tags of the memory range [ADDRESS, ADDRESS + LENGTH)\n\
3441to TAG_BYTES.\n\
3442\n\
3443If the number of tags is greater than or equal to the number of tag granules\n\
3444in the [ADDRESS, ADDRESS + LENGTH) range, only the tags up to the\n\
3445number of tag granules are updated.\n\
3446\n\
3447If the number of tags is less than the number of tag granules, then the\n\
3448command is a fill operation. The TAG_BYTES are interpreted as a pattern\n\
3449that gets repeated until the number of tag granules in the memory range\n\
3450[ADDRESS, ADDRESS + LENGTH) is updated."),
3451 &memory_tag_list);
3452 add_cmd ("check", class_vars, memory_tag_check_command,
3453 _("Validate a pointer's logical tag against the allocation tag.\n\
3454Usage: memory-tag check <POINTER>\n\
3455<POINTER> is an expression that evaluates to a pointer\n\
3456Fetch the logical and allocation tags for POINTER and compare them\n\
3457for equality. If the tags do not match, print additional information about\n\
3458the tag mismatch."),
3459 &memory_tag_list);
c906108c 3460}