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