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