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