d5fbeba2aba67b14587037da5a66d0450c0ab9b2
[deliverable/binutils-gdb.git] / gdb / printcmd.c
1 /* Print values for GNU debugger GDB.
2
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
23
24 #include "defs.h"
25 #include "gdb_string.h"
26 #include "frame.h"
27 #include "symtab.h"
28 #include "gdbtypes.h"
29 #include "value.h"
30 #include "language.h"
31 #include "expression.h"
32 #include "gdbcore.h"
33 #include "gdbcmd.h"
34 #include "target.h"
35 #include "breakpoint.h"
36 #include "demangle.h"
37 #include "valprint.h"
38 #include "annotate.h"
39 #include "symfile.h" /* for overlay functions */
40 #include "objfiles.h" /* ditto */
41 #include "completer.h" /* for completion functions */
42 #include "ui-out.h"
43 #include "gdb_assert.h"
44 #include "block.h"
45 #include "disasm.h"
46
47 #ifdef TUI
48 #include "tui/tui.h" /* For tui_active et.al. */
49 #endif
50
51 extern int asm_demangle; /* Whether to demangle syms in asm printouts */
52 extern int addressprint; /* Whether to print hex addresses in HLL " */
53
54 struct format_data
55 {
56 int count;
57 char format;
58 char size;
59 };
60
61 /* Last specified output format. */
62
63 static char last_format = 'x';
64
65 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
66
67 static char last_size = 'w';
68
69 /* Default address to examine next. */
70
71 static CORE_ADDR next_address;
72
73 /* Number of delay instructions following current disassembled insn. */
74
75 static int branch_delay_insns;
76
77 /* Last address examined. */
78
79 static CORE_ADDR last_examine_address;
80
81 /* Contents of last address examined.
82 This is not valid past the end of the `x' command! */
83
84 static struct value *last_examine_value;
85
86 /* Largest offset between a symbolic value and an address, that will be
87 printed as `0x1234 <symbol+offset>'. */
88
89 static unsigned int max_symbolic_offset = UINT_MAX;
90 static void
91 show_max_symbolic_offset (struct ui_file *file, int from_tty,
92 struct cmd_list_element *c, const char *value)
93 {
94 fprintf_filtered (file, _("\
95 The largest offset that will be printed in <symbol+1234> form is %s.\n"),
96 value);
97 }
98
99 /* Append the source filename and linenumber of the symbol when
100 printing a symbolic value as `<symbol at filename:linenum>' if set. */
101 static int print_symbol_filename = 0;
102 static void
103 show_print_symbol_filename (struct ui_file *file, int from_tty,
104 struct cmd_list_element *c, const char *value)
105 {
106 fprintf_filtered (file, _("\
107 Printing of source filename and line number with <symbol> is %s.\n"),
108 value);
109 }
110
111 /* Number of auto-display expression currently being displayed.
112 So that we can disable it if we get an error or a signal within it.
113 -1 when not doing one. */
114
115 int current_display_number;
116
117 /* Flag to low-level print routines that this value is being printed
118 in an epoch window. We'd like to pass this as a parameter, but
119 every routine would need to take it. Perhaps we can encapsulate
120 this in the I/O stream once we have GNU stdio. */
121
122 int inspect_it = 0;
123
124 struct display
125 {
126 /* Chain link to next auto-display item. */
127 struct display *next;
128 /* Expression to be evaluated and displayed. */
129 struct expression *exp;
130 /* Item number of this auto-display item. */
131 int number;
132 /* Display format specified. */
133 struct format_data format;
134 /* Innermost block required by this expression when evaluated */
135 struct block *block;
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 /* Prototypes for exported functions. */
148
149 void output_command (char *, int);
150
151 void _initialize_printcmd (void);
152
153 /* Prototypes for local functions. */
154
155 static void do_one_display (struct display *);
156 \f
157
158 /* Decode a format specification. *STRING_PTR should point to it.
159 OFORMAT and OSIZE are used as defaults for the format and size
160 if none are given in the format specification.
161 If OSIZE is zero, then the size field of the returned value
162 should be set only if a size is explicitly specified by the
163 user.
164 The structure returned describes all the data
165 found in the specification. In addition, *STRING_PTR is advanced
166 past the specification and past all whitespace following it. */
167
168 static struct format_data
169 decode_format (char **string_ptr, int oformat, int osize)
170 {
171 struct format_data val;
172 char *p = *string_ptr;
173
174 val.format = '?';
175 val.size = '?';
176 val.count = 1;
177
178 if (*p >= '0' && *p <= '9')
179 val.count = atoi (p);
180 while (*p >= '0' && *p <= '9')
181 p++;
182
183 /* Now process size or format letters that follow. */
184
185 while (1)
186 {
187 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
188 val.size = *p++;
189 else if (*p >= 'a' && *p <= 'z')
190 val.format = *p++;
191 else
192 break;
193 }
194
195 while (*p == ' ' || *p == '\t')
196 p++;
197 *string_ptr = p;
198
199 /* Set defaults for format and size if not specified. */
200 if (val.format == '?')
201 {
202 if (val.size == '?')
203 {
204 /* Neither has been specified. */
205 val.format = oformat;
206 val.size = osize;
207 }
208 else
209 /* If a size is specified, any format makes a reasonable
210 default except 'i'. */
211 val.format = oformat == 'i' ? 'x' : oformat;
212 }
213 else if (val.size == '?')
214 switch (val.format)
215 {
216 case 'a':
217 case 's':
218 /* Pick the appropriate size for an address. */
219 if (gdbarch_ptr_bit (current_gdbarch) == 64)
220 val.size = osize ? 'g' : osize;
221 else if (gdbarch_ptr_bit (current_gdbarch) == 32)
222 val.size = osize ? 'w' : osize;
223 else if (gdbarch_ptr_bit (current_gdbarch) == 16)
224 val.size = osize ? 'h' : osize;
225 else
226 /* Bad value for gdbarch_ptr_bit. */
227 internal_error (__FILE__, __LINE__,
228 _("failed internal consistency check"));
229 break;
230 case 'f':
231 /* Floating point has to be word or giantword. */
232 if (osize == 'w' || osize == 'g')
233 val.size = osize;
234 else
235 /* Default it to giantword if the last used size is not
236 appropriate. */
237 val.size = osize ? 'g' : osize;
238 break;
239 case 'c':
240 /* Characters default to one byte. */
241 val.size = osize ? 'b' : osize;
242 break;
243 default:
244 /* The default is the size most recently specified. */
245 val.size = osize;
246 }
247
248 return val;
249 }
250 \f
251 /* Print value VAL on stream according to FORMAT, a letter or 0.
252 Do not end with a newline.
253 0 means print VAL according to its own type.
254 SIZE is the letter for the size of datum being printed.
255 This is used to pad hex numbers so they line up. */
256
257 static void
258 print_formatted (struct value *val, int format, int size,
259 struct ui_file *stream)
260 {
261 struct type *type = check_typedef (value_type (val));
262 int len = TYPE_LENGTH (type);
263
264 if (VALUE_LVAL (val) == lval_memory)
265 next_address = VALUE_ADDRESS (val) + len;
266
267 switch (format)
268 {
269 case 's':
270 /* FIXME: Need to handle wchar_t's here... */
271 next_address = VALUE_ADDRESS (val)
272 + val_print_string (VALUE_ADDRESS (val), -1, 1, stream);
273 break;
274
275 case 'i':
276 /* The old comment says
277 "Force output out, print_insn not using _filtered".
278 I'm not completely sure what that means, I suspect most print_insn
279 now do use _filtered, so I guess it's obsolete.
280 --Yes, it does filter now, and so this is obsolete. -JB */
281
282 /* We often wrap here if there are long symbolic names. */
283 wrap_here (" ");
284 next_address = (VALUE_ADDRESS (val)
285 + gdb_print_insn (VALUE_ADDRESS (val), stream,
286 &branch_delay_insns));
287 break;
288
289 default:
290 if (format == 0
291 || TYPE_CODE (type) == TYPE_CODE_ARRAY
292 || TYPE_CODE (type) == TYPE_CODE_STRING
293 || TYPE_CODE (type) == TYPE_CODE_STRUCT
294 || TYPE_CODE (type) == TYPE_CODE_UNION
295 || TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
296 /* If format is 0, use the 'natural' format for that type of
297 value. If the type is non-scalar, we have to use language
298 rules to print it as a series of scalars. */
299 value_print (val, stream, format, Val_pretty_default);
300 else
301 /* User specified format, so don't look to the the type to
302 tell us what to do. */
303 print_scalar_formatted (value_contents (val), type,
304 format, size, stream);
305 }
306 }
307
308 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
309 according to letters FORMAT and SIZE on STREAM.
310 FORMAT may not be zero. Formats s and i are not supported at this level.
311
312 This is how the elements of an array or structure are printed
313 with a format. */
314
315 void
316 print_scalar_formatted (const void *valaddr, struct type *type,
317 int format, int size, struct ui_file *stream)
318 {
319 LONGEST val_long = 0;
320 unsigned int len = TYPE_LENGTH (type);
321
322 if (len > sizeof(LONGEST) &&
323 (TYPE_CODE (type) == TYPE_CODE_INT
324 || TYPE_CODE (type) == TYPE_CODE_ENUM))
325 {
326 switch (format)
327 {
328 case 'o':
329 print_octal_chars (stream, valaddr, len);
330 return;
331 case 'u':
332 case 'd':
333 print_decimal_chars (stream, valaddr, len);
334 return;
335 case 't':
336 print_binary_chars (stream, valaddr, len);
337 return;
338 case 'x':
339 print_hex_chars (stream, valaddr, len);
340 return;
341 case 'c':
342 print_char_chars (stream, valaddr, len);
343 return;
344 default:
345 break;
346 };
347 }
348
349 if (format != 'f')
350 val_long = unpack_long (type, valaddr);
351
352 /* If the value is a pointer, and pointers and addresses are not the
353 same, then at this point, the value's length (in target bytes) is
354 gdbarch_addr_bit/TARGET_CHAR_BIT, not TYPE_LENGTH (type). */
355 if (TYPE_CODE (type) == TYPE_CODE_PTR)
356 len = gdbarch_addr_bit (current_gdbarch) / TARGET_CHAR_BIT;
357
358 /* If we are printing it as unsigned, truncate it in case it is actually
359 a negative signed value (e.g. "print/u (short)-1" should print 65535
360 (if shorts are 16 bits) instead of 4294967295). */
361 if (format != 'd')
362 {
363 if (len < sizeof (LONGEST))
364 val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
365 }
366
367 switch (format)
368 {
369 case 'x':
370 if (!size)
371 {
372 /* No size specified, like in print. Print varying # of digits. */
373 print_longest (stream, 'x', 1, val_long);
374 }
375 else
376 switch (size)
377 {
378 case 'b':
379 case 'h':
380 case 'w':
381 case 'g':
382 print_longest (stream, size, 1, val_long);
383 break;
384 default:
385 error (_("Undefined output size \"%c\"."), size);
386 }
387 break;
388
389 case 'd':
390 print_longest (stream, 'd', 1, val_long);
391 break;
392
393 case 'u':
394 print_longest (stream, 'u', 0, val_long);
395 break;
396
397 case 'o':
398 if (val_long)
399 print_longest (stream, 'o', 1, val_long);
400 else
401 fprintf_filtered (stream, "0");
402 break;
403
404 case 'a':
405 {
406 CORE_ADDR addr = unpack_pointer (type, valaddr);
407 print_address (addr, stream);
408 }
409 break;
410
411 case 'c':
412 value_print (value_from_longest (builtin_type_true_char, val_long),
413 stream, 0, Val_pretty_default);
414 break;
415
416 case 'f':
417 if (len == TYPE_LENGTH (builtin_type_float))
418 type = builtin_type_float;
419 else if (len == TYPE_LENGTH (builtin_type_double))
420 type = builtin_type_double;
421 else if (len == TYPE_LENGTH (builtin_type_long_double))
422 type = builtin_type_long_double;
423 print_floating (valaddr, type, stream);
424 break;
425
426 case 0:
427 internal_error (__FILE__, __LINE__,
428 _("failed internal consistency check"));
429
430 case 't':
431 /* Binary; 't' stands for "two". */
432 {
433 char bits[8 * (sizeof val_long) + 1];
434 char buf[8 * (sizeof val_long) + 32];
435 char *cp = bits;
436 int width;
437
438 if (!size)
439 width = 8 * (sizeof val_long);
440 else
441 switch (size)
442 {
443 case 'b':
444 width = 8;
445 break;
446 case 'h':
447 width = 16;
448 break;
449 case 'w':
450 width = 32;
451 break;
452 case 'g':
453 width = 64;
454 break;
455 default:
456 error (_("Undefined output size \"%c\"."), size);
457 }
458
459 bits[width] = '\0';
460 while (width-- > 0)
461 {
462 bits[width] = (val_long & 1) ? '1' : '0';
463 val_long >>= 1;
464 }
465 if (!size)
466 {
467 while (*cp && *cp == '0')
468 cp++;
469 if (*cp == '\0')
470 cp--;
471 }
472 strcpy (buf, cp);
473 fputs_filtered (buf, stream);
474 }
475 break;
476
477 default:
478 error (_("Undefined output format \"%c\"."), format);
479 }
480 }
481
482 /* Specify default address for `x' command.
483 The `info lines' command uses this. */
484
485 void
486 set_next_address (CORE_ADDR addr)
487 {
488 next_address = addr;
489
490 /* Make address available to the user as $_. */
491 set_internalvar (lookup_internalvar ("_"),
492 value_from_pointer (lookup_pointer_type (builtin_type_void),
493 addr));
494 }
495
496 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
497 after LEADIN. Print nothing if no symbolic name is found nearby.
498 Optionally also print source file and line number, if available.
499 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
500 or to interpret it as a possible C++ name and convert it back to source
501 form. However note that DO_DEMANGLE can be overridden by the specific
502 settings of the demangle and asm_demangle variables. */
503
504 void
505 print_address_symbolic (CORE_ADDR addr, struct ui_file *stream,
506 int do_demangle, char *leadin)
507 {
508 char *name = NULL;
509 char *filename = NULL;
510 int unmapped = 0;
511 int offset = 0;
512 int line = 0;
513
514 /* Throw away both name and filename. */
515 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
516 make_cleanup (free_current_contents, &filename);
517
518 if (build_address_symbolic (addr, do_demangle, &name, &offset,
519 &filename, &line, &unmapped))
520 {
521 do_cleanups (cleanup_chain);
522 return;
523 }
524
525 fputs_filtered (leadin, stream);
526 if (unmapped)
527 fputs_filtered ("<*", stream);
528 else
529 fputs_filtered ("<", stream);
530 fputs_filtered (name, stream);
531 if (offset != 0)
532 fprintf_filtered (stream, "+%u", (unsigned int) offset);
533
534 /* Append source filename and line number if desired. Give specific
535 line # of this addr, if we have it; else line # of the nearest symbol. */
536 if (print_symbol_filename && filename != NULL)
537 {
538 if (line != -1)
539 fprintf_filtered (stream, " at %s:%d", filename, line);
540 else
541 fprintf_filtered (stream, " in %s", filename);
542 }
543 if (unmapped)
544 fputs_filtered ("*>", stream);
545 else
546 fputs_filtered (">", stream);
547
548 do_cleanups (cleanup_chain);
549 }
550
551 /* Given an address ADDR return all the elements needed to print the
552 address in a symbolic form. NAME can be mangled or not depending
553 on DO_DEMANGLE (and also on the asm_demangle global variable,
554 manipulated via ''set print asm-demangle''). Return 0 in case of
555 success, when all the info in the OUT paramters is valid. Return 1
556 otherwise. */
557 int
558 build_address_symbolic (CORE_ADDR addr, /* IN */
559 int do_demangle, /* IN */
560 char **name, /* OUT */
561 int *offset, /* OUT */
562 char **filename, /* OUT */
563 int *line, /* OUT */
564 int *unmapped) /* OUT */
565 {
566 struct minimal_symbol *msymbol;
567 struct symbol *symbol;
568 CORE_ADDR name_location = 0;
569 asection *section = 0;
570 char *name_temp = "";
571
572 /* Let's say it is unmapped. */
573 *unmapped = 0;
574
575 /* Determine if the address is in an overlay, and whether it is
576 mapped. */
577 if (overlay_debugging)
578 {
579 section = find_pc_overlay (addr);
580 if (pc_in_unmapped_range (addr, section))
581 {
582 *unmapped = 1;
583 addr = overlay_mapped_address (addr, section);
584 }
585 }
586
587 /* First try to find the address in the symbol table, then
588 in the minsyms. Take the closest one. */
589
590 /* This is defective in the sense that it only finds text symbols. So
591 really this is kind of pointless--we should make sure that the
592 minimal symbols have everything we need (by changing that we could
593 save some memory, but for many debug format--ELF/DWARF or
594 anything/stabs--it would be inconvenient to eliminate those minimal
595 symbols anyway). */
596 msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
597 symbol = find_pc_sect_function (addr, section);
598
599 if (symbol)
600 {
601 name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
602 if (do_demangle || asm_demangle)
603 name_temp = SYMBOL_PRINT_NAME (symbol);
604 else
605 name_temp = DEPRECATED_SYMBOL_NAME (symbol);
606 }
607
608 if (msymbol != NULL)
609 {
610 if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
611 {
612 /* The msymbol is closer to the address than the symbol;
613 use the msymbol instead. */
614 symbol = 0;
615 name_location = SYMBOL_VALUE_ADDRESS (msymbol);
616 if (do_demangle || asm_demangle)
617 name_temp = SYMBOL_PRINT_NAME (msymbol);
618 else
619 name_temp = DEPRECATED_SYMBOL_NAME (msymbol);
620 }
621 }
622 if (symbol == NULL && msymbol == NULL)
623 return 1;
624
625 /* If the nearest symbol is too far away, don't print anything symbolic. */
626
627 /* For when CORE_ADDR is larger than unsigned int, we do math in
628 CORE_ADDR. But when we detect unsigned wraparound in the
629 CORE_ADDR math, we ignore this test and print the offset,
630 because addr+max_symbolic_offset has wrapped through the end
631 of the address space back to the beginning, giving bogus comparison. */
632 if (addr > name_location + max_symbolic_offset
633 && name_location + max_symbolic_offset > name_location)
634 return 1;
635
636 *offset = addr - name_location;
637
638 *name = xstrdup (name_temp);
639
640 if (print_symbol_filename)
641 {
642 struct symtab_and_line sal;
643
644 sal = find_pc_sect_line (addr, section, 0);
645
646 if (sal.symtab)
647 {
648 *filename = xstrdup (sal.symtab->filename);
649 *line = sal.line;
650 }
651 }
652 return 0;
653 }
654
655 /* Print address ADDR on STREAM. USE_LOCAL means the same thing as for
656 print_longest. */
657 void
658 deprecated_print_address_numeric (CORE_ADDR addr, int use_local,
659 struct ui_file *stream)
660 {
661 if (use_local)
662 fputs_filtered (paddress (addr), stream);
663 else
664 {
665 int addr_bit = gdbarch_addr_bit (current_gdbarch);
666
667 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
668 addr &= ((CORE_ADDR) 1 << addr_bit) - 1;
669 print_longest (stream, 'x', 0, (ULONGEST) addr);
670 }
671 }
672
673 /* Print address ADDR symbolically on STREAM.
674 First print it as a number. Then perhaps print
675 <SYMBOL + OFFSET> after the number. */
676
677 void
678 print_address (CORE_ADDR addr, struct ui_file *stream)
679 {
680 deprecated_print_address_numeric (addr, 1, stream);
681 print_address_symbolic (addr, stream, asm_demangle, " ");
682 }
683
684 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
685 controls whether to print the symbolic name "raw" or demangled.
686 Global setting "addressprint" controls whether to print hex address
687 or not. */
688
689 void
690 print_address_demangle (CORE_ADDR addr, struct ui_file *stream,
691 int do_demangle)
692 {
693 if (addr == 0)
694 {
695 fprintf_filtered (stream, "0");
696 }
697 else if (addressprint)
698 {
699 deprecated_print_address_numeric (addr, 1, stream);
700 print_address_symbolic (addr, stream, do_demangle, " ");
701 }
702 else
703 {
704 print_address_symbolic (addr, stream, do_demangle, "");
705 }
706 }
707 \f
708
709 /* These are the types that $__ will get after an examine command of one
710 of these sizes. */
711
712 static struct type *examine_i_type;
713
714 static struct type *examine_b_type;
715 static struct type *examine_h_type;
716 static struct type *examine_w_type;
717 static struct type *examine_g_type;
718
719 /* Examine data at address ADDR in format FMT.
720 Fetch it from memory and print on gdb_stdout. */
721
722 static void
723 do_examine (struct format_data fmt, CORE_ADDR addr)
724 {
725 char format = 0;
726 char size;
727 int count = 1;
728 struct type *val_type = NULL;
729 int i;
730 int maxelts;
731
732 format = fmt.format;
733 size = fmt.size;
734 count = fmt.count;
735 next_address = addr;
736
737 /* String or instruction format implies fetch single bytes
738 regardless of the specified size. */
739 if (format == 's' || format == 'i')
740 size = 'b';
741
742 if (format == 'i')
743 val_type = examine_i_type;
744 else if (size == 'b')
745 val_type = examine_b_type;
746 else if (size == 'h')
747 val_type = examine_h_type;
748 else if (size == 'w')
749 val_type = examine_w_type;
750 else if (size == 'g')
751 val_type = examine_g_type;
752
753 maxelts = 8;
754 if (size == 'w')
755 maxelts = 4;
756 if (size == 'g')
757 maxelts = 2;
758 if (format == 's' || format == 'i')
759 maxelts = 1;
760
761 /* Print as many objects as specified in COUNT, at most maxelts per line,
762 with the address of the next one at the start of each line. */
763
764 while (count > 0)
765 {
766 QUIT;
767 print_address (next_address, gdb_stdout);
768 printf_filtered (":");
769 for (i = maxelts;
770 i > 0 && count > 0;
771 i--, count--)
772 {
773 printf_filtered ("\t");
774 /* Note that print_formatted sets next_address for the next
775 object. */
776 last_examine_address = next_address;
777
778 if (last_examine_value)
779 value_free (last_examine_value);
780
781 /* The value to be displayed is not fetched greedily.
782 Instead, to avoid the possibility of a fetched value not
783 being used, its retrieval is delayed until the print code
784 uses it. When examining an instruction stream, the
785 disassembler will perform its own memory fetch using just
786 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
787 the disassembler be modified so that LAST_EXAMINE_VALUE
788 is left with the byte sequence from the last complete
789 instruction fetched from memory? */
790 last_examine_value = value_at_lazy (val_type, next_address);
791
792 if (last_examine_value)
793 release_value (last_examine_value);
794
795 print_formatted (last_examine_value, format, size, gdb_stdout);
796
797 /* Display any branch delay slots following the final insn. */
798 if (format == 'i' && count == 1)
799 count += branch_delay_insns;
800 }
801 printf_filtered ("\n");
802 gdb_flush (gdb_stdout);
803 }
804 }
805 \f
806 static void
807 validate_format (struct format_data fmt, char *cmdname)
808 {
809 if (fmt.size != 0)
810 error (_("Size letters are meaningless in \"%s\" command."), cmdname);
811 if (fmt.count != 1)
812 error (_("Item count other than 1 is meaningless in \"%s\" command."),
813 cmdname);
814 if (fmt.format == 'i' || fmt.format == 's')
815 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
816 fmt.format, cmdname);
817 }
818
819 /* Evaluate string EXP as an expression in the current language and
820 print the resulting value. EXP may contain a format specifier as the
821 first argument ("/x myvar" for example, to print myvar in hex). */
822
823 static void
824 print_command_1 (char *exp, int inspect, int voidprint)
825 {
826 struct expression *expr;
827 struct cleanup *old_chain = 0;
828 char format = 0;
829 struct value *val;
830 struct format_data fmt;
831 int cleanup = 0;
832
833 /* Pass inspect flag to the rest of the print routines in a global
834 (sigh). */
835 inspect_it = inspect;
836
837 if (exp && *exp == '/')
838 {
839 exp++;
840 fmt = decode_format (&exp, last_format, 0);
841 validate_format (fmt, "print");
842 last_format = format = fmt.format;
843 }
844 else
845 {
846 fmt.count = 1;
847 fmt.format = 0;
848 fmt.size = 0;
849 }
850
851 if (exp && *exp)
852 {
853 struct type *type;
854 expr = parse_expression (exp);
855 old_chain = make_cleanup (free_current_contents, &expr);
856 cleanup = 1;
857 val = evaluate_expression (expr);
858 }
859 else
860 val = access_value_history (0);
861
862 if (voidprint || (val && value_type (val) &&
863 TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
864 {
865 int histindex = record_latest_value (val);
866
867 if (histindex >= 0)
868 annotate_value_history_begin (histindex, value_type (val));
869 else
870 annotate_value_begin (value_type (val));
871
872 if (inspect)
873 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"",
874 exp, histindex);
875 else if (histindex >= 0)
876 printf_filtered ("$%d = ", histindex);
877
878 if (histindex >= 0)
879 annotate_value_history_value ();
880
881 print_formatted (val, format, fmt.size, gdb_stdout);
882 printf_filtered ("\n");
883
884 if (histindex >= 0)
885 annotate_value_history_end ();
886 else
887 annotate_value_end ();
888
889 if (inspect)
890 printf_unfiltered ("\") )\030");
891 }
892
893 if (cleanup)
894 do_cleanups (old_chain);
895 inspect_it = 0; /* Reset print routines to normal. */
896 }
897
898 static void
899 print_command (char *exp, int from_tty)
900 {
901 print_command_1 (exp, 0, 1);
902 }
903
904 /* Same as print, except in epoch, it gets its own window. */
905 static void
906 inspect_command (char *exp, int from_tty)
907 {
908 extern int epoch_interface;
909
910 print_command_1 (exp, epoch_interface, 1);
911 }
912
913 /* Same as print, except it doesn't print void results. */
914 static void
915 call_command (char *exp, int from_tty)
916 {
917 print_command_1 (exp, 0, 0);
918 }
919
920 void
921 output_command (char *exp, int from_tty)
922 {
923 struct expression *expr;
924 struct cleanup *old_chain;
925 char format = 0;
926 struct value *val;
927 struct format_data fmt;
928
929 fmt.size = 0;
930
931 if (exp && *exp == '/')
932 {
933 exp++;
934 fmt = decode_format (&exp, 0, 0);
935 validate_format (fmt, "output");
936 format = fmt.format;
937 }
938
939 expr = parse_expression (exp);
940 old_chain = make_cleanup (free_current_contents, &expr);
941
942 val = evaluate_expression (expr);
943
944 annotate_value_begin (value_type (val));
945
946 print_formatted (val, format, fmt.size, gdb_stdout);
947
948 annotate_value_end ();
949
950 wrap_here ("");
951 gdb_flush (gdb_stdout);
952
953 do_cleanups (old_chain);
954 }
955
956 static void
957 set_command (char *exp, int from_tty)
958 {
959 struct expression *expr = parse_expression (exp);
960 struct cleanup *old_chain =
961 make_cleanup (free_current_contents, &expr);
962 evaluate_expression (expr);
963 do_cleanups (old_chain);
964 }
965
966 static void
967 sym_info (char *arg, int from_tty)
968 {
969 struct minimal_symbol *msymbol;
970 struct objfile *objfile;
971 struct obj_section *osect;
972 asection *sect;
973 CORE_ADDR addr, sect_addr;
974 int matches = 0;
975 unsigned int offset;
976
977 if (!arg)
978 error_no_arg (_("address"));
979
980 addr = parse_and_eval_address (arg);
981 ALL_OBJSECTIONS (objfile, osect)
982 {
983 /* Only process each object file once, even if there's a separate
984 debug file. */
985 if (objfile->separate_debug_objfile_backlink)
986 continue;
987
988 sect = osect->the_bfd_section;
989 sect_addr = overlay_mapped_address (addr, sect);
990
991 if (osect->addr <= sect_addr && sect_addr < osect->endaddr &&
992 (msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, sect)))
993 {
994 matches = 1;
995 offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
996 if (offset)
997 printf_filtered ("%s + %u in ",
998 SYMBOL_PRINT_NAME (msymbol), offset);
999 else
1000 printf_filtered ("%s in ",
1001 SYMBOL_PRINT_NAME (msymbol));
1002 if (pc_in_unmapped_range (addr, sect))
1003 printf_filtered (_("load address range of "));
1004 if (section_is_overlay (sect))
1005 printf_filtered (_("%s overlay "),
1006 section_is_mapped (sect) ? "mapped" : "unmapped");
1007 printf_filtered (_("section %s"), sect->name);
1008 printf_filtered ("\n");
1009 }
1010 }
1011 if (matches == 0)
1012 printf_filtered (_("No symbol matches %s.\n"), arg);
1013 }
1014
1015 static void
1016 address_info (char *exp, int from_tty)
1017 {
1018 struct symbol *sym;
1019 struct minimal_symbol *msymbol;
1020 long val;
1021 long basereg;
1022 asection *section;
1023 CORE_ADDR load_addr;
1024 int is_a_field_of_this; /* C++: lookup_symbol sets this to nonzero
1025 if exp is a field of `this'. */
1026
1027 if (exp == 0)
1028 error (_("Argument required."));
1029
1030 sym = lookup_symbol (exp, get_selected_block (0), VAR_DOMAIN,
1031 &is_a_field_of_this, (struct symtab **) NULL);
1032 if (sym == NULL)
1033 {
1034 if (is_a_field_of_this)
1035 {
1036 printf_filtered ("Symbol \"");
1037 fprintf_symbol_filtered (gdb_stdout, exp,
1038 current_language->la_language, DMGL_ANSI);
1039 printf_filtered ("\" is a field of the local class variable ");
1040 if (current_language->la_language == language_objc)
1041 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
1042 else
1043 printf_filtered ("`this'\n");
1044 return;
1045 }
1046
1047 msymbol = lookup_minimal_symbol (exp, NULL, NULL);
1048
1049 if (msymbol != NULL)
1050 {
1051 load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1052
1053 printf_filtered ("Symbol \"");
1054 fprintf_symbol_filtered (gdb_stdout, exp,
1055 current_language->la_language, DMGL_ANSI);
1056 printf_filtered ("\" is at ");
1057 deprecated_print_address_numeric (load_addr, 1, gdb_stdout);
1058 printf_filtered (" in a file compiled without debugging");
1059 section = SYMBOL_BFD_SECTION (msymbol);
1060 if (section_is_overlay (section))
1061 {
1062 load_addr = overlay_unmapped_address (load_addr, section);
1063 printf_filtered (",\n -- loaded at ");
1064 deprecated_print_address_numeric (load_addr, 1, gdb_stdout);
1065 printf_filtered (" in overlay section %s", section->name);
1066 }
1067 printf_filtered (".\n");
1068 }
1069 else
1070 error (_("No symbol \"%s\" in current context."), exp);
1071 return;
1072 }
1073
1074 printf_filtered ("Symbol \"");
1075 fprintf_symbol_filtered (gdb_stdout, DEPRECATED_SYMBOL_NAME (sym),
1076 current_language->la_language, DMGL_ANSI);
1077 printf_filtered ("\" is ");
1078 val = SYMBOL_VALUE (sym);
1079 basereg = SYMBOL_BASEREG (sym);
1080 section = SYMBOL_BFD_SECTION (sym);
1081
1082 switch (SYMBOL_CLASS (sym))
1083 {
1084 case LOC_CONST:
1085 case LOC_CONST_BYTES:
1086 printf_filtered ("constant");
1087 break;
1088
1089 case LOC_LABEL:
1090 printf_filtered ("a label at address ");
1091 deprecated_print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
1092 1, gdb_stdout);
1093 if (section_is_overlay (section))
1094 {
1095 load_addr = overlay_unmapped_address (load_addr, section);
1096 printf_filtered (",\n -- loaded at ");
1097 deprecated_print_address_numeric (load_addr, 1, gdb_stdout);
1098 printf_filtered (" in overlay section %s", section->name);
1099 }
1100 break;
1101
1102 case LOC_COMPUTED:
1103 case LOC_COMPUTED_ARG:
1104 /* FIXME: cagney/2004-01-26: It should be possible to
1105 unconditionally call the SYMBOL_OPS method when available.
1106 Unfortunately DWARF 2 stores the frame-base (instead of the
1107 function) location in a function's symbol. Oops! For the
1108 moment enable this when/where applicable. */
1109 SYMBOL_OPS (sym)->describe_location (sym, gdb_stdout);
1110 break;
1111
1112 case LOC_REGISTER:
1113 printf_filtered (_("a variable in register %s"),
1114 gdbarch_register_name (current_gdbarch, val));
1115 break;
1116
1117 case LOC_STATIC:
1118 printf_filtered (_("static storage at address "));
1119 deprecated_print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
1120 1, gdb_stdout);
1121 if (section_is_overlay (section))
1122 {
1123 load_addr = overlay_unmapped_address (load_addr, section);
1124 printf_filtered (_(",\n -- loaded at "));
1125 deprecated_print_address_numeric (load_addr, 1, gdb_stdout);
1126 printf_filtered (_(" in overlay section %s"), section->name);
1127 }
1128 break;
1129
1130 case LOC_INDIRECT:
1131 printf_filtered (_("external global (indirect addressing), at address *("));
1132 deprecated_print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
1133 1, gdb_stdout);
1134 printf_filtered (")");
1135 if (section_is_overlay (section))
1136 {
1137 load_addr = overlay_unmapped_address (load_addr, section);
1138 printf_filtered (_(",\n -- loaded at "));
1139 deprecated_print_address_numeric (load_addr, 1, gdb_stdout);
1140 printf_filtered (_(" in overlay section %s"), section->name);
1141 }
1142 break;
1143
1144 case LOC_REGPARM:
1145 printf_filtered (_("an argument in register %s"),
1146 gdbarch_register_name (current_gdbarch, val));
1147 break;
1148
1149 case LOC_REGPARM_ADDR:
1150 printf_filtered (_("address of an argument in register %s"),
1151 gdbarch_register_name (current_gdbarch, val));
1152 break;
1153
1154 case LOC_ARG:
1155 printf_filtered (_("an argument at offset %ld"), val);
1156 break;
1157
1158 case LOC_LOCAL_ARG:
1159 printf_filtered (_("an argument at frame offset %ld"), val);
1160 break;
1161
1162 case LOC_LOCAL:
1163 printf_filtered (_("a local variable at frame offset %ld"), val);
1164 break;
1165
1166 case LOC_REF_ARG:
1167 printf_filtered (_("a reference argument at offset %ld"), val);
1168 break;
1169
1170 case LOC_BASEREG:
1171 printf_filtered (_("a variable at offset %ld from register %s"),
1172 val, gdbarch_register_name (current_gdbarch, basereg));
1173 break;
1174
1175 case LOC_BASEREG_ARG:
1176 printf_filtered (_("an argument at offset %ld from register %s"),
1177 val, gdbarch_register_name (current_gdbarch, basereg));
1178 break;
1179
1180 case LOC_TYPEDEF:
1181 printf_filtered (_("a typedef"));
1182 break;
1183
1184 case LOC_BLOCK:
1185 printf_filtered (_("a function at address "));
1186 load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1187 deprecated_print_address_numeric (load_addr, 1, gdb_stdout);
1188 if (section_is_overlay (section))
1189 {
1190 load_addr = overlay_unmapped_address (load_addr, section);
1191 printf_filtered (_(",\n -- loaded at "));
1192 deprecated_print_address_numeric (load_addr, 1, gdb_stdout);
1193 printf_filtered (_(" in overlay section %s"), section->name);
1194 }
1195 break;
1196
1197 case LOC_UNRESOLVED:
1198 {
1199 struct minimal_symbol *msym;
1200
1201 msym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (sym), NULL, NULL);
1202 if (msym == NULL)
1203 printf_filtered ("unresolved");
1204 else
1205 {
1206 section = SYMBOL_BFD_SECTION (msym);
1207 printf_filtered (_("static storage at address "));
1208 load_addr = SYMBOL_VALUE_ADDRESS (msym);
1209 deprecated_print_address_numeric (load_addr, 1, gdb_stdout);
1210 if (section_is_overlay (section))
1211 {
1212 load_addr = overlay_unmapped_address (load_addr, section);
1213 printf_filtered (_(",\n -- loaded at "));
1214 deprecated_print_address_numeric (load_addr, 1, gdb_stdout);
1215 printf_filtered (_(" in overlay section %s"), section->name);
1216 }
1217 }
1218 }
1219 break;
1220
1221 case LOC_HP_THREAD_LOCAL_STATIC:
1222 printf_filtered (_("\
1223 a thread-local variable at offset %ld from the thread base register %s"),
1224 val, gdbarch_register_name (current_gdbarch, basereg));
1225 break;
1226
1227 case LOC_OPTIMIZED_OUT:
1228 printf_filtered (_("optimized out"));
1229 break;
1230
1231 default:
1232 printf_filtered (_("of unknown (botched) type"));
1233 break;
1234 }
1235 printf_filtered (".\n");
1236 }
1237 \f
1238
1239 static void
1240 x_command (char *exp, int from_tty)
1241 {
1242 struct expression *expr;
1243 struct format_data fmt;
1244 struct cleanup *old_chain;
1245 struct value *val;
1246
1247 fmt.format = last_format;
1248 fmt.size = last_size;
1249 fmt.count = 1;
1250
1251 if (exp && *exp == '/')
1252 {
1253 exp++;
1254 fmt = decode_format (&exp, last_format, last_size);
1255 }
1256
1257 /* If we have an expression, evaluate it and use it as the address. */
1258
1259 if (exp != 0 && *exp != 0)
1260 {
1261 expr = parse_expression (exp);
1262 /* Cause expression not to be there any more if this command is
1263 repeated with Newline. But don't clobber a user-defined
1264 command's definition. */
1265 if (from_tty)
1266 *exp = 0;
1267 old_chain = make_cleanup (free_current_contents, &expr);
1268 val = evaluate_expression (expr);
1269 if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
1270 val = value_ind (val);
1271 /* In rvalue contexts, such as this, functions are coerced into
1272 pointers to functions. This makes "x/i main" work. */
1273 if (/* last_format == 'i' && */
1274 TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
1275 && VALUE_LVAL (val) == lval_memory)
1276 next_address = VALUE_ADDRESS (val);
1277 else
1278 next_address = value_as_address (val);
1279 do_cleanups (old_chain);
1280 }
1281
1282 do_examine (fmt, next_address);
1283
1284 /* If the examine succeeds, we remember its size and format for next
1285 time. */
1286 last_size = fmt.size;
1287 last_format = fmt.format;
1288
1289 /* Set a couple of internal variables if appropriate. */
1290 if (last_examine_value)
1291 {
1292 /* Make last address examined available to the user as $_. Use
1293 the correct pointer type. */
1294 struct type *pointer_type
1295 = lookup_pointer_type (value_type (last_examine_value));
1296 set_internalvar (lookup_internalvar ("_"),
1297 value_from_pointer (pointer_type,
1298 last_examine_address));
1299
1300 /* Make contents of last address examined available to the user
1301 as $__. If the last value has not been fetched from memory
1302 then don't fetch it now; instead mark it by voiding the $__
1303 variable. */
1304 if (value_lazy (last_examine_value))
1305 set_internalvar (lookup_internalvar ("__"),
1306 allocate_value (builtin_type_void));
1307 else
1308 set_internalvar (lookup_internalvar ("__"), last_examine_value);
1309 }
1310 }
1311 \f
1312
1313 /* Add an expression to the auto-display chain.
1314 Specify the expression. */
1315
1316 static void
1317 display_command (char *exp, int from_tty)
1318 {
1319 struct format_data fmt;
1320 struct expression *expr;
1321 struct display *new;
1322 int display_it = 1;
1323
1324 #if defined(TUI)
1325 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1326 `tui_version'. */
1327 if (tui_active && exp != NULL && *exp == '$')
1328 display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
1329 #endif
1330
1331 if (display_it)
1332 {
1333 if (exp == 0)
1334 {
1335 do_displays ();
1336 return;
1337 }
1338
1339 if (*exp == '/')
1340 {
1341 exp++;
1342 fmt = decode_format (&exp, 0, 0);
1343 if (fmt.size && fmt.format == 0)
1344 fmt.format = 'x';
1345 if (fmt.format == 'i' || fmt.format == 's')
1346 fmt.size = 'b';
1347 }
1348 else
1349 {
1350 fmt.format = 0;
1351 fmt.size = 0;
1352 fmt.count = 0;
1353 }
1354
1355 innermost_block = 0;
1356 expr = parse_expression (exp);
1357
1358 new = (struct display *) xmalloc (sizeof (struct display));
1359
1360 new->exp = expr;
1361 new->block = innermost_block;
1362 new->next = display_chain;
1363 new->number = ++display_number;
1364 new->format = fmt;
1365 new->enabled_p = 1;
1366 display_chain = new;
1367
1368 if (from_tty && target_has_execution)
1369 do_one_display (new);
1370
1371 dont_repeat ();
1372 }
1373 }
1374
1375 static void
1376 free_display (struct display *d)
1377 {
1378 xfree (d->exp);
1379 xfree (d);
1380 }
1381
1382 /* Clear out the display_chain. Done when new symtabs are loaded,
1383 since this invalidates the types stored in many expressions. */
1384
1385 void
1386 clear_displays (void)
1387 {
1388 struct display *d;
1389
1390 while ((d = display_chain) != NULL)
1391 {
1392 xfree (d->exp);
1393 display_chain = d->next;
1394 xfree (d);
1395 }
1396 }
1397
1398 /* Delete the auto-display number NUM. */
1399
1400 static void
1401 delete_display (int num)
1402 {
1403 struct display *d1, *d;
1404
1405 if (!display_chain)
1406 error (_("No display number %d."), num);
1407
1408 if (display_chain->number == num)
1409 {
1410 d1 = display_chain;
1411 display_chain = d1->next;
1412 free_display (d1);
1413 }
1414 else
1415 for (d = display_chain;; d = d->next)
1416 {
1417 if (d->next == 0)
1418 error (_("No display number %d."), num);
1419 if (d->next->number == num)
1420 {
1421 d1 = d->next;
1422 d->next = d1->next;
1423 free_display (d1);
1424 break;
1425 }
1426 }
1427 }
1428
1429 /* Delete some values from the auto-display chain.
1430 Specify the element numbers. */
1431
1432 static void
1433 undisplay_command (char *args, int from_tty)
1434 {
1435 char *p = args;
1436 char *p1;
1437 int num;
1438
1439 if (args == 0)
1440 {
1441 if (query ("Delete all auto-display expressions? "))
1442 clear_displays ();
1443 dont_repeat ();
1444 return;
1445 }
1446
1447 while (*p)
1448 {
1449 p1 = p;
1450 while (*p1 >= '0' && *p1 <= '9')
1451 p1++;
1452 if (*p1 && *p1 != ' ' && *p1 != '\t')
1453 error (_("Arguments must be display numbers."));
1454
1455 num = atoi (p);
1456
1457 delete_display (num);
1458
1459 p = p1;
1460 while (*p == ' ' || *p == '\t')
1461 p++;
1462 }
1463 dont_repeat ();
1464 }
1465
1466 /* Display a single auto-display.
1467 Do nothing if the display cannot be printed in the current context,
1468 or if the display is disabled. */
1469
1470 static void
1471 do_one_display (struct display *d)
1472 {
1473 int within_current_scope;
1474
1475 if (d->enabled_p == 0)
1476 return;
1477
1478 if (d->block)
1479 within_current_scope = contained_in (get_selected_block (0), d->block);
1480 else
1481 within_current_scope = 1;
1482 if (!within_current_scope)
1483 return;
1484
1485 current_display_number = d->number;
1486
1487 annotate_display_begin ();
1488 printf_filtered ("%d", d->number);
1489 annotate_display_number_end ();
1490 printf_filtered (": ");
1491 if (d->format.size)
1492 {
1493 CORE_ADDR addr;
1494 struct value *val;
1495
1496 annotate_display_format ();
1497
1498 printf_filtered ("x/");
1499 if (d->format.count != 1)
1500 printf_filtered ("%d", d->format.count);
1501 printf_filtered ("%c", d->format.format);
1502 if (d->format.format != 'i' && d->format.format != 's')
1503 printf_filtered ("%c", d->format.size);
1504 printf_filtered (" ");
1505
1506 annotate_display_expression ();
1507
1508 print_expression (d->exp, gdb_stdout);
1509 annotate_display_expression_end ();
1510
1511 if (d->format.count != 1 || d->format.format == 'i')
1512 printf_filtered ("\n");
1513 else
1514 printf_filtered (" ");
1515
1516 val = evaluate_expression (d->exp);
1517 addr = value_as_address (val);
1518 if (d->format.format == 'i')
1519 addr = gdbarch_addr_bits_remove (current_gdbarch, addr);
1520
1521 annotate_display_value ();
1522
1523 do_examine (d->format, addr);
1524 }
1525 else
1526 {
1527 annotate_display_format ();
1528
1529 if (d->format.format)
1530 printf_filtered ("/%c ", d->format.format);
1531
1532 annotate_display_expression ();
1533
1534 print_expression (d->exp, gdb_stdout);
1535 annotate_display_expression_end ();
1536
1537 printf_filtered (" = ");
1538
1539 annotate_display_expression ();
1540
1541 print_formatted (evaluate_expression (d->exp),
1542 d->format.format, d->format.size, gdb_stdout);
1543 printf_filtered ("\n");
1544 }
1545
1546 annotate_display_end ();
1547
1548 gdb_flush (gdb_stdout);
1549 current_display_number = -1;
1550 }
1551
1552 /* Display all of the values on the auto-display chain which can be
1553 evaluated in the current scope. */
1554
1555 void
1556 do_displays (void)
1557 {
1558 struct display *d;
1559
1560 for (d = display_chain; d; d = d->next)
1561 do_one_display (d);
1562 }
1563
1564 /* Delete the auto-display which we were in the process of displaying.
1565 This is done when there is an error or a signal. */
1566
1567 void
1568 disable_display (int num)
1569 {
1570 struct display *d;
1571
1572 for (d = display_chain; d; d = d->next)
1573 if (d->number == num)
1574 {
1575 d->enabled_p = 0;
1576 return;
1577 }
1578 printf_unfiltered (_("No display number %d.\n"), num);
1579 }
1580
1581 void
1582 disable_current_display (void)
1583 {
1584 if (current_display_number >= 0)
1585 {
1586 disable_display (current_display_number);
1587 fprintf_unfiltered (gdb_stderr, _("\
1588 Disabling display %d to avoid infinite recursion.\n"),
1589 current_display_number);
1590 }
1591 current_display_number = -1;
1592 }
1593
1594 static void
1595 display_info (char *ignore, int from_tty)
1596 {
1597 struct display *d;
1598
1599 if (!display_chain)
1600 printf_unfiltered (_("There are no auto-display expressions now.\n"));
1601 else
1602 printf_filtered (_("Auto-display expressions now in effect:\n\
1603 Num Enb Expression\n"));
1604
1605 for (d = display_chain; d; d = d->next)
1606 {
1607 printf_filtered ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
1608 if (d->format.size)
1609 printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
1610 d->format.format);
1611 else if (d->format.format)
1612 printf_filtered ("/%c ", d->format.format);
1613 print_expression (d->exp, gdb_stdout);
1614 if (d->block && !contained_in (get_selected_block (0), d->block))
1615 printf_filtered (_(" (cannot be evaluated in the current context)"));
1616 printf_filtered ("\n");
1617 gdb_flush (gdb_stdout);
1618 }
1619 }
1620
1621 static void
1622 enable_display (char *args, int from_tty)
1623 {
1624 char *p = args;
1625 char *p1;
1626 int num;
1627 struct display *d;
1628
1629 if (p == 0)
1630 {
1631 for (d = display_chain; d; d = d->next)
1632 d->enabled_p = 1;
1633 }
1634 else
1635 while (*p)
1636 {
1637 p1 = p;
1638 while (*p1 >= '0' && *p1 <= '9')
1639 p1++;
1640 if (*p1 && *p1 != ' ' && *p1 != '\t')
1641 error (_("Arguments must be display numbers."));
1642
1643 num = atoi (p);
1644
1645 for (d = display_chain; d; d = d->next)
1646 if (d->number == num)
1647 {
1648 d->enabled_p = 1;
1649 goto win;
1650 }
1651 printf_unfiltered (_("No display number %d.\n"), num);
1652 win:
1653 p = p1;
1654 while (*p == ' ' || *p == '\t')
1655 p++;
1656 }
1657 }
1658
1659 static void
1660 disable_display_command (char *args, int from_tty)
1661 {
1662 char *p = args;
1663 char *p1;
1664 struct display *d;
1665
1666 if (p == 0)
1667 {
1668 for (d = display_chain; d; d = d->next)
1669 d->enabled_p = 0;
1670 }
1671 else
1672 while (*p)
1673 {
1674 p1 = p;
1675 while (*p1 >= '0' && *p1 <= '9')
1676 p1++;
1677 if (*p1 && *p1 != ' ' && *p1 != '\t')
1678 error (_("Arguments must be display numbers."));
1679
1680 disable_display (atoi (p));
1681
1682 p = p1;
1683 while (*p == ' ' || *p == '\t')
1684 p++;
1685 }
1686 }
1687 \f
1688
1689 /* Print the value in stack frame FRAME of a variable specified by a
1690 struct symbol. */
1691
1692 void
1693 print_variable_value (struct symbol *var, struct frame_info *frame,
1694 struct ui_file *stream)
1695 {
1696 struct value *val = read_var_value (var, frame);
1697
1698 value_print (val, stream, 0, Val_pretty_default);
1699 }
1700
1701 static void
1702 printf_command (char *arg, int from_tty)
1703 {
1704 char *f = NULL;
1705 char *s = arg;
1706 char *string = NULL;
1707 struct value **val_args;
1708 char *substrings;
1709 char *current_substring;
1710 int nargs = 0;
1711 int allocated_args = 20;
1712 struct cleanup *old_cleanups;
1713
1714 val_args = xmalloc (allocated_args * sizeof (struct value *));
1715 old_cleanups = make_cleanup (free_current_contents, &val_args);
1716
1717 if (s == 0)
1718 error_no_arg (_("format-control string and values to print"));
1719
1720 /* Skip white space before format string */
1721 while (*s == ' ' || *s == '\t')
1722 s++;
1723
1724 /* A format string should follow, enveloped in double quotes. */
1725 if (*s++ != '"')
1726 error (_("Bad format string, missing '\"'."));
1727
1728 /* Parse the format-control string and copy it into the string STRING,
1729 processing some kinds of escape sequence. */
1730
1731 f = string = (char *) alloca (strlen (s) + 1);
1732
1733 while (*s != '"')
1734 {
1735 int c = *s++;
1736 switch (c)
1737 {
1738 case '\0':
1739 error (_("Bad format string, non-terminated '\"'."));
1740
1741 case '\\':
1742 switch (c = *s++)
1743 {
1744 case '\\':
1745 *f++ = '\\';
1746 break;
1747 case 'a':
1748 *f++ = '\a';
1749 break;
1750 case 'b':
1751 *f++ = '\b';
1752 break;
1753 case 'f':
1754 *f++ = '\f';
1755 break;
1756 case 'n':
1757 *f++ = '\n';
1758 break;
1759 case 'r':
1760 *f++ = '\r';
1761 break;
1762 case 't':
1763 *f++ = '\t';
1764 break;
1765 case 'v':
1766 *f++ = '\v';
1767 break;
1768 case '"':
1769 *f++ = '"';
1770 break;
1771 default:
1772 /* ??? TODO: handle other escape sequences */
1773 error (_("Unrecognized escape character \\%c in format string."),
1774 c);
1775 }
1776 break;
1777
1778 default:
1779 *f++ = c;
1780 }
1781 }
1782
1783 /* Skip over " and following space and comma. */
1784 s++;
1785 *f++ = '\0';
1786 while (*s == ' ' || *s == '\t')
1787 s++;
1788
1789 if (*s != ',' && *s != 0)
1790 error (_("Invalid argument syntax"));
1791
1792 if (*s == ',')
1793 s++;
1794 while (*s == ' ' || *s == '\t')
1795 s++;
1796
1797 /* Need extra space for the '\0's. Doubling the size is sufficient. */
1798 substrings = alloca (strlen (string) * 2);
1799 current_substring = substrings;
1800
1801 {
1802 /* Now scan the string for %-specs and see what kinds of args they want.
1803 argclass[I] classifies the %-specs so we can give printf_filtered
1804 something of the right size. */
1805
1806 enum argclass
1807 {
1808 int_arg, long_arg, long_long_arg, ptr_arg, string_arg,
1809 double_arg, long_double_arg
1810 };
1811 enum argclass *argclass;
1812 enum argclass this_argclass;
1813 char *last_arg;
1814 int nargs_wanted;
1815 int i;
1816
1817 argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
1818 nargs_wanted = 0;
1819 f = string;
1820 last_arg = string;
1821 while (*f)
1822 if (*f++ == '%')
1823 {
1824 int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
1825 int seen_space = 0, seen_plus = 0;
1826 int seen_big_l = 0, seen_h = 0;
1827 int bad = 0;
1828
1829 /* Check the validity of the format specifier, and work
1830 out what argument it expects. We only accept C89
1831 format strings, with the exception of long long (which
1832 we autoconf for). */
1833
1834 /* Skip over "%%". */
1835 if (*f == '%')
1836 {
1837 f++;
1838 continue;
1839 }
1840
1841 /* The first part of a format specifier is a set of flag
1842 characters. */
1843 while (strchr ("0-+ #", *f))
1844 {
1845 if (*f == '#')
1846 seen_hash = 1;
1847 else if (*f == '0')
1848 seen_zero = 1;
1849 else if (*f == ' ')
1850 seen_space = 1;
1851 else if (*f == '+')
1852 seen_plus = 1;
1853 f++;
1854 }
1855
1856 /* The next part of a format specifier is a width. */
1857 while (strchr ("0123456789", *f))
1858 f++;
1859
1860 /* The next part of a format specifier is a precision. */
1861 if (*f == '.')
1862 {
1863 seen_prec = 1;
1864 f++;
1865 while (strchr ("0123456789", *f))
1866 f++;
1867 }
1868
1869 /* The next part of a format specifier is a length modifier. */
1870 if (*f == 'h')
1871 {
1872 seen_h = 1;
1873 f++;
1874 }
1875 else if (*f == 'l')
1876 {
1877 f++;
1878 lcount++;
1879 if (*f == 'l')
1880 {
1881 f++;
1882 lcount++;
1883 }
1884 }
1885 else if (*f == 'L')
1886 {
1887 seen_big_l = 1;
1888 f++;
1889 }
1890
1891 switch (*f)
1892 {
1893 case 'u':
1894 if (seen_hash)
1895 bad = 1;
1896 /* FALLTHROUGH */
1897
1898 case 'o':
1899 case 'x':
1900 case 'X':
1901 if (seen_space || seen_plus)
1902 bad = 1;
1903 /* FALLTHROUGH */
1904
1905 case 'd':
1906 case 'i':
1907 if (lcount == 0)
1908 this_argclass = int_arg;
1909 else if (lcount == 1)
1910 this_argclass = long_arg;
1911 else
1912 this_argclass = long_long_arg;
1913
1914 if (seen_big_l)
1915 bad = 1;
1916 break;
1917
1918 case 'c':
1919 this_argclass = int_arg;
1920 if (lcount || seen_h || seen_big_l)
1921 bad = 1;
1922 if (seen_prec || seen_zero || seen_space || seen_plus)
1923 bad = 1;
1924 break;
1925
1926 case 'p':
1927 this_argclass = ptr_arg;
1928 if (lcount || seen_h || seen_big_l)
1929 bad = 1;
1930 if (seen_prec || seen_zero || seen_space || seen_plus)
1931 bad = 1;
1932 break;
1933
1934 case 's':
1935 this_argclass = string_arg;
1936 if (lcount || seen_h || seen_big_l)
1937 bad = 1;
1938 if (seen_zero || seen_space || seen_plus)
1939 bad = 1;
1940 break;
1941
1942 case 'e':
1943 case 'f':
1944 case 'g':
1945 case 'E':
1946 case 'G':
1947 if (seen_big_l)
1948 this_argclass = long_double_arg;
1949 else
1950 this_argclass = double_arg;
1951
1952 if (lcount || seen_h)
1953 bad = 1;
1954 break;
1955
1956 case '*':
1957 error (_("`*' not supported for precision or width in printf"));
1958
1959 case 'n':
1960 error (_("Format specifier `n' not supported in printf"));
1961
1962 case '\0':
1963 error (_("Incomplete format specifier at end of format string"));
1964
1965 default:
1966 error (_("Unrecognized format specifier '%c' in printf"), *f);
1967 }
1968
1969 if (bad)
1970 error (_("Inappropriate modifiers to format specifier '%c' in printf"),
1971 *f);
1972
1973 f++;
1974 strncpy (current_substring, last_arg, f - last_arg);
1975 current_substring += f - last_arg;
1976 *current_substring++ = '\0';
1977 last_arg = f;
1978 argclass[nargs_wanted++] = this_argclass;
1979 }
1980
1981 /* Now, parse all arguments and evaluate them.
1982 Store the VALUEs in VAL_ARGS. */
1983
1984 while (*s != '\0')
1985 {
1986 char *s1;
1987 if (nargs == allocated_args)
1988 val_args = (struct value **) xrealloc ((char *) val_args,
1989 (allocated_args *= 2)
1990 * sizeof (struct value *));
1991 s1 = s;
1992 val_args[nargs] = parse_to_comma_and_eval (&s1);
1993
1994 /* If format string wants a float, unchecked-convert the value to
1995 floating point of the same size */
1996
1997 if (argclass[nargs] == double_arg)
1998 {
1999 struct type *type = value_type (val_args[nargs]);
2000 if (TYPE_LENGTH (type) == sizeof (float))
2001 deprecated_set_value_type (val_args[nargs], builtin_type_float);
2002 if (TYPE_LENGTH (type) == sizeof (double))
2003 deprecated_set_value_type (val_args[nargs], builtin_type_double);
2004 }
2005 nargs++;
2006 s = s1;
2007 if (*s == ',')
2008 s++;
2009 }
2010
2011 if (nargs != nargs_wanted)
2012 error (_("Wrong number of arguments for specified format-string"));
2013
2014 /* Now actually print them. */
2015 current_substring = substrings;
2016 for (i = 0; i < nargs; i++)
2017 {
2018 switch (argclass[i])
2019 {
2020 case string_arg:
2021 {
2022 gdb_byte *str;
2023 CORE_ADDR tem;
2024 int j;
2025 tem = value_as_address (val_args[i]);
2026
2027 /* This is a %s argument. Find the length of the string. */
2028 for (j = 0;; j++)
2029 {
2030 gdb_byte c;
2031 QUIT;
2032 read_memory (tem + j, &c, 1);
2033 if (c == 0)
2034 break;
2035 }
2036
2037 /* Copy the string contents into a string inside GDB. */
2038 str = (gdb_byte *) alloca (j + 1);
2039 if (j != 0)
2040 read_memory (tem, str, j);
2041 str[j] = 0;
2042
2043 printf_filtered (current_substring, (char *) str);
2044 }
2045 break;
2046 case double_arg:
2047 {
2048 double val = value_as_double (val_args[i]);
2049 printf_filtered (current_substring, val);
2050 break;
2051 }
2052 case long_double_arg:
2053 #ifdef HAVE_LONG_DOUBLE
2054 {
2055 long double val = value_as_double (val_args[i]);
2056 printf_filtered (current_substring, val);
2057 break;
2058 }
2059 #else
2060 error (_("long double not supported in printf"));
2061 #endif
2062 case long_long_arg:
2063 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2064 {
2065 long long val = value_as_long (val_args[i]);
2066 printf_filtered (current_substring, val);
2067 break;
2068 }
2069 #else
2070 error (_("long long not supported in printf"));
2071 #endif
2072 case int_arg:
2073 {
2074 int val = value_as_long (val_args[i]);
2075 printf_filtered (current_substring, val);
2076 break;
2077 }
2078 case long_arg:
2079 {
2080 long val = value_as_long (val_args[i]);
2081 printf_filtered (current_substring, val);
2082 break;
2083 }
2084 default:
2085 internal_error (__FILE__, __LINE__,
2086 _("failed internal consitency check"));
2087 }
2088 /* Skip to the next substring. */
2089 current_substring += strlen (current_substring) + 1;
2090 }
2091 /* Print the portion of the format string after the last argument. */
2092 puts_filtered (last_arg);
2093 }
2094 do_cleanups (old_cleanups);
2095 }
2096
2097 void
2098 _initialize_printcmd (void)
2099 {
2100 struct cmd_list_element *c;
2101
2102 current_display_number = -1;
2103
2104 add_info ("address", address_info,
2105 _("Describe where symbol SYM is stored."));
2106
2107 add_info ("symbol", sym_info, _("\
2108 Describe what symbol is at location ADDR.\n\
2109 Only for symbols with fixed locations (global or static scope)."));
2110
2111 add_com ("x", class_vars, x_command, _("\
2112 Examine memory: x/FMT ADDRESS.\n\
2113 ADDRESS is an expression for the memory address to examine.\n\
2114 FMT is a repeat count followed by a format letter and a size letter.\n\
2115 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2116 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2117 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2118 The specified number of objects of the specified size are printed\n\
2119 according to the format.\n\n\
2120 Defaults for format and size letters are those previously used.\n\
2121 Default count is 1. Default address is following last thing printed\n\
2122 with this command or \"print\"."));
2123
2124 #if 0
2125 add_com ("whereis", class_vars, whereis_command,
2126 _("Print line number and file of definition of variable."));
2127 #endif
2128
2129 add_info ("display", display_info, _("\
2130 Expressions to display when program stops, with code numbers."));
2131
2132 add_cmd ("undisplay", class_vars, undisplay_command, _("\
2133 Cancel some expressions to be displayed when program stops.\n\
2134 Arguments are the code numbers of the expressions to stop displaying.\n\
2135 No argument means cancel all automatic-display expressions.\n\
2136 \"delete display\" has the same effect as this command.\n\
2137 Do \"info display\" to see current list of code numbers."),
2138 &cmdlist);
2139
2140 add_com ("display", class_vars, display_command, _("\
2141 Print value of expression EXP each time the program stops.\n\
2142 /FMT may be used before EXP as in the \"print\" command.\n\
2143 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2144 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2145 and examining is done as in the \"x\" command.\n\n\
2146 With no argument, display all currently requested auto-display expressions.\n\
2147 Use \"undisplay\" to cancel display requests previously made."));
2148
2149 add_cmd ("display", class_vars, enable_display, _("\
2150 Enable some expressions to be displayed when program stops.\n\
2151 Arguments are the code numbers of the expressions to resume displaying.\n\
2152 No argument means enable all automatic-display expressions.\n\
2153 Do \"info display\" to see current list of code numbers."), &enablelist);
2154
2155 add_cmd ("display", class_vars, disable_display_command, _("\
2156 Disable some expressions to be displayed when program stops.\n\
2157 Arguments are the code numbers of the expressions to stop displaying.\n\
2158 No argument means disable all automatic-display expressions.\n\
2159 Do \"info display\" to see current list of code numbers."), &disablelist);
2160
2161 add_cmd ("display", class_vars, undisplay_command, _("\
2162 Cancel some expressions to be displayed when program stops.\n\
2163 Arguments are the code numbers of the expressions to stop displaying.\n\
2164 No argument means cancel all automatic-display expressions.\n\
2165 Do \"info display\" to see current list of code numbers."), &deletelist);
2166
2167 add_com ("printf", class_vars, printf_command, _("\
2168 printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2169 This is useful for formatted output in user-defined commands."));
2170
2171 add_com ("output", class_vars, output_command, _("\
2172 Like \"print\" but don't put in value history and don't print newline.\n\
2173 This is useful in user-defined commands."));
2174
2175 add_prefix_cmd ("set", class_vars, set_command, _("\
2176 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2177 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2178 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2179 with $), a register (a few standard names starting with $), or an actual\n\
2180 variable in the program being debugged. EXP is any valid expression.\n\
2181 Use \"set variable\" for variables with names identical to set subcommands.\n\
2182 \n\
2183 With a subcommand, this command modifies parts of the gdb environment.\n\
2184 You can see these environment settings with the \"show\" command."),
2185 &setlist, "set ", 1, &cmdlist);
2186 if (dbx_commands)
2187 add_com ("assign", class_vars, set_command, _("\
2188 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2189 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2190 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2191 with $), a register (a few standard names starting with $), or an actual\n\
2192 variable in the program being debugged. EXP is any valid expression.\n\
2193 Use \"set variable\" for variables with names identical to set subcommands.\n\
2194 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2195 You can see these environment settings with the \"show\" command."));
2196
2197 /* "call" is the same as "set", but handy for dbx users to call fns. */
2198 c = add_com ("call", class_vars, call_command, _("\
2199 Call a function in the program.\n\
2200 The argument is the function name and arguments, in the notation of the\n\
2201 current working language. The result is printed and saved in the value\n\
2202 history, if it is not void."));
2203 set_cmd_completer (c, location_completer);
2204
2205 add_cmd ("variable", class_vars, set_command, _("\
2206 Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2207 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2208 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2209 with $), a register (a few standard names starting with $), or an actual\n\
2210 variable in the program being debugged. EXP is any valid expression.\n\
2211 This may usually be abbreviated to simply \"set\"."),
2212 &setlist);
2213
2214 c = add_com ("print", class_vars, print_command, _("\
2215 Print value of expression EXP.\n\
2216 Variables accessible are those of the lexical environment of the selected\n\
2217 stack frame, plus all those whose scope is global or an entire file.\n\
2218 \n\
2219 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2220 $$NUM refers to NUM'th value back from the last one.\n\
2221 Names starting with $ refer to registers (with the values they would have\n\
2222 if the program were to return to the stack frame now selected, restoring\n\
2223 all registers saved by frames farther in) or else to debugger\n\
2224 \"convenience\" variables (any such name not a known register).\n\
2225 Use assignment expressions to give values to convenience variables.\n\
2226 \n\
2227 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2228 @ is a binary operator for treating consecutive data objects\n\
2229 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2230 element is FOO, whose second element is stored in the space following\n\
2231 where FOO is stored, etc. FOO must be an expression whose value\n\
2232 resides in memory.\n\
2233 \n\
2234 EXP may be preceded with /FMT, where FMT is a format letter\n\
2235 but no count or size letter (see \"x\" command)."));
2236 set_cmd_completer (c, location_completer);
2237 add_com_alias ("p", "print", class_vars, 1);
2238
2239 c = add_com ("inspect", class_vars, inspect_command, _("\
2240 Same as \"print\" command, except that if you are running in the epoch\n\
2241 environment, the value is printed in its own window."));
2242 set_cmd_completer (c, location_completer);
2243
2244 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
2245 &max_symbolic_offset, _("\
2246 Set the largest offset that will be printed in <symbol+1234> form."), _("\
2247 Show the largest offset that will be printed in <symbol+1234> form."), NULL,
2248 NULL,
2249 show_max_symbolic_offset,
2250 &setprintlist, &showprintlist);
2251 add_setshow_boolean_cmd ("symbol-filename", no_class,
2252 &print_symbol_filename, _("\
2253 Set printing of source filename and line number with <symbol>."), _("\
2254 Show printing of source filename and line number with <symbol>."), NULL,
2255 NULL,
2256 show_print_symbol_filename,
2257 &setprintlist, &showprintlist);
2258
2259 /* For examine/instruction a single byte quantity is specified as
2260 the data. This avoids problems with value_at_lazy() requiring a
2261 valid data type (and rejecting VOID). */
2262 examine_i_type = init_type (TYPE_CODE_INT, 1, 0, "examine_i_type", NULL);
2263
2264 examine_b_type = init_type (TYPE_CODE_INT, 1, 0, "examine_b_type", NULL);
2265 examine_h_type = init_type (TYPE_CODE_INT, 2, 0, "examine_h_type", NULL);
2266 examine_w_type = init_type (TYPE_CODE_INT, 4, 0, "examine_w_type", NULL);
2267 examine_g_type = init_type (TYPE_CODE_INT, 8, 0, "examine_g_type", NULL);
2268
2269 }
This page took 0.077603 seconds and 4 git commands to generate.