gdb-2.8.1
[deliverable/binutils-gdb.git] / gdb / printcmd.c
1 /* Print values for GNU debugger GDB.
2 Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
3
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
9
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
16
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
19 */
20
21 #include <stdio.h>
22 #include "defs.h"
23 #include "initialize.h"
24 #include "param.h"
25 #include "symtab.h"
26 #include "value.h"
27 #include "expression.h"
28
29 struct format_data
30 {
31 int count;
32 char format;
33 char size;
34 };
35
36 /* Last specified output format. */
37
38 static char last_format = 'x';
39
40 /* Last specified examination size. 'b', 'h', 'w' or `q'. */
41
42 static char last_size = 'w';
43
44 /* Default address to examine next. */
45
46 static CORE_ADDR next_address;
47
48 /* Last address examined. */
49
50 static CORE_ADDR last_examine_address;
51
52 /* Contents of last address examined.
53 This is not valid past the end of the `x' command! */
54
55 static value last_examine_value;
56
57 /* Number of auto-display expression currently being displayed.
58 So that we can deleted it if we get an error or a signal within it.
59 -1 when not doing one. */
60
61 int current_display_number;
62
63 static void do_one_display ();
64
65 void do_displays ();
66 void print_address ();
67 void print_scalar_formatted ();
68
69 START_FILE
70 \f
71 /* Decode a format specification. *STRING_PTR should point to it.
72 OFORMAT and OSIZE are used as defaults for the format and size
73 if none are given in the format specification.
74 The structure returned describes all the data
75 found in the specification. In addition, *STRING_PTR is advanced
76 past the specification and past all whitespace following it. */
77
78 struct format_data
79 decode_format (string_ptr, oformat, osize)
80 char **string_ptr;
81 char oformat;
82 char osize;
83 {
84 struct format_data val;
85 register char *p = *string_ptr;
86
87 val.format = oformat;
88 val.size = osize;
89 val.count = 1;
90
91 if (*p >= '0' && *p <= '9')
92 val.count = atoi (p);
93 while (*p >= '0' && *p <= '9') p++;
94
95 /* Now process size or format letters that follow. */
96
97 while (1)
98 {
99 if (*p == 'b' || *p == 'h' || *p == 'w' || *p == 'g')
100 val.size = *p++;
101 else if (*p >= 'a' && *p <= 'z')
102 val.format = *p++;
103 else
104 break;
105 }
106
107 /* Make sure 'g' size is not used on integer types. */
108 if (val.size == 'g' && val.format != 'f')
109 val.size = 'w';
110
111 while (*p == ' ' || *p == '\t') p++;
112 *string_ptr = p;
113
114 return val;
115 }
116 \f
117 /* Print value VAL on stdout according to FORMAT, a letter or 0.
118 Do not end with a newline.
119 0 means print VAL according to its own type.
120 SIZE is the letter for the size of datum being printed.
121 This is used to pad hex numbers so they line up. */
122
123 static void
124 print_formatted (val, format, size)
125 register value val;
126 register char format;
127 char size;
128 {
129 register CORE_ADDR val_long;
130 int len = TYPE_LENGTH (VALUE_TYPE (val));
131
132 if (VALUE_LVAL (val) == lval_memory)
133 next_address = VALUE_ADDRESS (val) + len;
134
135 switch (format)
136 {
137 case 's':
138 next_address = VALUE_ADDRESS (val)
139 + value_print (value_addr (val), stdout, 0);
140 break;
141
142 case 'i':
143 next_address = VALUE_ADDRESS (val)
144 + print_insn (VALUE_ADDRESS (val), stdout);
145 break;
146
147 default:
148 if (format == 0
149 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_ARRAY
150 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_STRUCT
151 || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_UNION)
152 value_print (val, stdout, format);
153 else
154 print_scalar_formatted (VALUE_CONTENTS (val), VALUE_TYPE (val),
155 format, size, stdout);
156 }
157 }
158
159 /* Print a scalar of data of type TYPE, pointed to in GDB by VALADDR,
160 according to letters FORMAT and SIZE on STREAM.
161 FORMAT may not be zero. Formats s and i are not supported at this level.
162
163 This is how the elements of an array or structure are printed
164 with a format. */
165
166 void
167 print_scalar_formatted (valaddr, type, format, size, stream)
168 char *valaddr;
169 struct type *type;
170 char format;
171 int size;
172 FILE *stream;
173 {
174 long val_long;
175 int len = TYPE_LENGTH (type);
176
177 val_long = unpack_long (type, valaddr);
178
179 /* If value is unsigned, truncate it in case negative. */
180 if (format != 'd')
181 {
182 if (len == sizeof (char))
183 val_long &= (1 << 8 * sizeof(char)) - 1;
184 else if (len == sizeof (short))
185 val_long &= (1 << 8 * sizeof(short)) - 1;
186 }
187
188 switch (format)
189 {
190 case 'x':
191 switch (size)
192 {
193 case 'b':
194 printf ("0x%02x", val_long);
195 break;
196 case 'h':
197 printf ("0x%04x", val_long);
198 break;
199 case 0: /* no size specified, like in print */
200 case 'w':
201 printf ("0x%08x", val_long);
202 break;
203 case 'g':
204 printf ("0x%16x", val_long);
205 break;
206 default:
207 error ("Undefined output size \"%c\".", size);
208 }
209 break;
210
211 case 'd':
212 printf ("%d", val_long);
213 break;
214
215 case 'u':
216 printf ("%u", val_long);
217 break;
218
219 case 'o':
220 if (val_long)
221 printf ("0%o", val_long);
222 else
223 printf ("0");
224 break;
225
226 case 'a':
227 print_address (val_long, stream);
228 break;
229
230 case 'c':
231 value_print (value_from_long (builtin_type_char, val_long), stream, 0);
232 break;
233
234 case 'f':
235 if (len == sizeof (float))
236 type = builtin_type_float;
237 if (len == sizeof (double))
238 type = builtin_type_double;
239 #ifdef IEEE_FLOAT
240 if (is_nan (unpack_double (type, valaddr)))
241 {
242 printf ("Nan");
243 break;
244 }
245 #endif
246 printf ("%g", unpack_double (type, valaddr));
247 break;
248
249 case 0:
250 abort ();
251
252 default:
253 error ("Undefined output format \"%c\".", format);
254 }
255 }
256
257 /* Specify default address for `x' command.
258 `info lines' uses this. */
259
260 void
261 set_next_address (addr)
262 CORE_ADDR addr;
263 {
264 next_address = addr;
265
266 /* Make address available to the user as $_. */
267 set_internalvar (lookup_internalvar ("_"),
268 value_from_long (builtin_type_int, addr));
269 }
270
271 /* Print address ADDR symbolically on STREAM.
272 First print it as a number. Then perhaps print
273 <SYMBOL + OFFSET> after the number. */
274
275 void
276 print_address (addr, stream)
277 CORE_ADDR addr;
278 FILE *stream;
279 {
280 register int i;
281
282 fprintf (stream, "0x%x", addr);
283
284 i = find_pc_misc_function (addr);
285 if (i >= 0)
286 if (misc_function_vector[i].address != addr)
287 fprintf (stream, " <%s+%d>",
288 misc_function_vector[i].name,
289 addr - misc_function_vector[i].address);
290 else
291 fprintf (stream, " <%s>", misc_function_vector[i].name);
292
293 }
294 \f
295 /* Examine data at address ADDR in format FMT.
296 Fetch it from memory and print on stdout. */
297
298 static void
299 do_examine (fmt, addr)
300 struct format_data fmt;
301 CORE_ADDR addr;
302 {
303 register char format = 0;
304 register char size;
305 register int count = 1;
306 struct type *val_type;
307 register int i;
308 register int maxelts;
309
310 format = fmt.format;
311 size = fmt.size;
312 count = fmt.count;
313 next_address = addr;
314
315 /* String or instruction format implies fetch single bytes
316 regardless of the specified size. */
317 if (format == 's' || format == 'i')
318 size = 'b';
319
320 if (size == 'b')
321 val_type = builtin_type_char;
322 else if (size == 'h')
323 val_type = builtin_type_short;
324 else if (size == 'w')
325 val_type = builtin_type_long;
326 else if (size == 'g')
327 val_type = builtin_type_double;
328
329 maxelts = 8;
330 if (size == 'w')
331 maxelts = 4;
332 if (size == 'g')
333 maxelts = 2;
334 if (format == 's' || format == 'i')
335 maxelts = 1;
336
337 /* Print as many objects as specified in COUNT, at most maxelts per line,
338 with the address of the next one at the start of each line. */
339
340 while (count > 0)
341 {
342 print_address (next_address, stdout);
343 fputc (':', stdout);
344 for (i = maxelts;
345 i > 0 && count > 0;
346 i--, count--)
347 {
348 fputc ('\t', stdout);
349 /* Note that this sets next_address for the next object. */
350 last_examine_address = next_address;
351 last_examine_value = value_at (val_type, next_address);
352 print_formatted (last_examine_value, format, size);
353 }
354 fputc ('\n', stdout);
355 fflush (stdout);
356 }
357 }
358 \f
359 static void
360 validate_format (fmt, cmdname)
361 struct format_data fmt;
362 char *cmdname;
363 {
364 if (fmt.size != 0)
365 error ("Size letters are meaningless in \"%s\" command.", cmdname);
366 if (fmt.count != 1)
367 error ("Item count other than 1 is meaningless in \"%s\" command.",
368 cmdname);
369 if (fmt.format == 'i' || fmt.format == 's')
370 error ("Format letter \"%c\" is meaningless in \"%s\" command.",
371 fmt.format, cmdname);
372 }
373
374 static void
375 print_command (exp)
376 char *exp;
377 {
378 struct expression *expr;
379 register struct cleanup *old_chain = 0;
380 register char format = 0;
381 register value val;
382 struct format_data fmt;
383 int histindex;
384 int cleanup = 0;
385
386 if (exp && *exp == '/')
387 {
388 exp++;
389 fmt = decode_format (&exp, last_format, 0);
390 validate_format (fmt, "print");
391 last_format = format = fmt.format;
392 }
393
394 if (exp && *exp)
395 {
396 expr = parse_c_expression (exp);
397 old_chain = make_cleanup (free_current_contents, &expr);
398 cleanup = 1;
399 val = evaluate_expression (expr);
400 }
401 else
402 val = access_value_history (0);
403
404 histindex = record_latest_value (val);
405 printf ("$%d = ", histindex);
406
407 print_formatted (val, format, fmt.size);
408 printf ("\n");
409
410 if (cleanup)
411 do_cleanups (old_chain);
412 }
413
414 static void
415 output_command (exp)
416 char *exp;
417 {
418 struct expression *expr;
419 register struct cleanup *old_chain;
420 register char format = 0;
421 register value val;
422 struct format_data fmt;
423
424 if (exp && *exp == '/')
425 {
426 exp++;
427 fmt = decode_format (&exp, 0, 0);
428 validate_format (fmt, "print");
429 format = fmt.format;
430 }
431
432 expr = parse_c_expression (exp);
433 old_chain = make_cleanup (free_current_contents, &expr);
434
435 val = evaluate_expression (expr);
436
437 print_formatted (val, format, fmt.size);
438
439 do_cleanups (old_chain);
440 }
441
442 static void
443 set_command (exp)
444 char *exp;
445 {
446 struct expression *expr = parse_c_expression (exp);
447 register struct cleanup *old_chain
448 = make_cleanup (free_current_contents, &expr);
449 evaluate_expression (expr);
450 do_cleanups (old_chain);
451 }
452
453 static void
454 address_info (exp)
455 char *exp;
456 {
457 register struct symbol *sym;
458 register CORE_ADDR val;
459
460 if (exp == 0)
461 error ("Argument required.");
462
463 sym = lookup_symbol (exp, get_selected_block (), VAR_NAMESPACE);
464 if (sym == 0)
465 {
466 register int i;
467
468 for (i = 0; i < misc_function_count; i++)
469 if (!strcmp (misc_function_vector[i].name, exp))
470 break;
471
472 if (i < misc_function_count)
473 printf ("Symbol \"%s\" is at 0x%x in a file compiled without -g.\n",
474 exp, misc_function_vector[i].address);
475 else
476 error ("No symbol \"%s\" in current context.", exp);
477 return;
478 }
479
480 printf ("Symbol \"%s\" is ", SYMBOL_NAME (sym));
481 val = SYMBOL_VALUE (sym);
482
483 switch (SYMBOL_CLASS (sym))
484 {
485 case LOC_CONST:
486 case LOC_CONST_BYTES:
487 printf ("constant");
488 break;
489
490 case LOC_LABEL:
491 printf ("a label at address 0x%x", val);
492 break;
493
494 case LOC_REGISTER:
495 printf ("a variable in register %s", reg_names[val]);
496 break;
497
498 case LOC_STATIC:
499 printf ("static at address 0x%x", val);
500 break;
501
502 case LOC_REGPARM:
503 printf ("an argument in register %s", reg_names[val]);
504 break;
505
506 case LOC_ARG:
507 printf ("an argument at offset %d", val);
508 break;
509
510 case LOC_LOCAL:
511 printf ("a local variable at frame offset %d", val);
512 break;
513
514 case LOC_TYPEDEF:
515 printf ("a typedef");
516 break;
517
518 case LOC_BLOCK:
519 printf ("a function at address 0x%x",
520 BLOCK_START (SYMBOL_BLOCK_VALUE (sym)));
521 break;
522 }
523 printf (".\n");
524 }
525 \f
526 static void
527 x_command (exp, from_tty)
528 char *exp;
529 int from_tty;
530 {
531 struct expression *expr;
532 struct format_data fmt;
533 struct cleanup *old_chain;
534
535 fmt.format = last_format;
536 fmt.size = last_size;
537 fmt.count = 1;
538
539 if (exp && *exp == '/')
540 {
541 exp++;
542 fmt = decode_format (&exp, last_format, last_size);
543 last_size = fmt.size;
544 last_format = fmt.format;
545 }
546
547 /* If we have an expression, evaluate it and use it as the address. */
548
549 if (exp != 0 && *exp != 0)
550 {
551 expr = parse_c_expression (exp);
552 /* Cause expression not to be there any more
553 if this command is repeated with Newline.
554 But don't clobber a user-defined command's definition. */
555 if (from_tty)
556 *exp = 0;
557 old_chain = make_cleanup (free_current_contents, &expr);
558 next_address = value_as_long (evaluate_expression (expr));
559 do_cleanups (old_chain);
560 }
561
562 do_examine (fmt, next_address);
563
564 /* Make last address examined available to the user as $_. */
565 set_internalvar (lookup_internalvar ("_"),
566 value_from_long (builtin_type_int, last_examine_address));
567
568 /* Make contents of last address examined available to the user as $__. */
569 set_internalvar (lookup_internalvar ("__"), last_examine_value);
570 }
571 \f
572 /* Commands for printing types of things. */
573
574 static void
575 whatis_command (exp)
576 char *exp;
577 {
578 struct expression *expr;
579 register value val;
580 register struct cleanup *old_chain;
581
582 if (exp)
583 {
584 expr = parse_c_expression (exp);
585 old_chain = make_cleanup (free_current_contents, &expr);
586 val = evaluate_type (expr);
587 }
588 else
589 val = access_value_history (0);
590
591 printf ("type = ");
592 type_print (VALUE_TYPE (val), "", stdout, 1);
593 printf ("\n");
594
595 if (exp)
596 do_cleanups (old_chain);
597 }
598
599 static void
600 ptype_command (typename)
601 char *typename;
602 {
603 register char *p = typename;
604 register int len;
605 extern struct block *get_current_block ();
606 register struct block *b
607 = (have_inferior_p () || have_core_file_p ()) ? get_current_block () : 0;
608 register struct type *type;
609
610 if (typename == 0)
611 error_no_arg ("type name");
612
613 while (*p && *p != ' ' && *p != '\t') p++;
614 len = p - typename;
615 while (*p == ' ' || *p == '\t') p++;
616
617 if (len == 6 && !strncmp (typename, "struct", 6))
618 type = lookup_struct (p, b);
619 else if (len == 5 && !strncmp (typename, "union", 5))
620 type = lookup_union (p, b);
621 else if (len == 4 && !strncmp (typename, "enum", 4))
622 type = lookup_enum (p, b);
623 else
624 {
625 type = lookup_typename (typename, b, 1);
626 if (type == 0)
627 {
628 register struct symbol *sym
629 = lookup_symbol (typename, b, STRUCT_NAMESPACE);
630 if (sym == 0)
631 error ("No type named %s.", typename);
632 printf ("No type named %s, but there is a ",
633 typename);
634 switch (TYPE_CODE (SYMBOL_TYPE (sym)))
635 {
636 case TYPE_CODE_STRUCT:
637 printf ("struct");
638 break;
639
640 case TYPE_CODE_UNION:
641 printf ("union");
642 break;
643
644 case TYPE_CODE_ENUM:
645 printf ("enum");
646 }
647 printf (" %s. Type \"help ptype\".\n", typename);
648 type = SYMBOL_TYPE (sym);
649 }
650 }
651
652 type_print (type, "", stdout, 1);
653 printf ("\n");
654 }
655 \f
656 struct display
657 {
658 /* Chain link to next auto-display item. */
659 struct display *next;
660 /* Expression to be evaluated and displayed. */
661 struct expression *exp;
662 /* Item number of this auto-display item. */
663 int number;
664 /* Display format specified. */
665 struct format_data format;
666 /* Block in which expression is to be evaluated. */
667 struct block *block;
668 };
669
670 /* Chain of expressions whose values should be displayed
671 automatically each time the program stops. */
672
673 static struct display *display_chain;
674
675 static int display_number;
676
677 /* Add an expression to the auto-display chain.
678 Specify the expression. */
679
680 static void
681 display_command (exp, from_tty)
682 char *exp;
683 int from_tty;
684 {
685 struct format_data fmt;
686 register struct expression *expr;
687 register struct display *new;
688
689 if (exp == 0)
690 {
691 do_displays ();
692 return;
693 }
694
695 if (*exp == '/')
696 {
697 exp++;
698 fmt = decode_format (&exp, 0, 0);
699 if (fmt.size && fmt.format == 0)
700 fmt.format = 'x';
701 if (fmt.format == 'i' || fmt.format == 's')
702 fmt.size = 'b';
703 }
704 else
705 {
706 fmt.format = 0;
707 fmt.size = 0;
708 fmt.count = 0;
709 }
710
711 expr = parse_c_expression (exp);
712
713 new = (struct display *) xmalloc (sizeof (struct display));
714
715 new->exp = expr;
716 new->next = display_chain;
717 new->number = ++display_number;
718 new->format = fmt;
719 display_chain = new;
720
721 if (from_tty)
722 do_one_display (new);
723
724 dont_repeat ();
725 }
726
727 static void
728 free_display (d)
729 struct display *d;
730 {
731 free (d->exp);
732 free (d);
733 }
734
735 /* Clear out the display_chain.
736 Done when new symtabs are loaded, since this invalidates
737 the types stored in many expressions. */
738
739 void
740 clear_displays ()
741 {
742 register struct display *d;
743
744 while (d = display_chain)
745 {
746 free (d->exp);
747 display_chain = d->next;
748 free (d);
749 }
750 }
751
752 /* Delete the auto-display number NUM. */
753
754 void
755 delete_display (num)
756 int num;
757 {
758 register struct display *d1, *d;
759
760 if (!display_chain)
761 error ("No display number %d.", num);
762
763 if (display_chain->number == num)
764 {
765 d1 = display_chain;
766 display_chain = d1->next;
767 free_display (d1);
768 }
769 else
770 for (d = display_chain; ; d = d->next)
771 {
772 if (d->next == 0)
773 error ("No display number %d.", num);
774 if (d->next->number == num)
775 {
776 d1 = d->next;
777 d->next = d1->next;
778 free_display (d1);
779 break;
780 }
781 }
782 }
783
784 /* Delete some values from the auto-display chain.
785 Specify the element numbers. */
786
787 static void
788 undisplay_command (args)
789 char *args;
790 {
791 register char *p = args;
792 register char *p1;
793 register int num;
794 register struct display *d, *d1;
795
796 if (args == 0)
797 {
798 if (query ("Delete all auto-display expressions? "))
799 clear_displays ();
800 dont_repeat ();
801 return;
802 }
803
804 while (*p)
805 {
806 p1 = p;
807 while (*p1 >= '0' && *p1 <= '9') p1++;
808 if (*p1 && *p1 != ' ' && *p1 != '\t')
809 error ("Arguments must be display numbers.");
810
811 num = atoi (p);
812
813 delete_display (num);
814
815 p = p1;
816 while (*p == ' ' || *p == '\t') p++;
817 }
818 dont_repeat ();
819 }
820
821 /* Display a single auto-display. */
822
823 static void
824 do_one_display (d)
825 struct display *d;
826 {
827 current_display_number = d->number;
828
829 printf ("%d: ", d->number);
830 if (d->format.size)
831 {
832 printf ("x/");
833 if (d->format.count != 1)
834 printf ("%d", d->format.count);
835 printf ("%c", d->format.format);
836 if (d->format.format != 'i' && d->format.format != 's')
837 printf ("%c", d->format.size);
838 printf (" ");
839 print_expression (d->exp, stdout);
840 if (d->format.count != 1)
841 printf ("\n");
842 else
843 printf (" ");
844 do_examine (d->format,
845 value_as_long (evaluate_expression (d->exp)));
846 }
847 else
848 {
849 if (d->format.format)
850 printf ("/%c ", d->format.format);
851 print_expression (d->exp, stdout);
852 printf (" = ");
853 print_formatted (evaluate_expression (d->exp),
854 d->format.format, d->format.size);
855 printf ("\n");
856 }
857
858 fflush (stdout);
859 current_display_number = -1;
860 }
861
862 /* Display all of the values on the auto-display chain. */
863
864 void
865 do_displays ()
866 {
867 register struct display *d;
868
869 for (d = display_chain; d; d = d->next)
870 do_one_display (d);
871 }
872
873 /* Delete the auto-display which we were in the process of displaying.
874 This is done when there is an error or a signal. */
875
876 void
877 delete_current_display ()
878 {
879 if (current_display_number >= 0)
880 {
881 delete_display (current_display_number);
882 fprintf (stderr, "Deleting display %d to avoid infinite recursion.\n",
883 current_display_number);
884 }
885 current_display_number = -1;
886 }
887
888 static void
889 display_info ()
890 {
891 register struct display *d;
892
893 if (!display_chain)
894 printf ("There are no auto-display expressions now.\n");
895 else
896 printf ("Auto-display expressions now in effect:\n");
897 for (d = display_chain; d; d = d->next)
898 {
899 printf ("%d: ", d->number);
900 if (d->format.size)
901 printf ("/%d%c%c ", d->format.count, d->format.size,
902 d->format.format);
903 else if (d->format.format)
904 printf ("/%c ", d->format.format);
905 print_expression (d->exp, stdout);
906 printf ("\n");
907 fflush (stdout);
908 }
909 }
910 \f
911 /* Print the value in stack frame FRAME of a variable
912 specified by a struct symbol. */
913
914 void
915 print_variable_value (var, frame, stream)
916 struct symbol *var;
917 CORE_ADDR frame;
918 FILE *stream;
919 {
920 value val = read_var_value (var, frame);
921 value_print (val, stream, 0);
922 }
923
924 /* Print the arguments of a stack frame, given the function FUNC
925 running in that frame (as a symbol), the address of the arglist,
926 and the number of args according to the stack frame (or -1 if unknown). */
927
928 static void print_frame_nameless_args ();
929
930 print_frame_args (func, addr, num, stream)
931 struct symbol *func;
932 register CORE_ADDR addr;
933 int num;
934 FILE *stream;
935 {
936 struct block *b;
937 int nsyms = 0;
938 int first = 1;
939 register int i;
940 register int last_offset = FRAME_ARGS_SKIP;
941 register int last_regparm = 0;
942 register struct symbol *sym, *nextsym;
943 register value val;
944
945 if (func)
946 {
947 b = SYMBOL_BLOCK_VALUE (func);
948 nsyms = BLOCK_NSYMS (b);
949 }
950
951 while (1)
952 {
953 /* Find first arg that is not before LAST_OFFSET. */
954 nextsym = 0;
955 for (i = 0; i < nsyms; i++)
956 {
957 QUIT;
958 sym = BLOCK_SYM (b, i);
959 if (SYMBOL_CLASS (sym) == LOC_ARG)
960 {
961 if (SYMBOL_VALUE (sym) >= last_offset
962 && (nextsym == 0
963 || SYMBOL_VALUE (sym) < SYMBOL_VALUE (nextsym)))
964 nextsym = sym;
965 }
966 else if (SYMBOL_CLASS (sym) == LOC_REGPARM)
967 {
968 if (SYMBOL_VALUE (sym) >= last_regparm
969 && (nextsym == 0
970 || SYMBOL_VALUE (sym) < SYMBOL_VALUE (nextsym)))
971 nextsym = sym;
972 }
973 }
974 if (nextsym == 0)
975 break;
976 sym = nextsym;
977 /* Print any nameless args between the last arg printed
978 and the next arg. */
979 if (SYMBOL_CLASS (sym) == LOC_ARG
980 && last_offset != (SYMBOL_VALUE (sym) / sizeof (int)) * sizeof (int))
981 {
982 print_frame_nameless_args (addr, last_offset, SYMBOL_VALUE (sym),
983 stream);
984 first = 0;
985 }
986 /* Print the next arg. */
987 if (SYMBOL_CLASS (sym) == LOC_REGPARM)
988 {
989 unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
990 unsigned char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
991
992 read_relative_register_raw_bytes (SYMBOL_VALUE (sym), raw_buffer);
993 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FLT)
994 val = value_from_double (SYMBOL_TYPE (sym), *(double *)raw_buffer);
995 else if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
996 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM)
997 val = value_from_long (SYMBOL_TYPE (sym), *(int *)raw_buffer);
998 else if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR)
999 {
1000 if (sizeof (char *) == sizeof (int))
1001 val = value_from_long (builtin_type_int, *(int *)raw_buffer);
1002 else if (sizeof (char *) == sizeof (long))
1003 val = value_from_long (builtin_type_long, *(long *)raw_buffer);
1004 else
1005 error ("pointer size not sizeof (int) or sizeof (long)");
1006 VALUE_TYPE (val) = SYMBOL_TYPE (sym);
1007 }
1008 else
1009 error ("can't extract non-scalar from register");
1010 }
1011 else
1012 val = value_at (SYMBOL_TYPE (sym), addr + SYMBOL_VALUE (sym));
1013
1014 if (! first)
1015 fprintf (stream, ", ");
1016 fprintf (stream, "%s=", SYMBOL_NAME (sym));
1017 value_print (val, stream, 0);
1018 first = 0;
1019 if (SYMBOL_CLASS (sym) == LOC_ARG)
1020 last_offset = SYMBOL_VALUE (sym) + TYPE_LENGTH (SYMBOL_TYPE (sym));
1021 else
1022 {
1023 last_regparm = SYMBOL_VALUE (sym) + 1;
1024 last_offset += TYPE_LENGTH (SYMBOL_TYPE (sym));
1025 }
1026 /* Round up address of next arg to multiple of size of int. */
1027 last_offset
1028 = ((last_offset + sizeof (int) - 1) / sizeof (int)) * sizeof (int);
1029 }
1030 if (num >= 0 && num * sizeof (int) + FRAME_ARGS_SKIP > last_offset)
1031 print_frame_nameless_args (addr, last_offset,
1032 num * sizeof (int) + FRAME_ARGS_SKIP, stream);
1033 }
1034
1035 static void
1036 print_frame_nameless_args (argsaddr, start, end, stream)
1037 CORE_ADDR argsaddr;
1038 int start;
1039 int end;
1040 FILE *stream;
1041 {
1042 while (start < end)
1043 {
1044 QUIT;
1045 if (start != FRAME_ARGS_SKIP)
1046 fprintf (stream, ", ");
1047 fprintf (stream, "%d",
1048 read_memory_integer (argsaddr + start, sizeof (int)));
1049 start += sizeof (int);
1050 }
1051 }
1052 \f
1053 static void
1054 printf_command (arg)
1055 char *arg;
1056 {
1057 register char *f;
1058 register char *s = arg;
1059 char *string;
1060 value *val_args;
1061 int nargs = 0;
1062 int allocated_args = 20;
1063 char *arg_bytes;
1064 char *argclass;
1065 int i;
1066 int argindex;
1067 int nargs_wanted;
1068
1069 val_args = (value *) xmalloc (allocated_args * sizeof (value));
1070
1071 if (s == 0)
1072 error_no_arg ("format-control string and values to print");
1073
1074 /* Skip white space before format string */
1075 while (*s == ' ' || *s == '\t') s++;
1076
1077 /* A format string should follow, enveloped in double quotes */
1078 if (*s++ != '"')
1079 error ("Bad format string, missing '\"'.");
1080
1081 /* Parse the format-control string and copy it into the string STRING,
1082 processing some kinds of escape sequence. */
1083
1084 f = string = (char *) alloca (strlen (s) + 1);
1085 while (*s != '"')
1086 {
1087 int c = *s++;
1088 switch (c)
1089 {
1090 case '\0':
1091 error ("Bad format string, non-terminated '\"'.");
1092 /* doesn't return */
1093
1094 case '\\':
1095 switch (c = *s++)
1096 {
1097 case '\\':
1098 *f++ = '\\';
1099 break;
1100 case 'n':
1101 *f++ = '\n';
1102 break;
1103 case 't':
1104 *f++ = '\t';
1105 break;
1106 case 'r':
1107 *f++ = '\r';
1108 break;
1109 case '"':
1110 *f++ = '"';
1111 break;
1112 default:
1113 /* ??? TODO: handle other escape sequences */
1114 error ("Unrecognized \\ escape character in format string.");
1115 }
1116 break;
1117
1118 default:
1119 *f++ = c;
1120 }
1121 }
1122
1123 /* Skip over " and following space and comma. */
1124 s++;
1125 *f++ = '\0';
1126 while (*s == ' ' || *s == '\t') s++;
1127
1128 if (*s != ',' && *s != 0)
1129 error ("Invalid argument syntax");
1130
1131 if (*s == ',') s++;
1132 while (*s == ' ' || *s == '\t') s++;
1133
1134 /* Now scan the string for %-specs and see what kinds of args they want.
1135 argclass[I] is set to 1 if the Ith arg should be a string. */
1136
1137 argclass = (char *) alloca (strlen (s));
1138 nargs_wanted = 0;
1139 f = string;
1140 while (*f)
1141 if (*f++ == '%')
1142 {
1143 while (index ("0123456789.hlL-+ #", *f)) f++;
1144 if (*f == 's')
1145 argclass[nargs_wanted++] = 1;
1146 else if (*f != '%')
1147 argclass[nargs_wanted++] = 0;
1148 f++;
1149 }
1150
1151 /* Now, parse all arguments and evaluate them.
1152 Store the VALUEs in VAL_ARGS. */
1153
1154 while (*s != '\0')
1155 {
1156 char *s1;
1157 if (nargs == allocated_args)
1158 val_args = (value *) xrealloc (val_args,
1159 (allocated_args *= 2)
1160 * sizeof (value));
1161 s1 = s;
1162 val_args[nargs++] = parse_to_comma_and_eval (&s1);
1163 s = s1;
1164 if (*s == ',')
1165 s++;
1166 }
1167
1168 if (nargs != nargs_wanted)
1169 error ("Wrong number of arguments for specified format-string");
1170
1171 /* Now lay out an argument-list containing the arguments
1172 as doubles, integers and C pointers. */
1173
1174 arg_bytes = (char *) alloca (sizeof (double) * nargs);
1175 argindex = 0;
1176 for (i = 0; i < nargs; i++)
1177 {
1178 if (argclass[i])
1179 {
1180 char *str;
1181 int tem, j;
1182 tem = value_as_long (val_args[i]);
1183
1184 /* This is a %s argument. Find the length of the string. */
1185 for (j = 0; ; j++)
1186 {
1187 char c;
1188 QUIT;
1189 read_memory (tem + j, &c, 1);
1190 if (c == 0)
1191 break;
1192 }
1193
1194 /* Copy the string contents into a string inside GDB. */
1195 str = (char *) alloca (j + 1);
1196 read_memory (tem, str, j);
1197 str[j] = 0;
1198
1199 /* Pass address of internal copy as the arg to vprintf. */
1200 *((int *) &arg_bytes[argindex]) = (int) str;
1201 argindex += sizeof (int);
1202 }
1203 else if (VALUE_TYPE (val_args[i])->code == TYPE_CODE_FLT)
1204 {
1205 *((double *) &arg_bytes[argindex]) = value_as_double (val_args[i]);
1206 argindex += sizeof (double);
1207 }
1208 else
1209 {
1210 *((int *) &arg_bytes[argindex]) = value_as_long (val_args[i]);
1211 argindex += sizeof (int);
1212 }
1213 }
1214
1215 vprintf (string, arg_bytes);
1216 }
1217 \f
1218 static
1219 initialize ()
1220 {
1221 current_display_number = -1;
1222
1223 add_info ("address", address_info,
1224 "Describe where variable VAR is stored.");
1225
1226 add_com ("x", class_vars, x_command,
1227 "Examine memory: x/FMT ADDRESS.\n\
1228 ADDRESS is an expression for the memory address to examine.\n\
1229 FMT is a repeat count followed by a format letter and a size letter.\n\
1230 Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),\n\
1231 f(float), a(address), i(instruction), c(char) and s(string).\n\
1232 Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).\n\
1233 g is meaningful only with f, for type double.\n\
1234 The specified number of objects of the specified size are printed\n\
1235 according to the format.\n\n\
1236 Defaults for format and size letters are those previously used.\n\
1237 Default count is 1. Default address is following last thing printed\n\
1238 with this command or \"print\".");
1239
1240 add_com ("ptype", class_vars, ptype_command,
1241 "Print definition of type TYPE.\n\
1242 Argument may be a type name defined by typedef, or \"struct STRUCTNAME\"\n\
1243 or \"union UNIONNAME\" or \"enum ENUMNAME\".\n\
1244 The selected stack frame's lexical context is used to look up the name.");
1245
1246 add_com ("whatis", class_vars, whatis_command,
1247 "Print data type of expression EXP.");
1248
1249 add_info ("display", display_info,
1250 "Expressions to display when program stops, with code numbers.");
1251 add_com ("undisplay", class_vars, undisplay_command,
1252 "Cancel some expressions to be displayed whenever program stops.\n\
1253 Arguments are the code numbers of the expressions to stop displaying.\n\
1254 No argument means cancel all automatic-display expressions.\n\
1255 Do \"info display\" to see current list of code numbers.");
1256 add_com ("display", class_vars, display_command,
1257 "Print value of expression EXP each time the program stops.\n\
1258 /FMT may be used before EXP as in the \"print\" command.\n\
1259 /FMT \"i\" or \"s\" or including a size-letter is allowed,\n\
1260 as in the \"x\" command, and then EXP is used to get the address to examine\n\
1261 and examining is done as in the \"x\" command.\n\n\
1262 With no argument, display all currently requested auto-display expressions.\n\
1263 Use \"undisplay\" to cancel display requests previously made.");
1264
1265 add_com ("printf", class_vars, printf_command,
1266 "printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
1267 This is useful for formatted output in user-defined commands.");
1268 add_com ("output", class_vars, output_command,
1269 "Like \"print\" but don't put in value history and don't print newline.\n\
1270 This is useful in user-defined commands.");
1271
1272 add_com ("set", class_vars, set_command,
1273 "Perform an assignment VAR = EXP. You must type the \"=\".\n\
1274 VAR may be a debugger \"convenience\" variables (names starting with $),\n\
1275 a register (a few standard names starting with $), or an actual variable\n\
1276 in the program being debugger. EXP is any expression.");
1277
1278 add_com ("print", class_vars, print_command,
1279 concat ("Print value of expression EXP.\n\
1280 Variables accessible are those of the lexical environment of the selected\n\
1281 stack frame, plus all those whose scope is global or an entire file.\n\
1282 \n\
1283 $NUM gets previous value number NUM. $ and $$ are the last two values.\n\
1284 $$NUM refers to NUM'th value back from the last one.\n\
1285 Names starting with $ refer to registers (with the values they would have\n\
1286 if the program were to return to the stack frame now selected, restoring\n\
1287 all registers saved by frames farther in) or else to debugger\n\
1288 \"convenience\" variables (any such name not a known register).\n\
1289 Use assignment expressions to give values to convenience variables.\n",
1290 "\n\
1291 \{TYPE}ADREXP refers to a datum of data type TYPE, located at address ADREXP.\n\
1292 @ is a binary operator for treating consecutive data objects\n\
1293 anywhere in memory as an array. FOO@NUM gives an array whose first\n\
1294 element is FOO, whose second element is stored in the space following\n\
1295 where FOO is stored, etc. FOO must be an expression whose value\n\
1296 resides in memory.\n",
1297 "\n\
1298 EXP may be preceded with /FMT, where FMT is a format letter\n\
1299 but no count or size letter (see \"x\" command)."));
1300 add_com_alias ("p", "print", class_vars, 1);
1301 }
1302
1303 END_FILE
This page took 0.058752 seconds and 5 git commands to generate.