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