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