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