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