2008-05-27 Martin Schwidefsky <schwidefsky@de.ibm.com>
[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 ("_"),
4478b372
JB
516 value_from_pointer (lookup_pointer_type (builtin_type_void),
517 addr));
c906108c
SS
518}
519
520/* Optionally print address ADDR symbolically as <SYMBOL+OFFSET> on STREAM,
521 after LEADIN. Print nothing if no symbolic name is found nearby.
522 Optionally also print source file and line number, if available.
523 DO_DEMANGLE controls whether to print a symbol in its native "raw" form,
524 or to interpret it as a possible C++ name and convert it back to source
525 form. However note that DO_DEMANGLE can be overridden by the specific
526 settings of the demangle and asm_demangle variables. */
527
528void
675dcf4f
MK
529print_address_symbolic (CORE_ADDR addr, struct ui_file *stream,
530 int do_demangle, char *leadin)
dfcd3bfb
JM
531{
532 char *name = NULL;
533 char *filename = NULL;
534 int unmapped = 0;
535 int offset = 0;
536 int line = 0;
537
675dcf4f 538 /* Throw away both name and filename. */
2f9429ae
AC
539 struct cleanup *cleanup_chain = make_cleanup (free_current_contents, &name);
540 make_cleanup (free_current_contents, &filename);
dfcd3bfb 541
675dcf4f
MK
542 if (build_address_symbolic (addr, do_demangle, &name, &offset,
543 &filename, &line, &unmapped))
2f9429ae
AC
544 {
545 do_cleanups (cleanup_chain);
546 return;
547 }
dfcd3bfb
JM
548
549 fputs_filtered (leadin, stream);
550 if (unmapped)
551 fputs_filtered ("<*", stream);
552 else
553 fputs_filtered ("<", stream);
554 fputs_filtered (name, stream);
555 if (offset != 0)
556 fprintf_filtered (stream, "+%u", (unsigned int) offset);
557
558 /* Append source filename and line number if desired. Give specific
559 line # of this addr, if we have it; else line # of the nearest symbol. */
560 if (print_symbol_filename && filename != NULL)
561 {
562 if (line != -1)
563 fprintf_filtered (stream, " at %s:%d", filename, line);
564 else
565 fprintf_filtered (stream, " in %s", filename);
566 }
567 if (unmapped)
568 fputs_filtered ("*>", stream);
569 else
570 fputs_filtered (">", stream);
571
572 do_cleanups (cleanup_chain);
573}
574
575/* Given an address ADDR return all the elements needed to print the
576 address in a symbolic form. NAME can be mangled or not depending
577 on DO_DEMANGLE (and also on the asm_demangle global variable,
578 manipulated via ''set print asm-demangle''). Return 0 in case of
579 success, when all the info in the OUT paramters is valid. Return 1
580 otherwise. */
581int
582build_address_symbolic (CORE_ADDR addr, /* IN */
583 int do_demangle, /* IN */
584 char **name, /* OUT */
585 int *offset, /* OUT */
586 char **filename, /* OUT */
587 int *line, /* OUT */
588 int *unmapped) /* OUT */
c906108c
SS
589{
590 struct minimal_symbol *msymbol;
591 struct symbol *symbol;
c906108c 592 CORE_ADDR name_location = 0;
c906108c 593 asection *section = 0;
dfcd3bfb
JM
594 char *name_temp = "";
595
675dcf4f 596 /* Let's say it is unmapped. */
dfcd3bfb 597 *unmapped = 0;
c906108c 598
dfcd3bfb 599 /* Determine if the address is in an overlay, and whether it is
675dcf4f 600 mapped. */
c906108c
SS
601 if (overlay_debugging)
602 {
603 section = find_pc_overlay (addr);
604 if (pc_in_unmapped_range (addr, section))
605 {
dfcd3bfb 606 *unmapped = 1;
c906108c
SS
607 addr = overlay_mapped_address (addr, section);
608 }
609 }
610
c906108c
SS
611 /* First try to find the address in the symbol table, then
612 in the minsyms. Take the closest one. */
613
614 /* This is defective in the sense that it only finds text symbols. So
615 really this is kind of pointless--we should make sure that the
616 minimal symbols have everything we need (by changing that we could
617 save some memory, but for many debug format--ELF/DWARF or
618 anything/stabs--it would be inconvenient to eliminate those minimal
619 symbols anyway). */
620 msymbol = lookup_minimal_symbol_by_pc_section (addr, section);
621 symbol = find_pc_sect_function (addr, section);
622
623 if (symbol)
624 {
625 name_location = BLOCK_START (SYMBOL_BLOCK_VALUE (symbol));
406fc7fb 626 if (do_demangle || asm_demangle)
de5ad195 627 name_temp = SYMBOL_PRINT_NAME (symbol);
c906108c 628 else
22abf04a 629 name_temp = DEPRECATED_SYMBOL_NAME (symbol);
c906108c
SS
630 }
631
632 if (msymbol != NULL)
633 {
634 if (SYMBOL_VALUE_ADDRESS (msymbol) > name_location || symbol == NULL)
635 {
636 /* The msymbol is closer to the address than the symbol;
637 use the msymbol instead. */
638 symbol = 0;
c906108c 639 name_location = SYMBOL_VALUE_ADDRESS (msymbol);
406fc7fb 640 if (do_demangle || asm_demangle)
de5ad195 641 name_temp = SYMBOL_PRINT_NAME (msymbol);
c906108c 642 else
22abf04a 643 name_temp = DEPRECATED_SYMBOL_NAME (msymbol);
c906108c
SS
644 }
645 }
646 if (symbol == NULL && msymbol == NULL)
dfcd3bfb 647 return 1;
c906108c 648
c906108c
SS
649 /* If the nearest symbol is too far away, don't print anything symbolic. */
650
651 /* For when CORE_ADDR is larger than unsigned int, we do math in
652 CORE_ADDR. But when we detect unsigned wraparound in the
653 CORE_ADDR math, we ignore this test and print the offset,
654 because addr+max_symbolic_offset has wrapped through the end
655 of the address space back to the beginning, giving bogus comparison. */
656 if (addr > name_location + max_symbolic_offset
657 && name_location + max_symbolic_offset > name_location)
dfcd3bfb 658 return 1;
c906108c 659
dfcd3bfb
JM
660 *offset = addr - name_location;
661
662 *name = xstrdup (name_temp);
c906108c 663
c906108c
SS
664 if (print_symbol_filename)
665 {
666 struct symtab_and_line sal;
667
668 sal = find_pc_sect_line (addr, section, 0);
669
670 if (sal.symtab)
dfcd3bfb
JM
671 {
672 *filename = xstrdup (sal.symtab->filename);
673 *line = sal.line;
674 }
c906108c 675 }
dfcd3bfb 676 return 0;
c906108c
SS
677}
678
c906108c
SS
679
680/* Print address ADDR symbolically on STREAM.
681 First print it as a number. Then perhaps print
682 <SYMBOL + OFFSET> after the number. */
683
684void
fba45db2 685print_address (CORE_ADDR addr, struct ui_file *stream)
c906108c 686{
ed49a04f 687 fputs_filtered (paddress (addr), stream);
c906108c
SS
688 print_address_symbolic (addr, stream, asm_demangle, " ");
689}
690
691/* Print address ADDR symbolically on STREAM. Parameter DEMANGLE
692 controls whether to print the symbolic name "raw" or demangled.
693 Global setting "addressprint" controls whether to print hex address
694 or not. */
695
696void
675dcf4f
MK
697print_address_demangle (CORE_ADDR addr, struct ui_file *stream,
698 int do_demangle)
c906108c
SS
699{
700 if (addr == 0)
701 {
702 fprintf_filtered (stream, "0");
703 }
704 else if (addressprint)
705 {
ed49a04f 706 fputs_filtered (paddress (addr), stream);
c906108c
SS
707 print_address_symbolic (addr, stream, do_demangle, " ");
708 }
709 else
710 {
711 print_address_symbolic (addr, stream, do_demangle, "");
712 }
713}
714\f
715
716/* These are the types that $__ will get after an examine command of one
717 of these sizes. */
718
719static struct type *examine_i_type;
720
721static struct type *examine_b_type;
722static struct type *examine_h_type;
723static struct type *examine_w_type;
724static struct type *examine_g_type;
725
726/* Examine data at address ADDR in format FMT.
727 Fetch it from memory and print on gdb_stdout. */
728
729static void
00a4c844 730do_examine (struct format_data fmt, CORE_ADDR addr)
c906108c 731{
52f0bd74
AC
732 char format = 0;
733 char size;
734 int count = 1;
c906108c 735 struct type *val_type = NULL;
52f0bd74
AC
736 int i;
737 int maxelts;
c906108c
SS
738
739 format = fmt.format;
740 size = fmt.size;
741 count = fmt.count;
742 next_address = addr;
c906108c
SS
743
744 /* String or instruction format implies fetch single bytes
745 regardless of the specified size. */
746 if (format == 's' || format == 'i')
747 size = 'b';
748
749 if (format == 'i')
750 val_type = examine_i_type;
751 else if (size == 'b')
752 val_type = examine_b_type;
753 else if (size == 'h')
754 val_type = examine_h_type;
755 else if (size == 'w')
756 val_type = examine_w_type;
757 else if (size == 'g')
758 val_type = examine_g_type;
759
760 maxelts = 8;
761 if (size == 'w')
762 maxelts = 4;
763 if (size == 'g')
764 maxelts = 2;
765 if (format == 's' || format == 'i')
766 maxelts = 1;
767
768 /* Print as many objects as specified in COUNT, at most maxelts per line,
769 with the address of the next one at the start of each line. */
770
771 while (count > 0)
772 {
773 QUIT;
774 print_address (next_address, gdb_stdout);
775 printf_filtered (":");
776 for (i = maxelts;
777 i > 0 && count > 0;
778 i--, count--)
779 {
780 printf_filtered ("\t");
781 /* Note that print_formatted sets next_address for the next
782 object. */
783 last_examine_address = next_address;
784
785 if (last_examine_value)
786 value_free (last_examine_value);
787
788 /* The value to be displayed is not fetched greedily.
5d51a2db
MR
789 Instead, to avoid the possibility of a fetched value not
790 being used, its retrieval is delayed until the print code
c5aa993b
JM
791 uses it. When examining an instruction stream, the
792 disassembler will perform its own memory fetch using just
793 the address stored in LAST_EXAMINE_VALUE. FIXME: Should
794 the disassembler be modified so that LAST_EXAMINE_VALUE
795 is left with the byte sequence from the last complete
796 instruction fetched from memory? */
00a4c844 797 last_examine_value = value_at_lazy (val_type, next_address);
c906108c
SS
798
799 if (last_examine_value)
800 release_value (last_examine_value);
801
2acceee2 802 print_formatted (last_examine_value, format, size, gdb_stdout);
a4642986
MR
803
804 /* Display any branch delay slots following the final insn. */
805 if (format == 'i' && count == 1)
806 count += branch_delay_insns;
c906108c
SS
807 }
808 printf_filtered ("\n");
809 gdb_flush (gdb_stdout);
810 }
811}
812\f
813static void
fba45db2 814validate_format (struct format_data fmt, char *cmdname)
c906108c
SS
815{
816 if (fmt.size != 0)
8a3fe4f8 817 error (_("Size letters are meaningless in \"%s\" command."), cmdname);
c906108c 818 if (fmt.count != 1)
8a3fe4f8 819 error (_("Item count other than 1 is meaningless in \"%s\" command."),
c906108c 820 cmdname);
ea37ba09 821 if (fmt.format == 'i')
8a3fe4f8 822 error (_("Format letter \"%c\" is meaningless in \"%s\" command."),
c906108c
SS
823 fmt.format, cmdname);
824}
825
675dcf4f 826/* Evaluate string EXP as an expression in the current language and
c5aa993b 827 print the resulting value. EXP may contain a format specifier as the
675dcf4f 828 first argument ("/x myvar" for example, to print myvar in hex). */
c906108c
SS
829
830static void
fba45db2 831print_command_1 (char *exp, int inspect, int voidprint)
c906108c
SS
832{
833 struct expression *expr;
52f0bd74
AC
834 struct cleanup *old_chain = 0;
835 char format = 0;
3d6d86c6 836 struct value *val;
c906108c
SS
837 struct format_data fmt;
838 int cleanup = 0;
839
675dcf4f
MK
840 /* Pass inspect flag to the rest of the print routines in a global
841 (sigh). */
c906108c
SS
842 inspect_it = inspect;
843
844 if (exp && *exp == '/')
845 {
846 exp++;
847 fmt = decode_format (&exp, last_format, 0);
848 validate_format (fmt, "print");
849 last_format = format = fmt.format;
850 }
851 else
852 {
853 fmt.count = 1;
854 fmt.format = 0;
855 fmt.size = 0;
856 }
857
858 if (exp && *exp)
859 {
c906108c
SS
860 struct type *type;
861 expr = parse_expression (exp);
c13c43fd 862 old_chain = make_cleanup (free_current_contents, &expr);
c906108c
SS
863 cleanup = 1;
864 val = evaluate_expression (expr);
c906108c
SS
865 }
866 else
867 val = access_value_history (0);
868
df407dfe
AC
869 if (voidprint || (val && value_type (val) &&
870 TYPE_CODE (value_type (val)) != TYPE_CODE_VOID))
c906108c
SS
871 {
872 int histindex = record_latest_value (val);
873
874 if (histindex >= 0)
df407dfe 875 annotate_value_history_begin (histindex, value_type (val));
c906108c 876 else
df407dfe 877 annotate_value_begin (value_type (val));
c906108c
SS
878
879 if (inspect)
675dcf4f
MK
880 printf_unfiltered ("\031(gdb-makebuffer \"%s\" %d '(\"",
881 exp, histindex);
c5aa993b
JM
882 else if (histindex >= 0)
883 printf_filtered ("$%d = ", histindex);
c906108c
SS
884
885 if (histindex >= 0)
886 annotate_value_history_value ();
887
2acceee2 888 print_formatted (val, format, fmt.size, gdb_stdout);
c906108c
SS
889 printf_filtered ("\n");
890
891 if (histindex >= 0)
892 annotate_value_history_end ();
893 else
894 annotate_value_end ();
895
896 if (inspect)
c5aa993b 897 printf_unfiltered ("\") )\030");
c906108c
SS
898 }
899
900 if (cleanup)
901 do_cleanups (old_chain);
675dcf4f 902 inspect_it = 0; /* Reset print routines to normal. */
c906108c
SS
903}
904
c906108c 905static void
fba45db2 906print_command (char *exp, int from_tty)
c906108c
SS
907{
908 print_command_1 (exp, 0, 1);
909}
910
675dcf4f 911/* Same as print, except in epoch, it gets its own window. */
c906108c 912static void
fba45db2 913inspect_command (char *exp, int from_tty)
c906108c
SS
914{
915 extern int epoch_interface;
916
917 print_command_1 (exp, epoch_interface, 1);
918}
919
675dcf4f 920/* Same as print, except it doesn't print void results. */
c906108c 921static void
fba45db2 922call_command (char *exp, int from_tty)
c906108c
SS
923{
924 print_command_1 (exp, 0, 0);
925}
926
c906108c 927void
fba45db2 928output_command (char *exp, int from_tty)
c906108c
SS
929{
930 struct expression *expr;
52f0bd74
AC
931 struct cleanup *old_chain;
932 char format = 0;
3d6d86c6 933 struct value *val;
c906108c
SS
934 struct format_data fmt;
935
777ea8f1
DJ
936 fmt.size = 0;
937
c906108c
SS
938 if (exp && *exp == '/')
939 {
940 exp++;
941 fmt = decode_format (&exp, 0, 0);
942 validate_format (fmt, "output");
943 format = fmt.format;
944 }
945
946 expr = parse_expression (exp);
c13c43fd 947 old_chain = make_cleanup (free_current_contents, &expr);
c906108c
SS
948
949 val = evaluate_expression (expr);
950
df407dfe 951 annotate_value_begin (value_type (val));
c906108c 952
2acceee2 953 print_formatted (val, format, fmt.size, gdb_stdout);
c906108c
SS
954
955 annotate_value_end ();
956
2acceee2
JM
957 wrap_here ("");
958 gdb_flush (gdb_stdout);
959
c906108c
SS
960 do_cleanups (old_chain);
961}
962
c906108c 963static void
fba45db2 964set_command (char *exp, int from_tty)
c906108c
SS
965{
966 struct expression *expr = parse_expression (exp);
52f0bd74 967 struct cleanup *old_chain =
c13c43fd 968 make_cleanup (free_current_contents, &expr);
c906108c
SS
969 evaluate_expression (expr);
970 do_cleanups (old_chain);
971}
972
c906108c 973static void
fba45db2 974sym_info (char *arg, int from_tty)
c906108c
SS
975{
976 struct minimal_symbol *msymbol;
c5aa993b
JM
977 struct objfile *objfile;
978 struct obj_section *osect;
979 asection *sect;
980 CORE_ADDR addr, sect_addr;
981 int matches = 0;
982 unsigned int offset;
c906108c
SS
983
984 if (!arg)
e2e0b3e5 985 error_no_arg (_("address"));
c906108c
SS
986
987 addr = parse_and_eval_address (arg);
988 ALL_OBJSECTIONS (objfile, osect)
c5aa993b 989 {
94277a38
DJ
990 /* Only process each object file once, even if there's a separate
991 debug file. */
992 if (objfile->separate_debug_objfile_backlink)
993 continue;
994
c5aa993b
JM
995 sect = osect->the_bfd_section;
996 sect_addr = overlay_mapped_address (addr, sect);
c906108c 997
c5aa993b
JM
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 ",
de5ad195 1005 SYMBOL_PRINT_NAME (msymbol), offset);
c5aa993b
JM
1006 else
1007 printf_filtered ("%s in ",
de5ad195 1008 SYMBOL_PRINT_NAME (msymbol));
c5aa993b 1009 if (pc_in_unmapped_range (addr, sect))
a3f17187 1010 printf_filtered (_("load address range of "));
c5aa993b 1011 if (section_is_overlay (sect))
a3f17187 1012 printf_filtered (_("%s overlay "),
c5aa993b 1013 section_is_mapped (sect) ? "mapped" : "unmapped");
a3f17187 1014 printf_filtered (_("section %s"), sect->name);
c5aa993b
JM
1015 printf_filtered ("\n");
1016 }
1017 }
c906108c 1018 if (matches == 0)
a3f17187 1019 printf_filtered (_("No symbol matches %s.\n"), arg);
c906108c
SS
1020}
1021
c906108c 1022static void
fba45db2 1023address_info (char *exp, int from_tty)
c906108c 1024{
52f0bd74
AC
1025 struct symbol *sym;
1026 struct minimal_symbol *msymbol;
1027 long val;
c906108c
SS
1028 asection *section;
1029 CORE_ADDR load_addr;
1030 int is_a_field_of_this; /* C++: lookup_symbol sets this to nonzero
1031 if exp is a field of `this'. */
1032
1033 if (exp == 0)
8a3fe4f8 1034 error (_("Argument required."));
c906108c 1035
176620f1 1036 sym = lookup_symbol (exp, get_selected_block (0), VAR_DOMAIN,
2570f2b7 1037 &is_a_field_of_this);
c906108c
SS
1038 if (sym == NULL)
1039 {
1040 if (is_a_field_of_this)
1041 {
1042 printf_filtered ("Symbol \"");
1043 fprintf_symbol_filtered (gdb_stdout, exp,
1044 current_language->la_language, DMGL_ANSI);
e2b23ee9
AF
1045 printf_filtered ("\" is a field of the local class variable ");
1046 if (current_language->la_language == language_objc)
2625d86c 1047 printf_filtered ("`self'\n"); /* ObjC equivalent of "this" */
e2b23ee9 1048 else
2625d86c 1049 printf_filtered ("`this'\n");
c906108c
SS
1050 return;
1051 }
1052
1053 msymbol = lookup_minimal_symbol (exp, NULL, NULL);
1054
1055 if (msymbol != NULL)
1056 {
1057 load_addr = SYMBOL_VALUE_ADDRESS (msymbol);
1058
1059 printf_filtered ("Symbol \"");
1060 fprintf_symbol_filtered (gdb_stdout, exp,
1061 current_language->la_language, DMGL_ANSI);
1062 printf_filtered ("\" is at ");
ed49a04f 1063 fputs_filtered (paddress (load_addr), gdb_stdout);
c906108c
SS
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 ");
ed49a04f 1070 fputs_filtered (paddress (load_addr), gdb_stdout);
c906108c
SS
1071 printf_filtered (" in overlay section %s", section->name);
1072 }
1073 printf_filtered (".\n");
1074 }
1075 else
8a3fe4f8 1076 error (_("No symbol \"%s\" in current context."), exp);
c906108c
SS
1077 return;
1078 }
1079
1080 printf_filtered ("Symbol \"");
22abf04a 1081 fprintf_symbol_filtered (gdb_stdout, DEPRECATED_SYMBOL_NAME (sym),
c906108c
SS
1082 current_language->la_language, DMGL_ANSI);
1083 printf_filtered ("\" is ");
c5aa993b 1084 val = SYMBOL_VALUE (sym);
c906108c
SS
1085 section = SYMBOL_BFD_SECTION (sym);
1086
1087 switch (SYMBOL_CLASS (sym))
1088 {
1089 case LOC_CONST:
1090 case LOC_CONST_BYTES:
1091 printf_filtered ("constant");
1092 break;
1093
1094 case LOC_LABEL:
1095 printf_filtered ("a label at address ");
ed49a04f
MD
1096 fputs_filtered (paddress (load_addr = SYMBOL_VALUE_ADDRESS (sym)),
1097 gdb_stdout);
c906108c
SS
1098 if (section_is_overlay (section))
1099 {
1100 load_addr = overlay_unmapped_address (load_addr, section);
1101 printf_filtered (",\n -- loaded at ");
ed49a04f 1102 fputs_filtered (paddress (load_addr), gdb_stdout);
c906108c
SS
1103 printf_filtered (" in overlay section %s", section->name);
1104 }
1105 break;
1106
4c2df51b
DJ
1107 case LOC_COMPUTED:
1108 case LOC_COMPUTED_ARG:
a67af2b9
AC
1109 /* FIXME: cagney/2004-01-26: It should be possible to
1110 unconditionally call the SYMBOL_OPS method when available.
d3efc286 1111 Unfortunately DWARF 2 stores the frame-base (instead of the
a67af2b9
AC
1112 function) location in a function's symbol. Oops! For the
1113 moment enable this when/where applicable. */
1114 SYMBOL_OPS (sym)->describe_location (sym, gdb_stdout);
4c2df51b
DJ
1115 break;
1116
c906108c 1117 case LOC_REGISTER:
c9f4d572
UW
1118 printf_filtered (_("a variable in register %s"),
1119 gdbarch_register_name (current_gdbarch, val));
c906108c
SS
1120 break;
1121
1122 case LOC_STATIC:
a3f17187 1123 printf_filtered (_("static storage at address "));
ed49a04f
MD
1124 fputs_filtered (paddress (load_addr = SYMBOL_VALUE_ADDRESS (sym)),
1125 gdb_stdout);
c906108c
SS
1126 if (section_is_overlay (section))
1127 {
1128 load_addr = overlay_unmapped_address (load_addr, section);
a3f17187 1129 printf_filtered (_(",\n -- loaded at "));
ed49a04f 1130 fputs_filtered (paddress (load_addr), gdb_stdout);
a3f17187 1131 printf_filtered (_(" in overlay section %s"), section->name);
c906108c
SS
1132 }
1133 break;
1134
c906108c 1135 case LOC_REGPARM:
c9f4d572
UW
1136 printf_filtered (_("an argument in register %s"),
1137 gdbarch_register_name (current_gdbarch, val));
c906108c
SS
1138 break;
1139
1140 case LOC_REGPARM_ADDR:
675dcf4f 1141 printf_filtered (_("address of an argument in register %s"),
c9f4d572 1142 gdbarch_register_name (current_gdbarch, val));
c906108c
SS
1143 break;
1144
1145 case LOC_ARG:
a3f17187 1146 printf_filtered (_("an argument at offset %ld"), val);
c906108c
SS
1147 break;
1148
c906108c 1149 case LOC_LOCAL:
a3f17187 1150 printf_filtered (_("a local variable at frame offset %ld"), val);
c906108c
SS
1151 break;
1152
1153 case LOC_REF_ARG:
a3f17187 1154 printf_filtered (_("a reference argument at offset %ld"), val);
c906108c
SS
1155 break;
1156
c906108c 1157 case LOC_TYPEDEF:
a3f17187 1158 printf_filtered (_("a typedef"));
c906108c
SS
1159 break;
1160
1161 case LOC_BLOCK:
a3f17187 1162 printf_filtered (_("a function at address "));
675dcf4f 1163 load_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
ed49a04f 1164 fputs_filtered (paddress (load_addr), gdb_stdout);
c906108c
SS
1165 if (section_is_overlay (section))
1166 {
1167 load_addr = overlay_unmapped_address (load_addr, section);
a3f17187 1168 printf_filtered (_(",\n -- loaded at "));
ed49a04f 1169 fputs_filtered (paddress (load_addr), gdb_stdout);
a3f17187 1170 printf_filtered (_(" in overlay section %s"), section->name);
c906108c
SS
1171 }
1172 break;
1173
1174 case LOC_UNRESOLVED:
1175 {
1176 struct minimal_symbol *msym;
1177
22abf04a 1178 msym = lookup_minimal_symbol (DEPRECATED_SYMBOL_NAME (sym), NULL, NULL);
c906108c
SS
1179 if (msym == NULL)
1180 printf_filtered ("unresolved");
1181 else
1182 {
1183 section = SYMBOL_BFD_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);
a3f17187 1192 printf_filtered (_(" in overlay section %s"), section->name);
c906108c
SS
1193 }
1194 }
1195 }
1196 break;
1197
c906108c 1198 case LOC_OPTIMIZED_OUT:
a3f17187 1199 printf_filtered (_("optimized out"));
c906108c 1200 break;
c5aa993b 1201
c906108c 1202 default:
a3f17187 1203 printf_filtered (_("of unknown (botched) type"));
c906108c
SS
1204 break;
1205 }
1206 printf_filtered (".\n");
1207}
1208\f
675dcf4f
MK
1209
1210static void
fba45db2 1211x_command (char *exp, int from_tty)
c906108c
SS
1212{
1213 struct expression *expr;
1214 struct format_data fmt;
1215 struct cleanup *old_chain;
1216 struct value *val;
1217
1218 fmt.format = last_format;
1219 fmt.size = last_size;
1220 fmt.count = 1;
1221
1222 if (exp && *exp == '/')
1223 {
1224 exp++;
1225 fmt = decode_format (&exp, last_format, last_size);
1226 }
1227
1228 /* If we have an expression, evaluate it and use it as the address. */
1229
1230 if (exp != 0 && *exp != 0)
1231 {
1232 expr = parse_expression (exp);
675dcf4f
MK
1233 /* Cause expression not to be there any more if this command is
1234 repeated with Newline. But don't clobber a user-defined
1235 command's definition. */
c906108c
SS
1236 if (from_tty)
1237 *exp = 0;
c13c43fd 1238 old_chain = make_cleanup (free_current_contents, &expr);
c906108c 1239 val = evaluate_expression (expr);
df407dfe 1240 if (TYPE_CODE (value_type (val)) == TYPE_CODE_REF)
c906108c
SS
1241 val = value_ind (val);
1242 /* In rvalue contexts, such as this, functions are coerced into
c5aa993b 1243 pointers to functions. This makes "x/i main" work. */
c0d8fd9a 1244 if (/* last_format == 'i' && */
df407dfe 1245 TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
c5aa993b 1246 && VALUE_LVAL (val) == lval_memory)
c906108c
SS
1247 next_address = VALUE_ADDRESS (val);
1248 else
1aa20aa8 1249 next_address = value_as_address (val);
c906108c
SS
1250 do_cleanups (old_chain);
1251 }
1252
00a4c844 1253 do_examine (fmt, next_address);
c906108c 1254
675dcf4f
MK
1255 /* If the examine succeeds, we remember its size and format for next
1256 time. */
c906108c
SS
1257 last_size = fmt.size;
1258 last_format = fmt.format;
1259
1260 /* Set a couple of internal variables if appropriate. */
1261 if (last_examine_value)
1262 {
1263 /* Make last address examined available to the user as $_. Use
c5aa993b 1264 the correct pointer type. */
4478b372 1265 struct type *pointer_type
df407dfe 1266 = lookup_pointer_type (value_type (last_examine_value));
c906108c 1267 set_internalvar (lookup_internalvar ("_"),
4478b372
JB
1268 value_from_pointer (pointer_type,
1269 last_examine_address));
c5aa993b 1270
675dcf4f
MK
1271 /* Make contents of last address examined available to the user
1272 as $__. If the last value has not been fetched from memory
1273 then don't fetch it now; instead mark it by voiding the $__
1274 variable. */
d69fe07e 1275 if (value_lazy (last_examine_value))
c906108c
SS
1276 set_internalvar (lookup_internalvar ("__"),
1277 allocate_value (builtin_type_void));
1278 else
1279 set_internalvar (lookup_internalvar ("__"), last_examine_value);
1280 }
1281}
c906108c 1282\f
c5aa993b 1283
c906108c
SS
1284/* Add an expression to the auto-display chain.
1285 Specify the expression. */
1286
1287static void
fba45db2 1288display_command (char *exp, int from_tty)
c906108c
SS
1289{
1290 struct format_data fmt;
52f0bd74
AC
1291 struct expression *expr;
1292 struct display *new;
c906108c
SS
1293 int display_it = 1;
1294
1295#if defined(TUI)
021e7609
AC
1296 /* NOTE: cagney/2003-02-13 The `tui_active' was previously
1297 `tui_version'. */
fd33e6cb 1298 if (tui_active && exp != NULL && *exp == '$')
080ce8c0 1299 display_it = (tui_set_layout_for_display_command (exp) == TUI_FAILURE);
c906108c
SS
1300#endif
1301
1302 if (display_it)
1303 {
1304 if (exp == 0)
1305 {
1306 do_displays ();
1307 return;
1308 }
1309
1310 if (*exp == '/')
1311 {
1312 exp++;
1313 fmt = decode_format (&exp, 0, 0);
1314 if (fmt.size && fmt.format == 0)
1315 fmt.format = 'x';
1316 if (fmt.format == 'i' || fmt.format == 's')
1317 fmt.size = 'b';
1318 }
1319 else
1320 {
1321 fmt.format = 0;
1322 fmt.size = 0;
1323 fmt.count = 0;
1324 }
1325
1326 innermost_block = 0;
1327 expr = parse_expression (exp);
1328
1329 new = (struct display *) xmalloc (sizeof (struct display));
1330
1331 new->exp = expr;
1332 new->block = innermost_block;
1333 new->next = display_chain;
1334 new->number = ++display_number;
1335 new->format = fmt;
b5de0fa7 1336 new->enabled_p = 1;
c906108c
SS
1337 display_chain = new;
1338
1339 if (from_tty && target_has_execution)
1340 do_one_display (new);
1341
1342 dont_repeat ();
1343 }
1344}
1345
1346static void
fba45db2 1347free_display (struct display *d)
c906108c 1348{
b8c9b27d
KB
1349 xfree (d->exp);
1350 xfree (d);
c906108c
SS
1351}
1352
675dcf4f
MK
1353/* Clear out the display_chain. Done when new symtabs are loaded,
1354 since this invalidates the types stored in many expressions. */
c906108c
SS
1355
1356void
fba45db2 1357clear_displays (void)
c906108c 1358{
52f0bd74 1359 struct display *d;
c906108c
SS
1360
1361 while ((d = display_chain) != NULL)
1362 {
b8c9b27d 1363 xfree (d->exp);
c906108c 1364 display_chain = d->next;
b8c9b27d 1365 xfree (d);
c906108c
SS
1366 }
1367}
1368
1369/* Delete the auto-display number NUM. */
1370
1371static void
fba45db2 1372delete_display (int num)
c906108c 1373{
52f0bd74 1374 struct display *d1, *d;
c906108c
SS
1375
1376 if (!display_chain)
8a3fe4f8 1377 error (_("No display number %d."), num);
c906108c
SS
1378
1379 if (display_chain->number == num)
1380 {
1381 d1 = display_chain;
1382 display_chain = d1->next;
1383 free_display (d1);
1384 }
1385 else
c5aa993b 1386 for (d = display_chain;; d = d->next)
c906108c
SS
1387 {
1388 if (d->next == 0)
8a3fe4f8 1389 error (_("No display number %d."), num);
c906108c
SS
1390 if (d->next->number == num)
1391 {
1392 d1 = d->next;
1393 d->next = d1->next;
1394 free_display (d1);
1395 break;
1396 }
1397 }
1398}
1399
1400/* Delete some values from the auto-display chain.
1401 Specify the element numbers. */
1402
1403static void
fba45db2 1404undisplay_command (char *args, int from_tty)
c906108c 1405{
52f0bd74
AC
1406 char *p = args;
1407 char *p1;
1408 int num;
c906108c
SS
1409
1410 if (args == 0)
1411 {
1412 if (query ("Delete all auto-display expressions? "))
1413 clear_displays ();
1414 dont_repeat ();
1415 return;
1416 }
1417
1418 while (*p)
1419 {
1420 p1 = p;
c5aa993b
JM
1421 while (*p1 >= '0' && *p1 <= '9')
1422 p1++;
c906108c 1423 if (*p1 && *p1 != ' ' && *p1 != '\t')
8a3fe4f8 1424 error (_("Arguments must be display numbers."));
c906108c
SS
1425
1426 num = atoi (p);
1427
1428 delete_display (num);
1429
1430 p = p1;
c5aa993b
JM
1431 while (*p == ' ' || *p == '\t')
1432 p++;
c906108c
SS
1433 }
1434 dont_repeat ();
1435}
1436
1437/* Display a single auto-display.
1438 Do nothing if the display cannot be printed in the current context,
1439 or if the display is disabled. */
1440
1441static void
fba45db2 1442do_one_display (struct display *d)
c906108c
SS
1443{
1444 int within_current_scope;
1445
b5de0fa7 1446 if (d->enabled_p == 0)
c906108c
SS
1447 return;
1448
1449 if (d->block)
ae767bfb 1450 within_current_scope = contained_in (get_selected_block (0), d->block);
c906108c
SS
1451 else
1452 within_current_scope = 1;
1453 if (!within_current_scope)
1454 return;
1455
1456 current_display_number = d->number;
1457
1458 annotate_display_begin ();
1459 printf_filtered ("%d", d->number);
1460 annotate_display_number_end ();
1461 printf_filtered (": ");
1462 if (d->format.size)
1463 {
1464 CORE_ADDR addr;
3d6d86c6 1465 struct value *val;
c906108c
SS
1466
1467 annotate_display_format ();
1468
1469 printf_filtered ("x/");
1470 if (d->format.count != 1)
1471 printf_filtered ("%d", d->format.count);
1472 printf_filtered ("%c", d->format.format);
1473 if (d->format.format != 'i' && d->format.format != 's')
1474 printf_filtered ("%c", d->format.size);
1475 printf_filtered (" ");
1476
1477 annotate_display_expression ();
1478
1479 print_expression (d->exp, gdb_stdout);
1480 annotate_display_expression_end ();
1481
6a2eb474 1482 if (d->format.count != 1 || d->format.format == 'i')
c906108c
SS
1483 printf_filtered ("\n");
1484 else
1485 printf_filtered (" ");
c5aa993b 1486
c906108c 1487 val = evaluate_expression (d->exp);
1aa20aa8 1488 addr = value_as_address (val);
c906108c 1489 if (d->format.format == 'i')
bf6ae464 1490 addr = gdbarch_addr_bits_remove (current_gdbarch, addr);
c906108c
SS
1491
1492 annotate_display_value ();
1493
00a4c844 1494 do_examine (d->format, addr);
c906108c
SS
1495 }
1496 else
1497 {
1498 annotate_display_format ();
1499
1500 if (d->format.format)
1501 printf_filtered ("/%c ", d->format.format);
1502
1503 annotate_display_expression ();
1504
1505 print_expression (d->exp, gdb_stdout);
1506 annotate_display_expression_end ();
1507
1508 printf_filtered (" = ");
1509
1510 annotate_display_expression ();
1511
1512 print_formatted (evaluate_expression (d->exp),
2acceee2 1513 d->format.format, d->format.size, gdb_stdout);
c906108c
SS
1514 printf_filtered ("\n");
1515 }
1516
1517 annotate_display_end ();
1518
1519 gdb_flush (gdb_stdout);
1520 current_display_number = -1;
1521}
1522
1523/* Display all of the values on the auto-display chain which can be
1524 evaluated in the current scope. */
1525
1526void
fba45db2 1527do_displays (void)
c906108c 1528{
52f0bd74 1529 struct display *d;
c906108c
SS
1530
1531 for (d = display_chain; d; d = d->next)
1532 do_one_display (d);
1533}
1534
1535/* Delete the auto-display which we were in the process of displaying.
1536 This is done when there is an error or a signal. */
1537
1538void
fba45db2 1539disable_display (int num)
c906108c 1540{
52f0bd74 1541 struct display *d;
c906108c
SS
1542
1543 for (d = display_chain; d; d = d->next)
1544 if (d->number == num)
1545 {
b5de0fa7 1546 d->enabled_p = 0;
c906108c
SS
1547 return;
1548 }
a3f17187 1549 printf_unfiltered (_("No display number %d.\n"), num);
c906108c 1550}
c5aa993b 1551
c906108c 1552void
fba45db2 1553disable_current_display (void)
c906108c
SS
1554{
1555 if (current_display_number >= 0)
1556 {
1557 disable_display (current_display_number);
675dcf4f
MK
1558 fprintf_unfiltered (gdb_stderr, _("\
1559Disabling display %d to avoid infinite recursion.\n"),
c5aa993b 1560 current_display_number);
c906108c
SS
1561 }
1562 current_display_number = -1;
1563}
1564
1565static void
fba45db2 1566display_info (char *ignore, int from_tty)
c906108c 1567{
52f0bd74 1568 struct display *d;
c906108c
SS
1569
1570 if (!display_chain)
a3f17187 1571 printf_unfiltered (_("There are no auto-display expressions now.\n"));
c906108c 1572 else
a3f17187
AC
1573 printf_filtered (_("Auto-display expressions now in effect:\n\
1574Num Enb Expression\n"));
c906108c
SS
1575
1576 for (d = display_chain; d; d = d->next)
1577 {
b5de0fa7 1578 printf_filtered ("%d: %c ", d->number, "ny"[(int) d->enabled_p]);
c906108c
SS
1579 if (d->format.size)
1580 printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
c5aa993b 1581 d->format.format);
c906108c
SS
1582 else if (d->format.format)
1583 printf_filtered ("/%c ", d->format.format);
1584 print_expression (d->exp, gdb_stdout);
ae767bfb 1585 if (d->block && !contained_in (get_selected_block (0), d->block))
a3f17187 1586 printf_filtered (_(" (cannot be evaluated in the current context)"));
c906108c
SS
1587 printf_filtered ("\n");
1588 gdb_flush (gdb_stdout);
1589 }
1590}
1591
1592static void
fba45db2 1593enable_display (char *args, int from_tty)
c906108c 1594{
52f0bd74
AC
1595 char *p = args;
1596 char *p1;
1597 int num;
1598 struct display *d;
c906108c
SS
1599
1600 if (p == 0)
1601 {
1602 for (d = display_chain; d; d = d->next)
b5de0fa7 1603 d->enabled_p = 1;
c906108c
SS
1604 }
1605 else
1606 while (*p)
1607 {
1608 p1 = p;
1609 while (*p1 >= '0' && *p1 <= '9')
1610 p1++;
1611 if (*p1 && *p1 != ' ' && *p1 != '\t')
8a3fe4f8 1612 error (_("Arguments must be display numbers."));
c5aa993b 1613
c906108c 1614 num = atoi (p);
c5aa993b 1615
c906108c
SS
1616 for (d = display_chain; d; d = d->next)
1617 if (d->number == num)
1618 {
b5de0fa7 1619 d->enabled_p = 1;
c906108c
SS
1620 goto win;
1621 }
a3f17187 1622 printf_unfiltered (_("No display number %d.\n"), num);
c906108c
SS
1623 win:
1624 p = p1;
1625 while (*p == ' ' || *p == '\t')
1626 p++;
1627 }
1628}
1629
c906108c 1630static void
fba45db2 1631disable_display_command (char *args, int from_tty)
c906108c 1632{
52f0bd74
AC
1633 char *p = args;
1634 char *p1;
1635 struct display *d;
c906108c
SS
1636
1637 if (p == 0)
1638 {
1639 for (d = display_chain; d; d = d->next)
b5de0fa7 1640 d->enabled_p = 0;
c906108c
SS
1641 }
1642 else
1643 while (*p)
1644 {
1645 p1 = p;
1646 while (*p1 >= '0' && *p1 <= '9')
1647 p1++;
1648 if (*p1 && *p1 != ' ' && *p1 != '\t')
8a3fe4f8 1649 error (_("Arguments must be display numbers."));
c5aa993b 1650
c906108c
SS
1651 disable_display (atoi (p));
1652
1653 p = p1;
1654 while (*p == ' ' || *p == '\t')
1655 p++;
1656 }
1657}
c906108c 1658\f
c5aa993b 1659
675dcf4f
MK
1660/* Print the value in stack frame FRAME of a variable specified by a
1661 struct symbol. */
c906108c
SS
1662
1663void
fba45db2
KB
1664print_variable_value (struct symbol *var, struct frame_info *frame,
1665 struct ui_file *stream)
c906108c 1666{
3d6d86c6 1667 struct value *val = read_var_value (var, frame);
c906108c
SS
1668
1669 value_print (val, stream, 0, Val_pretty_default);
1670}
1671
c906108c 1672static void
fba45db2 1673printf_command (char *arg, int from_tty)
c906108c 1674{
52f0bd74
AC
1675 char *f = NULL;
1676 char *s = arg;
c906108c 1677 char *string = NULL;
3d6d86c6 1678 struct value **val_args;
c906108c
SS
1679 char *substrings;
1680 char *current_substring;
1681 int nargs = 0;
1682 int allocated_args = 20;
1683 struct cleanup *old_cleanups;
1684
675dcf4f 1685 val_args = xmalloc (allocated_args * sizeof (struct value *));
c13c43fd 1686 old_cleanups = make_cleanup (free_current_contents, &val_args);
c906108c
SS
1687
1688 if (s == 0)
e2e0b3e5 1689 error_no_arg (_("format-control string and values to print"));
c906108c
SS
1690
1691 /* Skip white space before format string */
c5aa993b
JM
1692 while (*s == ' ' || *s == '\t')
1693 s++;
c906108c 1694
675dcf4f 1695 /* A format string should follow, enveloped in double quotes. */
c906108c 1696 if (*s++ != '"')
8a3fe4f8 1697 error (_("Bad format string, missing '\"'."));
c906108c
SS
1698
1699 /* Parse the format-control string and copy it into the string STRING,
1700 processing some kinds of escape sequence. */
1701
1702 f = string = (char *) alloca (strlen (s) + 1);
1703
1704 while (*s != '"')
1705 {
1706 int c = *s++;
1707 switch (c)
1708 {
1709 case '\0':
8a3fe4f8 1710 error (_("Bad format string, non-terminated '\"'."));
c906108c
SS
1711
1712 case '\\':
1713 switch (c = *s++)
1714 {
1715 case '\\':
1716 *f++ = '\\';
1717 break;
1718 case 'a':
c906108c 1719 *f++ = '\a';
c906108c
SS
1720 break;
1721 case 'b':
1722 *f++ = '\b';
1723 break;
1724 case 'f':
1725 *f++ = '\f';
1726 break;
1727 case 'n':
1728 *f++ = '\n';
1729 break;
1730 case 'r':
1731 *f++ = '\r';
1732 break;
1733 case 't':
1734 *f++ = '\t';
1735 break;
1736 case 'v':
1737 *f++ = '\v';
1738 break;
1739 case '"':
1740 *f++ = '"';
1741 break;
1742 default:
1743 /* ??? TODO: handle other escape sequences */
8a3fe4f8 1744 error (_("Unrecognized escape character \\%c in format string."),
c906108c
SS
1745 c);
1746 }
1747 break;
1748
1749 default:
1750 *f++ = c;
1751 }
1752 }
1753
1754 /* Skip over " and following space and comma. */
1755 s++;
1756 *f++ = '\0';
c5aa993b
JM
1757 while (*s == ' ' || *s == '\t')
1758 s++;
c906108c
SS
1759
1760 if (*s != ',' && *s != 0)
8a3fe4f8 1761 error (_("Invalid argument syntax"));
c906108c 1762
c5aa993b
JM
1763 if (*s == ',')
1764 s++;
1765 while (*s == ' ' || *s == '\t')
1766 s++;
c906108c
SS
1767
1768 /* Need extra space for the '\0's. Doubling the size is sufficient. */
1769 substrings = alloca (strlen (string) * 2);
1770 current_substring = substrings;
1771
1772 {
1773 /* Now scan the string for %-specs and see what kinds of args they want.
1774 argclass[I] classifies the %-specs so we can give printf_filtered
1775 something of the right size. */
1776
c5aa993b
JM
1777 enum argclass
1778 {
46e9880c 1779 int_arg, long_arg, long_long_arg, ptr_arg, string_arg,
1a619819 1780 double_arg, long_double_arg, decfloat_arg
c5aa993b 1781 };
c906108c
SS
1782 enum argclass *argclass;
1783 enum argclass this_argclass;
1784 char *last_arg;
1785 int nargs_wanted;
c906108c
SS
1786 int i;
1787
1788 argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
1789 nargs_wanted = 0;
1790 f = string;
1791 last_arg = string;
1792 while (*f)
1793 if (*f++ == '%')
1794 {
46e9880c
DJ
1795 int seen_hash = 0, seen_zero = 0, lcount = 0, seen_prec = 0;
1796 int seen_space = 0, seen_plus = 0;
0aea4bf3
LM
1797 int seen_big_l = 0, seen_h = 0, seen_big_h = 0;
1798 int seen_big_d = 0, seen_double_big_d = 0;
46e9880c
DJ
1799 int bad = 0;
1800
1801 /* Check the validity of the format specifier, and work
1802 out what argument it expects. We only accept C89
1803 format strings, with the exception of long long (which
1804 we autoconf for). */
1805
1806 /* Skip over "%%". */
1807 if (*f == '%')
c906108c 1808 {
c906108c 1809 f++;
46e9880c 1810 continue;
c906108c 1811 }
46e9880c
DJ
1812
1813 /* The first part of a format specifier is a set of flag
1814 characters. */
1815 while (strchr ("0-+ #", *f))
1816 {
1817 if (*f == '#')
1818 seen_hash = 1;
1819 else if (*f == '0')
1820 seen_zero = 1;
1821 else if (*f == ' ')
1822 seen_space = 1;
1823 else if (*f == '+')
1824 seen_plus = 1;
1825 f++;
1826 }
1827
1828 /* The next part of a format specifier is a width. */
1829 while (strchr ("0123456789", *f))
1830 f++;
1831
1832 /* The next part of a format specifier is a precision. */
1833 if (*f == '.')
1834 {
1835 seen_prec = 1;
1836 f++;
1837 while (strchr ("0123456789", *f))
1838 f++;
1839 }
1840
1841 /* The next part of a format specifier is a length modifier. */
1842 if (*f == 'h')
1843 {
1844 seen_h = 1;
1845 f++;
1846 }
1847 else if (*f == 'l')
1848 {
1849 f++;
1850 lcount++;
1851 if (*f == 'l')
1852 {
1853 f++;
1854 lcount++;
1855 }
1856 }
1857 else if (*f == 'L')
1858 {
1859 seen_big_l = 1;
1860 f++;
1861 }
0aea4bf3
LM
1862 /* Decimal32 modifier. */
1863 else if (*f == 'H')
1864 {
1865 seen_big_h = 1;
1866 f++;
1867 }
1868 /* Decimal64 and Decimal128 modifiers. */
1869 else if (*f == 'D')
1870 {
1871 f++;
1872
1873 /* Check for a Decimal128. */
1874 if (*f == 'D')
1875 {
1876 f++;
1877 seen_double_big_d = 1;
1878 }
1879 else
1880 seen_big_d = 1;
1881 }
46e9880c 1882
c906108c
SS
1883 switch (*f)
1884 {
46e9880c
DJ
1885 case 'u':
1886 if (seen_hash)
1887 bad = 1;
1888 /* FALLTHROUGH */
1889
1890 case 'o':
1891 case 'x':
1892 case 'X':
1893 if (seen_space || seen_plus)
1894 bad = 1;
1895 /* FALLTHROUGH */
1896
1897 case 'd':
1898 case 'i':
1899 if (lcount == 0)
1900 this_argclass = int_arg;
1901 else if (lcount == 1)
1902 this_argclass = long_arg;
1903 else
1904 this_argclass = long_long_arg;
1905
1906 if (seen_big_l)
1907 bad = 1;
1908 break;
1909
1910 case 'c':
1911 this_argclass = int_arg;
1912 if (lcount || seen_h || seen_big_l)
1913 bad = 1;
1914 if (seen_prec || seen_zero || seen_space || seen_plus)
1915 bad = 1;
1916 break;
1917
1918 case 'p':
1919 this_argclass = ptr_arg;
1920 if (lcount || seen_h || seen_big_l)
1921 bad = 1;
1922 if (seen_prec || seen_zero || seen_space || seen_plus)
1923 bad = 1;
1924 break;
1925
c906108c
SS
1926 case 's':
1927 this_argclass = string_arg;
46e9880c
DJ
1928 if (lcount || seen_h || seen_big_l)
1929 bad = 1;
1930 if (seen_zero || seen_space || seen_plus)
1931 bad = 1;
c906108c
SS
1932 break;
1933
1934 case 'e':
1935 case 'f':
1936 case 'g':
46e9880c
DJ
1937 case 'E':
1938 case 'G':
0aea4bf3
LM
1939 if (seen_big_h || seen_big_d || seen_double_big_d)
1940 this_argclass = decfloat_arg;
1941 else if (seen_big_l)
46e9880c
DJ
1942 this_argclass = long_double_arg;
1943 else
1944 this_argclass = double_arg;
1945
1946 if (lcount || seen_h)
1947 bad = 1;
c906108c
SS
1948 break;
1949
1950 case '*':
8a3fe4f8 1951 error (_("`*' not supported for precision or width in printf"));
c906108c
SS
1952
1953 case 'n':
8a3fe4f8 1954 error (_("Format specifier `n' not supported in printf"));
c906108c 1955
46e9880c
DJ
1956 case '\0':
1957 error (_("Incomplete format specifier at end of format string"));
c906108c
SS
1958
1959 default:
46e9880c 1960 error (_("Unrecognized format specifier '%c' in printf"), *f);
c906108c 1961 }
46e9880c
DJ
1962
1963 if (bad)
1964 error (_("Inappropriate modifiers to format specifier '%c' in printf"),
1965 *f);
1966
c906108c 1967 f++;
09d71d23
AS
1968
1969 if (lcount > 1 && USE_PRINTF_I64)
1970 {
1971 /* Windows' printf does support long long, but not the usual way.
1972 Convert %lld to %I64d. */
1973 int length_before_ll = f - last_arg - 1 - lcount;
1974 strncpy (current_substring, last_arg, length_before_ll);
1975 strcpy (current_substring + length_before_ll, "I64");
1976 current_substring[length_before_ll + 3] =
1977 last_arg[length_before_ll + lcount];
1978 current_substring += length_before_ll + 4;
1979 }
1980 else
1981 {
1982 strncpy (current_substring, last_arg, f - last_arg);
1983 current_substring += f - last_arg;
1984 }
46e9880c
DJ
1985 *current_substring++ = '\0';
1986 last_arg = f;
1987 argclass[nargs_wanted++] = this_argclass;
c906108c
SS
1988 }
1989
1990 /* Now, parse all arguments and evaluate them.
1991 Store the VALUEs in VAL_ARGS. */
1992
1993 while (*s != '\0')
1994 {
1995 char *s1;
1996 if (nargs == allocated_args)
f976f6d4
AC
1997 val_args = (struct value **) xrealloc ((char *) val_args,
1998 (allocated_args *= 2)
1999 * sizeof (struct value *));
c906108c
SS
2000 s1 = s;
2001 val_args[nargs] = parse_to_comma_and_eval (&s1);
c5aa993b 2002
c906108c
SS
2003 /* If format string wants a float, unchecked-convert the value to
2004 floating point of the same size */
c5aa993b 2005
c906108c
SS
2006 if (argclass[nargs] == double_arg)
2007 {
df407dfe 2008 struct type *type = value_type (val_args[nargs]);
c906108c 2009 if (TYPE_LENGTH (type) == sizeof (float))
04624583 2010 deprecated_set_value_type (val_args[nargs], builtin_type_float);
c906108c 2011 if (TYPE_LENGTH (type) == sizeof (double))
04624583 2012 deprecated_set_value_type (val_args[nargs], builtin_type_double);
c906108c
SS
2013 }
2014 nargs++;
2015 s = s1;
2016 if (*s == ',')
2017 s++;
2018 }
c5aa993b 2019
c906108c 2020 if (nargs != nargs_wanted)
8a3fe4f8 2021 error (_("Wrong number of arguments for specified format-string"));
c906108c
SS
2022
2023 /* Now actually print them. */
2024 current_substring = substrings;
2025 for (i = 0; i < nargs; i++)
2026 {
2027 switch (argclass[i])
2028 {
2029 case string_arg:
2030 {
777ea8f1 2031 gdb_byte *str;
c906108c
SS
2032 CORE_ADDR tem;
2033 int j;
1aa20aa8 2034 tem = value_as_address (val_args[i]);
c906108c
SS
2035
2036 /* This is a %s argument. Find the length of the string. */
c5aa993b 2037 for (j = 0;; j++)
c906108c 2038 {
777ea8f1 2039 gdb_byte c;
c906108c 2040 QUIT;
d4b2399a 2041 read_memory (tem + j, &c, 1);
c906108c
SS
2042 if (c == 0)
2043 break;
2044 }
2045
2046 /* Copy the string contents into a string inside GDB. */
777ea8f1 2047 str = (gdb_byte *) alloca (j + 1);
7b92f6e1
MS
2048 if (j != 0)
2049 read_memory (tem, str, j);
c906108c
SS
2050 str[j] = 0;
2051
777ea8f1 2052 printf_filtered (current_substring, (char *) str);
c906108c
SS
2053 }
2054 break;
2055 case double_arg:
2056 {
2057 double val = value_as_double (val_args[i]);
2058 printf_filtered (current_substring, val);
2059 break;
2060 }
46e9880c
DJ
2061 case long_double_arg:
2062#ifdef HAVE_LONG_DOUBLE
2063 {
2064 long double val = value_as_double (val_args[i]);
2065 printf_filtered (current_substring, val);
2066 break;
2067 }
2068#else
2069 error (_("long double not supported in printf"));
2070#endif
c906108c
SS
2071 case long_long_arg:
2072#if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2073 {
2074 long long val = value_as_long (val_args[i]);
2075 printf_filtered (current_substring, val);
2076 break;
2077 }
2078#else
8a3fe4f8 2079 error (_("long long not supported in printf"));
c906108c
SS
2080#endif
2081 case int_arg:
2082 {
46e9880c
DJ
2083 int val = value_as_long (val_args[i]);
2084 printf_filtered (current_substring, val);
2085 break;
2086 }
2087 case long_arg:
2088 {
c906108c
SS
2089 long val = value_as_long (val_args[i]);
2090 printf_filtered (current_substring, val);
2091 break;
2092 }
1a619819 2093
0aea4bf3
LM
2094 /* Handles decimal floating values. */
2095 case decfloat_arg:
1a619819 2096 {
0aea4bf3 2097 const gdb_byte *param_ptr = value_contents (val_args[i]);
1a619819 2098#if defined (PRINTF_HAS_DECFLOAT)
0aea4bf3
LM
2099 /* If we have native support for Decimal floating
2100 printing, handle it here. */
2101 printf_filtered (current_substring, param_ptr);
1a619819 2102#else
1a619819
LM
2103
2104 /* As a workaround until vasprintf has native support for DFP
0aea4bf3
LM
2105 we convert the DFP values to string and print them using
2106 the %s format specifier. */
2107
2108 char *eos, *sos;
2109 int nnull_chars = 0;
2110
2111 /* Parameter data. */
2112 struct type *param_type = value_type (val_args[i]);
2113 unsigned int param_len = TYPE_LENGTH (param_type);
2114
2115 /* DFP output data. */
2116 struct value *dfp_value = NULL;
2117 gdb_byte *dfp_ptr;
2118 int dfp_len = 16;
2119 gdb_byte dec[16];
2120 struct type *dfp_type = NULL;
2121 char decstr[MAX_DECIMAL_STRING];
1a619819
LM
2122
2123 /* Points to the end of the string so that we can go back
0aea4bf3 2124 and check for DFP length modifiers. */
1a619819
LM
2125 eos = current_substring + strlen (current_substring);
2126
0aea4bf3
LM
2127 /* Look for the float/double format specifier. */
2128 while (*eos != 'f' && *eos != 'e' && *eos != 'E'
2129 && *eos != 'g' && *eos != 'G')
2130 eos--;
2131
2132 sos = eos;
2133
2134 /* Search for the '%' char and extract the size and type of
2135 the output decimal value based on its modifiers
2136 (%Hf, %Df, %DDf). */
2137 while (*--sos != '%')
2138 {
2139 if (*sos == 'H')
2140 {
2141 dfp_len = 4;
2142 dfp_type = builtin_type (current_gdbarch)->builtin_decfloat;
2143 }
2144 else if (*sos == 'D' && *(sos - 1) == 'D')
2145 {
2146 dfp_len = 16;
2147 dfp_type = builtin_type (current_gdbarch)->builtin_declong;
2148 sos--;
2149 }
2150 else
2151 {
2152 dfp_len = 8;
2153 dfp_type = builtin_type (current_gdbarch)->builtin_decdouble;
2154 }
2155 }
2156
2157 /* Replace %Hf, %Df and %DDf with %s's. */
2158 *++sos = 's';
2159
2160 /* Go through the whole format string and pull the correct
2161 number of chars back to compensate for the change in the
2162 format specifier. */
2163 while (nnull_chars < nargs - i)
2164 {
2165 if (*eos == '\0')
2166 nnull_chars++;
2167
2168 *++sos = *++eos;
2169 }
2170
2171 /* Conversion between different DFP types. */
2172 if (TYPE_CODE (param_type) == TYPE_CODE_DECFLOAT)
2173 decimal_convert (param_ptr, param_len, dec, dfp_len);
2174 else
2175 /* If this is a non-trivial conversion, just output 0.
2176 A correct converted value can be displayed by explicitly
2177 casting to a DFP type. */
2178 decimal_from_string (dec, dfp_len, "0");
2179
2180 dfp_value = value_from_decfloat (dfp_type, dec);
2181
2182 dfp_ptr = (gdb_byte *) value_contents (dfp_value);
2183
2184 decimal_to_string (dfp_ptr, dfp_len, decstr);
1a619819
LM
2185
2186 /* Print the DFP value. */
2187 printf_filtered (current_substring, decstr);
0aea4bf3 2188
1a619819
LM
2189 break;
2190#endif
2191 }
2192
2025a643
DJ
2193 case ptr_arg:
2194 {
2195 /* We avoid the host's %p because pointers are too
2196 likely to be the wrong size. The only interesting
2197 modifier for %p is a width; extract that, and then
2198 handle %p as glibc would: %#x or a literal "(nil)". */
2199
2200 char *p, *fmt, *fmt_p;
2201#if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2202 long long val = value_as_long (val_args[i]);
2203#else
2204 long val = value_as_long (val_args[i]);
2205#endif
2206
2207 fmt = alloca (strlen (current_substring) + 5);
2208
2209 /* Copy up to the leading %. */
2210 p = current_substring;
2211 fmt_p = fmt;
2212 while (*p)
2213 {
2214 int is_percent = (*p == '%');
2215 *fmt_p++ = *p++;
2216 if (is_percent)
2217 {
2218 if (*p == '%')
2219 *fmt_p++ = *p++;
2220 else
2221 break;
2222 }
2223 }
2224
2225 if (val != 0)
2226 *fmt_p++ = '#';
2227
2228 /* Copy any width. */
2229 while (*p >= '0' && *p < '9')
2230 *fmt_p++ = *p++;
2231
2232 gdb_assert (*p == 'p' && *(p + 1) == '\0');
2233 if (val != 0)
2234 {
2235#if defined (CC_HAS_LONG_LONG) && defined (PRINTF_HAS_LONG_LONG)
2236 *fmt_p++ = 'l';
2237#endif
2238 *fmt_p++ = 'l';
2239 *fmt_p++ = 'x';
2240 *fmt_p++ = '\0';
2241 printf_filtered (fmt, val);
2242 }
2243 else
2244 {
2245 *fmt_p++ = 's';
2246 *fmt_p++ = '\0';
2247 printf_filtered (fmt, "(nil)");
2248 }
2249
2250 break;
2251 }
675dcf4f
MK
2252 default:
2253 internal_error (__FILE__, __LINE__,
2025a643 2254 _("failed internal consistency check"));
c906108c
SS
2255 }
2256 /* Skip to the next substring. */
2257 current_substring += strlen (current_substring) + 1;
2258 }
2259 /* Print the portion of the format string after the last argument. */
306d9ac5 2260 puts_filtered (last_arg);
c906108c
SS
2261 }
2262 do_cleanups (old_cleanups);
2263}
c906108c 2264
c906108c 2265void
fba45db2 2266_initialize_printcmd (void)
c906108c 2267{
c94fdfd0
EZ
2268 struct cmd_list_element *c;
2269
c906108c
SS
2270 current_display_number = -1;
2271
2272 add_info ("address", address_info,
1bedd215 2273 _("Describe where symbol SYM is stored."));
c906108c 2274
1bedd215
AC
2275 add_info ("symbol", sym_info, _("\
2276Describe what symbol is at location ADDR.\n\
2277Only for symbols with fixed locations (global or static scope)."));
c906108c 2278
1bedd215
AC
2279 add_com ("x", class_vars, x_command, _("\
2280Examine memory: x/FMT ADDRESS.\n\
c906108c
SS
2281ADDRESS is an expression for the memory address to examine.\n\
2282FMT is a repeat count followed by a format letter and a size letter.\n\
2283Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
1bedd215
AC
2284 t(binary), f(float), a(address), i(instruction), c(char) and s(string).\n\
2285Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
c906108c
SS
2286The specified number of objects of the specified size are printed\n\
2287according to the format.\n\n\
2288Defaults for format and size letters are those previously used.\n\
2289Default count is 1. Default address is following last thing printed\n\
1bedd215 2290with this command or \"print\"."));
c906108c 2291
c906108c
SS
2292#if 0
2293 add_com ("whereis", class_vars, whereis_command,
1bedd215 2294 _("Print line number and file of definition of variable."));
c906108c 2295#endif
c5aa993b 2296
1bedd215
AC
2297 add_info ("display", display_info, _("\
2298Expressions to display when program stops, with code numbers."));
c906108c 2299
1a966eab
AC
2300 add_cmd ("undisplay", class_vars, undisplay_command, _("\
2301Cancel some expressions to be displayed when program stops.\n\
c906108c
SS
2302Arguments are the code numbers of the expressions to stop displaying.\n\
2303No argument means cancel all automatic-display expressions.\n\
2304\"delete display\" has the same effect as this command.\n\
1a966eab 2305Do \"info display\" to see current list of code numbers."),
c5aa993b 2306 &cmdlist);
c906108c 2307
1bedd215
AC
2308 add_com ("display", class_vars, display_command, _("\
2309Print value of expression EXP each time the program stops.\n\
c906108c
SS
2310/FMT may be used before EXP as in the \"print\" command.\n\
2311/FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
2312as in the \"x\" command, and then EXP is used to get the address to examine\n\
2313and examining is done as in the \"x\" command.\n\n\
2314With no argument, display all currently requested auto-display expressions.\n\
1bedd215 2315Use \"undisplay\" to cancel display requests previously made."));
c906108c 2316
1a966eab
AC
2317 add_cmd ("display", class_vars, enable_display, _("\
2318Enable some expressions to be displayed when program stops.\n\
c906108c
SS
2319Arguments are the code numbers of the expressions to resume displaying.\n\
2320No argument means enable all automatic-display expressions.\n\
1a966eab 2321Do \"info display\" to see current list of code numbers."), &enablelist);
c906108c 2322
1a966eab
AC
2323 add_cmd ("display", class_vars, disable_display_command, _("\
2324Disable some expressions to be displayed when program stops.\n\
c906108c
SS
2325Arguments are the code numbers of the expressions to stop displaying.\n\
2326No argument means disable all automatic-display expressions.\n\
1a966eab 2327Do \"info display\" to see current list of code numbers."), &disablelist);
c906108c 2328
1a966eab
AC
2329 add_cmd ("display", class_vars, undisplay_command, _("\
2330Cancel some expressions to be displayed when program stops.\n\
c906108c
SS
2331Arguments are the code numbers of the expressions to stop displaying.\n\
2332No argument means cancel all automatic-display expressions.\n\
1a966eab 2333Do \"info display\" to see current list of code numbers."), &deletelist);
c906108c 2334
1bedd215
AC
2335 add_com ("printf", class_vars, printf_command, _("\
2336printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
2337This is useful for formatted output in user-defined commands."));
c906108c 2338
1bedd215
AC
2339 add_com ("output", class_vars, output_command, _("\
2340Like \"print\" but don't put in value history and don't print newline.\n\
2341This is useful in user-defined commands."));
c906108c 2342
1bedd215
AC
2343 add_prefix_cmd ("set", class_vars, set_command, _("\
2344Evaluate expression EXP and assign result to variable VAR, using assignment\n\
c906108c
SS
2345syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2346example). VAR may be a debugger \"convenience\" variable (names starting\n\
2347with $), a register (a few standard names starting with $), or an actual\n\
1bedd215
AC
2348variable in the program being debugged. EXP is any valid expression.\n\
2349Use \"set variable\" for variables with names identical to set subcommands.\n\
2350\n\
2351With a subcommand, this command modifies parts of the gdb environment.\n\
2352You can see these environment settings with the \"show\" command."),
c5aa993b 2353 &setlist, "set ", 1, &cmdlist);
c906108c 2354 if (dbx_commands)
1bedd215
AC
2355 add_com ("assign", class_vars, set_command, _("\
2356Evaluate expression EXP and assign result to variable VAR, using assignment\n\
c906108c
SS
2357syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2358example). VAR may be a debugger \"convenience\" variable (names starting\n\
2359with $), a register (a few standard names starting with $), or an actual\n\
1bedd215
AC
2360variable in the program being debugged. EXP is any valid expression.\n\
2361Use \"set variable\" for variables with names identical to set subcommands.\n\
c906108c 2362\nWith a subcommand, this command modifies parts of the gdb environment.\n\
1bedd215 2363You can see these environment settings with the \"show\" command."));
c906108c
SS
2364
2365 /* "call" is the same as "set", but handy for dbx users to call fns. */
1bedd215
AC
2366 c = add_com ("call", class_vars, call_command, _("\
2367Call a function in the program.\n\
c906108c
SS
2368The argument is the function name and arguments, in the notation of the\n\
2369current working language. The result is printed and saved in the value\n\
1bedd215 2370history, if it is not void."));
5ba2abeb 2371 set_cmd_completer (c, location_completer);
c906108c 2372
1a966eab
AC
2373 add_cmd ("variable", class_vars, set_command, _("\
2374Evaluate expression EXP and assign result to variable VAR, using assignment\n\
c906108c
SS
2375syntax appropriate for the current language (VAR = EXP or VAR := EXP for\n\
2376example). VAR may be a debugger \"convenience\" variable (names starting\n\
2377with $), a register (a few standard names starting with $), or an actual\n\
2378variable in the program being debugged. EXP is any valid expression.\n\
1a966eab 2379This may usually be abbreviated to simply \"set\"."),
c5aa993b 2380 &setlist);
c906108c 2381
1bedd215
AC
2382 c = add_com ("print", class_vars, print_command, _("\
2383Print value of expression EXP.\n\
c906108c
SS
2384Variables accessible are those of the lexical environment of the selected\n\
2385stack frame, plus all those whose scope is global or an entire file.\n\
2386\n\
2387$NUM gets previous value number NUM. $ and $$ are the last two values.\n\
2388$$NUM refers to NUM'th value back from the last one.\n\
1bedd215
AC
2389Names starting with $ refer to registers (with the values they would have\n\
2390if the program were to return to the stack frame now selected, restoring\n\
c906108c
SS
2391all registers saved by frames farther in) or else to debugger\n\
2392\"convenience\" variables (any such name not a known register).\n\
1bedd215
AC
2393Use assignment expressions to give values to convenience variables.\n\
2394\n\
c906108c
SS
2395{TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
2396@ is a binary operator for treating consecutive data objects\n\
2397anywhere in memory as an array. FOO@NUM gives an array whose first\n\
2398element is FOO, whose second element is stored in the space following\n\
2399where FOO is stored, etc. FOO must be an expression whose value\n\
1bedd215
AC
2400resides in memory.\n\
2401\n\
c906108c 2402EXP may be preceded with /FMT, where FMT is a format letter\n\
1bedd215 2403but no count or size letter (see \"x\" command)."));
5ba2abeb 2404 set_cmd_completer (c, location_completer);
c906108c
SS
2405 add_com_alias ("p", "print", class_vars, 1);
2406
1bedd215
AC
2407 c = add_com ("inspect", class_vars, inspect_command, _("\
2408Same as \"print\" command, except that if you are running in the epoch\n\
2409environment, the value is printed in its own window."));
5ba2abeb 2410 set_cmd_completer (c, location_completer);
c906108c 2411
35096d9d
AC
2412 add_setshow_uinteger_cmd ("max-symbolic-offset", no_class,
2413 &max_symbolic_offset, _("\
2414Set the largest offset that will be printed in <symbol+1234> form."), _("\
2415Show the largest offset that will be printed in <symbol+1234> form."), NULL,
2416 NULL,
920d2a44 2417 show_max_symbolic_offset,
35096d9d 2418 &setprintlist, &showprintlist);
5bf193a2
AC
2419 add_setshow_boolean_cmd ("symbol-filename", no_class,
2420 &print_symbol_filename, _("\
2421Set printing of source filename and line number with <symbol>."), _("\
2422Show printing of source filename and line number with <symbol>."), NULL,
2423 NULL,
920d2a44 2424 show_print_symbol_filename,
5bf193a2 2425 &setprintlist, &showprintlist);
c906108c
SS
2426
2427 /* For examine/instruction a single byte quantity is specified as
2428 the data. This avoids problems with value_at_lazy() requiring a
2429 valid data type (and rejecting VOID). */
2430 examine_i_type = init_type (TYPE_CODE_INT, 1, 0, "examine_i_type", NULL);
2431
2432 examine_b_type = init_type (TYPE_CODE_INT, 1, 0, "examine_b_type", NULL);
2433 examine_h_type = init_type (TYPE_CODE_INT, 2, 0, "examine_h_type", NULL);
2434 examine_w_type = init_type (TYPE_CODE_INT, 4, 0, "examine_w_type", NULL);
2435 examine_g_type = init_type (TYPE_CODE_INT, 8, 0, "examine_g_type", NULL);
2436
2437}
This page took 0.850017 seconds and 4 git commands to generate.