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