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