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