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