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