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