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