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