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