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