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