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