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