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