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