]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/printcmd.c
* printcmd.c (printf_command): Guard against 0-length string.
[thirdparty/binutils-gdb.git] / gdb / printcmd.c
1 /* Print values for GNU debugger GDB.
2
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1993, 1994, 1995,
4 1998, 2000 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include "frame.h"
26 #include "symtab.h"
27 #include "gdbtypes.h"
28 #include "value.h"
29 #include "language.h"
30 #include "expression.h"
31 #include "gdbcore.h"
32 #include "gdbcmd.h"
33 #include "target.h"
34 #include "breakpoint.h"
35 #include "demangle.h"
36 #include "valprint.h"
37 #include "annotate.h"
38 #include "symfile.h" /* for overlay functions */
39 #include "objfiles.h" /* ditto */
40 #ifdef UI_OUT
41 #include "ui-out.h"
42 #endif
43
44 extern int asm_demangle; /* Whether to demangle syms in asm printouts */
45 extern int addressprint; /* Whether to print hex addresses in HLL " */
46
47 struct format_data
48 {
49 int count;
50 char format;
51 char size;
52 };
53
54 /* Last specified output format. */
55
56 static char last_format = 'x';
57
58 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
59
60 static char last_size = 'w';
61
62 /* Default address to examine next. */
63
64 static CORE_ADDR next_address;
65
66 /* Default section to examine next. */
67
68 static asection *next_section;
69
70 /* Last address examined. */
71
72 static CORE_ADDR last_examine_address;
73
74 /* Contents of last address examined.
75 This is not valid past the end of the `x' command! */
76
77 static value_ptr last_examine_value;
78
79 /* Largest offset between a symbolic value and an address, that will be
80 printed as `0x1234 <symbol+offset>'. */
81
82 static unsigned int max_symbolic_offset = UINT_MAX;
83
84 /* Append the source filename and linenumber of the symbol when
85 printing a symbolic value as `<symbol at filename:linenum>' if set. */
86 static int print_symbol_filename = 0;
87
88 /* Number of auto-display expression currently being displayed.
89 So that we can disable it if we get an error or a signal within it.
90 -1 when not doing one. */
91
92 int current_display_number;
93
94 /* Flag to low-level print routines that this value is being printed
95 in an epoch window. We'd like to pass this as a parameter, but
96 every routine would need to take it. Perhaps we can encapsulate
97 this in the I/O stream once we have GNU stdio. */
98
99 int inspect_it = 0;
100
101 struct display
102 {
103 /* Chain link to next auto-display item. */
104 struct display *next;
105 /* Expression to be evaluated and displayed. */
106 struct expression *exp;
107 /* Item number of this auto-display item. */
108 int number;
109 /* Display format specified. */
110 struct format_data format;
111 /* Innermost block required by this expression when evaluated */
112 struct block *block;
113 /* Status of this display (enabled or disabled) */
114 enum enable status;
115 };
116
117 /* Chain of expressions whose values should be displayed
118 automatically each time the program stops. */
119
120 static struct display *display_chain;
121
122 static int display_number;
123
124 /* Prototypes for exported functions. */
125
126 void output_command (char *, int);
127
128 void _initialize_printcmd (void);
129
130 /* Prototypes for local functions. */
131
132 static void delete_display (int);
133
134 static void enable_display (char *, int);
135
136 static void disable_display_command (char *, int);
137
138 static void disassemble_command (char *, int);
139
140 static void printf_command (char *, int);
141
142 static void print_frame_nameless_args (struct frame_info *, long,
143 int, int, struct ui_file *);
144
145 static void display_info (char *, int);
146
147 static void do_one_display (struct display *);
148
149 static void undisplay_command (char *, int);
150
151 static void free_display (struct display *);
152
153 static void display_command (char *, int);
154
155 void x_command (char *, int);
156
157 static void address_info (char *, int);
158
159 static void set_command (char *, int);
160
161 static void call_command (char *, int);
162
163 static void inspect_command (char *, int);
164
165 static void print_command (char *, int);
166
167 static void print_command_1 (char *, int, int);
168
169 static void validate_format (struct format_data, char *);
170
171 static void do_examine (struct format_data, CORE_ADDR addr,
172 asection * section);
173
174 static void print_formatted (value_ptr, int, int, struct ui_file *);
175
176 static struct format_data decode_format (char **, int, int);
177
178 static int print_insn (CORE_ADDR, struct ui_file *);
179
180 static void sym_info (char *, int);
181 \f
182
183 /* Decode a format specification. *STRING_PTR should point to it.
184 OFORMAT and OSIZE are used as defaults for the format and size
185 if none are given in the format specification.
186 If OSIZE is zero, then the size field of the returned value
187 should be set only if a size is explicitly specified by the
188 user.
189 The structure returned describes all the data
190 found in the specification. In addition, *STRING_PTR is advanced
191 past the specification and past all whitespace following it. */
192
193 static struct format_data
194 decode_format (string_ptr, oformat, osize)
195 char **string_ptr;
196 int oformat;
197 int osize;
198 {
199 struct format_data val;
200 register char *p = *string_ptr;
201
202 val.format = '?';
203 val.size = '?';
204 val.count = 1;
205
206 if (*p >= '0' && *p <= '9')
207 val.count = atoi (p);
208 while (*p >= '0' && *p <= '9')
209 p++;
210
211 /* Now process size or format letters that follow. */
212
213 while (1)
214 {
215 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
216 val.size = *p++;
217 else if (*p >= 'a' && *p <= 'z')
218 val.format = *p++;
219 else
220 break;
221 }
222
223 while (*p == ' ' || *p == '\t')
224 p++;
225 *string_ptr = p;
226
227 /* Set defaults for format and size if not specified. */
228 if (val.format == '?')
229 {
230 if (val.size == '?')
231 {
232 /* Neither has been specified. */
233 val.format = oformat;
234 val.size = osize;
235 }
236 else
237 /* If a size is specified, any format makes a reasonable
238 default except 'i'. */
239 val.format = oformat == 'i' ? 'x' : oformat;
240 }
241 else if (val.size == '?')
242 switch (val.format)
243 {
244 case 'a':
245 case 's':
246 /* Pick the appropriate size for an address. */
247 if (TARGET_PTR_BIT == 64)
248 val.size = osize ? 'g' : osize;
249 else if (TARGET_PTR_BIT == 32)
250 val.size = osize ? 'w' : osize;
251 else if (TARGET_PTR_BIT == 16)
252 val.size = osize ? 'h' : osize;
253 else
254 /* Bad value for TARGET_PTR_BIT */
255 abort ();
256 break;
257 case 'f':
258 /* Floating point has to be word or giantword. */
259 if (osize == 'w' || osize == 'g')
260 val.size = osize;
261 else
262 /* Default it to giantword if the last used size is not
263 appropriate. */
264 val.size = osize ? 'g' : osize;
265 break;
266 case 'c':
267 /* Characters default to one byte. */
268 val.size = osize ? 'b' : osize;
269 break;
270 default:
271 /* The default is the size most recently specified. */
272 val.size = osize;
273 }
274
275 return val;
276 }
277 \f
278 /* Print value VAL on stream according to FORMAT, a letter or 0.
279 Do not end with a newline.
280 0 means print VAL according to its own type.
281 SIZE is the letter for the size of datum being printed.
282 This is used to pad hex numbers so they line up. */
283
284 static void
285 print_formatted (val, format, size, stream)
286 register value_ptr val;
287 register int format;
288 int size;
289 struct ui_file *stream;
290 {
291 struct type *type = check_typedef (VALUE_TYPE (val));
292 int len = TYPE_LENGTH (type);
293
294 if (VALUE_LVAL (val) == lval_memory)
295 {
296 next_address = VALUE_ADDRESS (val) + len;
297 next_section = VALUE_BFD_SECTION (val);
298 }
299
300 switch (format)
301 {
302 case 's':
303 /* FIXME: Need to handle wchar_t's here... */
304 next_address = VALUE_ADDRESS (val)
305 + val_print_string (VALUE_ADDRESS (val), -1, 1, stream);
306 next_section = VALUE_BFD_SECTION (val);
307 break;
308
309 case 'i':
310 /* The old comment says
311 "Force output out, print_insn not using _filtered".
312 I'm not completely sure what that means, I suspect most print_insn
313 now do use _filtered, so I guess it's obsolete.
314 --Yes, it does filter now, and so this is obsolete. -JB */
315
316 /* We often wrap here if there are long symbolic names. */
317 wrap_here (" ");
318 next_address = VALUE_ADDRESS (val)
319 + print_insn (VALUE_ADDRESS (val), stream);
320 next_section = VALUE_BFD_SECTION (val);
321 break;
322
323 default:
324 if (format == 0
325 || TYPE_CODE (type) == TYPE_CODE_ARRAY
326 || TYPE_CODE (type) == TYPE_CODE_STRING
327 || TYPE_CODE (type) == TYPE_CODE_STRUCT
328 || TYPE_CODE (type) == TYPE_CODE_UNION)
329 /* If format is 0, use the 'natural' format for
330 * that type of value. If the type is non-scalar,
331 * we have to use language rules to print it as
332 * a series of scalars.
333 */
334 value_print (val, stream, format, Val_pretty_default);
335 else
336 /* User specified format, so don't look to the
337 * the type to tell us what to do.
338 */
339 print_scalar_formatted (VALUE_CONTENTS (val), type,
340 format, size, stream);
341 }
342 }
343
344 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
345 according to letters FORMAT and SIZE on STREAM.
346 FORMAT may not be zero. Formats s and i are not supported at this level.
347
348 This is how the elements of an array or structure are printed
349 with a format. */
350
351 void
352 print_scalar_formatted (valaddr, type, format, size, stream)
353 char *valaddr;
354 struct type *type;
355 int format;
356 int size;
357 struct ui_file *stream;
358 {
359 LONGEST val_long;
360 unsigned int len = TYPE_LENGTH (type);
361
362 if (len > sizeof (LONGEST)
363 && (format == 't'
364 || format == 'c'
365 || format == 'o'
366 || format == 'u'
367 || format == 'd'
368 || format == 'x'))
369 {
370 if (!TYPE_UNSIGNED (type)
371 || !extract_long_unsigned_integer (valaddr, len, &val_long))
372 {
373 /* We can't print it normally, but we can print it in hex.
374 Printing it in the wrong radix is more useful than saying
375 "use /x, you dummy". */
376 /* FIXME: we could also do octal or binary if that was the
377 desired format. */
378 /* FIXME: we should be using the size field to give us a
379 minimum field width to print. */
380
381 if (format == 'o')
382 print_octal_chars (stream, valaddr, len);
383 else if (format == 'd')
384 print_decimal_chars (stream, valaddr, len);
385 else if (format == 't')
386 print_binary_chars (stream, valaddr, len);
387 else
388 /* replace with call to print_hex_chars? Looks
389 like val_print_type_code_int is redoing
390 work. - edie */
391
392 val_print_type_code_int (type, valaddr, stream);
393
394 return;
395 }
396
397 /* If we get here, extract_long_unsigned_integer set val_long. */
398 }
399 else if (format != 'f')
400 val_long = unpack_long (type, valaddr);
401
402 /* If we are printing it as unsigned, truncate it in case it is actually
403 a negative signed value (e.g. "print/u (short)-1" should print 65535
404 (if shorts are 16 bits) instead of 4294967295). */
405 if (format != 'd')
406 {
407 if (len < sizeof (LONGEST))
408 val_long &= ((LONGEST) 1 << HOST_CHAR_BIT * len) - 1;
409 }
410
411 switch (format)
412 {
413 case 'x':
414 if (!size)
415 {
416 /* no size specified, like in print. Print varying # of digits. */
417 print_longest (stream, 'x', 1, val_long);
418 }
419 else
420 switch (size)
421 {
422 case 'b':
423 case 'h':
424 case 'w':
425 case 'g':
426 print_longest (stream, size, 1, val_long);
427 break;
428 default:
429 error ("Undefined output size \"%c\".", size);
430 }
431 break;
432
433 case 'd':
434 print_longest (stream, 'd', 1, val_long);
435 break;
436
437 case 'u':
438 print_longest (stream, 'u', 0, val_long);
439 break;
440
441 case 'o':
442 if (val_long)
443 print_longest (stream, 'o', 1, val_long);
444 else
445 fprintf_filtered (stream, "0");
446 break;
447
448 case 'a':
449 {
450 CORE_ADDR addr = unpack_pointer (type, valaddr);
451 print_address (addr, stream);
452 }
453 break;
454
455 case 'c':
456 value_print (value_from_longest (builtin_type_true_char, val_long),
457 stream, 0, Val_pretty_default);
458 break;
459
460 case 'f':
461 if (len == sizeof (float))
462 type = builtin_type_float;
463 else if (len == sizeof (double))
464 type = builtin_type_double;
465 print_floating (valaddr, type, stream);
466 break;
467
468 case 0:
469 abort ();
470
471 case 't':
472 /* Binary; 't' stands for "two". */
473 {
474 char bits[8 * (sizeof val_long) + 1];
475 char buf[8 * (sizeof val_long) + 32];
476 char *cp = bits;
477 int width;
478
479 if (!size)
480 width = 8 * (sizeof val_long);
481 else
482 switch (size)
483 {
484 case 'b':
485 width = 8;
486 break;
487 case 'h':
488 width = 16;
489 break;
490 case 'w':
491 width = 32;
492 break;
493 case 'g':
494 width = 64;
495 break;
496 default:
497 error ("Undefined output size \"%c\".", size);
498 }
499
500 bits[width] = '\0';
501 while (width-- > 0)
502 {
503 bits[width] = (val_long & 1) ? '1' : '0';
504 val_long >>= 1;
505 }
506 if (!size)
507 {
508 while (*cp && *cp == '0')
509 cp++;
510 if (*cp == '\0')
511 cp--;
512 }
513 strcpy (buf, local_binary_format_prefix ());
514 strcat (buf, cp);
515 strcat (buf, local_binary_format_suffix ());
516 fprintf_filtered (stream, buf);
517 }
518 break;
519
520 default:
521 error ("Undefined output format \"%c\".", format);
522 }
523 }
524
525 /* Specify default address for `x' command.
526 `info lines' uses this. */
527
528 void
529 set_next_address (addr)
530 CORE_ADDR addr;
531 {
532 next_address = addr;
533
534 /* Make address available to the user as $_. */
535 set_internalvar (lookup_internalvar ("_"),
536 value_from_pointer (lookup_pointer_type (builtin_type_void),
537 addr));
538 }
539
540 /* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
541 after LEADIN. Print nothing if no symbolic name is found nearby.
542 Optionally also print source file and line number, if available.
543 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
544 or to interpret it as a possible C++ name and convert it back to source
545 form. However note that DO_DEMANGLE can be overridden by the specific
546 settings of the demangle and asm_demangle variables. */
547
548 void
549 print_address_symbolic (addr, stream, do_demangle, leadin)
550 CORE_ADDR addr;
551 struct ui_file *stream;
552 int do_demangle;
553 char *leadin;
554 {
555 char *name = NULL;
556 char *filename = NULL;
557 int unmapped = 0;
558 int offset = 0;
559 int line = 0;
560
561 /* throw away both name and filename */
562 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
563 make_cleanup (free_current_contents, &filename);
564
565 if (build_address_symbolic (addr, do_demangle, &name, &offset, &filename, &line, &unmapped))
566 {
567 do_cleanups (cleanup_chain);
568 return;
569 }
570
571 fputs_filtered (leadin, stream);
572 if (unmapped)
573 fputs_filtered ("<*", stream);
574 else
575 fputs_filtered ("<", stream);
576 fputs_filtered (name, stream);
577 if (offset != 0)
578 fprintf_filtered (stream, "+%u", (unsigned int) offset);
579
580 /* Append source filename and line number if desired. Give specific
581 line # of this addr, if we have it; else line # of the nearest symbol. */
582 if (print_symbol_filename && filename != NULL)
583 {
584 if (line != -1)
585 fprintf_filtered (stream, " at %s:%d", filename, line);
586 else
587 fprintf_filtered (stream, " in %s", filename);
588 }
589 if (unmapped)
590 fputs_filtered ("*>", stream);
591 else
592 fputs_filtered (">", stream);
593
594 do_cleanups (cleanup_chain);
595 }
596
597 /* Given an address ADDR return all the elements needed to print the
598 address in a symbolic form. NAME can be mangled or not depending
599 on DO_DEMANGLE (and also on the asm_demangle global variable,
600 manipulated via ''set print asm-demangle''). Return 0 in case of
601 success, when all the info in the OUT paramters is valid. Return 1
602 otherwise. */
603 int
604 build_address_symbolic (CORE_ADDR addr, /* IN */
605 int do_demangle, /* IN */
606 char **name, /* OUT */
607 int *offset, /* OUT */
608 char **filename, /* OUT */
609 int *line, /* OUT */
610 int *unmapped) /* OUT */
611 {
612 struct minimal_symbol *msymbol;
613 struct symbol *symbol;
614 struct symtab *symtab = 0;
615 CORE_ADDR name_location = 0;
616 asection *section = 0;
617 char *name_temp = "";
618
619 /* Let's say it is unmapped. */
620 *unmapped = 0;
621
622 /* Determine if the address is in an overlay, and whether it is
623 mapped. */
624 if (overlay_debugging)
625 {
626 section = find_pc_overlay (addr);
627 if (pc_in_unmapped_range (addr, section))
628 {
629 *unmapped = 1;
630 addr = overlay_mapped_address (addr, section);
631 }
632 }
633
634 /* On some targets, add in extra "flag" bits to PC for
635 disassembly. This should ensure that "rounding errors" in
636 symbol addresses that are masked for disassembly favour the
637 the correct symbol. */
638
639 #ifdef GDB_TARGET_UNMASK_DISAS_PC
640 addr = GDB_TARGET_UNMASK_DISAS_PC (addr);
641 #endif
642
643 /* First try to find the address in the symbol table, then
644 in the minsyms. Take the closest one. */
645
646 /* This is defective in the sense that it only finds text symbols. So
647 really this is kind of pointless--we should make sure that the
648 minimal symbols have everything we need (by changing that we could
649 save some memory, but for many debug format--ELF/DWARF or
650 anything/stabs--it would be inconvenient to eliminate those minimal
651 symbols anyway). */
652 msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
653 symbol = find_pc_sect_function (addr, section);
654
655 if (symbol)
656 {
657 name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
658 if (do_demangle)
659 name_temp = SYMBOL_SOURCE_NAME (symbol);
660 else
661 name_temp = SYMBOL_LINKAGE_NAME (symbol);
662 }
663
664 if (msymbol != NULL)
665 {
666 if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
667 {
668 /* The msymbol is closer to the address than the symbol;
669 use the msymbol instead. */
670 symbol = 0;
671 symtab = 0;
672 name_location = SYMBOL_VALUE_ADDRESS (msymbol);
673 if (do_demangle)
674 name_temp = SYMBOL_SOURCE_NAME (msymbol);
675 else
676 name_temp = SYMBOL_LINKAGE_NAME (msymbol);
677 }
678 }
679 if (symbol == NULL && msymbol == NULL)
680 return 1;
681
682 /* On some targets, mask out extra "flag" bits from PC for handsome
683 disassembly. */
684
685 #ifdef GDB_TARGET_MASK_DISAS_PC
686 name_location = GDB_TARGET_MASK_DISAS_PC (name_location);
687 addr = GDB_TARGET_MASK_DISAS_PC (addr);
688 #endif
689
690 /* If the nearest symbol is too far away, don't print anything symbolic. */
691
692 /* For when CORE_ADDR is larger than unsigned int, we do math in
693 CORE_ADDR. But when we detect unsigned wraparound in the
694 CORE_ADDR math, we ignore this test and print the offset,
695 because addr+max_symbolic_offset has wrapped through the end
696 of the address space back to the beginning, giving bogus comparison. */
697 if (addr > name_location + max_symbolic_offset
698 && name_location + max_symbolic_offset > name_location)
699 return 1;
700
701 *offset = addr - name_location;
702
703 *name = xstrdup (name_temp);
704
705 if (print_symbol_filename)
706 {
707 struct symtab_and_line sal;
708
709 sal = find_pc_sect_line (addr, section, 0);
710
711 if (sal.symtab)
712 {
713 *filename = xstrdup (sal.symtab->filename);
714 *line = sal.line;
715 }
716 else if (symtab && symbol && symbol->line)
717 {
718 *filename = xstrdup (symtab->filename);
719 *line = symbol->line;
720 }
721 else if (symtab)
722 {
723 *filename = xstrdup (symtab->filename);
724 *line = -1;
725 }
726 }
727 return 0;
728 }
729
730 /* Print address ADDR on STREAM. USE_LOCAL means the same thing as for
731 print_longest. */
732 void
733 print_address_numeric (addr, use_local, stream)
734 CORE_ADDR addr;
735 int use_local;
736 struct ui_file *stream;
737 {
738 /* Truncate address to the size of a target pointer, avoiding shifts
739 larger or equal than the width of a CORE_ADDR. The local
740 variable PTR_BIT stops the compiler reporting a shift overflow
741 when it won't occure. */
742 /* NOTE: This assumes that the significant address information is
743 kept in the least significant bits of ADDR - the upper bits were
744 either zero or sign extended. Should ADDRESS_TO_POINTER() or
745 some ADDRESS_TO_PRINTABLE() be used to do the conversion? */
746 int ptr_bit = TARGET_PTR_BIT;
747 if (ptr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
748 addr &= ((CORE_ADDR) 1 << ptr_bit) - 1;
749 print_longest (stream, 'x', use_local, (ULONGEST) addr);
750 }
751
752 /* Print address ADDR symbolically on STREAM.
753 First print it as a number. Then perhaps print
754 <SYMBOL + OFFSET> after the number. */
755
756 void
757 print_address (addr, stream)
758 CORE_ADDR addr;
759 struct ui_file *stream;
760 {
761 print_address_numeric (addr, 1, stream);
762 print_address_symbolic (addr, stream, asm_demangle, " ");
763 }
764
765 /* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
766 controls whether to print the symbolic name "raw" or demangled.
767 Global setting "addressprint" controls whether to print hex address
768 or not. */
769
770 void
771 print_address_demangle (addr, stream, do_demangle)
772 CORE_ADDR addr;
773 struct ui_file *stream;
774 int do_demangle;
775 {
776 if (addr == 0)
777 {
778 fprintf_filtered (stream, "0");
779 }
780 else if (addressprint)
781 {
782 print_address_numeric (addr, 1, stream);
783 print_address_symbolic (addr, stream, do_demangle, " ");
784 }
785 else
786 {
787 print_address_symbolic (addr, stream, do_demangle, "");
788 }
789 }
790 \f
791
792 /* These are the types that $__ will get after an examine command of one
793 of these sizes. */
794
795 static struct type *examine_i_type;
796
797 static struct type *examine_b_type;
798 static struct type *examine_h_type;
799 static struct type *examine_w_type;
800 static struct type *examine_g_type;
801
802 /* Examine data at address ADDR in format FMT.
803 Fetch it from memory and print on gdb_stdout. */
804
805 static void
806 do_examine (fmt, addr, sect)
807 struct format_data fmt;
808 CORE_ADDR addr;
809 asection *sect;
810 {
811 register char format = 0;
812 register char size;
813 register int count = 1;
814 struct type *val_type = NULL;
815 register int i;
816 register int maxelts;
817
818 format = fmt.format;
819 size = fmt.size;
820 count = fmt.count;
821 next_address = addr;
822 next_section = sect;
823
824 /* String or instruction format implies fetch single bytes
825 regardless of the specified size. */
826 if (format == 's' || format == 'i')
827 size = 'b';
828
829 if (format == 'i')
830 val_type = examine_i_type;
831 else if (size == 'b')
832 val_type = examine_b_type;
833 else if (size == 'h')
834 val_type = examine_h_type;
835 else if (size == 'w')
836 val_type = examine_w_type;
837 else if (size == 'g')
838 val_type = examine_g_type;
839
840 maxelts = 8;
841 if (size == 'w')
842 maxelts = 4;
843 if (size == 'g')
844 maxelts = 2;
845 if (format == 's' || format == 'i')
846 maxelts = 1;
847
848 /* Print as many objects as specified in COUNT, at most maxelts per line,
849 with the address of the next one at the start of each line. */
850
851 while (count > 0)
852 {
853 QUIT;
854 print_address (next_address, gdb_stdout);
855 printf_filtered (":");
856 for (i = maxelts;
857 i > 0 && count > 0;
858 i--, count--)
859 {
860 printf_filtered ("\t");
861 /* Note that print_formatted sets next_address for the next
862 object. */
863 last_examine_address = next_address;
864
865 if (last_examine_value)
866 value_free (last_examine_value);
867
868 /* The value to be displayed is not fetched greedily.
869 Instead, to avoid the posibility of a fetched value not
870 being used, its retreval is delayed until the print code
871 uses it. When examining an instruction stream, the
872 disassembler will perform its own memory fetch using just
873 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
874 the disassembler be modified so that LAST_EXAMINE_VALUE
875 is left with the byte sequence from the last complete
876 instruction fetched from memory? */
877 last_examine_value = value_at_lazy (val_type, next_address, sect);
878
879 if (last_examine_value)
880 release_value (last_examine_value);
881
882 print_formatted (last_examine_value, format, size, gdb_stdout);
883 }
884 printf_filtered ("\n");
885 gdb_flush (gdb_stdout);
886 }
887 }
888 \f
889 static void
890 validate_format (fmt, cmdname)
891 struct format_data fmt;
892 char *cmdname;
893 {
894 if (fmt.size != 0)
895 error ("Size letters are meaningless in \"%s\" command.", cmdname);
896 if (fmt.count != 1)
897 error ("Item count other than 1 is meaningless in \"%s\" command.",
898 cmdname);
899 if (fmt.format == 'i' || fmt.format == 's')
900 error ("Format letter \"%c\" is meaningless in \"%s\" command.",
901 fmt.format, cmdname);
902 }
903
904 /* Evaluate string EXP as an expression in the current language and
905 print the resulting value. EXP may contain a format specifier as the
906 first argument ("/x myvar" for example, to print myvar in hex).
907 */
908
909 static void
910 print_command_1 (exp, inspect, voidprint)
911 char *exp;
912 int inspect;
913 int voidprint;
914 {
915 struct expression *expr;
916 register struct cleanup *old_chain = 0;
917 register char format = 0;
918 register value_ptr val;
919 struct format_data fmt;
920 int cleanup = 0;
921
922 /* Pass inspect flag to the rest of the print routines in a global (sigh). */
923 inspect_it = inspect;
924
925 if (exp && *exp == '/')
926 {
927 exp++;
928 fmt = decode_format (&exp, last_format, 0);
929 validate_format (fmt, "print");
930 last_format = format = fmt.format;
931 }
932 else
933 {
934 fmt.count = 1;
935 fmt.format = 0;
936 fmt.size = 0;
937 }
938
939 if (exp && *exp)
940 {
941 struct type *type;
942 expr = parse_expression (exp);
943 old_chain = make_cleanup (free_current_contents, &expr);
944 cleanup = 1;
945 val = evaluate_expression (expr);
946
947 /* C++: figure out what type we actually want to print it as. */
948 type = VALUE_TYPE (val);
949
950 if (objectprint
951 && (TYPE_CODE (type) == TYPE_CODE_PTR
952 || TYPE_CODE (type) == TYPE_CODE_REF)
953 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_STRUCT
954 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_UNION))
955 {
956 value_ptr v;
957
958 v = value_from_vtable_info (val, TYPE_TARGET_TYPE (type));
959 if (v != 0)
960 {
961 val = v;
962 type = VALUE_TYPE (val);
963 }
964 }
965 }
966 else
967 val = access_value_history (0);
968
969 if (voidprint || (val && VALUE_TYPE (val) &&
970 TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_VOID))
971 {
972 int histindex = record_latest_value (val);
973
974 if (histindex >= 0)
975 annotate_value_history_begin (histindex, VALUE_TYPE (val));
976 else
977 annotate_value_begin (VALUE_TYPE (val));
978
979 if (inspect)
980 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"", exp, histindex);
981 else if (histindex >= 0)
982 printf_filtered ("$%d = ", histindex);
983
984 if (histindex >= 0)
985 annotate_value_history_value ();
986
987 print_formatted (val, format, fmt.size, gdb_stdout);
988 printf_filtered ("\n");
989
990 if (histindex >= 0)
991 annotate_value_history_end ();
992 else
993 annotate_value_end ();
994
995 if (inspect)
996 printf_unfiltered ("\") )\030");
997 }
998
999 if (cleanup)
1000 do_cleanups (old_chain);
1001 inspect_it = 0; /* Reset print routines to normal */
1002 }
1003
1004 /* ARGSUSED */
1005 static void
1006 print_command (exp, from_tty)
1007 char *exp;
1008 int from_tty;
1009 {
1010 print_command_1 (exp, 0, 1);
1011 }
1012
1013 /* Same as print, except in epoch, it gets its own window */
1014 /* ARGSUSED */
1015 static void
1016 inspect_command (exp, from_tty)
1017 char *exp;
1018 int from_tty;
1019 {
1020 extern int epoch_interface;
1021
1022 print_command_1 (exp, epoch_interface, 1);
1023 }
1024
1025 /* Same as print, except it doesn't print void results. */
1026 /* ARGSUSED */
1027 static void
1028 call_command (exp, from_tty)
1029 char *exp;
1030 int from_tty;
1031 {
1032 print_command_1 (exp, 0, 0);
1033 }
1034
1035 /* ARGSUSED */
1036 void
1037 output_command (exp, from_tty)
1038 char *exp;
1039 int from_tty;
1040 {
1041 struct expression *expr;
1042 register struct cleanup *old_chain;
1043 register char format = 0;
1044 register value_ptr val;
1045 struct format_data fmt;
1046
1047 if (exp && *exp == '/')
1048 {
1049 exp++;
1050 fmt = decode_format (&exp, 0, 0);
1051 validate_format (fmt, "output");
1052 format = fmt.format;
1053 }
1054
1055 expr = parse_expression (exp);
1056 old_chain = make_cleanup (free_current_contents, &expr);
1057
1058 val = evaluate_expression (expr);
1059
1060 annotate_value_begin (VALUE_TYPE (val));
1061
1062 print_formatted (val, format, fmt.size, gdb_stdout);
1063
1064 annotate_value_end ();
1065
1066 wrap_here ("");
1067 gdb_flush (gdb_stdout);
1068
1069 do_cleanups (old_chain);
1070 }
1071
1072 /* ARGSUSED */
1073 static void
1074 set_command (exp, from_tty)
1075 char *exp;
1076 int from_tty;
1077 {
1078 struct expression *expr = parse_expression (exp);
1079 register struct cleanup *old_chain =
1080 make_cleanup (free_current_contents, &expr);
1081 evaluate_expression (expr);
1082 do_cleanups (old_chain);
1083 }
1084
1085 /* ARGSUSED */
1086 static void
1087 sym_info (arg, from_tty)
1088 char *arg;
1089 int from_tty;
1090 {
1091 struct minimal_symbol *msymbol;
1092 struct objfile *objfile;
1093 struct obj_section *osect;
1094 asection *sect;
1095 CORE_ADDR addr, sect_addr;
1096 int matches = 0;
1097 unsigned int offset;
1098
1099 if (!arg)
1100 error_no_arg ("address");
1101
1102 addr = parse_and_eval_address (arg);
1103 ALL_OBJSECTIONS (objfile, osect)
1104 {
1105 sect = osect->the_bfd_section;
1106 sect_addr = overlay_mapped_address (addr, sect);
1107
1108 if (osect->addr <= sect_addr && sect_addr < osect->endaddr &&
1109 (msymbol = lookup_minimal_symbol_by_pc_section (sect_addr, sect)))
1110 {
1111 matches = 1;
1112 offset = sect_addr - SYMBOL_VALUE_ADDRESS (msymbol);
1113 if (offset)
1114 printf_filtered ("%s + %u in ",
1115 SYMBOL_SOURCE_NAME (msymbol), offset);
1116 else
1117 printf_filtered ("%s in ",
1118 SYMBOL_SOURCE_NAME (msymbol));
1119 if (pc_in_unmapped_range (addr, sect))
1120 printf_filtered ("load address range of ");
1121 if (section_is_overlay (sect))
1122 printf_filtered ("%s overlay ",
1123 section_is_mapped (sect) ? "mapped" : "unmapped");
1124 printf_filtered ("section %s", sect->name);
1125 printf_filtered ("\n");
1126 }
1127 }
1128 if (matches == 0)
1129 printf_filtered ("No symbol matches %s.\n", arg);
1130 }
1131
1132 /* ARGSUSED */
1133 static void
1134 address_info (exp, from_tty)
1135 char *exp;
1136 int from_tty;
1137 {
1138 register struct symbol *sym;
1139 register struct minimal_symbol *msymbol;
1140 register long val;
1141 register long basereg;
1142 asection *section;
1143 CORE_ADDR load_addr;
1144 int is_a_field_of_this; /* C++: lookup_symbol sets this to nonzero
1145 if exp is a field of `this'. */
1146
1147 if (exp == 0)
1148 error ("Argument required.");
1149
1150 sym = lookup_symbol (exp, get_selected_block (), VAR_NAMESPACE,
1151 &is_a_field_of_this, (struct symtab **) NULL);
1152 if (sym == NULL)
1153 {
1154 if (is_a_field_of_this)
1155 {
1156 printf_filtered ("Symbol \"");
1157 fprintf_symbol_filtered (gdb_stdout, exp,
1158 current_language->la_language, DMGL_ANSI);
1159 printf_filtered ("\" is a field of the local class variable `this'\n");
1160 return;
1161 }
1162
1163 msymbol = lookup_minimal_symbol (exp, NULL, NULL);
1164
1165 if (msymbol != NULL)
1166 {
1167 load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1168
1169 printf_filtered ("Symbol \"");
1170 fprintf_symbol_filtered (gdb_stdout, exp,
1171 current_language->la_language, DMGL_ANSI);
1172 printf_filtered ("\" is at ");
1173 print_address_numeric (load_addr, 1, gdb_stdout);
1174 printf_filtered (" in a file compiled without debugging");
1175 section = SYMBOL_BFD_SECTION (msymbol);
1176 if (section_is_overlay (section))
1177 {
1178 load_addr = overlay_unmapped_address (load_addr, section);
1179 printf_filtered (",\n -- loaded at ");
1180 print_address_numeric (load_addr, 1, gdb_stdout);
1181 printf_filtered (" in overlay section %s", section->name);
1182 }
1183 printf_filtered (".\n");
1184 }
1185 else
1186 error ("No symbol \"%s\" in current context.", exp);
1187 return;
1188 }
1189
1190 printf_filtered ("Symbol \"");
1191 fprintf_symbol_filtered (gdb_stdout, SYMBOL_NAME (sym),
1192 current_language->la_language, DMGL_ANSI);
1193 printf_filtered ("\" is ");
1194 val = SYMBOL_VALUE (sym);
1195 basereg = SYMBOL_BASEREG (sym);
1196 section = SYMBOL_BFD_SECTION (sym);
1197
1198 switch (SYMBOL_CLASS (sym))
1199 {
1200 case LOC_CONST:
1201 case LOC_CONST_BYTES:
1202 printf_filtered ("constant");
1203 break;
1204
1205 case LOC_LABEL:
1206 printf_filtered ("a label at address ");
1207 print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
1208 1, gdb_stdout);
1209 if (section_is_overlay (section))
1210 {
1211 load_addr = overlay_unmapped_address (load_addr, section);
1212 printf_filtered (",\n -- loaded at ");
1213 print_address_numeric (load_addr, 1, gdb_stdout);
1214 printf_filtered (" in overlay section %s", section->name);
1215 }
1216 break;
1217
1218 case LOC_REGISTER:
1219 printf_filtered ("a variable in register %s", REGISTER_NAME (val));
1220 break;
1221
1222 case LOC_STATIC:
1223 printf_filtered ("static storage at address ");
1224 print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
1225 1, gdb_stdout);
1226 if (section_is_overlay (section))
1227 {
1228 load_addr = overlay_unmapped_address (load_addr, section);
1229 printf_filtered (",\n -- loaded at ");
1230 print_address_numeric (load_addr, 1, gdb_stdout);
1231 printf_filtered (" in overlay section %s", section->name);
1232 }
1233 break;
1234
1235 case LOC_INDIRECT:
1236 printf_filtered ("external global (indirect addressing), at address *(");
1237 print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (sym),
1238 1, gdb_stdout);
1239 printf_filtered (")");
1240 if (section_is_overlay (section))
1241 {
1242 load_addr = overlay_unmapped_address (load_addr, section);
1243 printf_filtered (",\n -- loaded at ");
1244 print_address_numeric (load_addr, 1, gdb_stdout);
1245 printf_filtered (" in overlay section %s", section->name);
1246 }
1247 break;
1248
1249 case LOC_REGPARM:
1250 printf_filtered ("an argument in register %s", REGISTER_NAME (val));
1251 break;
1252
1253 case LOC_REGPARM_ADDR:
1254 printf_filtered ("address of an argument in register %s", REGISTER_NAME (val));
1255 break;
1256
1257 case LOC_ARG:
1258 printf_filtered ("an argument at offset %ld", val);
1259 break;
1260
1261 case LOC_LOCAL_ARG:
1262 printf_filtered ("an argument at frame offset %ld", val);
1263 break;
1264
1265 case LOC_LOCAL:
1266 printf_filtered ("a local variable at frame offset %ld", val);
1267 break;
1268
1269 case LOC_REF_ARG:
1270 printf_filtered ("a reference argument at offset %ld", val);
1271 break;
1272
1273 case LOC_BASEREG:
1274 printf_filtered ("a variable at offset %ld from register %s",
1275 val, REGISTER_NAME (basereg));
1276 break;
1277
1278 case LOC_BASEREG_ARG:
1279 printf_filtered ("an argument at offset %ld from register %s",
1280 val, REGISTER_NAME (basereg));
1281 break;
1282
1283 case LOC_TYPEDEF:
1284 printf_filtered ("a typedef");
1285 break;
1286
1287 case LOC_BLOCK:
1288 printf_filtered ("a function at address ");
1289 #ifdef GDB_TARGET_MASK_DISAS_PC
1290 print_address_numeric
1291 (load_addr = GDB_TARGET_MASK_DISAS_PC (BLOCK_START (SYMBOL_BLOCK_VALUE (sym))),
1292 1, gdb_stdout);
1293 #else
1294 print_address_numeric (load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)),
1295 1, gdb_stdout);
1296 #endif
1297 if (section_is_overlay (section))
1298 {
1299 load_addr = overlay_unmapped_address (load_addr, section);
1300 printf_filtered (",\n -- loaded at ");
1301 print_address_numeric (load_addr, 1, gdb_stdout);
1302 printf_filtered (" in overlay section %s", section->name);
1303 }
1304 break;
1305
1306 case LOC_UNRESOLVED:
1307 {
1308 struct minimal_symbol *msym;
1309
1310 msym = lookup_minimal_symbol (SYMBOL_NAME (sym), NULL, NULL);
1311 if (msym == NULL)
1312 printf_filtered ("unresolved");
1313 else
1314 {
1315 section = SYMBOL_BFD_SECTION (msym);
1316 printf_filtered ("static storage at address ");
1317 print_address_numeric (load_addr = SYMBOL_VALUE_ADDRESS (msym),
1318 1, gdb_stdout);
1319 if (section_is_overlay (section))
1320 {
1321 load_addr = overlay_unmapped_address (load_addr, section);
1322 printf_filtered (",\n -- loaded at ");
1323 print_address_numeric (load_addr, 1, gdb_stdout);
1324 printf_filtered (" in overlay section %s", section->name);
1325 }
1326 }
1327 }
1328 break;
1329
1330 case LOC_THREAD_LOCAL_STATIC:
1331 printf_filtered (
1332 "a thread-local variable at offset %ld from the thread base register %s",
1333 val, REGISTER_NAME (basereg));
1334 break;
1335
1336 case LOC_OPTIMIZED_OUT:
1337 printf_filtered ("optimized out");
1338 break;
1339
1340 default:
1341 printf_filtered ("of unknown (botched) type");
1342 break;
1343 }
1344 printf_filtered (".\n");
1345 }
1346 \f
1347 void
1348 x_command (exp, from_tty)
1349 char *exp;
1350 int from_tty;
1351 {
1352 struct expression *expr;
1353 struct format_data fmt;
1354 struct cleanup *old_chain;
1355 struct value *val;
1356
1357 fmt.format = last_format;
1358 fmt.size = last_size;
1359 fmt.count = 1;
1360
1361 if (exp && *exp == '/')
1362 {
1363 exp++;
1364 fmt = decode_format (&exp, last_format, last_size);
1365 }
1366
1367 /* If we have an expression, evaluate it and use it as the address. */
1368
1369 if (exp != 0 && *exp != 0)
1370 {
1371 expr = parse_expression (exp);
1372 /* Cause expression not to be there any more
1373 if this command is repeated with Newline.
1374 But don't clobber a user-defined command's definition. */
1375 if (from_tty)
1376 *exp = 0;
1377 old_chain = make_cleanup (free_current_contents, &expr);
1378 val = evaluate_expression (expr);
1379 if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_REF)
1380 val = value_ind (val);
1381 /* In rvalue contexts, such as this, functions are coerced into
1382 pointers to functions. This makes "x/i main" work. */
1383 if ( /* last_format == 'i'
1384 && */ TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC
1385 && VALUE_LVAL (val) == lval_memory)
1386 next_address = VALUE_ADDRESS (val);
1387 else
1388 next_address = value_as_pointer (val);
1389 if (VALUE_BFD_SECTION (val))
1390 next_section = VALUE_BFD_SECTION (val);
1391 do_cleanups (old_chain);
1392 }
1393
1394 do_examine (fmt, next_address, next_section);
1395
1396 /* If the examine succeeds, we remember its size and format for next time. */
1397 last_size = fmt.size;
1398 last_format = fmt.format;
1399
1400 /* Set a couple of internal variables if appropriate. */
1401 if (last_examine_value)
1402 {
1403 /* Make last address examined available to the user as $_. Use
1404 the correct pointer type. */
1405 struct type *pointer_type
1406 = lookup_pointer_type (VALUE_TYPE (last_examine_value));
1407 set_internalvar (lookup_internalvar ("_"),
1408 value_from_pointer (pointer_type,
1409 last_examine_address));
1410
1411 /* Make contents of last address examined available to the user as $__. */
1412 /* If the last value has not been fetched from memory then don't
1413 fetch it now - instead mark it by voiding the $__ variable. */
1414 if (VALUE_LAZY (last_examine_value))
1415 set_internalvar (lookup_internalvar ("__"),
1416 allocate_value (builtin_type_void));
1417 else
1418 set_internalvar (lookup_internalvar ("__"), last_examine_value);
1419 }
1420 }
1421 \f
1422
1423 /* Add an expression to the auto-display chain.
1424 Specify the expression. */
1425
1426 static void
1427 display_command (exp, from_tty)
1428 char *exp;
1429 int from_tty;
1430 {
1431 struct format_data fmt;
1432 register struct expression *expr;
1433 register struct display *new;
1434 int display_it = 1;
1435
1436 #if defined(TUI)
1437 if (tui_version && *exp == '$')
1438 display_it = ((TuiStatus) tuiDo (
1439 (TuiOpaqueFuncPtr) tui_vSetLayoutTo, exp) == TUI_FAILURE);
1440 #endif
1441
1442 if (display_it)
1443 {
1444 if (exp == 0)
1445 {
1446 do_displays ();
1447 return;
1448 }
1449
1450 if (*exp == '/')
1451 {
1452 exp++;
1453 fmt = decode_format (&exp, 0, 0);
1454 if (fmt.size && fmt.format == 0)
1455 fmt.format = 'x';
1456 if (fmt.format == 'i' || fmt.format == 's')
1457 fmt.size = 'b';
1458 }
1459 else
1460 {
1461 fmt.format = 0;
1462 fmt.size = 0;
1463 fmt.count = 0;
1464 }
1465
1466 innermost_block = 0;
1467 expr = parse_expression (exp);
1468
1469 new = (struct display *) xmalloc (sizeof (struct display));
1470
1471 new->exp = expr;
1472 new->block = innermost_block;
1473 new->next = display_chain;
1474 new->number = ++display_number;
1475 new->format = fmt;
1476 new->status = enabled;
1477 display_chain = new;
1478
1479 if (from_tty && target_has_execution)
1480 do_one_display (new);
1481
1482 dont_repeat ();
1483 }
1484 }
1485
1486 static void
1487 free_display (d)
1488 struct display *d;
1489 {
1490 free ((PTR) d->exp);
1491 free ((PTR) d);
1492 }
1493
1494 /* Clear out the display_chain.
1495 Done when new symtabs are loaded, since this invalidates
1496 the types stored in many expressions. */
1497
1498 void
1499 clear_displays ()
1500 {
1501 register struct display *d;
1502
1503 while ((d = display_chain) != NULL)
1504 {
1505 free ((PTR) d->exp);
1506 display_chain = d->next;
1507 free ((PTR) d);
1508 }
1509 }
1510
1511 /* Delete the auto-display number NUM. */
1512
1513 static void
1514 delete_display (num)
1515 int num;
1516 {
1517 register struct display *d1, *d;
1518
1519 if (!display_chain)
1520 error ("No display number %d.", num);
1521
1522 if (display_chain->number == num)
1523 {
1524 d1 = display_chain;
1525 display_chain = d1->next;
1526 free_display (d1);
1527 }
1528 else
1529 for (d = display_chain;; d = d->next)
1530 {
1531 if (d->next == 0)
1532 error ("No display number %d.", num);
1533 if (d->next->number == num)
1534 {
1535 d1 = d->next;
1536 d->next = d1->next;
1537 free_display (d1);
1538 break;
1539 }
1540 }
1541 }
1542
1543 /* Delete some values from the auto-display chain.
1544 Specify the element numbers. */
1545
1546 static void
1547 undisplay_command (args, from_tty)
1548 char *args;
1549 int from_tty;
1550 {
1551 register char *p = args;
1552 register char *p1;
1553 register int num;
1554
1555 if (args == 0)
1556 {
1557 if (query ("Delete all auto-display expressions? "))
1558 clear_displays ();
1559 dont_repeat ();
1560 return;
1561 }
1562
1563 while (*p)
1564 {
1565 p1 = p;
1566 while (*p1 >= '0' && *p1 <= '9')
1567 p1++;
1568 if (*p1 && *p1 != ' ' && *p1 != '\t')
1569 error ("Arguments must be display numbers.");
1570
1571 num = atoi (p);
1572
1573 delete_display (num);
1574
1575 p = p1;
1576 while (*p == ' ' || *p == '\t')
1577 p++;
1578 }
1579 dont_repeat ();
1580 }
1581
1582 /* Display a single auto-display.
1583 Do nothing if the display cannot be printed in the current context,
1584 or if the display is disabled. */
1585
1586 static void
1587 do_one_display (d)
1588 struct display *d;
1589 {
1590 int within_current_scope;
1591
1592 if (d->status == disabled)
1593 return;
1594
1595 if (d->block)
1596 within_current_scope = contained_in (get_selected_block (), d->block);
1597 else
1598 within_current_scope = 1;
1599 if (!within_current_scope)
1600 return;
1601
1602 current_display_number = d->number;
1603
1604 annotate_display_begin ();
1605 printf_filtered ("%d", d->number);
1606 annotate_display_number_end ();
1607 printf_filtered (": ");
1608 if (d->format.size)
1609 {
1610 CORE_ADDR addr;
1611 value_ptr val;
1612
1613 annotate_display_format ();
1614
1615 printf_filtered ("x/");
1616 if (d->format.count != 1)
1617 printf_filtered ("%d", d->format.count);
1618 printf_filtered ("%c", d->format.format);
1619 if (d->format.format != 'i' && d->format.format != 's')
1620 printf_filtered ("%c", d->format.size);
1621 printf_filtered (" ");
1622
1623 annotate_display_expression ();
1624
1625 print_expression (d->exp, gdb_stdout);
1626 annotate_display_expression_end ();
1627
1628 if (d->format.count != 1)
1629 printf_filtered ("\n");
1630 else
1631 printf_filtered (" ");
1632
1633 val = evaluate_expression (d->exp);
1634 addr = value_as_pointer (val);
1635 if (d->format.format == 'i')
1636 addr = ADDR_BITS_REMOVE (addr);
1637
1638 annotate_display_value ();
1639
1640 do_examine (d->format, addr, VALUE_BFD_SECTION (val));
1641 }
1642 else
1643 {
1644 annotate_display_format ();
1645
1646 if (d->format.format)
1647 printf_filtered ("/%c ", d->format.format);
1648
1649 annotate_display_expression ();
1650
1651 print_expression (d->exp, gdb_stdout);
1652 annotate_display_expression_end ();
1653
1654 printf_filtered (" = ");
1655
1656 annotate_display_expression ();
1657
1658 print_formatted (evaluate_expression (d->exp),
1659 d->format.format, d->format.size, gdb_stdout);
1660 printf_filtered ("\n");
1661 }
1662
1663 annotate_display_end ();
1664
1665 gdb_flush (gdb_stdout);
1666 current_display_number = -1;
1667 }
1668
1669 /* Display all of the values on the auto-display chain which can be
1670 evaluated in the current scope. */
1671
1672 void
1673 do_displays ()
1674 {
1675 register struct display *d;
1676
1677 for (d = display_chain; d; d = d->next)
1678 do_one_display (d);
1679 }
1680
1681 /* Delete the auto-display which we were in the process of displaying.
1682 This is done when there is an error or a signal. */
1683
1684 void
1685 disable_display (num)
1686 int num;
1687 {
1688 register struct display *d;
1689
1690 for (d = display_chain; d; d = d->next)
1691 if (d->number == num)
1692 {
1693 d->status = disabled;
1694 return;
1695 }
1696 printf_unfiltered ("No display number %d.\n", num);
1697 }
1698
1699 void
1700 disable_current_display ()
1701 {
1702 if (current_display_number >= 0)
1703 {
1704 disable_display (current_display_number);
1705 fprintf_unfiltered (gdb_stderr, "Disabling display %d to avoid infinite recursion.\n",
1706 current_display_number);
1707 }
1708 current_display_number = -1;
1709 }
1710
1711 static void
1712 display_info (ignore, from_tty)
1713 char *ignore;
1714 int from_tty;
1715 {
1716 register struct display *d;
1717
1718 if (!display_chain)
1719 printf_unfiltered ("There are no auto-display expressions now.\n");
1720 else
1721 printf_filtered ("Auto-display expressions now in effect:\n\
1722 Num Enb Expression\n");
1723
1724 for (d = display_chain; d; d = d->next)
1725 {
1726 printf_filtered ("%d: %c ", d->number, "ny"[(int) d->status]);
1727 if (d->format.size)
1728 printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
1729 d->format.format);
1730 else if (d->format.format)
1731 printf_filtered ("/%c ", d->format.format);
1732 print_expression (d->exp, gdb_stdout);
1733 if (d->block && !contained_in (get_selected_block (), d->block))
1734 printf_filtered (" (cannot be evaluated in the current context)");
1735 printf_filtered ("\n");
1736 gdb_flush (gdb_stdout);
1737 }
1738 }
1739
1740 static void
1741 enable_display (args, from_tty)
1742 char *args;
1743 int from_tty;
1744 {
1745 register char *p = args;
1746 register char *p1;
1747 register int num;
1748 register struct display *d;
1749
1750 if (p == 0)
1751 {
1752 for (d = display_chain; d; d = d->next)
1753 d->status = enabled;
1754 }
1755 else
1756 while (*p)
1757 {
1758 p1 = p;
1759 while (*p1 >= '0' && *p1 <= '9')
1760 p1++;
1761 if (*p1 && *p1 != ' ' && *p1 != '\t')
1762 error ("Arguments must be display numbers.");
1763
1764 num = atoi (p);
1765
1766 for (d = display_chain; d; d = d->next)
1767 if (d->number == num)
1768 {
1769 d->status = enabled;
1770 goto win;
1771 }
1772 printf_unfiltered ("No display number %d.\n", num);
1773 win:
1774 p = p1;
1775 while (*p == ' ' || *p == '\t')
1776 p++;
1777 }
1778 }
1779
1780 /* ARGSUSED */
1781 static void
1782 disable_display_command (args, from_tty)
1783 char *args;
1784 int from_tty;
1785 {
1786 register char *p = args;
1787 register char *p1;
1788 register struct display *d;
1789
1790 if (p == 0)
1791 {
1792 for (d = display_chain; d; d = d->next)
1793 d->status = disabled;
1794 }
1795 else
1796 while (*p)
1797 {
1798 p1 = p;
1799 while (*p1 >= '0' && *p1 <= '9')
1800 p1++;
1801 if (*p1 && *p1 != ' ' && *p1 != '\t')
1802 error ("Arguments must be display numbers.");
1803
1804 disable_display (atoi (p));
1805
1806 p = p1;
1807 while (*p == ' ' || *p == '\t')
1808 p++;
1809 }
1810 }
1811 \f
1812
1813 /* Print the value in stack frame FRAME of a variable
1814 specified by a struct symbol. */
1815
1816 void
1817 print_variable_value (var, frame, stream)
1818 struct symbol *var;
1819 struct frame_info *frame;
1820 struct ui_file *stream;
1821 {
1822 value_ptr val = read_var_value (var, frame);
1823
1824 value_print (val, stream, 0, Val_pretty_default);
1825 }
1826
1827 /* Print the arguments of a stack frame, given the function FUNC
1828 running in that frame (as a symbol), the info on the frame,
1829 and the number of args according to the stack frame (or -1 if unknown). */
1830
1831 /* References here and elsewhere to "number of args according to the
1832 stack frame" appear in all cases to refer to "number of ints of args
1833 according to the stack frame". At least for VAX, i386, isi. */
1834
1835 void
1836 print_frame_args (func, fi, num, stream)
1837 struct symbol *func;
1838 struct frame_info *fi;
1839 int num;
1840 struct ui_file *stream;
1841 {
1842 struct block *b = NULL;
1843 int nsyms = 0;
1844 int first = 1;
1845 register int i;
1846 register struct symbol *sym;
1847 register value_ptr val;
1848 /* Offset of next stack argument beyond the one we have seen that is
1849 at the highest offset.
1850 -1 if we haven't come to a stack argument yet. */
1851 long highest_offset = -1;
1852 int arg_size;
1853 /* Number of ints of arguments that we have printed so far. */
1854 int args_printed = 0;
1855 #ifdef UI_OUT
1856 struct cleanup *old_chain;
1857 struct ui_stream *stb;
1858
1859 stb = ui_out_stream_new (uiout);
1860 old_chain = make_cleanup_ui_out_stream_delete (stb);
1861 #endif /* UI_OUT */
1862
1863 if (func)
1864 {
1865 b = SYMBOL_BLOCK_VALUE (func);
1866 nsyms = BLOCK_NSYMS (b);
1867 }
1868
1869 for (i = 0; i < nsyms; i++)
1870 {
1871 QUIT;
1872 sym = BLOCK_SYM (b, i);
1873
1874 /* Keep track of the highest stack argument offset seen, and
1875 skip over any kinds of symbols we don't care about. */
1876
1877 switch (SYMBOL_CLASS (sym))
1878 {
1879 case LOC_ARG:
1880 case LOC_REF_ARG:
1881 {
1882 long current_offset = SYMBOL_VALUE (sym);
1883 arg_size = TYPE_LENGTH (SYMBOL_TYPE (sym));
1884
1885 /* Compute address of next argument by adding the size of
1886 this argument and rounding to an int boundary. */
1887 current_offset =
1888 ((current_offset + arg_size + sizeof (int) - 1)
1889 & ~(sizeof (int) - 1));
1890
1891 /* If this is the highest offset seen yet, set highest_offset. */
1892 if (highest_offset == -1
1893 || (current_offset > highest_offset))
1894 highest_offset = current_offset;
1895
1896 /* Add the number of ints we're about to print to args_printed. */
1897 args_printed += (arg_size + sizeof (int) - 1) / sizeof (int);
1898 }
1899
1900 /* We care about types of symbols, but don't need to keep track of
1901 stack offsets in them. */
1902 case LOC_REGPARM:
1903 case LOC_REGPARM_ADDR:
1904 case LOC_LOCAL_ARG:
1905 case LOC_BASEREG_ARG:
1906 break;
1907
1908 /* Other types of symbols we just skip over. */
1909 default:
1910 continue;
1911 }
1912
1913 /* We have to look up the symbol because arguments can have
1914 two entries (one a parameter, one a local) and the one we
1915 want is the local, which lookup_symbol will find for us.
1916 This includes gcc1 (not gcc2) on the sparc when passing a
1917 small structure and gcc2 when the argument type is float
1918 and it is passed as a double and converted to float by
1919 the prologue (in the latter case the type of the LOC_ARG
1920 symbol is double and the type of the LOC_LOCAL symbol is
1921 float). */
1922 /* But if the parameter name is null, don't try it.
1923 Null parameter names occur on the RS/6000, for traceback tables.
1924 FIXME, should we even print them? */
1925
1926 if (*SYMBOL_NAME (sym))
1927 {
1928 struct symbol *nsym;
1929 nsym = lookup_symbol
1930 (SYMBOL_NAME (sym),
1931 b, VAR_NAMESPACE, (int *) NULL, (struct symtab **) NULL);
1932 if (SYMBOL_CLASS (nsym) == LOC_REGISTER)
1933 {
1934 /* There is a LOC_ARG/LOC_REGISTER pair. This means that
1935 it was passed on the stack and loaded into a register,
1936 or passed in a register and stored in a stack slot.
1937 GDB 3.x used the LOC_ARG; GDB 4.0-4.11 used the LOC_REGISTER.
1938
1939 Reasons for using the LOC_ARG:
1940 (1) because find_saved_registers may be slow for remote
1941 debugging,
1942 (2) because registers are often re-used and stack slots
1943 rarely (never?) are. Therefore using the stack slot is
1944 much less likely to print garbage.
1945
1946 Reasons why we might want to use the LOC_REGISTER:
1947 (1) So that the backtrace prints the same value as
1948 "print foo". I see no compelling reason why this needs
1949 to be the case; having the backtrace print the value which
1950 was passed in, and "print foo" print the value as modified
1951 within the called function, makes perfect sense to me.
1952
1953 Additional note: It might be nice if "info args" displayed
1954 both values.
1955 One more note: There is a case with sparc structure passing
1956 where we need to use the LOC_REGISTER, but this is dealt with
1957 by creating a single LOC_REGPARM in symbol reading. */
1958
1959 /* Leave sym (the LOC_ARG) alone. */
1960 ;
1961 }
1962 else
1963 sym = nsym;
1964 }
1965
1966 #ifdef UI_OUT
1967 /* Print the current arg. */
1968 if (!first)
1969 ui_out_text (uiout, ", ");
1970 ui_out_wrap_hint (uiout, " ");
1971
1972 annotate_arg_begin ();
1973
1974 ui_out_list_begin (uiout, NULL);
1975 fprintf_symbol_filtered (stb->stream, SYMBOL_SOURCE_NAME (sym),
1976 SYMBOL_LANGUAGE (sym), DMGL_PARAMS | DMGL_ANSI);
1977 ui_out_field_stream (uiout, "name", stb);
1978 annotate_arg_name_end ();
1979 ui_out_text (uiout, "=");
1980 #else
1981 /* Print the current arg. */
1982 if (!first)
1983 fprintf_filtered (stream, ", ");
1984 wrap_here (" ");
1985
1986 annotate_arg_begin ();
1987
1988 fprintf_symbol_filtered (stream, SYMBOL_SOURCE_NAME (sym),
1989 SYMBOL_LANGUAGE (sym), DMGL_PARAMS | DMGL_ANSI);
1990 annotate_arg_name_end ();
1991 fputs_filtered ("=", stream);
1992 #endif
1993
1994 /* Avoid value_print because it will deref ref parameters. We just
1995 want to print their addresses. Print ??? for args whose address
1996 we do not know. We pass 2 as "recurse" to val_print because our
1997 standard indentation here is 4 spaces, and val_print indents
1998 2 for each recurse. */
1999 val = read_var_value (sym, fi);
2000
2001 annotate_arg_value (val == NULL ? NULL : VALUE_TYPE (val));
2002
2003 if (val)
2004 {
2005 if (GDB_TARGET_IS_D10V
2006 && SYMBOL_CLASS (sym) == LOC_REGPARM && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_PTR)
2007 TYPE_LENGTH (VALUE_TYPE (val)) = 2;
2008 #ifdef UI_OUT
2009 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val), 0,
2010 VALUE_ADDRESS (val),
2011 stb->stream, 0, 0, 2, Val_no_prettyprint);
2012 ui_out_field_stream (uiout, "value", stb);
2013 }
2014 else
2015 ui_out_text (uiout, "???");
2016
2017 ui_out_list_end (uiout);
2018 #else
2019 val_print (VALUE_TYPE (val), VALUE_CONTENTS (val), 0,
2020 VALUE_ADDRESS (val),
2021 stream, 0, 0, 2, Val_no_prettyprint);
2022 }
2023 else
2024 fputs_filtered ("???", stream);
2025 #endif
2026
2027 annotate_arg_end ();
2028
2029 first = 0;
2030 }
2031
2032 /* Don't print nameless args in situations where we don't know
2033 enough about the stack to find them. */
2034 if (num != -1)
2035 {
2036 long start;
2037
2038 if (highest_offset == -1)
2039 start = FRAME_ARGS_SKIP;
2040 else
2041 start = highest_offset;
2042
2043 print_frame_nameless_args (fi, start, num - args_printed,
2044 first, stream);
2045 }
2046 #ifdef UI_OUT
2047 do_cleanups (old_chain);
2048 #endif /* no UI_OUT */
2049 }
2050
2051 /* Print nameless args on STREAM.
2052 FI is the frameinfo for this frame, START is the offset
2053 of the first nameless arg, and NUM is the number of nameless args to
2054 print. FIRST is nonzero if this is the first argument (not just
2055 the first nameless arg). */
2056
2057 static void
2058 print_frame_nameless_args (fi, start, num, first, stream)
2059 struct frame_info *fi;
2060 long start;
2061 int num;
2062 int first;
2063 struct ui_file *stream;
2064 {
2065 int i;
2066 CORE_ADDR argsaddr;
2067 long arg_value;
2068
2069 for (i = 0; i < num; i++)
2070 {
2071 QUIT;
2072 #ifdef NAMELESS_ARG_VALUE
2073 NAMELESS_ARG_VALUE (fi, start, &arg_value);
2074 #else
2075 argsaddr = FRAME_ARGS_ADDRESS (fi);
2076 if (!argsaddr)
2077 return;
2078
2079 arg_value = read_memory_integer (argsaddr + start, sizeof (int));
2080 #endif
2081
2082 if (!first)
2083 fprintf_filtered (stream, ", ");
2084
2085 #ifdef PRINT_NAMELESS_INTEGER
2086 PRINT_NAMELESS_INTEGER (stream, arg_value);
2087 #else
2088 #ifdef PRINT_TYPELESS_INTEGER
2089 PRINT_TYPELESS_INTEGER (stream, builtin_type_int, (LONGEST) arg_value);
2090 #else
2091 fprintf_filtered (stream, "%ld", arg_value);
2092 #endif /* PRINT_TYPELESS_INTEGER */
2093 #endif /* PRINT_NAMELESS_INTEGER */
2094 first = 0;
2095 start += sizeof (int);
2096 }
2097 }
2098 \f
2099 /* ARGSUSED */
2100 static void
2101 printf_command (arg, from_tty)
2102 char *arg;
2103 int from_tty;
2104 {
2105 register char *f = NULL;
2106 register char *s = arg;
2107 char *string = NULL;
2108 value_ptr *val_args;
2109 char *substrings;
2110 char *current_substring;
2111 int nargs = 0;
2112 int allocated_args = 20;
2113 struct cleanup *old_cleanups;
2114
2115 val_args = (value_ptr *) xmalloc (allocated_args * sizeof (value_ptr));
2116 old_cleanups = make_cleanup (free_current_contents, &val_args);
2117
2118 if (s == 0)
2119 error_no_arg ("format-control string and values to print");
2120
2121 /* Skip white space before format string */
2122 while (*s == ' ' || *s == '\t')
2123 s++;
2124
2125 /* A format string should follow, enveloped in double quotes */
2126 if (*s++ != '"')
2127 error ("Bad format string, missing '\"'.");
2128
2129 /* Parse the format-control string and copy it into the string STRING,
2130 processing some kinds of escape sequence. */
2131
2132 f = string = (char *) alloca (strlen (s) + 1);
2133
2134 while (*s != '"')
2135 {
2136 int c = *s++;
2137 switch (c)
2138 {
2139 case '\0':
2140 error ("Bad format string, non-terminated '\"'.");
2141
2142 case '\\':
2143 switch (c = *s++)
2144 {
2145 case '\\':
2146 *f++ = '\\';
2147 break;
2148 case 'a':
2149 #ifdef __STDC__
2150 *f++ = '\a';
2151 #else
2152 *f++ = '\007'; /* Bell */
2153 #endif
2154 break;
2155 case 'b':
2156 *f++ = '\b';
2157 break;
2158 case 'f':
2159 *f++ = '\f';
2160 break;
2161 case 'n':
2162 *f++ = '\n';
2163 break;
2164 case 'r':
2165 *f++ = '\r';
2166 break;
2167 case 't':
2168 *f++ = '\t';
2169 break;
2170 case 'v':
2171 *f++ = '\v';
2172 break;
2173 case '"':
2174 *f++ = '"';
2175 break;
2176 default:
2177 /* ??? TODO: handle other escape sequences */
2178 error ("Unrecognized escape character \\%c in format string.",
2179 c);
2180 }
2181 break;
2182
2183 default:
2184 *f++ = c;
2185 }
2186 }
2187
2188 /* Skip over " and following space and comma. */
2189 s++;
2190 *f++ = '\0';
2191 while (*s == ' ' || *s == '\t')
2192 s++;
2193
2194 if (*s != ',' && *s != 0)
2195 error ("Invalid argument syntax");
2196
2197 if (*s == ',')
2198 s++;
2199 while (*s == ' ' || *s == '\t')
2200 s++;
2201
2202 /* Need extra space for the '\0's. Doubling the size is sufficient. */
2203 substrings = alloca (strlen (string) * 2);
2204 current_substring = substrings;
2205
2206 {
2207 /* Now scan the string for %-specs and see what kinds of args they want.
2208 argclass[I] classifies the %-specs so we can give printf_filtered
2209 something of the right size. */
2210
2211 enum argclass
2212 {
2213 no_arg, int_arg, string_arg, double_arg, long_long_arg
2214 };
2215 enum argclass *argclass;
2216 enum argclass this_argclass;
2217 char *last_arg;
2218 int nargs_wanted;
2219 int lcount;
2220 int i;
2221
2222 argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
2223 nargs_wanted = 0;
2224 f = string;
2225 last_arg = string;
2226 while (*f)
2227 if (*f++ == '%')
2228 {
2229 lcount = 0;
2230 while (strchr ("0123456789.hlL-+ #", *f))
2231 {
2232 if (*f == 'l' || *f == 'L')
2233 lcount++;
2234 f++;
2235 }
2236 switch (*f)
2237 {
2238 case 's':
2239 this_argclass = string_arg;
2240 break;
2241
2242 case 'e':
2243 case 'f':
2244 case 'g':
2245 this_argclass = double_arg;
2246 break;
2247
2248 case '*':
2249 error ("`*' not supported for precision or width in printf");
2250
2251 case 'n':
2252 error ("Format specifier `n' not supported in printf");
2253
2254 case '%':
2255 this_argclass = no_arg;
2256 break;
2257
2258 default:
2259 if (lcount > 1)
2260 this_argclass = long_long_arg;
2261 else
2262 this_argclass = int_arg;
2263 break;
2264 }
2265 f++;
2266 if (this_argclass != no_arg)
2267 {
2268 strncpy (current_substring, last_arg, f - last_arg);
2269 current_substring += f - last_arg;
2270 *current_substring++ = '\0';
2271 last_arg = f;
2272 argclass[nargs_wanted++] = this_argclass;
2273 }
2274 }
2275
2276 /* Now, parse all arguments and evaluate them.
2277 Store the VALUEs in VAL_ARGS. */
2278
2279 while (*s != '\0')
2280 {
2281 char *s1;
2282 if (nargs == allocated_args)
2283 val_args = (value_ptr *) xrealloc ((char *) val_args,
2284 (allocated_args *= 2)
2285 * sizeof (value_ptr));
2286 s1 = s;
2287 val_args[nargs] = parse_to_comma_and_eval (&s1);
2288
2289 /* If format string wants a float, unchecked-convert the value to
2290 floating point of the same size */
2291
2292 if (argclass[nargs] == double_arg)
2293 {
2294 struct type *type = VALUE_TYPE (val_args[nargs]);
2295 if (TYPE_LENGTH (type) == sizeof (float))
2296 VALUE_TYPE (val_args[nargs]) = builtin_type_float;
2297 if (TYPE_LENGTH (type) == sizeof (double))
2298 VALUE_TYPE (val_args[nargs]) = builtin_type_double;
2299 }
2300 nargs++;
2301 s = s1;
2302 if (*s == ',')
2303 s++;
2304 }
2305
2306 if (nargs != nargs_wanted)
2307 error ("Wrong number of arguments for specified format-string");
2308
2309 /* Now actually print them. */
2310 current_substring = substrings;
2311 for (i = 0; i < nargs; i++)
2312 {
2313 switch (argclass[i])
2314 {
2315 case string_arg:
2316 {
2317 char *str;
2318 CORE_ADDR tem;
2319 int j;
2320 tem = value_as_pointer (val_args[i]);
2321
2322 /* This is a %s argument. Find the length of the string. */
2323 for (j = 0;; j++)
2324 {
2325 char c;
2326 QUIT;
2327 read_memory (tem + j, &c, 1);
2328 if (c == 0)
2329 break;
2330 }
2331
2332 /* Copy the string contents into a string inside GDB. */
2333 str = (char *) alloca (j + 1);
2334 if (j != 0)
2335 read_memory (tem, str, j);
2336 str[j] = 0;
2337
2338 printf_filtered (current_substring, str);
2339 }
2340 break;
2341 case double_arg:
2342 {
2343 double val = value_as_double (val_args[i]);
2344 printf_filtered (current_substring, val);
2345 break;
2346 }
2347 case long_long_arg:
2348 #if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2349 {
2350 long long val = value_as_long (val_args[i]);
2351 printf_filtered (current_substring, val);
2352 break;
2353 }
2354 #else
2355 error ("long long not supported in printf");
2356 #endif
2357 case int_arg:
2358 {
2359 /* FIXME: there should be separate int_arg and long_arg. */
2360 long val = value_as_long (val_args[i]);
2361 printf_filtered (current_substring, val);
2362 break;
2363 }
2364 default: /* purecov: deadcode */
2365 error ("internal error in printf_command"); /* purecov: deadcode */
2366 }
2367 /* Skip to the next substring. */
2368 current_substring += strlen (current_substring) + 1;
2369 }
2370 /* Print the portion of the format string after the last argument. */
2371 printf_filtered (last_arg);
2372 }
2373 do_cleanups (old_cleanups);
2374 }
2375 \f
2376 /* Dump a specified section of assembly code. With no command line
2377 arguments, this command will dump the assembly code for the
2378 function surrounding the pc value in the selected frame. With one
2379 argument, it will dump the assembly code surrounding that pc value.
2380 Two arguments are interpeted as bounds within which to dump
2381 assembly. */
2382
2383 /* ARGSUSED */
2384 static void
2385 disassemble_command (arg, from_tty)
2386 char *arg;
2387 int from_tty;
2388 {
2389 CORE_ADDR low, high;
2390 char *name;
2391 CORE_ADDR pc, pc_masked;
2392 char *space_index;
2393 #if 0
2394 asection *section;
2395 #endif
2396
2397 name = NULL;
2398 if (!arg)
2399 {
2400 if (!selected_frame)
2401 error ("No frame selected.\n");
2402
2403 pc = get_frame_pc (selected_frame);
2404 if (find_pc_partial_function (pc, &name, &low, &high) == 0)
2405 error ("No function contains program counter for selected frame.\n");
2406 #if defined(TUI)
2407 else if (tui_version)
2408 low = (CORE_ADDR) tuiDo ((TuiOpaqueFuncPtr) tui_vGetLowDisassemblyAddress,
2409 (Opaque) low,
2410 (Opaque) pc);
2411 #endif
2412 low += FUNCTION_START_OFFSET;
2413 }
2414 else if (!(space_index = (char *) strchr (arg, ' ')))
2415 {
2416 /* One argument. */
2417 pc = parse_and_eval_address (arg);
2418 if (find_pc_partial_function (pc, &name, &low, &high) == 0)
2419 error ("No function contains specified address.\n");
2420 #if defined(TUI)
2421 else if (tui_version)
2422 low = (CORE_ADDR) tuiDo ((TuiOpaqueFuncPtr) tui_vGetLowDisassemblyAddress,
2423 (Opaque) low,
2424 (Opaque) pc);
2425 #endif
2426 #if 0
2427 if (overlay_debugging)
2428 {
2429 section = find_pc_overlay (pc);
2430 if (pc_in_unmapped_range (pc, section))
2431 {
2432 /* find_pc_partial_function will have returned low and high
2433 relative to the symbolic (mapped) address range. Need to
2434 translate them back to the unmapped range where PC is. */
2435 low = overlay_unmapped_address (low, section);
2436 high = overlay_unmapped_address (high, section);
2437 }
2438 }
2439 #endif
2440 low += FUNCTION_START_OFFSET;
2441 }
2442 else
2443 {
2444 /* Two arguments. */
2445 *space_index = '\0';
2446 low = parse_and_eval_address (arg);
2447 high = parse_and_eval_address (space_index + 1);
2448 }
2449
2450 #if defined(TUI)
2451 if (!tui_version ||
2452 m_winPtrIsNull (disassemWin) || !disassemWin->generic.isVisible)
2453 #endif
2454 {
2455 printf_filtered ("Dump of assembler code ");
2456 if (name != NULL)
2457 {
2458 printf_filtered ("for function %s:\n", name);
2459 }
2460 else
2461 {
2462 printf_filtered ("from ");
2463 print_address_numeric (low, 1, gdb_stdout);
2464 printf_filtered (" to ");
2465 print_address_numeric (high, 1, gdb_stdout);
2466 printf_filtered (":\n");
2467 }
2468
2469 /* Dump the specified range. */
2470 pc = low;
2471
2472 #ifdef GDB_TARGET_MASK_DISAS_PC
2473 pc_masked = GDB_TARGET_MASK_DISAS_PC (pc);
2474 #else
2475 pc_masked = pc;
2476 #endif
2477
2478 while (pc_masked < high)
2479 {
2480 QUIT;
2481 print_address (pc_masked, gdb_stdout);
2482 printf_filtered (":\t");
2483 /* We often wrap here if there are long symbolic names. */
2484 wrap_here (" ");
2485 pc += print_insn (pc, gdb_stdout);
2486 printf_filtered ("\n");
2487
2488 #ifdef GDB_TARGET_MASK_DISAS_PC
2489 pc_masked = GDB_TARGET_MASK_DISAS_PC (pc);
2490 #else
2491 pc_masked = pc;
2492 #endif
2493 }
2494 printf_filtered ("End of assembler dump.\n");
2495 gdb_flush (gdb_stdout);
2496 }
2497 #if defined(TUI)
2498 else
2499 {
2500 tuiDo ((TuiOpaqueFuncPtr) tui_vAddWinToLayout, DISASSEM_WIN);
2501 tuiDo ((TuiOpaqueFuncPtr) tui_vUpdateSourceWindowsWithAddr, low);
2502 }
2503 #endif
2504 }
2505
2506 /* Print the instruction at address MEMADDR in debugged memory,
2507 on STREAM. Returns length of the instruction, in bytes. */
2508
2509 static int
2510 print_insn (memaddr, stream)
2511 CORE_ADDR memaddr;
2512 struct ui_file *stream;
2513 {
2514 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
2515 TARGET_PRINT_INSN_INFO->endian = BFD_ENDIAN_BIG;
2516 else
2517 TARGET_PRINT_INSN_INFO->endian = BFD_ENDIAN_LITTLE;
2518
2519 if (TARGET_ARCHITECTURE != NULL)
2520 TARGET_PRINT_INSN_INFO->mach = TARGET_ARCHITECTURE->mach;
2521 /* else: should set .mach=0 but some disassemblers don't grok this */
2522
2523 return TARGET_PRINT_INSN (memaddr, TARGET_PRINT_INSN_INFO);
2524 }
2525 \f
2526
2527 void
2528 _initialize_printcmd ()
2529 {
2530 current_display_number = -1;
2531
2532 add_info ("address", address_info,
2533 "Describe where symbol SYM is stored.");
2534
2535 add_info ("symbol", sym_info,
2536 "Describe what symbol is at location ADDR.\n\
2537 Only for symbols with fixed locations (global or static scope).");
2538
2539 add_com ("x", class_vars, x_command,
2540 concat ("Examine memory: x/FMT ADDRESS.\n\
2541 ADDRESS is an expression for the memory address to examine.\n\
2542 FMT is a repeat count followed by a format letter and a size letter.\n\
2543 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
2544 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n",
2545 "Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
2546 The specified number of objects of the specified size are printed\n\
2547 according to the format.\n\n\
2548 Defaults for format and size letters are those previously used.\n\
2549 Default count is 1. Default address is following last thing printed\n\
2550 with this command or \"print\".", NULL));
2551
2552 add_com ("disassemble", class_vars, disassemble_command,
2553 "Disassemble a specified section of memory.\n\
2554 Default is the function surrounding the pc of the selected frame.\n\
2555 With a single argument, the function surrounding that address is dumped.\n\
2556 Two arguments are taken as a range of memory to dump.");
2557 if (xdb_commands)
2558 add_com_alias ("va", "disassemble", class_xdb, 0);
2559
2560 #if 0
2561 add_com ("whereis", class_vars, whereis_command,
2562 "Print line number and file of definition of variable.");
2563 #endif
2564
2565 add_info ("display", display_info,
2566 "Expressions to display when program stops, with code numbers.");
2567
2568 add_cmd ("undisplay", class_vars, undisplay_command,
2569 "Cancel some expressions to be displayed when program stops.\n\
2570 Arguments are the code numbers of the expressions to stop displaying.\n\
2571 No argument means cancel all automatic-display expressions.\n\
2572 \"delete display\" has the same effect as this command.\n\
2573 Do \"info display\" to see current list of code numbers.",
2574 &cmdlist);
2575
2576 add_com ("display", class_vars, display_command,
2577 "Print value of expression EXP each time the program stops.\n\
2578 /FMT may be used before EXP as in the \"print\" command.\n\
2579 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2580 as in the \"x\" command, and then EXP is used to get the address to examine\n\
2581 and examining is done as in the \"x\" command.\n\n\
2582 With no argument, display all currently requested auto-display expressions.\n\
2583 Use \"undisplay\" to cancel display requests previously made."
2584 );
2585
2586 add_cmd ("display", class_vars, enable_display,
2587 "Enable some expressions to be displayed when program stops.\n\
2588 Arguments are the code numbers of the expressions to resume displaying.\n\
2589 No argument means enable all automatic-display expressions.\n\
2590 Do \"info display\" to see current list of code numbers.", &enablelist);
2591
2592 add_cmd ("display", class_vars, disable_display_command,
2593 "Disable some expressions to be displayed when program stops.\n\
2594 Arguments are the code numbers of the expressions to stop displaying.\n\
2595 No argument means disable all automatic-display expressions.\n\
2596 Do \"info display\" to see current list of code numbers.", &disablelist);
2597
2598 add_cmd ("display", class_vars, undisplay_command,
2599 "Cancel some expressions to be displayed when program stops.\n\
2600 Arguments are the code numbers of the expressions to stop displaying.\n\
2601 No argument means cancel all automatic-display expressions.\n\
2602 Do \"info display\" to see current list of code numbers.", &deletelist);
2603
2604 add_com ("printf", class_vars, printf_command,
2605 "printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2606 This is useful for formatted output in user-defined commands.");
2607
2608 add_com ("output", class_vars, output_command,
2609 "Like \"print\" but don't put in value history and don't print newline.\n\
2610 This is useful in user-defined commands.");
2611
2612 add_prefix_cmd ("set", class_vars, set_command,
2613 concat ("Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2614 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2615 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2616 with $), a register (a few standard names starting with $), or an actual\n\
2617 variable in the program being debugged. EXP is any valid expression.\n",
2618 "Use \"set variable\" for variables with names identical to set subcommands.\n\
2619 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2620 You can see these environment settings with the \"show\" command.", NULL),
2621 &setlist, "set ", 1, &cmdlist);
2622 if (dbx_commands)
2623 add_com ("assign", class_vars, set_command, concat ("Evaluate expression \
2624 EXP and assign result to variable VAR, using assignment\n\
2625 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2626 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2627 with $), a register (a few standard names starting with $), or an actual\n\
2628 variable in the program being debugged. EXP is any valid expression.\n",
2629 "Use \"set variable\" for variables with names identical to set subcommands.\n\
2630 \nWith a subcommand, this command modifies parts of the gdb environment.\n\
2631 You can see these environment settings with the \"show\" command.", NULL));
2632
2633 /* "call" is the same as "set", but handy for dbx users to call fns. */
2634 add_com ("call", class_vars, call_command,
2635 "Call a function in the program.\n\
2636 The argument is the function name and arguments, in the notation of the\n\
2637 current working language. The result is printed and saved in the value\n\
2638 history, if it is not void.");
2639
2640 add_cmd ("variable", class_vars, set_command,
2641 "Evaluate expression EXP and assign result to variable VAR, using assignment\n\
2642 syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2643 example). VAR may be a debugger \"convenience\" variable (names starting\n\
2644 with $), a register (a few standard names starting with $), or an actual\n\
2645 variable in the program being debugged. EXP is any valid expression.\n\
2646 This may usually be abbreviated to simply \"set\".",
2647 &setlist);
2648
2649 add_com ("print", class_vars, print_command,
2650 concat ("Print value of expression EXP.\n\
2651 Variables accessible are those of the lexical environment of the selected\n\
2652 stack frame, plus all those whose scope is global or an entire file.\n\
2653 \n\
2654 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2655 $$NUM refers to NUM'th value back from the last one.\n\
2656 Names starting with $ refer to registers (with the values they would have\n",
2657 "if the program were to return to the stack frame now selected, restoring\n\
2658 all registers saved by frames farther in) or else to debugger\n\
2659 \"convenience\" variables (any such name not a known register).\n\
2660 Use assignment expressions to give values to convenience variables.\n",
2661 "\n\
2662 {TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2663 @ is a binary operator for treating consecutive data objects\n\
2664 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2665 element is FOO, whose second element is stored in the space following\n\
2666 where FOO is stored, etc. FOO must be an expression whose value\n\
2667 resides in memory.\n",
2668 "\n\
2669 EXP may be preceded with /FMT, where FMT is a format letter\n\
2670 but no count or size letter (see \"x\" command).", NULL));
2671 add_com_alias ("p", "print", class_vars, 1);
2672
2673 add_com ("inspect", class_vars, inspect_command,
2674 "Same as \"print\" command, except that if you are running in the epoch\n\
2675 environment, the value is printed in its own window.");
2676
2677 add_show_from_set (
2678 add_set_cmd ("max-symbolic-offset", no_class, var_uinteger,
2679 (char *) &max_symbolic_offset,
2680 "Set the largest offset that will be printed in <symbol+1234> form.",
2681 &setprintlist),
2682 &showprintlist);
2683 add_show_from_set (
2684 add_set_cmd ("symbol-filename", no_class, var_boolean,
2685 (char *) &print_symbol_filename,
2686 "Set printing of source filename and line number with <symbol>.",
2687 &setprintlist),
2688 &showprintlist);
2689
2690 /* For examine/instruction a single byte quantity is specified as
2691 the data. This avoids problems with value_at_lazy() requiring a
2692 valid data type (and rejecting VOID). */
2693 examine_i_type = init_type (TYPE_CODE_INT, 1, 0, "examine_i_type", NULL);
2694
2695 examine_b_type = init_type (TYPE_CODE_INT, 1, 0, "examine_b_type", NULL);
2696 examine_h_type = init_type (TYPE_CODE_INT, 2, 0, "examine_h_type", NULL);
2697 examine_w_type = init_type (TYPE_CODE_INT, 4, 0, "examine_w_type", NULL);
2698 examine_g_type = init_type (TYPE_CODE_INT, 8, 0, "examine_g_type", NULL);
2699
2700 }