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