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