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