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