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