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