gdb-3.5
[deliverable/binutils-gdb.git] / gdb / printcmd.c
CommitLineData
3bf57d21 1/* Print values for GNU debugger GDB.
4187119d 2 Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
7b4ac7e1 3
4187119d 4This file is part of GDB.
7b4ac7e1 5
4187119d 6GDB is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
7b4ac7e1 10
4187119d 11GDB is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GDB; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
7b4ac7e1 19
20#include <stdio.h>
21#include "defs.h"
7b4ac7e1 22#include "param.h"
e91b87a3 23#include "frame.h"
7b4ac7e1 24#include "symtab.h"
25#include "value.h"
26#include "expression.h"
27
28struct format_data
29{
30 int count;
31 char format;
32 char size;
33};
34
35/* Last specified output format. */
36
37static char last_format = 'x';
38
39/* Last specified examination size. 'b', 'h', 'w' or `q'. */
40
41static char last_size = 'w';
42
43/* Default address to examine next. */
44
45static CORE_ADDR next_address;
46
47/* Last address examined. */
48
49static CORE_ADDR last_examine_address;
50
51/* Contents of last address examined.
52 This is not valid past the end of the `x' command! */
53
54static value last_examine_value;
55
3bf57d21 56/* Number of auto-display expression currently being displayed.
57 So that we can deleted it if we get an error or a signal within it.
58 -1 when not doing one. */
59
60int current_display_number;
61
62static void do_one_display ();
63
7b4ac7e1 64void do_displays ();
65void print_address ();
3bf57d21 66void print_scalar_formatted ();
7b4ac7e1 67
7b4ac7e1 68\f
69/* Decode a format specification. *STRING_PTR should point to it.
70 OFORMAT and OSIZE are used as defaults for the format and size
71 if none are given in the format specification.
4187119d 72 If OSIZE is zero, then the size field of the returned value
73 should be set only if a size is explicitly specified by the
74 user.
7b4ac7e1 75 The structure returned describes all the data
76 found in the specification. In addition, *STRING_PTR is advanced
77 past the specification and past all whitespace following it. */
78
79struct format_data
80decode_format (string_ptr, oformat, osize)
81 char **string_ptr;
82 char oformat;
83 char osize;
84{
85 struct format_data val;
86 register char *p = *string_ptr;
87
4187119d 88 val.format = '?';
89 val.size = '?';
7b4ac7e1 90 val.count = 1;
91
92 if (*p >= '0' && *p <= '9')
93 val.count = atoi (p);
94 while (*p >= '0' && *p <= '9') p++;
95
96 /* Now process size or format letters that follow. */
97
98 while (1)
99 {
100 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
101 val.size = *p++;
e91b87a3 102#ifdef LONG_LONG
103 else if (*p == 'l')
104 {
105 val.size = 'g';
106 p++;
107 }
108#endif
7b4ac7e1 109 else if (*p >= 'a' && *p <= 'z')
110 val.format = *p++;
111 else
112 break;
113 }
114
e91b87a3 115#ifndef LONG_LONG
116 /* Make sure 'g' size is not used on integer types.
117 Well, actually, we can handle hex. */
118 if (val.size == 'g' && val.format != 'f' && val.format != 'x')
632ea0cc 119 val.size = 'w';
e91b87a3 120#endif
632ea0cc 121
7b4ac7e1 122 while (*p == ' ' || *p == '\t') p++;
123 *string_ptr = p;
124
4187119d 125 /* Set defaults for format and size if not specified. */
126 if (val.format == '?')
127 {
128 if (val.size == '?')
129 {
130 /* Neither has been specified. */
131 val.format = oformat;
132 val.size = osize;
133 }
134 else
135 /* If a size is specified, any format makes a reasonable
136 default except 'i'. */
137 val.format = oformat == 'i' ? 'x' : oformat;
138 }
139 else if (val.size == '?')
140 switch (val.format)
141 {
142 case 'a':
143 case 's':
144 /* Addresses must be words. */
145 val.size = osize ? 'w' : osize;
146 break;
147 case 'f':
148 /* Floating point has to be word or giantword. */
149 if (osize == 'w' || osize == 'g')
150 val.size = osize;
151 else
152 /* Default it to giantword if the last used size is not
153 appropriate. */
154 val.size = osize ? 'g' : osize;
155 break;
156 case 'c':
157 /* Characters default to one byte. */
158 val.size = osize ? 'b' : osize;
159 break;
160 default:
161 /* The default is the size most recently specified. */
162 val.size = osize;
163 }
164
7b4ac7e1 165 return val;
166}
167\f
168/* Print value VAL on stdout according to FORMAT, a letter or 0.
169 Do not end with a newline.
632ea0cc 170 0 means print VAL according to its own type.
171 SIZE is the letter for the size of datum being printed.
172 This is used to pad hex numbers so they line up. */
7b4ac7e1 173
174static void
632ea0cc 175print_formatted (val, format, size)
7b4ac7e1 176 register value val;
177 register char format;
632ea0cc 178 char size;
7b4ac7e1 179{
7b4ac7e1 180 int len = TYPE_LENGTH (VALUE_TYPE (val));
181
182 if (VALUE_LVAL (val) == lval_memory)
183 next_address = VALUE_ADDRESS (val) + len;
184
7b4ac7e1 185 switch (format)
186 {
187 case 's':
188 next_address = VALUE_ADDRESS (val)
4187119d 189 + value_print (value_addr (val), stdout, 0, Val_pretty_default);
7b4ac7e1 190 break;
191
192 case 'i':
193 next_address = VALUE_ADDRESS (val)
194 + print_insn (VALUE_ADDRESS (val), stdout);
195 break;
196
3bf57d21 197 default:
198 if (format == 0
199 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_ARRAY
200 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_STRUCT
e91b87a3 201 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_UNION
202 || VALUE_REPEATED (val))
4187119d 203 value_print (val, stdout, format, Val_pretty_default);
3bf57d21 204 else
205 print_scalar_formatted (VALUE_CONTENTS (val), VALUE_TYPE (val),
206 format, size, stdout);
207 }
208}
209
210/* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
211 according to letters FORMAT and SIZE on STREAM.
212 FORMAT may not be zero. Formats s and i are not supported at this level.
213
214 This is how the elements of an array or structure are printed
215 with a format. */
216
217void
218print_scalar_formatted (valaddr, type, format, size, stream)
219 char *valaddr;
220 struct type *type;
221 char format;
222 int size;
223 FILE *stream;
224{
e91b87a3 225 LONGEST val_long;
3bf57d21 226 int len = TYPE_LENGTH (type);
227
e91b87a3 228 if (size == 'g' && sizeof (LONGEST) < 8
229 && format == 'x')
230 {
231 /* ok, we're going to have to get fancy here. Assumption: a
232 long is four bytes. */
233 unsigned long v1, v2, tmp;
234
235 v1 = unpack_long (builtin_type_long, valaddr);
236 v2 = unpack_long (builtin_type_long, valaddr + 4);
237
238#ifdef BYTES_BIG_ENDIAN
239#else
240 /* Little endian -- swap the two for printing */
241 tmp = v1;
242 v1 = v2;
243 v2 = tmp;
244#endif
4187119d 245
e91b87a3 246 switch (format)
247 {
248 case 'x':
4187119d 249 fprintf_filtered (stream, "0x%08x%08x", v1, v2);
e91b87a3 250 break;
251 default:
252 error ("Output size \"g\" unimplemented for format \"%c\".",
253 format);
254 }
255 return;
256 }
257
3bf57d21 258 val_long = unpack_long (type, valaddr);
259
260 /* If value is unsigned, truncate it in case negative. */
261 if (format != 'd')
262 {
263 if (len == sizeof (char))
264 val_long &= (1 << 8 * sizeof(char)) - 1;
265 else if (len == sizeof (short))
266 val_long &= (1 << 8 * sizeof(short)) - 1;
e91b87a3 267 else if (len == sizeof (long))
268 val_long &= (unsigned long) - 1;
3bf57d21 269 }
270
271 switch (format)
272 {
7b4ac7e1 273 case 'x':
e91b87a3 274#ifdef LONG_LONG
275 if (!size)
276 size = (len < sizeof (long long) ? 'w' : 'g');
277 switch (size)
278 {
279 case 'b':
4187119d 280 fprintf_filtered (stream, "0x%02llx", val_long);
e91b87a3 281 break;
282 case 'h':
4187119d 283 fprintf_filtered (stream, "0x%04llx", val_long);
e91b87a3 284 break;
285 case 0: /* no size specified, like in print */
286 case 'w':
4187119d 287 fprintf_filtered (stream, "0x%08llx", val_long);
e91b87a3 288 break;
289 case 'g':
4187119d 290 fprintf_filtered (stream, "0x%016llx", val_long);
e91b87a3 291 break;
292 default:
293 error ("Undefined output size \"%c\".", size);
294 }
295#else
632ea0cc 296 switch (size)
297 {
298 case 'b':
4187119d 299 fprintf_filtered (stream, "0x%02x", val_long);
632ea0cc 300 break;
301 case 'h':
4187119d 302 fprintf_filtered (stream, "0x%04x", val_long);
632ea0cc 303 break;
304 case 0: /* no size specified, like in print */
305 case 'w':
4187119d 306 fprintf_filtered (stream, "0x%08x", val_long);
632ea0cc 307 break;
308 case 'g':
4187119d 309 fprintf_filtered (stream, "0x%o16x", val_long);
632ea0cc 310 break;
311 default:
312 error ("Undefined output size \"%c\".", size);
313 }
e91b87a3 314#endif /* not LONG_LONG */
7b4ac7e1 315 break;
316
317 case 'd':
e91b87a3 318#ifdef LONG_LONG
4187119d 319 fprintf_filtered (stream, "%lld", val_long);
e91b87a3 320#else
4187119d 321 fprintf_filtered (stream, "%d", val_long);
e91b87a3 322#endif
7b4ac7e1 323 break;
324
325 case 'u':
e91b87a3 326#ifdef LONG_LONG
4187119d 327 fprintf_filtered (stream, "%llu", val_long);
e91b87a3 328#else
4187119d 329 fprintf_filtered (stream, "%u", val_long);
e91b87a3 330#endif
7b4ac7e1 331 break;
332
333 case 'o':
334 if (val_long)
e91b87a3 335#ifdef LONG_LONG
4187119d 336 fprintf_filtered (stream, "0%llo", val_long);
e91b87a3 337#else
4187119d 338 fprintf_filtered (stream, "0%o", val_long);
e91b87a3 339#endif
7b4ac7e1 340 else
4187119d 341 fprintf_filtered (stream, "0");
7b4ac7e1 342 break;
343
344 case 'a':
e91b87a3 345 print_address ((CORE_ADDR) val_long, stream);
7b4ac7e1 346 break;
347
348 case 'c':
4187119d 349 value_print (value_from_long (builtin_type_char, val_long), stream, 0,
350 Val_pretty_default);
7b4ac7e1 351 break;
352
353 case 'f':
3bf57d21 354 if (len == sizeof (float))
355 type = builtin_type_float;
e91b87a3 356 else if (len == sizeof (double))
3bf57d21 357 type = builtin_type_double;
7b4ac7e1 358#ifdef IEEE_FLOAT
4187119d 359 if (is_nan (type, valaddr))
7b4ac7e1 360 {
4187119d 361 fprintf_filtered (stream, "Nan");
7b4ac7e1 362 break;
363 }
364#endif
e91b87a3 365 {
366 double doub;
367 int inv;
4187119d 368
e91b87a3 369 doub = unpack_double (type, valaddr, &inv);
370 if (inv)
4187119d 371 fprintf_filtered (stream, "Invalid float value");
372 else
373 fprintf_filtered (stream, len > 4 ? "%.16g" : "%.6g", doub);
e91b87a3 374 }
7b4ac7e1 375 break;
376
377 case 0:
3bf57d21 378 abort ();
7b4ac7e1 379
380 default:
381 error ("Undefined output format \"%c\".", format);
382 }
383}
384
385/* Specify default address for `x' command.
386 `info lines' uses this. */
387
388void
389set_next_address (addr)
390 CORE_ADDR addr;
391{
392 next_address = addr;
393
394 /* Make address available to the user as $_. */
395 set_internalvar (lookup_internalvar ("_"),
e91b87a3 396 value_from_long (builtin_type_int, (LONGEST) addr));
7b4ac7e1 397}
398
399/* Print address ADDR symbolically on STREAM.
400 First print it as a number. Then perhaps print
401 <SYMBOL + OFFSET> after the number. */
402
403void
404print_address (addr, stream)
405 CORE_ADDR addr;
406 FILE *stream;
407{
4187119d 408 register int i = 0;
409 register char *format;
410 register struct symbol *fs;
e91b87a3 411 char *name;
412 int name_location;
7b4ac7e1 413
4187119d 414 i = find_pc_partial_function (addr, &name, &name_location);
7b4ac7e1 415
4187119d 416 /* If nothing comes out, don't print anything symbolic. */
417
418 if (i == 0)
419 format = "0x%x";
420 else if (addr - name_location)
421 format = "0x%x <%s+%d>";
e91b87a3 422 else
4187119d 423 format = "0x%x <%s>";
e91b87a3 424
4187119d 425 fprintf_filtered (stream, format, addr, name, addr - name_location);
7b4ac7e1 426}
427\f
428/* Examine data at address ADDR in format FMT.
429 Fetch it from memory and print on stdout. */
430
431static void
432do_examine (fmt, addr)
433 struct format_data fmt;
434 CORE_ADDR addr;
435{
436 register char format = 0;
437 register char size;
438 register int count = 1;
439 struct type *val_type;
440 register int i;
441 register int maxelts;
442
443 format = fmt.format;
444 size = fmt.size;
445 count = fmt.count;
446 next_address = addr;
447
448 /* String or instruction format implies fetch single bytes
449 regardless of the specified size. */
450 if (format == 's' || format == 'i')
451 size = 'b';
452
453 if (size == 'b')
454 val_type = builtin_type_char;
455 else if (size == 'h')
456 val_type = builtin_type_short;
457 else if (size == 'w')
458 val_type = builtin_type_long;
459 else if (size == 'g')
e91b87a3 460#ifndef LONG_LONG
7b4ac7e1 461 val_type = builtin_type_double;
e91b87a3 462#else
463 val_type = builtin_type_long_long;
464#endif
7b4ac7e1 465
466 maxelts = 8;
467 if (size == 'w')
468 maxelts = 4;
469 if (size == 'g')
470 maxelts = 2;
471 if (format == 's' || format == 'i')
472 maxelts = 1;
473
474 /* Print as many objects as specified in COUNT, at most maxelts per line,
475 with the address of the next one at the start of each line. */
476
477 while (count > 0)
478 {
479 print_address (next_address, stdout);
4187119d 480 printf_filtered (":");
7b4ac7e1 481 for (i = maxelts;
482 i > 0 && count > 0;
483 i--, count--)
484 {
4187119d 485 printf_filtered ("\t");
e91b87a3 486 /* Note that print_formatted sets next_address for the next
487 object. */
7b4ac7e1 488 last_examine_address = next_address;
489 last_examine_value = value_at (val_type, next_address);
632ea0cc 490 print_formatted (last_examine_value, format, size);
7b4ac7e1 491 }
4187119d 492 printf_filtered ("\n");
7b4ac7e1 493 fflush (stdout);
494 }
495}
496\f
497static void
498validate_format (fmt, cmdname)
499 struct format_data fmt;
500 char *cmdname;
501{
502 if (fmt.size != 0)
503 error ("Size letters are meaningless in \"%s\" command.", cmdname);
504 if (fmt.count != 1)
505 error ("Item count other than 1 is meaningless in \"%s\" command.",
506 cmdname);
507 if (fmt.format == 'i' || fmt.format == 's')
508 error ("Format letter \"%c\" is meaningless in \"%s\" command.",
509 fmt.format, cmdname);
510}
511
512static void
513print_command (exp)
514 char *exp;
515{
516 struct expression *expr;
517 register struct cleanup *old_chain = 0;
518 register char format = 0;
519 register value val;
520 struct format_data fmt;
521 int histindex;
522 int cleanup = 0;
523
524 if (exp && *exp == '/')
525 {
526 exp++;
527 fmt = decode_format (&exp, last_format, 0);
528 validate_format (fmt, "print");
529 last_format = format = fmt.format;
530 }
531
532 if (exp && *exp)
533 {
534 expr = parse_c_expression (exp);
535 old_chain = make_cleanup (free_current_contents, &expr);
536 cleanup = 1;
537 val = evaluate_expression (expr);
538 }
539 else
540 val = access_value_history (0);
541
542 histindex = record_latest_value (val);
4187119d 543 if (histindex >= 0) printf_filtered ("$%d = ", histindex);
7b4ac7e1 544
632ea0cc 545 print_formatted (val, format, fmt.size);
4187119d 546 printf_filtered ("\n");
7b4ac7e1 547
548 if (cleanup)
549 do_cleanups (old_chain);
550}
551
552static void
553output_command (exp)
554 char *exp;
555{
556 struct expression *expr;
557 register struct cleanup *old_chain;
558 register char format = 0;
559 register value val;
560 struct format_data fmt;
561
562 if (exp && *exp == '/')
563 {
564 exp++;
565 fmt = decode_format (&exp, 0, 0);
566 validate_format (fmt, "print");
567 format = fmt.format;
568 }
569
570 expr = parse_c_expression (exp);
571 old_chain = make_cleanup (free_current_contents, &expr);
572
573 val = evaluate_expression (expr);
574
632ea0cc 575 print_formatted (val, format, fmt.size);
7b4ac7e1 576
577 do_cleanups (old_chain);
578}
579
580static void
581set_command (exp)
582 char *exp;
583{
584 struct expression *expr = parse_c_expression (exp);
585 register struct cleanup *old_chain
586 = make_cleanup (free_current_contents, &expr);
587 evaluate_expression (expr);
588 do_cleanups (old_chain);
589}
590
591static void
592address_info (exp)
593 char *exp;
594{
595 register struct symbol *sym;
596 register CORE_ADDR val;
e91b87a3 597 int is_a_field_of_this; /* C++: lookup_symbol sets this to nonzero
598 if exp is a field of `this'. */
7b4ac7e1 599
600 if (exp == 0)
601 error ("Argument required.");
602
e91b87a3 603 sym = lookup_symbol (exp, get_selected_block (), VAR_NAMESPACE,
604 &is_a_field_of_this);
7b4ac7e1 605 if (sym == 0)
606 {
607 register int i;
608
e91b87a3 609 if (is_a_field_of_this)
610 {
4187119d 611 printf ("Symbol \"%s\" is a field of the local class variable `this'\n", exp);
e91b87a3 612 return;
613 }
614
7b4ac7e1 615 for (i = 0; i < misc_function_count; i++)
616 if (!strcmp (misc_function_vector[i].name, exp))
617 break;
618
619 if (i < misc_function_count)
620 printf ("Symbol \"%s\" is at 0x%x in a file compiled without -g.\n",
621 exp, misc_function_vector[i].address);
622 else
623 error ("No symbol \"%s\" in current context.", exp);
624 return;
625 }
626
627 printf ("Symbol \"%s\" is ", SYMBOL_NAME (sym));
628 val = SYMBOL_VALUE (sym);
629
630 switch (SYMBOL_CLASS (sym))
631 {
632 case LOC_CONST:
633 case LOC_CONST_BYTES:
634 printf ("constant");
635 break;
636
637 case LOC_LABEL:
638 printf ("a label at address 0x%x", val);
639 break;
640
641 case LOC_REGISTER:
642 printf ("a variable in register %s", reg_names[val]);
643 break;
644
645 case LOC_STATIC:
646 printf ("static at address 0x%x", val);
647 break;
648
bb7592f0 649 case LOC_REGPARM:
650 printf ("an argument in register %s", reg_names[val]);
651 break;
e91b87a3 652
7b4ac7e1 653 case LOC_ARG:
654 printf ("an argument at offset %d", val);
655 break;
656
657 case LOC_LOCAL:
658 printf ("a local variable at frame offset %d", val);
659 break;
660
4187119d 661 case LOC_REF_ARG:
662 printf ("a reference argument at offset %d", val);
663 break;
664
7b4ac7e1 665 case LOC_TYPEDEF:
666 printf ("a typedef");
667 break;
668
669 case LOC_BLOCK:
670 printf ("a function at address 0x%x",
671 BLOCK_START (SYMBOL_BLOCK_VALUE (sym)));
672 break;
673 }
674 printf (".\n");
675}
676\f
677static void
678x_command (exp, from_tty)
679 char *exp;
680 int from_tty;
681{
682 struct expression *expr;
683 struct format_data fmt;
684 struct cleanup *old_chain;
4187119d 685 struct value *val;
7b4ac7e1 686
687 fmt.format = last_format;
688 fmt.size = last_size;
689 fmt.count = 1;
690
691 if (exp && *exp == '/')
692 {
693 exp++;
694 fmt = decode_format (&exp, last_format, last_size);
695 last_size = fmt.size;
696 last_format = fmt.format;
697 }
698
699 /* If we have an expression, evaluate it and use it as the address. */
700
701 if (exp != 0 && *exp != 0)
702 {
703 expr = parse_c_expression (exp);
704 /* Cause expression not to be there any more
705 if this command is repeated with Newline.
706 But don't clobber a user-defined command's definition. */
707 if (from_tty)
708 *exp = 0;
709 old_chain = make_cleanup (free_current_contents, &expr);
4187119d 710 val = evaluate_expression (expr);
1c997a4a 711 /* In rvalue contexts, such as this, functions are coerced into
712 pointers to functions. This makes "x/i main" work. */
713 if (/* last_format == 'i'
714 && */ TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC
4187119d 715 && VALUE_LVAL (val) == lval_memory)
716 next_address = VALUE_ADDRESS (val);
717 else
718 next_address = (CORE_ADDR) value_as_long (val);
7b4ac7e1 719 do_cleanups (old_chain);
720 }
721
722 do_examine (fmt, next_address);
723
e91b87a3 724 /* Set a couple of internal variables if appropriate. */
725 if (last_examine_value)
726 {
727 /* Make last address examined available to the user as $_. */
728 set_internalvar (lookup_internalvar ("_"),
729 value_from_long (builtin_type_int,
730 (LONGEST) last_examine_address));
731
732 /* Make contents of last address examined available to the user as $__.*/
733 set_internalvar (lookup_internalvar ("__"), last_examine_value);
734 }
7b4ac7e1 735}
736\f
737/* Commands for printing types of things. */
738
739static void
740whatis_command (exp)
741 char *exp;
742{
743 struct expression *expr;
744 register value val;
745 register struct cleanup *old_chain;
746
747 if (exp)
748 {
749 expr = parse_c_expression (exp);
750 old_chain = make_cleanup (free_current_contents, &expr);
751 val = evaluate_type (expr);
752 }
753 else
754 val = access_value_history (0);
755
4187119d 756 printf_filtered ("type = ");
757 /* Most of the time users do not want to see all the fields
758 in a structure. If they do they can use the "ptype" command.
759 Hence the "-1" below. */
760 type_print (VALUE_TYPE (val), "", stdout, -1);
761 printf_filtered ("\n");
7b4ac7e1 762
763 if (exp)
764 do_cleanups (old_chain);
765}
766
767static void
768ptype_command (typename)
769 char *typename;
770{
771 register char *p = typename;
772 register int len;
773 extern struct block *get_current_block ();
774 register struct block *b
775 = (have_inferior_p () || have_core_file_p ()) ? get_current_block () : 0;
776 register struct type *type;
777
778 if (typename == 0)
779 error_no_arg ("type name");
780
781 while (*p && *p != ' ' && *p != '\t') p++;
782 len = p - typename;
783 while (*p == ' ' || *p == '\t') p++;
784
785 if (len == 6 && !strncmp (typename, "struct", 6))
786 type = lookup_struct (p, b);
787 else if (len == 5 && !strncmp (typename, "union", 5))
788 type = lookup_union (p, b);
789 else if (len == 4 && !strncmp (typename, "enum", 4))
790 type = lookup_enum (p, b);
791 else
792 {
793 type = lookup_typename (typename, b, 1);
794 if (type == 0)
795 {
796 register struct symbol *sym
e91b87a3 797 = lookup_symbol (typename, b, STRUCT_NAMESPACE, 0);
7b4ac7e1 798 if (sym == 0)
799 error ("No type named %s.", typename);
4187119d 800 printf_filtered ("No type named %s, but there is a ",
7b4ac7e1 801 typename);
802 switch (TYPE_CODE (SYMBOL_TYPE (sym)))
803 {
804 case TYPE_CODE_STRUCT:
4187119d 805 printf_filtered ("struct");
7b4ac7e1 806 break;
807
808 case TYPE_CODE_UNION:
4187119d 809 printf_filtered ("union");
7b4ac7e1 810 break;
811
812 case TYPE_CODE_ENUM:
4187119d 813 printf_filtered ("enum");
7b4ac7e1 814 }
4187119d 815 printf_filtered (" %s. Type \"help ptype\".\n", typename);
7b4ac7e1 816 type = SYMBOL_TYPE (sym);
817 }
818 }
819
820 type_print (type, "", stdout, 1);
4187119d 821 printf_filtered ("\n");
7b4ac7e1 822}
823\f
e91b87a3 824enum display_status {disabled, enabled};
825
7b4ac7e1 826struct display
827{
828 /* Chain link to next auto-display item. */
829 struct display *next;
830 /* Expression to be evaluated and displayed. */
831 struct expression *exp;
832 /* Item number of this auto-display item. */
833 int number;
834 /* Display format specified. */
835 struct format_data format;
e91b87a3 836 /* Innermost block required by this expression when evaluated */
7b4ac7e1 837 struct block *block;
e91b87a3 838 /* Status of this display (enabled or disabled) */
839 enum display_status status;
7b4ac7e1 840};
841
842/* Chain of expressions whose values should be displayed
843 automatically each time the program stops. */
844
845static struct display *display_chain;
846
847static int display_number;
848
849/* Add an expression to the auto-display chain.
850 Specify the expression. */
851
852static void
3bf57d21 853display_command (exp, from_tty)
7b4ac7e1 854 char *exp;
3bf57d21 855 int from_tty;
7b4ac7e1 856{
857 struct format_data fmt;
858 register struct expression *expr;
859 register struct display *new;
e91b87a3 860 extern struct block *innermost_block;
7b4ac7e1 861
862 if (exp == 0)
863 {
864 do_displays ();
865 return;
866 }
867
868 if (*exp == '/')
869 {
870 exp++;
871 fmt = decode_format (&exp, 0, 0);
872 if (fmt.size && fmt.format == 0)
873 fmt.format = 'x';
874 if (fmt.format == 'i' || fmt.format == 's')
875 fmt.size = 'b';
876 }
877 else
878 {
879 fmt.format = 0;
880 fmt.size = 0;
881 fmt.count = 0;
882 }
883
e91b87a3 884 innermost_block = 0;
7b4ac7e1 885 expr = parse_c_expression (exp);
886
887 new = (struct display *) xmalloc (sizeof (struct display));
888
889 new->exp = expr;
e91b87a3 890 new->block = innermost_block;
7b4ac7e1 891 new->next = display_chain;
892 new->number = ++display_number;
893 new->format = fmt;
e91b87a3 894 new->status = enabled;
7b4ac7e1 895 display_chain = new;
896
e91b87a3 897 if (from_tty && have_inferior_p ())
3bf57d21 898 do_one_display (new);
899
7b4ac7e1 900 dont_repeat ();
901}
902
903static void
904free_display (d)
905 struct display *d;
906{
907 free (d->exp);
908 free (d);
909}
910
911/* Clear out the display_chain.
912 Done when new symtabs are loaded, since this invalidates
913 the types stored in many expressions. */
914
915void
916clear_displays ()
917{
918 register struct display *d;
919
920 while (d = display_chain)
921 {
922 free (d->exp);
923 display_chain = d->next;
924 free (d);
925 }
926}
927
3bf57d21 928/* Delete the auto-display number NUM. */
929
930void
931delete_display (num)
932 int num;
933{
934 register struct display *d1, *d;
935
936 if (!display_chain)
937 error ("No display number %d.", num);
938
939 if (display_chain->number == num)
940 {
941 d1 = display_chain;
942 display_chain = d1->next;
943 free_display (d1);
944 }
945 else
946 for (d = display_chain; ; d = d->next)
947 {
948 if (d->next == 0)
949 error ("No display number %d.", num);
950 if (d->next->number == num)
951 {
952 d1 = d->next;
953 d->next = d1->next;
954 free_display (d1);
955 break;
956 }
957 }
958}
959
7b4ac7e1 960/* Delete some values from the auto-display chain.
961 Specify the element numbers. */
962
963static void
964undisplay_command (args)
965 char *args;
966{
967 register char *p = args;
968 register char *p1;
969 register int num;
970 register struct display *d, *d1;
971
972 if (args == 0)
973 {
974 if (query ("Delete all auto-display expressions? "))
975 clear_displays ();
976 dont_repeat ();
977 return;
978 }
979
980 while (*p)
981 {
982 p1 = p;
983 while (*p1 >= '0' && *p1 <= '9') p1++;
984 if (*p1 && *p1 != ' ' && *p1 != '\t')
985 error ("Arguments must be display numbers.");
986
987 num = atoi (p);
988
3bf57d21 989 delete_display (num);
7b4ac7e1 990
991 p = p1;
992 while (*p == ' ' || *p == '\t') p++;
993 }
994 dont_repeat ();
995}
996
e91b87a3 997/* Display a single auto-display.
998 Do nothing if the display cannot be printed in the current context,
999 or if the display is disabled. */
3bf57d21 1000
1001static void
1002do_one_display (d)
1003 struct display *d;
1004{
e91b87a3 1005 int within_current_scope;
1006
1007 if (d->status == disabled)
1008 return;
1009
1010 if (d->block)
1011 within_current_scope = contained_in (get_selected_block (), d->block);
1012 else
1013 within_current_scope = 1;
1014 if (!within_current_scope)
1015 return;
1016
3bf57d21 1017 current_display_number = d->number;
1018
4187119d 1019 printf_filtered ("%d: ", d->number);
3bf57d21 1020 if (d->format.size)
1021 {
4187119d 1022 printf_filtered ("x/");
3bf57d21 1023 if (d->format.count != 1)
4187119d 1024 printf_filtered ("%d", d->format.count);
1025 printf_filtered ("%c", d->format.format);
3bf57d21 1026 if (d->format.format != 'i' && d->format.format != 's')
4187119d 1027 printf_filtered ("%c", d->format.size);
1028 printf_filtered (" ");
3bf57d21 1029 print_expression (d->exp, stdout);
1030 if (d->format.count != 1)
4187119d 1031 printf_filtered ("\n");
3bf57d21 1032 else
4187119d 1033 printf_filtered (" ");
3bf57d21 1034 do_examine (d->format,
e91b87a3 1035 (CORE_ADDR) value_as_long (evaluate_expression (d->exp)));
1036
3bf57d21 1037 }
1038 else
1039 {
1040 if (d->format.format)
4187119d 1041 printf_filtered ("/%c ", d->format.format);
3bf57d21 1042 print_expression (d->exp, stdout);
4187119d 1043 printf_filtered (" = ");
3bf57d21 1044 print_formatted (evaluate_expression (d->exp),
1045 d->format.format, d->format.size);
4187119d 1046 printf_filtered ("\n");
3bf57d21 1047 }
1048
1049 fflush (stdout);
1050 current_display_number = -1;
1051}
1052
e91b87a3 1053/* Display all of the values on the auto-display chain which can be
1054 evaluated in the current scope. */
7b4ac7e1 1055
1056void
1057do_displays ()
1058{
1059 register struct display *d;
1060
1061 for (d = display_chain; d; d = d->next)
3bf57d21 1062 do_one_display (d);
1063}
1064
1065/* Delete the auto-display which we were in the process of displaying.
1066 This is done when there is an error or a signal. */
1067
1068void
4187119d 1069disable_display (num)
1070 int num;
1071{
1072 register struct display *d;
1073
1074 for (d = display_chain; d; d = d->next)
1075 if (d->number == num)
1076 {
1077 d->status = disabled;
1078 return;
1079 }
1080 printf ("No display number %d.\n", num);
1081}
1082
1083void
1084disable_current_display ()
3bf57d21 1085{
1086 if (current_display_number >= 0)
7b4ac7e1 1087 {
4187119d 1088 disable_display (current_display_number);
1089 fprintf (stderr, "Disabling display %d to avoid infinite recursion.\n",
3bf57d21 1090 current_display_number);
7b4ac7e1 1091 }
3bf57d21 1092 current_display_number = -1;
7b4ac7e1 1093}
1094
1095static void
1096display_info ()
1097{
1098 register struct display *d;
1099
1100 if (!display_chain)
1101 printf ("There are no auto-display expressions now.\n");
1102 else
4187119d 1103 printf_filtered ("Auto-display expressions now in effect:\n\
e91b87a3 1104Num Enb Expression\n");
1105
7b4ac7e1 1106 for (d = display_chain; d; d = d->next)
1107 {
4187119d 1108 printf_filtered ("%d: %c ", d->number, "ny"[(int)d->status]);
7b4ac7e1 1109 if (d->format.size)
4187119d 1110 printf_filtered ("/%d%c%c ", d->format.count, d->format.size,
7b4ac7e1 1111 d->format.format);
1112 else if (d->format.format)
4187119d 1113 printf_filtered ("/%c ", d->format.format);
7b4ac7e1 1114 print_expression (d->exp, stdout);
e91b87a3 1115 if (d->block && !contained_in (get_selected_block (), d->block))
4187119d 1116 printf_filtered (" (cannot be evaluated in the current context)");
1117 printf_filtered ("\n");
7b4ac7e1 1118 fflush (stdout);
1119 }
1120}
e91b87a3 1121
1122void
1123enable_display (args)
1124 char *args;
1125{
1126 register char *p = args;
1127 register char *p1;
1128 register int num;
1129 register struct display *d;
1130
1131 if (p == 0)
1132 {
4187119d 1133 for (d = display_chain; d; d = d->next)
e91b87a3 1134 d->status = enabled;
1135 }
1136 else
1137 while (*p)
1138 {
1139 p1 = p;
1140 while (*p1 >= '0' && *p1 <= '9')
1141 p1++;
1142 if (*p1 && *p1 != ' ' && *p1 != '\t')
1143 error ("Arguments must be display numbers.");
1144
1145 num = atoi (p);
1146
1147 for (d = display_chain; d; d = d->next)
1148 if (d->number == num)
1149 {
1150 d->status = enabled;
1151 goto win;
1152 }
1153 printf ("No display number %d.\n", num);
1154 win:
1155 p = p1;
1156 while (*p == ' ' || *p == '\t')
1157 p++;
1158 }
1159}
1160
1161void
4187119d 1162disable_display_command (args, from_tty)
e91b87a3 1163 char *args;
4187119d 1164 int from_tty;
e91b87a3 1165{
1166 register char *p = args;
1167 register char *p1;
1168 register int num;
1169 register struct display *d;
1170
1171 if (p == 0)
1172 {
4187119d 1173 for (d = display_chain; d; d = d->next)
e91b87a3 1174 d->status = disabled;
1175 }
1176 else
1177 while (*p)
1178 {
1179 p1 = p;
1180 while (*p1 >= '0' && *p1 <= '9')
1181 p1++;
1182 if (*p1 && *p1 != ' ' && *p1 != '\t')
1183 error ("Arguments must be display numbers.");
1184
1185 num = atoi (p);
1186
4187119d 1187 disable_display (atoi (p));
1188
e91b87a3 1189 p = p1;
1190 while (*p == ' ' || *p == '\t')
1191 p++;
1192 }
1193}
1194
7b4ac7e1 1195\f
1196/* Print the value in stack frame FRAME of a variable
1197 specified by a struct symbol. */
1198
1199void
1200print_variable_value (var, frame, stream)
1201 struct symbol *var;
1202 CORE_ADDR frame;
1203 FILE *stream;
1204{
1205 value val = read_var_value (var, frame);
4187119d 1206 value_print (val, stream, 0, Val_pretty_default);
1207}
1208
1209static int
1210compare_ints (i, j)
1211 int *i, *j;
1212{
1213 return *i - *j;
7b4ac7e1 1214}
1215
1216/* Print the arguments of a stack frame, given the function FUNC
e91b87a3 1217 running in that frame (as a symbol), the info on the frame,
7b4ac7e1 1218 and the number of args according to the stack frame (or -1 if unknown). */
1219
1220static void print_frame_nameless_args ();
1221
e91b87a3 1222void
1223print_frame_args (func, fi, num, stream)
7b4ac7e1 1224 struct symbol *func;
e91b87a3 1225 struct frame_info *fi;
7b4ac7e1 1226 int num;
1227 FILE *stream;
1228{
1229 struct block *b;
1230 int nsyms = 0;
1231 int first = 1;
1232 register int i;
bb7592f0 1233 register int last_regparm = 0;
e91b87a3 1234 register struct symbol *lastsym, *sym, *nextsym;
7b4ac7e1 1235 register value val;
7a67dd45 1236 /* Offset of stack argument that is at the highest offset.
1237 -1 if we haven't come to a stack argument yet. */
1238 CORE_ADDR highest_offset = (CORE_ADDR) -1;
e91b87a3 1239 register CORE_ADDR addr = FRAME_ARGS_ADDRESS (fi);
7b4ac7e1 1240
1241 if (func)
1242 {
1243 b = SYMBOL_BLOCK_VALUE (func);
1244 nsyms = BLOCK_NSYMS (b);
1245 }
1246
4187119d 1247 for (i = 0; i < nsyms; i++)
7b4ac7e1 1248 {
4187119d 1249 QUIT;
1250 sym = BLOCK_SYM (b, i);
1251
1252 if (SYMBOL_CLASS (sym) != LOC_REGPARM
1253 && SYMBOL_CLASS (sym) != LOC_ARG
1254 && SYMBOL_CLASS (sym) != LOC_REF_ARG)
1255 continue;
1256
7b4ac7e1 1257 /* Print the next arg. */
bb7592f0 1258 if (SYMBOL_CLASS (sym) == LOC_REGPARM)
e91b87a3 1259 val = value_from_register (SYMBOL_TYPE (sym),
1260 SYMBOL_VALUE (sym),
1261 FRAME_INFO_ID (fi));
bb7592f0 1262 else
4187119d 1263 {
1264 int current_offset = SYMBOL_VALUE (sym);
1265 int arg_size = TYPE_LENGTH (SYMBOL_TYPE (sym));
1266
1267 if (SYMBOL_CLASS (sym) == LOC_REF_ARG)
1268 val = value_at (SYMBOL_TYPE (sym),
1269 read_memory_integer (addr + current_offset,
1270 sizeof (CORE_ADDR)));
1271 else
1272 val = value_at (SYMBOL_TYPE (sym), addr + current_offset);
1273
1274 /* Round up address of next arg to multiple of size of int. */
1275 current_offset
1276 = (((current_offset + sizeof (int) - 1) / sizeof (int))
1277 * sizeof (int));
7a67dd45 1278
1279 /* If this is the highest offset seen yet, set highest_offset. */
1280 if (highest_offset == (CORE_ADDR)-1
1281 || ((current_offset
1282 + (arg_size - sizeof (int) + 3) / (sizeof (int)))
1283 > highest_offset))
4187119d 1284 highest_offset = current_offset;
1285 }
bb7592f0 1286
7b4ac7e1 1287 if (! first)
4187119d 1288 fprintf_filtered (stream, ", ");
1289 fputs_filtered (SYMBOL_NAME (sym), stream);
1290 fputs_filtered ("=", stream);
7a67dd45 1291
1292/* Nonzero if a LOC_ARG which is a struct is useless. */
1293#if !defined (STRUCT_ARG_SYM_GARBAGE)
1294#define STRUCT_ARG_SYM_GARBAGE(gcc_p) 0
1295#endif
1296
1297 if (STRUCT_ARG_SYM_GARBAGE (b->gcc_compile_flag)
1298 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
1299 && SYMBOL_CLASS (sym) == LOC_ARG)
1300 {
1301 /* Try looking up that name. SunOS4 puts out a usable
1302 symbol as a local variable (in addition to the one
1303 for the arg). */
1304 struct symbol *sym2 =
1305 lookup_symbol (SYMBOL_NAME (sym), b, VAR_NAMESPACE, 0);
1306
1307 if (sym2 != NULL)
1308 val = value_of_variable (sym2);
1309 else
1310 {
1311 fputs_filtered ("?", stream);
1312 first = 0;
1313 continue;
1314 }
1315 }
1316
4187119d 1317 value_print (val, stream, 0, Val_no_prettyprint);
7b4ac7e1 1318 first = 0;
7b4ac7e1 1319 }
4187119d 1320
1321 /* Don't print nameless args in situations where we don't know
1322 enough about the stack to find them. */
1323 if (num != -1)
1324 {
7a67dd45 1325 if (highest_offset != (CORE_ADDR) -1
1326 && num * sizeof (int) + FRAME_ARGS_SKIP > highest_offset)
4187119d 1327 print_frame_nameless_args (addr,
1328 highest_offset + sizeof (int),
1329 num * sizeof (int) + FRAME_ARGS_SKIP,
1330 stream);
1331 else
1332 print_frame_nameless_args (addr, FRAME_ARGS_SKIP,
1333 num * sizeof (int) + FRAME_ARGS_SKIP,
1334 stream);
1335 }
7b4ac7e1 1336}
1337
1338static void
1339print_frame_nameless_args (argsaddr, start, end, stream)
1340 CORE_ADDR argsaddr;
1341 int start;
1342 int end;
1343 FILE *stream;
1344{
1345 while (start < end)
1346 {
1347 QUIT;
1348 if (start != FRAME_ARGS_SKIP)
4187119d 1349 fprintf_filtered (stream, ", ");
1350#ifndef PRINT_TYPELESS_INTEGER
1351 fprintf_filtered (stream, "%d",
7b4ac7e1 1352 read_memory_integer (argsaddr + start, sizeof (int)));
4187119d 1353#else
1354 PRINT_TYPELESS_INTEGER (stream, builtin_type_int,
1355 (LONGEST)
1356 read_memory_integer (argsaddr + start,
1357 sizeof (int)));
1358#endif
7b4ac7e1 1359 start += sizeof (int);
1360 }
1361}
1362\f
632ea0cc 1363static void
1364printf_command (arg)
1365 char *arg;
1366{
1367 register char *f;
1368 register char *s = arg;
1369 char *string;
1370 value *val_args;
1371 int nargs = 0;
1372 int allocated_args = 20;
1373 char *arg_bytes;
632ea0cc 1374
1375 val_args = (value *) xmalloc (allocated_args * sizeof (value));
1376
1377 if (s == 0)
1378 error_no_arg ("format-control string and values to print");
1379
1380 /* Skip white space before format string */
1381 while (*s == ' ' || *s == '\t') s++;
1382
1383 /* A format string should follow, enveloped in double quotes */
1384 if (*s++ != '"')
1385 error ("Bad format string, missing '\"'.");
1386
1387 /* Parse the format-control string and copy it into the string STRING,
1388 processing some kinds of escape sequence. */
1389
1390 f = string = (char *) alloca (strlen (s) + 1);
1391 while (*s != '"')
1392 {
1393 int c = *s++;
1394 switch (c)
1395 {
1396 case '\0':
1397 error ("Bad format string, non-terminated '\"'.");
1398 /* doesn't return */
1399
1400 case '\\':
1401 switch (c = *s++)
1402 {
1403 case '\\':
1404 *f++ = '\\';
1405 break;
1406 case 'n':
1407 *f++ = '\n';
1408 break;
1409 case 't':
1410 *f++ = '\t';
1411 break;
1412 case 'r':
1413 *f++ = '\r';
1414 break;
1415 case '"':
1416 *f++ = '"';
1417 break;
1418 default:
1419 /* ??? TODO: handle other escape sequences */
1420 error ("Unrecognized \\ escape character in format string.");
1421 }
1422 break;
1423
1424 default:
1425 *f++ = c;
1426 }
1427 }
1428
1429 /* Skip over " and following space and comma. */
1430 s++;
1431 *f++ = '\0';
1432 while (*s == ' ' || *s == '\t') s++;
1433
1434 if (*s != ',' && *s != 0)
1435 error ("Invalid argument syntax");
1436
1437 if (*s == ',') s++;
1438 while (*s == ' ' || *s == '\t') s++;
1439
4187119d 1440 {
1441 /* Now scan the string for %-specs and see what kinds of args they want.
1442 argclass[I] classifies the %-specs so we can give vprintf something
1443 of the right size. */
1444
1445 enum argclass {int_arg, string_arg, double_arg, long_long_arg};
1446 enum argclass *argclass;
1447 int nargs_wanted;
1448 int argindex;
1449 int lcount;
1450 int i;
1451
1452 argclass = (enum argclass *) alloca (strlen (s) * sizeof *argclass);
1453 nargs_wanted = 0;
1454 f = string;
1455 while (*f)
1456 if (*f++ == '%')
1457 {
1458 lcount = 0;
1459 while (index ("0123456789.hlL-+ #", *f))
1460 {
1461 if (*f == 'l' || *f == 'L')
1462 lcount++;
1463 f++;
1464 }
1465 if (*f == 's')
1466 argclass[nargs_wanted++] = string_arg;
1467 else if (*f == 'e' || *f == 'f' || *f == 'g')
1468 argclass[nargs_wanted++] = double_arg;
1469 else if (lcount > 1)
1470 argclass[nargs_wanted++] = long_long_arg;
1471 else if (*f != '%')
1472 argclass[nargs_wanted++] = int_arg;
1473 f++;
1474 }
1475
1476 /* Now, parse all arguments and evaluate them.
1477 Store the VALUEs in VAL_ARGS. */
1478
1479 while (*s != '\0')
632ea0cc 1480 {
4187119d 1481 char *s1;
1482 if (nargs == allocated_args)
1483 val_args = (value *) xrealloc (val_args,
1484 (allocated_args *= 2)
1485 * sizeof (value));
1486 s1 = s;
1487 val_args[nargs] = parse_to_comma_and_eval (&s1);
1488
1489 /* If format string wants a float, unchecked-convert the value to
1490 floating point of the same size */
1491
1492 if (argclass[nargs] == double_arg)
1493 {
1494 if (TYPE_LENGTH (VALUE_TYPE (val_args[nargs])) == sizeof (float))
1495 VALUE_TYPE (val_args[nargs]) = builtin_type_float;
1496 if (TYPE_LENGTH (VALUE_TYPE (val_args[nargs])) == sizeof (double))
1497 VALUE_TYPE (val_args[nargs]) = builtin_type_double;
1498 }
1499 nargs++;
1500 s = s1;
1501 if (*s == ',')
1502 s++;
1503 }
1504
1505 if (nargs != nargs_wanted)
1506 error ("Wrong number of arguments for specified format-string");
1507
1508 /* Now lay out an argument-list containing the arguments
1509 as doubles, integers and C pointers. */
1510
1511 arg_bytes = (char *) alloca (sizeof (double) * nargs);
1512 argindex = 0;
1513 for (i = 0; i < nargs; i++)
1514 {
1515 if (argclass[i] == string_arg)
1516 {
1517 char *str;
1518 int tem, j;
1519 tem = value_as_long (val_args[i]);
1520
1521 /* This is a %s argument. Find the length of the string. */
1522 for (j = 0; ; j++)
1523 {
1524 char c;
1525 QUIT;
1526 read_memory (tem + j, &c, 1);
1527 if (c == 0)
1528 break;
1529 }
1530
1531 /* Copy the string contents into a string inside GDB. */
1532 str = (char *) alloca (j + 1);
1533 read_memory (tem, str, j);
1534 str[j] = 0;
1535
1536 /* Pass address of internal copy as the arg to vprintf. */
1537 *((int *) &arg_bytes[argindex]) = (int) str;
1538 argindex += sizeof (int);
1539 }
1540 else if (VALUE_TYPE (val_args[i])->code == TYPE_CODE_FLT)
1541 {
1542 *((double *) &arg_bytes[argindex]) = value_as_double (val_args[i]);
1543 argindex += sizeof (double);
1544 }
1545 else
1546#ifdef LONG_LONG
1547 if (argclass[i] == long_long_arg)
1548 {
1549 *(long long *) &arg_bytes[argindex] = value_as_long (val_args[i]);
1550 argindex += sizeof (long long);
1551 }
1552 else
1553#endif
1554 {
1555 *((int *) &arg_bytes[argindex]) = value_as_long (val_args[i]);
1556 argindex += sizeof (int);
1557 }
632ea0cc 1558 }
4187119d 1559 }
1560 vprintf (string, arg_bytes);
1561}
1562\f
1563/* Helper function for asdump_command. Finds the bounds of a function
1564 for a specified section of text. PC is an address within the
1565 function which you want bounds for; *LOW and *HIGH are set to the
1566 beginning (inclusive) and end (exclusive) of the function. This
1567 function returns 1 on success and 0 on failure. */
1568
1569static int
1570containing_function_bounds (pc, low, high)
1571 CORE_ADDR pc, *low, *high;
1572{
1573 int scan;
632ea0cc 1574
4187119d 1575 if (!find_pc_partial_function (pc, 0, low))
1576 return 0;
632ea0cc 1577
4187119d 1578 scan = *low;
1579 do {
1580 scan++;
1581 if (!find_pc_partial_function (scan, 0, high))
1582 return 0;
1583 } while (*low == *high);
632ea0cc 1584
4187119d 1585 return 1;
1586}
632ea0cc 1587
4187119d 1588/* Dump a specified section of assembly code. With no command line
1589 arguments, this command will dump the assembly code for the
1590 function surrounding the pc value in the selected frame. With one
1591 argument, it will dump the assembly code surrounding that pc value.
1592 Two arguments are interpeted as bounds within which to dump
1593 assembly. */
632ea0cc 1594
4187119d 1595static void
1596disassemble_command (arg, from_tty)
1597 char *arg;
1598 int from_tty;
1599{
1600 CORE_ADDR low, high;
1601 CORE_ADDR pc;
1602 char *space_index;
632ea0cc 1603
4187119d 1604 if (!arg)
1605 {
1606 if (!selected_frame)
1607 error ("No frame selected.\n");
632ea0cc 1608
4187119d 1609 pc = get_frame_pc (selected_frame);
1610 if (!containing_function_bounds (pc, &low, &high))
1611 error ("No function contains pc specified by selected frame.\n");
1612 }
1613 else if (!(space_index = (char *) index (arg, ' ')))
1614 {
1615 /* One argument. */
1616 pc = parse_and_eval_address (arg);
1617 if (!containing_function_bounds (pc, &low, &high))
1618 error ("No function contains specified pc.\n");
1619 }
1620 else
1621 {
1622 /* Two arguments. */
1623 *space_index = '\0';
1624 low = parse_and_eval_address (arg);
1625 high = parse_and_eval_address (space_index + 1);
1626 }
632ea0cc 1627
4187119d 1628 printf_filtered ("Dump of assembler code ");
1629 if (!space_index)
1630 {
1631 char *name;
1632 find_pc_partial_function (pc, &name, 0);
1633 printf_filtered ("for function %s:\n", name);
632ea0cc 1634 }
4187119d 1635 else
1636 printf_filtered ("from 0x%x to 0x%x:\n", low, high);
632ea0cc 1637
4187119d 1638 /* Dump the specified range. */
1639 for (pc = low; pc < high; )
1640 {
1641 QUIT;
1642 print_address (pc, stdout);
1643 printf_filtered (":\t");
1644 pc += print_insn (pc, stdout);
1645 printf_filtered ("\n");
1646 }
1647 printf_filtered ("End of assembler dump.\n");
1648 fflush (stdout);
632ea0cc 1649}
4187119d 1650
632ea0cc 1651\f
e91b87a3 1652extern struct cmd_list_element *enablelist, *disablelist, *deletelist;
1653extern struct cmd_list_element *cmdlist, *setlist;
1654
1655void
1656_initialize_printcmd ()
7b4ac7e1 1657{
3bf57d21 1658 current_display_number = -1;
1659
7b4ac7e1 1660 add_info ("address", address_info,
1661 "Describe where variable VAR is stored.");
1662
1663 add_com ("x", class_vars, x_command,
1664 "Examine memory: x/FMT ADDRESS.\n\
1665ADDRESS is an expression for the memory address to examine.\n\
1666FMT is a repeat count followed by a format letter and a size letter.\n\
1667Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
1668 f(float), a(address), i(instruction), c(char) and s(string).\n\
1669Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
1670 g is meaningful only with f, for type double.\n\
1671The specified number of objects of the specified size are printed\n\
1672according to the format.\n\n\
1673Defaults for format and size letters are those previously used.\n\
1674Default count is 1. Default address is following last thing printed\n\
1675with this command or \"print\".");
1676
4187119d 1677 add_com ("disassemble", class_vars, disassemble_command,
1678 "Disassemble a specified section of memory.\n\
1679Default is the function surrounding the pc of the selected frame.\n\
1680With a single argument, the function surrounding that address is dumped.\n\
1681Two arguments are taken as a range of memory to dump.");
1682
7b4ac7e1 1683 add_com ("ptype", class_vars, ptype_command,
1684 "Print definition of type TYPE.\n\
1685Argument may be a type name defined by typedef, or \"struct STRUCTNAME\"\n\
1686or \"union UNIONNAME\" or \"enum ENUMNAME\".\n\
1687The selected stack frame's lexical context is used to look up the name.");
1688
1689 add_com ("whatis", class_vars, whatis_command,
1690 "Print data type of expression EXP.");
1691
1692 add_info ("display", display_info,
1693 "Expressions to display when program stops, with code numbers.");
e91b87a3 1694
4187119d 1695 add_cmd ("undisplay", class_vars, undisplay_command,
e91b87a3 1696 "Cancel some expressions to be displayed when program stops.\n\
7b4ac7e1 1697Arguments are the code numbers of the expressions to stop displaying.\n\
1698No argument means cancel all automatic-display expressions.\n\
e91b87a3 1699\"delete display\" has the same effect as this command.\n\
1700Do \"info display\" to see current list of code numbers.",
1701 &cmdlist);
1702
7b4ac7e1 1703 add_com ("display", class_vars, display_command,
1704 "Print value of expression EXP each time the program stops.\n\
1705/FMT may be used before EXP as in the \"print\" command.\n\
1706/FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
1707as in the \"x\" command, and then EXP is used to get the address to examine\n\
1708and examining is done as in the \"x\" command.\n\n\
1709With no argument, display all currently requested auto-display expressions.\n\
1710Use \"undisplay\" to cancel display requests previously made.");
1711
e91b87a3 1712 add_cmd ("display", class_vars, enable_display,
1713 "Enable some expressions to be displayed when program stops.\n\
1714Arguments are the code numbers of the expressions to resume displaying.\n\
1715No argument means enable all automatic-display expressions.\n\
1716Do \"info display\" to see current list of code numbers.", &enablelist);
1717
4187119d 1718 add_cmd ("display", class_vars, disable_display_command,
e91b87a3 1719 "Disable some expressions to be displayed when program stops.\n\
1720Arguments are the code numbers of the expressions to stop displaying.\n\
1721No argument means disable all automatic-display expressions.\n\
1722Do \"info display\" to see current list of code numbers.", &disablelist);
1723
1724 add_cmd ("display", class_vars, undisplay_command,
1725 "Cancel some expressions to be displayed when program stops.\n\
1726Arguments are the code numbers of the expressions to stop displaying.\n\
1727No argument means cancel all automatic-display expressions.\n\
1728Do \"info display\" to see current list of code numbers.", &deletelist);
1729
632ea0cc 1730 add_com ("printf", class_vars, printf_command,
1731 "printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
1732This is useful for formatted output in user-defined commands.");
7b4ac7e1 1733 add_com ("output", class_vars, output_command,
1734 "Like \"print\" but don't put in value history and don't print newline.\n\
1735This is useful in user-defined commands.");
1736
e91b87a3 1737 add_prefix_cmd ("set", class_vars, set_command,
1738"Perform an assignment VAR = EXP.\n\
1739You must type the \"=\". VAR may be a debugger \"convenience\" variable\n\
1740(names starting with $), a register (a few standard names starting with $),\n\
1741or an actual variable in the program being debugged. EXP is any expression.\n\
1742Use \"set variable\" for variables with names identical to set subcommands.\n\
1743\nWith a subcommand, this command modifies parts of the gdb environment",
1744 &setlist, "set ", 1, &cmdlist);
1745
1746 add_cmd ("variable", class_vars, set_command,
1747 "Perform an assignment VAR = EXP.\n\
1748You must type the \"=\". VAR may be a debugger \"convenience\" variable\n\
1749(names starting with $), a register (a few standard names starting with $),\n\
1750or an actual variable in the program being debugged. EXP is any expression.\n\
1751This may usually be abbreviated to simply \"set\".",
1752 &setlist);
7b4ac7e1 1753
1754 add_com ("print", class_vars, print_command,
1755 concat ("Print value of expression EXP.\n\
1756Variables accessible are those of the lexical environment of the selected\n\
1757stack frame, plus all those whose scope is global or an entire file.\n\
1758\n\
1759$NUM gets previous value number NUM. $ and $$ are the last two values.\n\
1760$$NUM refers to NUM'th value back from the last one.\n\
1761Names starting with $ refer to registers (with the values they would have\n\
1762if the program were to return to the stack frame now selected, restoring\n\
1763all registers saved by frames farther in) or else to debugger\n\
1764\"convenience\" variables (any such name not a known register).\n\
1765Use assignment expressions to give values to convenience variables.\n",
1766 "\n\
1767\{TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
1768@ is a binary operator for treating consecutive data objects\n\
1769anywhere in memory as an array. FOO@NUM gives an array whose first\n\
1770element is FOO, whose second element is stored in the space following\n\
1771where FOO is stored, etc. FOO must be an expression whose value\n\
1772resides in memory.\n",
1773 "\n\
1774EXP may be preceded with /FMT, where FMT is a format letter\n\
1775but no count or size letter (see \"x\" command)."));
1776 add_com_alias ("p", "print", class_vars, 1);
1777}
This page took 0.104802 seconds and 4 git commands to generate.