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