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