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