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