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