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