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