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