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