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