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