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