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