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