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