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