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