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