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