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