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