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