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