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