* stack.c (print_frame_arguments_choices): New static global.
[binutils-gdb.git] / gdb / stack.c
1 /* Print and select stack frames for GDB, the GNU debugger.
2
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
5 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
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 3 of the License, or
12 (at your option) any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "value.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "language.h"
28 #include "frame.h"
29 #include "gdbcmd.h"
30 #include "gdbcore.h"
31 #include "target.h"
32 #include "source.h"
33 #include "breakpoint.h"
34 #include "demangle.h"
35 #include "inferior.h"
36 #include "annotate.h"
37 #include "ui-out.h"
38 #include "block.h"
39 #include "stack.h"
40 #include "dictionary.h"
41 #include "exceptions.h"
42 #include "reggroups.h"
43 #include "regcache.h"
44 #include "solib.h"
45 #include "valprint.h"
46
47 #include "gdb_assert.h"
48 #include <ctype.h>
49 #include "gdb_string.h"
50
51 void (*deprecated_selected_frame_level_changed_hook) (int);
52
53 /* The possible choices of "set print frame-arguments, and the value
54 of this setting. */
55
56 static const char *print_frame_arguments_choices[] =
57 {"all", "scalars", "none", NULL};
58 static const char *print_frame_arguments = "all";
59
60 /* Prototypes for local functions. */
61
62 static void print_frame_local_vars (struct frame_info *, int,
63 struct ui_file *);
64
65 static void print_frame (struct frame_info *frame, int print_level,
66 enum print_what print_what, int print_args,
67 struct symtab_and_line sal);
68
69 /* Zero means do things normally; we are interacting directly with the
70 user. One means print the full filename and linenumber when a
71 frame is printed, and do so in a format emacs18/emacs19.22 can
72 parse. Two means print similar annotations, but in many more
73 cases and in a slightly different syntax. */
74
75 int annotation_level = 0;
76 \f
77
78 struct print_stack_frame_args
79 {
80 struct frame_info *frame;
81 int print_level;
82 enum print_what print_what;
83 int print_args;
84 };
85
86 /* Show or print the frame arguments; stub for catch_errors. */
87
88 static int
89 print_stack_frame_stub (void *args)
90 {
91 struct print_stack_frame_args *p = args;
92 int center = (p->print_what == SRC_LINE || p->print_what == SRC_AND_LOC);
93
94 print_frame_info (p->frame, p->print_level, p->print_what, p->print_args);
95 set_current_sal_from_frame (p->frame, center);
96 return 0;
97 }
98
99 /* Show or print a stack frame FRAME briefly. The output is format
100 according to PRINT_LEVEL and PRINT_WHAT printing the frame's
101 relative level, function name, argument list, and file name and
102 line number. If the frame's PC is not at the beginning of the
103 source line, the actual PC is printed at the beginning. */
104
105 void
106 print_stack_frame (struct frame_info *frame, int print_level,
107 enum print_what print_what)
108 {
109 struct print_stack_frame_args args;
110
111 args.frame = frame;
112 args.print_level = print_level;
113 args.print_what = print_what;
114 /* For mi, alway print location and address. */
115 args.print_what = ui_out_is_mi_like_p (uiout) ? LOC_AND_ADDRESS : print_what;
116 args.print_args = 1;
117
118 catch_errors (print_stack_frame_stub, &args, "", RETURN_MASK_ALL);
119 }
120
121 struct print_args_args
122 {
123 struct symbol *func;
124 struct frame_info *frame;
125 struct ui_file *stream;
126 };
127
128 static int print_args_stub (void *args);
129
130 /* Print nameless arguments of frame FRAME on STREAM, where START is
131 the offset of the first nameless argument, and NUM is the number of
132 nameless arguments to print. FIRST is nonzero if this is the first
133 argument (not just the first nameless argument). */
134
135 static void
136 print_frame_nameless_args (struct frame_info *frame, long start, int num,
137 int first, struct ui_file *stream)
138 {
139 int i;
140 CORE_ADDR argsaddr;
141 long arg_value;
142
143 for (i = 0; i < num; i++)
144 {
145 QUIT;
146 argsaddr = get_frame_args_address (frame);
147 if (!argsaddr)
148 return;
149 arg_value = read_memory_integer (argsaddr + start, sizeof (int));
150 if (!first)
151 fprintf_filtered (stream, ", ");
152 fprintf_filtered (stream, "%ld", arg_value);
153 first = 0;
154 start += sizeof (int);
155 }
156 }
157
158 /* Return non-zero if the debugger should print the value of the provided
159 symbol parameter (SYM). */
160
161 static int
162 print_this_frame_argument_p (struct symbol *sym)
163 {
164 struct type *type;
165
166 /* If the user asked to print no argument at all, then obviously
167 do not print this argument. */
168
169 if (strcmp (print_frame_arguments, "none") == 0)
170 return 0;
171
172 /* If the user asked to print all arguments, then we should print
173 that one. */
174
175 if (strcmp (print_frame_arguments, "all") == 0)
176 return 1;
177
178 /* The user asked to print only the scalar arguments, so do not
179 print the non-scalar ones. */
180
181 type = CHECK_TYPEDEF (SYMBOL_TYPE (sym));
182 switch (TYPE_CODE (type))
183 {
184 case TYPE_CODE_ARRAY:
185 case TYPE_CODE_STRUCT:
186 case TYPE_CODE_UNION:
187 case TYPE_CODE_SET:
188 case TYPE_CODE_STRING:
189 case TYPE_CODE_BITSTRING:
190 return 0;
191 default:
192 return 1;
193 }
194 }
195
196 /* Print the arguments of frame FRAME on STREAM, given the function
197 FUNC running in that frame (as a symbol), where NUM is the number
198 of arguments according to the stack frame (or -1 if the number of
199 arguments is unknown). */
200
201 /* Note that currently the "number of argumentss according to the
202 stack frame" is only known on VAX where i refers to the "number of
203 ints of argumentss according to the stack frame". */
204
205 static void
206 print_frame_args (struct symbol *func, struct frame_info *frame,
207 int num, struct ui_file *stream)
208 {
209 int first = 1;
210 /* Offset of next stack argument beyond the one we have seen that is
211 at the highest offset, or -1 if we haven't come to a stack
212 argument yet. */
213 long highest_offset = -1;
214 /* Number of ints of arguments that we have printed so far. */
215 int args_printed = 0;
216 struct cleanup *old_chain, *list_chain;
217 struct ui_stream *stb;
218
219 stb = ui_out_stream_new (uiout);
220 old_chain = make_cleanup_ui_out_stream_delete (stb);
221
222 if (func)
223 {
224 struct block *b = SYMBOL_BLOCK_VALUE (func);
225 struct dict_iterator iter;
226 struct symbol *sym;
227 struct value *val;
228
229 ALL_BLOCK_SYMBOLS (b, iter, sym)
230 {
231 QUIT;
232
233 /* Keep track of the highest stack argument offset seen, and
234 skip over any kinds of symbols we don't care about. */
235
236 switch (SYMBOL_CLASS (sym))
237 {
238 case LOC_ARG:
239 case LOC_REF_ARG:
240 {
241 long current_offset = SYMBOL_VALUE (sym);
242 int arg_size = TYPE_LENGTH (SYMBOL_TYPE (sym));
243
244 /* Compute address of next argument by adding the size of
245 this argument and rounding to an int boundary. */
246 current_offset =
247 ((current_offset + arg_size + sizeof (int) - 1)
248 & ~(sizeof (int) - 1));
249
250 /* If this is the highest offset seen yet, set
251 highest_offset. */
252 if (highest_offset == -1
253 || (current_offset > highest_offset))
254 highest_offset = current_offset;
255
256 /* Add the number of ints we're about to print to
257 args_printed. */
258 args_printed += (arg_size + sizeof (int) - 1) / sizeof (int);
259 }
260
261 /* We care about types of symbols, but don't need to
262 keep track of stack offsets in them. */
263 case LOC_REGPARM:
264 case LOC_REGPARM_ADDR:
265 case LOC_LOCAL_ARG:
266 case LOC_BASEREG_ARG:
267 case LOC_COMPUTED_ARG:
268 break;
269
270 /* Other types of symbols we just skip over. */
271 default:
272 continue;
273 }
274
275 /* We have to look up the symbol because arguments can have
276 two entries (one a parameter, one a local) and the one we
277 want is the local, which lookup_symbol will find for us.
278 This includes gcc1 (not gcc2) on SPARC when passing a
279 small structure and gcc2 when the argument type is float
280 and it is passed as a double and converted to float by
281 the prologue (in the latter case the type of the LOC_ARG
282 symbol is double and the type of the LOC_LOCAL symbol is
283 float). */
284 /* But if the parameter name is null, don't try it. Null
285 parameter names occur on the RS/6000, for traceback
286 tables. FIXME, should we even print them? */
287
288 if (*DEPRECATED_SYMBOL_NAME (sym))
289 {
290 struct symbol *nsym;
291 nsym = lookup_symbol (DEPRECATED_SYMBOL_NAME (sym),
292 b, VAR_DOMAIN, NULL, NULL);
293 gdb_assert (nsym != NULL);
294 if (SYMBOL_CLASS (nsym) == LOC_REGISTER)
295 {
296 /* There is a LOC_ARG/LOC_REGISTER pair. This means
297 that it was passed on the stack and loaded into a
298 register, or passed in a register and stored in a
299 stack slot. GDB 3.x used the LOC_ARG; GDB
300 4.0-4.11 used the LOC_REGISTER.
301
302 Reasons for using the LOC_ARG:
303
304 (1) Because find_saved_registers may be slow for
305 remote debugging.
306
307 (2) Because registers are often re-used and stack
308 slots rarely (never?) are. Therefore using
309 the stack slot is much less likely to print
310 garbage.
311
312 Reasons why we might want to use the LOC_REGISTER:
313
314 (1) So that the backtrace prints the same value
315 as "print foo". I see no compelling reason
316 why this needs to be the case; having the
317 backtrace print the value which was passed
318 in, and "print foo" print the value as
319 modified within the called function, makes
320 perfect sense to me.
321
322 Additional note: It might be nice if "info args"
323 displayed both values.
324
325 One more note: There is a case with SPARC
326 structure passing where we need to use the
327 LOC_REGISTER, but this is dealt with by creating
328 a single LOC_REGPARM in symbol reading. */
329
330 /* Leave sym (the LOC_ARG) alone. */
331 ;
332 }
333 else
334 sym = nsym;
335 }
336
337 /* Print the current arg. */
338 if (!first)
339 ui_out_text (uiout, ", ");
340 ui_out_wrap_hint (uiout, " ");
341
342 annotate_arg_begin ();
343
344 list_chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
345 fprintf_symbol_filtered (stb->stream, SYMBOL_PRINT_NAME (sym),
346 SYMBOL_LANGUAGE (sym),
347 DMGL_PARAMS | DMGL_ANSI);
348 ui_out_field_stream (uiout, "name", stb);
349 annotate_arg_name_end ();
350 ui_out_text (uiout, "=");
351
352 if (print_this_frame_argument_p (sym))
353 {
354 /* Avoid value_print because it will deref ref parameters.
355 We just want to print their addresses. Print ??? for
356 args whose address we do not know. We pass 2 as
357 "recurse" to val_print because our standard indentation
358 here is 4 spaces, and val_print indents 2 for each
359 recurse. */
360 val = read_var_value (sym, frame);
361
362 annotate_arg_value (val == NULL ? NULL : value_type (val));
363
364 if (val)
365 {
366 common_val_print (val, stb->stream, 0, 0, 2,
367 Val_no_prettyprint);
368 ui_out_field_stream (uiout, "value", stb);
369 }
370 else
371 ui_out_text (uiout, "???");
372 }
373 else
374 ui_out_text (uiout, "...");
375
376
377 /* Invoke ui_out_tuple_end. */
378 do_cleanups (list_chain);
379
380 annotate_arg_end ();
381
382 first = 0;
383 }
384 }
385
386 /* Don't print nameless args in situations where we don't know
387 enough about the stack to find them. */
388 if (num != -1)
389 {
390 long start;
391
392 if (highest_offset == -1)
393 start = gdbarch_frame_args_skip (get_frame_arch (frame));
394 else
395 start = highest_offset;
396
397 print_frame_nameless_args (frame, start, num - args_printed,
398 first, stream);
399 }
400
401 do_cleanups (old_chain);
402 }
403
404 /* Stub for catch_errors. */
405
406 static int
407 print_args_stub (void *args)
408 {
409 struct print_args_args *p = args;
410 int numargs;
411
412 if (gdbarch_frame_num_args_p (current_gdbarch))
413 {
414 numargs = gdbarch_frame_num_args (current_gdbarch, p->frame);
415 gdb_assert (numargs >= 0);
416 }
417 else
418 numargs = -1;
419 print_frame_args (p->func, p->frame, numargs, p->stream);
420 return 0;
421 }
422
423 /* Set the current source and line to the location given by frame
424 FRAME, if possible. When CENTER is true, adjust so the relevant
425 line is in the center of the next 'list'. */
426
427 void
428 set_current_sal_from_frame (struct frame_info *frame, int center)
429 {
430 struct symtab_and_line sal;
431
432 find_frame_sal (frame, &sal);
433 if (sal.symtab)
434 {
435 if (center)
436 sal.line = max (sal.line - get_lines_to_list () / 2, 1);
437 set_current_source_symtab_and_line (&sal);
438 }
439 }
440
441 /* Print information about frame FRAME. The output is format according
442 to PRINT_LEVEL and PRINT_WHAT and PRINT ARGS. The meaning of
443 PRINT_WHAT is:
444
445 SRC_LINE: Print only source line.
446 LOCATION: Print only location.
447 LOC_AND_SRC: Print location and source line.
448
449 Used in "where" output, and to emit breakpoint or step
450 messages. */
451
452 void
453 print_frame_info (struct frame_info *frame, int print_level,
454 enum print_what print_what, int print_args)
455 {
456 struct symtab_and_line sal;
457 int source_print;
458 int location_print;
459
460 if (get_frame_type (frame) == DUMMY_FRAME
461 || get_frame_type (frame) == SIGTRAMP_FRAME)
462 {
463 struct cleanup *uiout_cleanup
464 = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
465
466 annotate_frame_begin (print_level ? frame_relative_level (frame) : 0,
467 get_frame_pc (frame));
468
469 /* Do this regardless of SOURCE because we don't have any source
470 to list for this frame. */
471 if (print_level)
472 {
473 ui_out_text (uiout, "#");
474 ui_out_field_fmt_int (uiout, 2, ui_left, "level",
475 frame_relative_level (frame));
476 }
477 if (ui_out_is_mi_like_p (uiout))
478 {
479 annotate_frame_address ();
480 ui_out_field_core_addr (uiout, "addr", get_frame_pc (frame));
481 annotate_frame_address_end ();
482 }
483
484 if (get_frame_type (frame) == DUMMY_FRAME)
485 {
486 annotate_function_call ();
487 ui_out_field_string (uiout, "func", "<function called from gdb>");
488 }
489 else if (get_frame_type (frame) == SIGTRAMP_FRAME)
490 {
491 annotate_signal_handler_caller ();
492 ui_out_field_string (uiout, "func", "<signal handler called>");
493 }
494 ui_out_text (uiout, "\n");
495 annotate_frame_end ();
496
497 do_cleanups (uiout_cleanup);
498 return;
499 }
500
501 /* If FRAME is not the innermost frame, that normally means that
502 FRAME->pc points to *after* the call instruction, and we want to
503 get the line containing the call, never the next line. But if
504 the next frame is a SIGTRAMP_FRAME or a DUMMY_FRAME, then the
505 next frame was not entered as the result of a call, and we want
506 to get the line containing FRAME->pc. */
507 find_frame_sal (frame, &sal);
508
509 location_print = (print_what == LOCATION
510 || print_what == LOC_AND_ADDRESS
511 || print_what == SRC_AND_LOC);
512
513 if (location_print || !sal.symtab)
514 print_frame (frame, print_level, print_what, print_args, sal);
515
516 source_print = (print_what == SRC_LINE || print_what == SRC_AND_LOC);
517
518 if (source_print && sal.symtab)
519 {
520 int done = 0;
521 int mid_statement = ((print_what == SRC_LINE)
522 && (get_frame_pc (frame) != sal.pc));
523
524 if (annotation_level)
525 done = identify_source_line (sal.symtab, sal.line, mid_statement,
526 get_frame_pc (frame));
527 if (!done)
528 {
529 if (deprecated_print_frame_info_listing_hook)
530 deprecated_print_frame_info_listing_hook (sal.symtab,
531 sal.line,
532 sal.line + 1, 0);
533 else
534 {
535 /* We used to do this earlier, but that is clearly
536 wrong. This function is used by many different
537 parts of gdb, including normal_stop in infrun.c,
538 which uses this to print out the current PC
539 when we stepi/nexti into the middle of a source
540 line. Only the command line really wants this
541 behavior. Other UIs probably would like the
542 ability to decide for themselves if it is desired. */
543 if (addressprint && mid_statement)
544 {
545 ui_out_field_core_addr (uiout, "addr", get_frame_pc (frame));
546 ui_out_text (uiout, "\t");
547 }
548
549 print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
550 }
551 }
552 }
553
554 if (print_what != LOCATION)
555 set_default_breakpoint (1, get_frame_pc (frame), sal.symtab, sal.line);
556
557 annotate_frame_end ();
558
559 gdb_flush (gdb_stdout);
560 }
561
562 static void
563 print_frame (struct frame_info *frame, int print_level,
564 enum print_what print_what, int print_args,
565 struct symtab_and_line sal)
566 {
567 struct symbol *func;
568 char *funname = NULL;
569 enum language funlang = language_unknown;
570 struct ui_stream *stb;
571 struct cleanup *old_chain, *list_chain;
572
573 stb = ui_out_stream_new (uiout);
574 old_chain = make_cleanup_ui_out_stream_delete (stb);
575
576 func = find_pc_function (get_frame_address_in_block (frame));
577 if (func)
578 {
579 /* In certain pathological cases, the symtabs give the wrong
580 function (when we are in the first function in a file which
581 is compiled without debugging symbols, the previous function
582 is compiled with debugging symbols, and the "foo.o" symbol
583 that is supposed to tell us where the file with debugging
584 symbols ends has been truncated by ar because it is longer
585 than 15 characters). This also occurs if the user uses asm()
586 to create a function but not stabs for it (in a file compiled
587 with -g).
588
589 So look in the minimal symbol tables as well, and if it comes
590 up with a larger address for the function use that instead.
591 I don't think this can ever cause any problems; there
592 shouldn't be any minimal symbols in the middle of a function;
593 if this is ever changed many parts of GDB will need to be
594 changed (and we'll create a find_pc_minimal_function or some
595 such). */
596
597 struct minimal_symbol *msymbol =
598 lookup_minimal_symbol_by_pc (get_frame_address_in_block (frame));
599
600 if (msymbol != NULL
601 && (SYMBOL_VALUE_ADDRESS (msymbol)
602 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
603 {
604 /* We also don't know anything about the function besides
605 its address and name. */
606 func = 0;
607 funname = DEPRECATED_SYMBOL_NAME (msymbol);
608 funlang = SYMBOL_LANGUAGE (msymbol);
609 }
610 else
611 {
612 funname = DEPRECATED_SYMBOL_NAME (func);
613 funlang = SYMBOL_LANGUAGE (func);
614 if (funlang == language_cplus)
615 {
616 /* It seems appropriate to use SYMBOL_PRINT_NAME() here,
617 to display the demangled name that we already have
618 stored in the symbol table, but we stored a version
619 with DMGL_PARAMS turned on, and here we don't want to
620 display parameters. So call the demangler again, with
621 DMGL_ANSI only.
622
623 Yes, printf_symbol_filtered() will again try to
624 demangle the name on the fly, but the issue is that
625 if cplus_demangle() fails here, it will fail there
626 too. So we want to catch the failure (where DEMANGLED
627 is NULL below) here, while we still have our hands on
628 the function symbol.) */
629 char *demangled = cplus_demangle (funname, DMGL_ANSI);
630 if (demangled == NULL)
631 /* If the demangler fails, try the demangled name from
632 the symbol table. That'll have parameters, but
633 that's preferable to displaying a mangled name. */
634 funname = SYMBOL_PRINT_NAME (func);
635 else
636 xfree (demangled);
637 }
638 }
639 }
640 else
641 {
642 struct minimal_symbol *msymbol =
643 lookup_minimal_symbol_by_pc (get_frame_address_in_block (frame));
644
645 if (msymbol != NULL)
646 {
647 funname = DEPRECATED_SYMBOL_NAME (msymbol);
648 funlang = SYMBOL_LANGUAGE (msymbol);
649 }
650 }
651
652 annotate_frame_begin (print_level ? frame_relative_level (frame) : 0,
653 get_frame_pc (frame));
654
655 list_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
656
657 if (print_level)
658 {
659 ui_out_text (uiout, "#");
660 ui_out_field_fmt_int (uiout, 2, ui_left, "level",
661 frame_relative_level (frame));
662 }
663 if (addressprint)
664 if (get_frame_pc (frame) != sal.pc || !sal.symtab
665 || print_what == LOC_AND_ADDRESS)
666 {
667 annotate_frame_address ();
668 ui_out_field_core_addr (uiout, "addr", get_frame_pc (frame));
669 annotate_frame_address_end ();
670 ui_out_text (uiout, " in ");
671 }
672 annotate_frame_function_name ();
673 fprintf_symbol_filtered (stb->stream, funname ? funname : "??",
674 funlang, DMGL_ANSI);
675 ui_out_field_stream (uiout, "func", stb);
676 ui_out_wrap_hint (uiout, " ");
677 annotate_frame_args ();
678
679 ui_out_text (uiout, " (");
680 if (print_args)
681 {
682 struct print_args_args args;
683 struct cleanup *args_list_chain;
684 args.frame = frame;
685 args.func = func;
686 args.stream = gdb_stdout;
687 args_list_chain = make_cleanup_ui_out_list_begin_end (uiout, "args");
688 catch_errors (print_args_stub, &args, "", RETURN_MASK_ALL);
689 /* FIXME: ARGS must be a list. If one argument is a string it
690 will have " that will not be properly escaped. */
691 /* Invoke ui_out_tuple_end. */
692 do_cleanups (args_list_chain);
693 QUIT;
694 }
695 ui_out_text (uiout, ")");
696 if (sal.symtab && sal.symtab->filename)
697 {
698 annotate_frame_source_begin ();
699 ui_out_wrap_hint (uiout, " ");
700 ui_out_text (uiout, " at ");
701 annotate_frame_source_file ();
702 ui_out_field_string (uiout, "file", sal.symtab->filename);
703 if (ui_out_is_mi_like_p (uiout))
704 {
705 const char *fullname = symtab_to_fullname (sal.symtab);
706 if (fullname != NULL)
707 ui_out_field_string (uiout, "fullname", fullname);
708 }
709 annotate_frame_source_file_end ();
710 ui_out_text (uiout, ":");
711 annotate_frame_source_line ();
712 ui_out_field_int (uiout, "line", sal.line);
713 annotate_frame_source_end ();
714 }
715
716 if (!funname || (!sal.symtab || !sal.symtab->filename))
717 {
718 #ifdef PC_SOLIB
719 char *lib = PC_SOLIB (get_frame_pc (frame));
720 #else
721 char *lib = solib_address (get_frame_pc (frame));
722 #endif
723 if (lib)
724 {
725 annotate_frame_where ();
726 ui_out_wrap_hint (uiout, " ");
727 ui_out_text (uiout, " from ");
728 ui_out_field_string (uiout, "from", lib);
729 }
730 }
731
732 /* do_cleanups will call ui_out_tuple_end() for us. */
733 do_cleanups (list_chain);
734 ui_out_text (uiout, "\n");
735 do_cleanups (old_chain);
736 }
737 \f
738 /* Show the frame info. If this is the tui, it will be shown in the
739 source display otherwise, nothing is done. */
740
741 void
742 show_stack_frame (struct frame_info *frame)
743 {
744 }
745 \f
746
747 /* Read a frame specification in whatever the appropriate format is
748 from FRAME_EXP. Call error(), printing MESSAGE, if the
749 specification is in any way invalid (so this function never returns
750 NULL). When SEPECTED_P is non-NULL set its target to indicate that
751 the default selected frame was used. */
752
753 static struct frame_info *
754 parse_frame_specification_1 (const char *frame_exp, const char *message,
755 int *selected_frame_p)
756 {
757 int numargs;
758 struct value *args[4];
759 CORE_ADDR addrs[ARRAY_SIZE (args)];
760
761 if (frame_exp == NULL)
762 numargs = 0;
763 else
764 {
765 char *addr_string;
766 struct cleanup *tmp_cleanup;
767
768 numargs = 0;
769 while (1)
770 {
771 char *addr_string;
772 struct cleanup *cleanup;
773 const char *p;
774
775 /* Skip leading white space, bail of EOL. */
776 while (isspace (*frame_exp))
777 frame_exp++;
778 if (!*frame_exp)
779 break;
780
781 /* Parse the argument, extract it, save it. */
782 for (p = frame_exp;
783 *p && !isspace (*p);
784 p++);
785 addr_string = savestring (frame_exp, p - frame_exp);
786 frame_exp = p;
787 cleanup = make_cleanup (xfree, addr_string);
788
789 /* NOTE: Parse and evaluate expression, but do not use
790 functions such as parse_and_eval_long or
791 parse_and_eval_address to also extract the value.
792 Instead value_as_long and value_as_address are used.
793 This avoids problems with expressions that contain
794 side-effects. */
795 if (numargs >= ARRAY_SIZE (args))
796 error (_("Too many args in frame specification"));
797 args[numargs++] = parse_and_eval (addr_string);
798
799 do_cleanups (cleanup);
800 }
801 }
802
803 /* If no args, default to the selected frame. */
804 if (numargs == 0)
805 {
806 if (selected_frame_p != NULL)
807 (*selected_frame_p) = 1;
808 return get_selected_frame (message);
809 }
810
811 /* None of the remaining use the selected frame. */
812 if (selected_frame_p != NULL)
813 (*selected_frame_p) = 0;
814
815 /* Assume the single arg[0] is an integer, and try using that to
816 select a frame relative to current. */
817 if (numargs == 1)
818 {
819 struct frame_info *fid;
820 int level = value_as_long (args[0]);
821 fid = find_relative_frame (get_current_frame (), &level);
822 if (level == 0)
823 /* find_relative_frame was successful */
824 return fid;
825 }
826
827 /* Convert each value into a corresponding address. */
828 {
829 int i;
830 for (i = 0; i < numargs; i++)
831 addrs[i] = value_as_address (args[0]);
832 }
833
834 /* Assume that the single arg[0] is an address, use that to identify
835 a frame with a matching ID. Should this also accept stack/pc or
836 stack/pc/special. */
837 if (numargs == 1)
838 {
839 struct frame_id id = frame_id_build_wild (addrs[0]);
840 struct frame_info *fid;
841
842 /* If (s)he specifies the frame with an address, he deserves
843 what (s)he gets. Still, give the highest one that matches.
844 (NOTE: cagney/2004-10-29: Why highest, or outer-most, I don't
845 know). */
846 for (fid = get_current_frame ();
847 fid != NULL;
848 fid = get_prev_frame (fid))
849 {
850 if (frame_id_eq (id, get_frame_id (fid)))
851 {
852 while (frame_id_eq (id, frame_unwind_id (fid)))
853 fid = get_prev_frame (fid);
854 return fid;
855 }
856 }
857 }
858
859 /* We couldn't identify the frame as an existing frame, but
860 perhaps we can create one with a single argument. */
861 if (numargs == 1)
862 return create_new_frame (addrs[0], 0);
863 else if (numargs == 2)
864 return create_new_frame (addrs[0], addrs[1]);
865 else
866 error (_("Too many args in frame specification"));
867 }
868
869 static struct frame_info *
870 parse_frame_specification (char *frame_exp)
871 {
872 return parse_frame_specification_1 (frame_exp, NULL, NULL);
873 }
874
875 /* Print verbosely the selected frame or the frame at address
876 ADDR_EXP. Absolutely all information in the frame is printed. */
877
878 static void
879 frame_info (char *addr_exp, int from_tty)
880 {
881 struct frame_info *fi;
882 struct symtab_and_line sal;
883 struct symbol *func;
884 struct symtab *s;
885 struct frame_info *calling_frame_info;
886 int i, count, numregs;
887 char *funname = 0;
888 enum language funlang = language_unknown;
889 const char *pc_regname;
890 int selected_frame_p;
891 struct gdbarch *gdbarch;
892
893 fi = parse_frame_specification_1 (addr_exp, "No stack.", &selected_frame_p);
894
895 /* Name of the value returned by get_frame_pc(). Per comments, "pc"
896 is not a good name. */
897 if (gdbarch_pc_regnum (current_gdbarch) >= 0)
898 /* OK, this is weird. The gdbarch_pc_regnum hardware register's value can
899 easily not match that of the internal value returned by
900 get_frame_pc(). */
901 pc_regname = gdbarch_register_name (current_gdbarch,
902 gdbarch_pc_regnum (current_gdbarch));
903 else
904 /* But then, this is weird to. Even without gdbarch_pc_regnum, an
905 architectures will often have a hardware register called "pc",
906 and that register's value, again, can easily not match
907 get_frame_pc(). */
908 pc_regname = "pc";
909
910 find_frame_sal (fi, &sal);
911 gdbarch = get_frame_arch (fi);
912 func = get_frame_function (fi);
913 /* FIXME: cagney/2002-11-28: Why bother? Won't sal.symtab contain
914 the same value? */
915 s = find_pc_symtab (get_frame_pc (fi));
916 if (func)
917 {
918 /* It seems appropriate to use SYMBOL_PRINT_NAME() here, to
919 display the demangled name that we already have stored in the
920 symbol table, but we stored a version with DMGL_PARAMS turned
921 on, and here we don't want to display parameters. So call the
922 demangler again, with DMGL_ANSI only.
923
924 Yes, printf_symbol_filtered() will again try to demangle the
925 name on the fly, but the issue is that if cplus_demangle()
926 fails here, it will fail there too. So we want to catch the
927 failure (where DEMANGLED is NULL below) here, while we still
928 have our hands on the function symbol.) */
929 funname = DEPRECATED_SYMBOL_NAME (func);
930 funlang = SYMBOL_LANGUAGE (func);
931 if (funlang == language_cplus)
932 {
933 char *demangled = cplus_demangle (funname, DMGL_ANSI);
934 /* If the demangler fails, try the demangled name from the
935 symbol table. That'll have parameters, but that's
936 preferable to displaying a mangled name. */
937 if (demangled == NULL)
938 funname = SYMBOL_PRINT_NAME (func);
939 else
940 xfree (demangled);
941 }
942 }
943 else
944 {
945 struct minimal_symbol *msymbol;
946
947 msymbol = lookup_minimal_symbol_by_pc (get_frame_pc (fi));
948 if (msymbol != NULL)
949 {
950 funname = DEPRECATED_SYMBOL_NAME (msymbol);
951 funlang = SYMBOL_LANGUAGE (msymbol);
952 }
953 }
954 calling_frame_info = get_prev_frame (fi);
955
956 if (selected_frame_p && frame_relative_level (fi) >= 0)
957 {
958 printf_filtered (_("Stack level %d, frame at "),
959 frame_relative_level (fi));
960 }
961 else
962 {
963 printf_filtered (_("Stack frame at "));
964 }
965 deprecated_print_address_numeric (get_frame_base (fi), 1, gdb_stdout);
966 printf_filtered (":\n");
967 printf_filtered (" %s = ", pc_regname);
968 deprecated_print_address_numeric (get_frame_pc (fi), 1, gdb_stdout);
969
970 wrap_here (" ");
971 if (funname)
972 {
973 printf_filtered (" in ");
974 fprintf_symbol_filtered (gdb_stdout, funname, funlang,
975 DMGL_ANSI | DMGL_PARAMS);
976 }
977 wrap_here (" ");
978 if (sal.symtab)
979 printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
980 puts_filtered ("; ");
981 wrap_here (" ");
982 printf_filtered ("saved %s ", pc_regname);
983 deprecated_print_address_numeric (frame_pc_unwind (fi), 1, gdb_stdout);
984 printf_filtered ("\n");
985
986 if (calling_frame_info == NULL)
987 {
988 enum unwind_stop_reason reason;
989
990 reason = get_frame_unwind_stop_reason (fi);
991 if (reason != UNWIND_NO_REASON)
992 printf_filtered (_(" Outermost frame: %s\n"),
993 frame_stop_reason_string (reason));
994 }
995
996 if (calling_frame_info)
997 {
998 printf_filtered (" called by frame at ");
999 deprecated_print_address_numeric (get_frame_base (calling_frame_info),
1000 1, gdb_stdout);
1001 }
1002 if (get_next_frame (fi) && calling_frame_info)
1003 puts_filtered (",");
1004 wrap_here (" ");
1005 if (get_next_frame (fi))
1006 {
1007 printf_filtered (" caller of frame at ");
1008 deprecated_print_address_numeric (get_frame_base (get_next_frame (fi)), 1,
1009 gdb_stdout);
1010 }
1011 if (get_next_frame (fi) || calling_frame_info)
1012 puts_filtered ("\n");
1013
1014 if (s)
1015 printf_filtered (" source language %s.\n",
1016 language_str (s->language));
1017
1018 {
1019 /* Address of the argument list for this frame, or 0. */
1020 CORE_ADDR arg_list = get_frame_args_address (fi);
1021 /* Number of args for this frame, or -1 if unknown. */
1022 int numargs;
1023
1024 if (arg_list == 0)
1025 printf_filtered (" Arglist at unknown address.\n");
1026 else
1027 {
1028 printf_filtered (" Arglist at ");
1029 deprecated_print_address_numeric (arg_list, 1, gdb_stdout);
1030 printf_filtered (",");
1031
1032 if (!gdbarch_frame_num_args_p (gdbarch))
1033 {
1034 numargs = -1;
1035 puts_filtered (" args: ");
1036 }
1037 else
1038 {
1039 numargs = gdbarch_frame_num_args (gdbarch, fi);
1040 gdb_assert (numargs >= 0);
1041 if (numargs == 0)
1042 puts_filtered (" no args.");
1043 else if (numargs == 1)
1044 puts_filtered (" 1 arg: ");
1045 else
1046 printf_filtered (" %d args: ", numargs);
1047 }
1048 print_frame_args (func, fi, numargs, gdb_stdout);
1049 puts_filtered ("\n");
1050 }
1051 }
1052 {
1053 /* Address of the local variables for this frame, or 0. */
1054 CORE_ADDR arg_list = get_frame_locals_address (fi);
1055
1056 if (arg_list == 0)
1057 printf_filtered (" Locals at unknown address,");
1058 else
1059 {
1060 printf_filtered (" Locals at ");
1061 deprecated_print_address_numeric (arg_list, 1, gdb_stdout);
1062 printf_filtered (",");
1063 }
1064 }
1065
1066 /* Print as much information as possible on the location of all the
1067 registers. */
1068 {
1069 enum lval_type lval;
1070 int optimized;
1071 CORE_ADDR addr;
1072 int realnum;
1073 int count;
1074 int i;
1075 int need_nl = 1;
1076
1077 /* The sp is special; what's displayed isn't the save address, but
1078 the value of the previous frame's sp. This is a legacy thing,
1079 at one stage the frame cached the previous frame's SP instead
1080 of its address, hence it was easiest to just display the cached
1081 value. */
1082 if (gdbarch_sp_regnum (gdbarch) >= 0)
1083 {
1084 /* Find out the location of the saved stack pointer with out
1085 actually evaluating it. */
1086 frame_register_unwind (fi, gdbarch_sp_regnum (gdbarch),
1087 &optimized, &lval, &addr,
1088 &realnum, NULL);
1089 if (!optimized && lval == not_lval)
1090 {
1091 gdb_byte value[MAX_REGISTER_SIZE];
1092 CORE_ADDR sp;
1093 frame_register_unwind (fi, gdbarch_sp_regnum (gdbarch),
1094 &optimized, &lval, &addr,
1095 &realnum, value);
1096 /* NOTE: cagney/2003-05-22: This is assuming that the
1097 stack pointer was packed as an unsigned integer. That
1098 may or may not be valid. */
1099 sp = extract_unsigned_integer (value,
1100 register_size (gdbarch,
1101 gdbarch_sp_regnum (gdbarch)));
1102 printf_filtered (" Previous frame's sp is ");
1103 deprecated_print_address_numeric (sp, 1, gdb_stdout);
1104 printf_filtered ("\n");
1105 need_nl = 0;
1106 }
1107 else if (!optimized && lval == lval_memory)
1108 {
1109 printf_filtered (" Previous frame's sp at ");
1110 deprecated_print_address_numeric (addr, 1, gdb_stdout);
1111 printf_filtered ("\n");
1112 need_nl = 0;
1113 }
1114 else if (!optimized && lval == lval_register)
1115 {
1116 printf_filtered (" Previous frame's sp in %s\n",
1117 gdbarch_register_name (gdbarch, realnum));
1118 need_nl = 0;
1119 }
1120 /* else keep quiet. */
1121 }
1122
1123 count = 0;
1124 numregs = gdbarch_num_regs (gdbarch)
1125 + gdbarch_num_pseudo_regs (gdbarch);
1126 for (i = 0; i < numregs; i++)
1127 if (i != gdbarch_sp_regnum (gdbarch)
1128 && gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
1129 {
1130 /* Find out the location of the saved register without
1131 fetching the corresponding value. */
1132 frame_register_unwind (fi, i, &optimized, &lval, &addr, &realnum,
1133 NULL);
1134 /* For moment, only display registers that were saved on the
1135 stack. */
1136 if (!optimized && lval == lval_memory)
1137 {
1138 if (count == 0)
1139 puts_filtered (" Saved registers:\n ");
1140 else
1141 puts_filtered (",");
1142 wrap_here (" ");
1143 printf_filtered (" %s at ",
1144 gdbarch_register_name (gdbarch, i));
1145 deprecated_print_address_numeric (addr, 1, gdb_stdout);
1146 count++;
1147 }
1148 }
1149 if (count || need_nl)
1150 puts_filtered ("\n");
1151 }
1152 }
1153
1154 /* Print briefly all stack frames or just the innermost COUNT_EXP
1155 frames. */
1156
1157 static void
1158 backtrace_command_1 (char *count_exp, int show_locals, int from_tty)
1159 {
1160 struct frame_info *fi;
1161 int count;
1162 int i;
1163 struct frame_info *trailing;
1164 int trailing_level;
1165
1166 if (!target_has_stack)
1167 error (_("No stack."));
1168
1169 /* The following code must do two things. First, it must set the
1170 variable TRAILING to the frame from which we should start
1171 printing. Second, it must set the variable count to the number
1172 of frames which we should print, or -1 if all of them. */
1173 trailing = get_current_frame ();
1174
1175 /* The target can be in a state where there is no valid frames
1176 (e.g., just connected). */
1177 if (trailing == NULL)
1178 error (_("No stack."));
1179
1180 trailing_level = 0;
1181 if (count_exp)
1182 {
1183 count = parse_and_eval_long (count_exp);
1184 if (count < 0)
1185 {
1186 struct frame_info *current;
1187
1188 count = -count;
1189
1190 current = trailing;
1191 while (current && count--)
1192 {
1193 QUIT;
1194 current = get_prev_frame (current);
1195 }
1196
1197 /* Will stop when CURRENT reaches the top of the stack.
1198 TRAILING will be COUNT below it. */
1199 while (current)
1200 {
1201 QUIT;
1202 trailing = get_prev_frame (trailing);
1203 current = get_prev_frame (current);
1204 trailing_level++;
1205 }
1206
1207 count = -1;
1208 }
1209 }
1210 else
1211 count = -1;
1212
1213 if (info_verbose)
1214 {
1215 struct partial_symtab *ps;
1216
1217 /* Read in symbols for all of the frames. Need to do this in a
1218 separate pass so that "Reading in symbols for xxx" messages
1219 don't screw up the appearance of the backtrace. Also if
1220 people have strong opinions against reading symbols for
1221 backtrace this may have to be an option. */
1222 i = count;
1223 for (fi = trailing; fi != NULL && i--; fi = get_prev_frame (fi))
1224 {
1225 QUIT;
1226 ps = find_pc_psymtab (get_frame_address_in_block (fi));
1227 if (ps)
1228 PSYMTAB_TO_SYMTAB (ps); /* Force syms to come in. */
1229 }
1230 }
1231
1232 for (i = 0, fi = trailing; fi && count--; i++, fi = get_prev_frame (fi))
1233 {
1234 QUIT;
1235
1236 /* Don't use print_stack_frame; if an error() occurs it probably
1237 means further attempts to backtrace would fail (on the other
1238 hand, perhaps the code does or could be fixed to make sure
1239 the frame->prev field gets set to NULL in that case). */
1240 print_frame_info (fi, 1, LOCATION, 1);
1241 if (show_locals)
1242 print_frame_local_vars (fi, 1, gdb_stdout);
1243
1244 /* Save the last frame to check for error conditions. */
1245 trailing = fi;
1246 }
1247
1248 /* If we've stopped before the end, mention that. */
1249 if (fi && from_tty)
1250 printf_filtered (_("(More stack frames follow...)\n"));
1251
1252 /* If we've run out of frames, and the reason appears to be an error
1253 condition, print it. */
1254 if (fi == NULL && trailing != NULL)
1255 {
1256 enum unwind_stop_reason reason;
1257
1258 reason = get_frame_unwind_stop_reason (trailing);
1259 if (reason > UNWIND_FIRST_ERROR)
1260 printf_filtered (_("Backtrace stopped: %s\n"),
1261 frame_stop_reason_string (reason));
1262 }
1263 }
1264
1265 struct backtrace_command_args
1266 {
1267 char *count_exp;
1268 int show_locals;
1269 int from_tty;
1270 };
1271
1272 /* Stub for catch_errors. */
1273
1274 static int
1275 backtrace_command_stub (void *data)
1276 {
1277 struct backtrace_command_args *args = data;
1278 backtrace_command_1 (args->count_exp, args->show_locals, args->from_tty);
1279 return 0;
1280 }
1281
1282 static void
1283 backtrace_command (char *arg, int from_tty)
1284 {
1285 struct cleanup *old_chain = NULL;
1286 int fulltrace_arg = -1, arglen = 0, argc = 0;
1287 struct backtrace_command_args btargs;
1288
1289 if (arg)
1290 {
1291 char **argv;
1292 int i;
1293
1294 argv = buildargv (arg);
1295 old_chain = make_cleanup_freeargv (argv);
1296 argc = 0;
1297 for (i = 0; argv[i]; i++)
1298 {
1299 unsigned int j;
1300
1301 for (j = 0; j < strlen (argv[i]); j++)
1302 argv[i][j] = tolower (argv[i][j]);
1303
1304 if (fulltrace_arg < 0 && subset_compare (argv[i], "full"))
1305 fulltrace_arg = argc;
1306 else
1307 {
1308 arglen += strlen (argv[i]);
1309 argc++;
1310 }
1311 }
1312 arglen += argc;
1313 if (fulltrace_arg >= 0)
1314 {
1315 if (arglen > 0)
1316 {
1317 arg = xmalloc (arglen + 1);
1318 memset (arg, 0, arglen + 1);
1319 for (i = 0; i < (argc + 1); i++)
1320 {
1321 if (i != fulltrace_arg)
1322 {
1323 strcat (arg, argv[i]);
1324 strcat (arg, " ");
1325 }
1326 }
1327 }
1328 else
1329 arg = NULL;
1330 }
1331 }
1332
1333 btargs.count_exp = arg;
1334 btargs.show_locals = (fulltrace_arg >= 0);
1335 btargs.from_tty = from_tty;
1336 catch_errors (backtrace_command_stub, &btargs, "", RETURN_MASK_ERROR);
1337
1338 if (fulltrace_arg >= 0 && arglen > 0)
1339 xfree (arg);
1340
1341 if (old_chain)
1342 do_cleanups (old_chain);
1343 }
1344
1345 static void
1346 backtrace_full_command (char *arg, int from_tty)
1347 {
1348 struct backtrace_command_args btargs;
1349 btargs.count_exp = arg;
1350 btargs.show_locals = 1;
1351 btargs.from_tty = from_tty;
1352 catch_errors (backtrace_command_stub, &btargs, "", RETURN_MASK_ERROR);
1353 }
1354 \f
1355
1356 /* Print the local variables of a block B active in FRAME on STREAM.
1357 Return 1 if any variables were printed; 0 otherwise. */
1358
1359 static int
1360 print_block_frame_locals (struct block *b, struct frame_info *frame,
1361 int num_tabs, struct ui_file *stream)
1362 {
1363 struct dict_iterator iter;
1364 struct symbol *sym;
1365 int values_printed = 0;
1366 int j;
1367
1368 ALL_BLOCK_SYMBOLS (b, iter, sym)
1369 {
1370 switch (SYMBOL_CLASS (sym))
1371 {
1372 case LOC_LOCAL:
1373 case LOC_REGISTER:
1374 case LOC_STATIC:
1375 case LOC_BASEREG:
1376 case LOC_COMPUTED:
1377 values_printed = 1;
1378 for (j = 0; j < num_tabs; j++)
1379 fputs_filtered ("\t", stream);
1380 fputs_filtered (SYMBOL_PRINT_NAME (sym), stream);
1381 fputs_filtered (" = ", stream);
1382 print_variable_value (sym, frame, stream);
1383 fprintf_filtered (stream, "\n");
1384 break;
1385
1386 default:
1387 /* Ignore symbols which are not locals. */
1388 break;
1389 }
1390 }
1391
1392 return values_printed;
1393 }
1394
1395 /* Same, but print labels. */
1396
1397 static int
1398 print_block_frame_labels (struct block *b, int *have_default,
1399 struct ui_file *stream)
1400 {
1401 struct dict_iterator iter;
1402 struct symbol *sym;
1403 int values_printed = 0;
1404
1405 ALL_BLOCK_SYMBOLS (b, iter, sym)
1406 {
1407 if (strcmp (DEPRECATED_SYMBOL_NAME (sym), "default") == 0)
1408 {
1409 if (*have_default)
1410 continue;
1411 *have_default = 1;
1412 }
1413 if (SYMBOL_CLASS (sym) == LOC_LABEL)
1414 {
1415 struct symtab_and_line sal;
1416 sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
1417 values_printed = 1;
1418 fputs_filtered (SYMBOL_PRINT_NAME (sym), stream);
1419 if (addressprint)
1420 {
1421 fprintf_filtered (stream, " ");
1422 deprecated_print_address_numeric (SYMBOL_VALUE_ADDRESS (sym), 1, stream);
1423 }
1424 fprintf_filtered (stream, " in file %s, line %d\n",
1425 sal.symtab->filename, sal.line);
1426 }
1427 }
1428
1429 return values_printed;
1430 }
1431
1432 /* Print on STREAM all the local variables in frame FRAME, including
1433 all the blocks active in that frame at its current PC.
1434
1435 Returns 1 if the job was done, or 0 if nothing was printed because
1436 we have no info on the function running in FRAME. */
1437
1438 static void
1439 print_frame_local_vars (struct frame_info *frame, int num_tabs,
1440 struct ui_file *stream)
1441 {
1442 struct block *block = get_frame_block (frame, 0);
1443 int values_printed = 0;
1444
1445 if (block == 0)
1446 {
1447 fprintf_filtered (stream, "No symbol table info available.\n");
1448 return;
1449 }
1450
1451 while (block)
1452 {
1453 if (print_block_frame_locals (block, frame, num_tabs, stream))
1454 values_printed = 1;
1455 /* After handling the function's top-level block, stop. Don't
1456 continue to its superblock, the block of per-file symbols. */
1457 if (BLOCK_FUNCTION (block))
1458 break;
1459 block = BLOCK_SUPERBLOCK (block);
1460 }
1461
1462 if (!values_printed)
1463 fprintf_filtered (stream, _("No locals.\n"));
1464 }
1465
1466 /* Same, but print labels. */
1467
1468 static void
1469 print_frame_label_vars (struct frame_info *frame, int this_level_only,
1470 struct ui_file *stream)
1471 {
1472 struct blockvector *bl;
1473 struct block *block = get_frame_block (frame, 0);
1474 int values_printed = 0;
1475 int index, have_default = 0;
1476 char *blocks_printed;
1477 CORE_ADDR pc = get_frame_pc (frame);
1478
1479 if (block == 0)
1480 {
1481 fprintf_filtered (stream, "No symbol table info available.\n");
1482 return;
1483 }
1484
1485 bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
1486 blocks_printed = alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1487 memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1488
1489 while (block != 0)
1490 {
1491 CORE_ADDR end = BLOCK_END (block) - 4;
1492 int last_index;
1493
1494 if (bl != blockvector_for_pc (end, &index))
1495 error (_("blockvector blotch"));
1496 if (BLOCKVECTOR_BLOCK (bl, index) != block)
1497 error (_("blockvector botch"));
1498 last_index = BLOCKVECTOR_NBLOCKS (bl);
1499 index += 1;
1500
1501 /* Don't print out blocks that have gone by. */
1502 while (index < last_index
1503 && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
1504 index++;
1505
1506 while (index < last_index
1507 && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
1508 {
1509 if (blocks_printed[index] == 0)
1510 {
1511 if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index),
1512 &have_default, stream))
1513 values_printed = 1;
1514 blocks_printed[index] = 1;
1515 }
1516 index++;
1517 }
1518 if (have_default)
1519 return;
1520 if (values_printed && this_level_only)
1521 return;
1522
1523 /* After handling the function's top-level block, stop. Don't
1524 continue to its superblock, the block of per-file symbols. */
1525 if (BLOCK_FUNCTION (block))
1526 break;
1527 block = BLOCK_SUPERBLOCK (block);
1528 }
1529
1530 if (!values_printed && !this_level_only)
1531 fprintf_filtered (stream, _("No catches.\n"));
1532 }
1533
1534 void
1535 locals_info (char *args, int from_tty)
1536 {
1537 print_frame_local_vars (get_selected_frame (_("No frame selected.")),
1538 0, gdb_stdout);
1539 }
1540
1541 static void
1542 catch_info (char *ignore, int from_tty)
1543 {
1544 struct symtab_and_line *sal;
1545
1546 /* Check for target support for exception handling */
1547 sal = target_enable_exception_callback (EX_EVENT_CATCH, 1);
1548 if (sal)
1549 {
1550 /* Currently not handling this. Ideally, here we should
1551 interact with the C++ runtime system to find the list of
1552 active handlers, etc. */
1553 fprintf_filtered (gdb_stdout, _("\
1554 Info catch not supported with this target/compiler combination.\n"));
1555 }
1556 else
1557 {
1558 /* Assume g++ compiled code; old GDB 4.16 behaviour. */
1559 print_frame_label_vars (get_selected_frame (_("No frame selected.")),
1560 0, gdb_stdout);
1561 }
1562 }
1563
1564 static void
1565 print_frame_arg_vars (struct frame_info *frame, struct ui_file *stream)
1566 {
1567 struct symbol *func = get_frame_function (frame);
1568 struct block *b;
1569 struct dict_iterator iter;
1570 struct symbol *sym, *sym2;
1571 int values_printed = 0;
1572
1573 if (func == 0)
1574 {
1575 fprintf_filtered (stream, _("No symbol table info available.\n"));
1576 return;
1577 }
1578
1579 b = SYMBOL_BLOCK_VALUE (func);
1580 ALL_BLOCK_SYMBOLS (b, iter, sym)
1581 {
1582 switch (SYMBOL_CLASS (sym))
1583 {
1584 case LOC_ARG:
1585 case LOC_LOCAL_ARG:
1586 case LOC_REF_ARG:
1587 case LOC_REGPARM:
1588 case LOC_REGPARM_ADDR:
1589 case LOC_BASEREG_ARG:
1590 case LOC_COMPUTED_ARG:
1591 values_printed = 1;
1592 fputs_filtered (SYMBOL_PRINT_NAME (sym), stream);
1593 fputs_filtered (" = ", stream);
1594
1595 /* We have to look up the symbol because arguments can have
1596 two entries (one a parameter, one a local) and the one we
1597 want is the local, which lookup_symbol will find for us.
1598 This includes gcc1 (not gcc2) on the sparc when passing a
1599 small structure and gcc2 when the argument type is float
1600 and it is passed as a double and converted to float by
1601 the prologue (in the latter case the type of the LOC_ARG
1602 symbol is double and the type of the LOC_LOCAL symbol is
1603 float). There are also LOC_ARG/LOC_REGISTER pairs which
1604 are not combined in symbol-reading. */
1605
1606 sym2 = lookup_symbol (DEPRECATED_SYMBOL_NAME (sym),
1607 b, VAR_DOMAIN, NULL, NULL);
1608 print_variable_value (sym2, frame, stream);
1609 fprintf_filtered (stream, "\n");
1610 break;
1611
1612 default:
1613 /* Don't worry about things which aren't arguments. */
1614 break;
1615 }
1616 }
1617
1618 if (!values_printed)
1619 fprintf_filtered (stream, _("No arguments.\n"));
1620 }
1621
1622 void
1623 args_info (char *ignore, int from_tty)
1624 {
1625 print_frame_arg_vars (get_selected_frame (_("No frame selected.")),
1626 gdb_stdout);
1627 }
1628
1629
1630 static void
1631 args_plus_locals_info (char *ignore, int from_tty)
1632 {
1633 args_info (ignore, from_tty);
1634 locals_info (ignore, from_tty);
1635 }
1636 \f
1637
1638 /* Select frame FRAME. Also print the stack frame and show the source
1639 if this is the tui version. */
1640 static void
1641 select_and_print_frame (struct frame_info *frame)
1642 {
1643 select_frame (frame);
1644 if (frame)
1645 print_stack_frame (frame, 1, SRC_AND_LOC);
1646 }
1647 \f
1648 /* Return the symbol-block in which the selected frame is executing.
1649 Can return zero under various legitimate circumstances.
1650
1651 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the relevant
1652 code address within the block returned. We use this to decide
1653 which macros are in scope. */
1654
1655 struct block *
1656 get_selected_block (CORE_ADDR *addr_in_block)
1657 {
1658 if (!target_has_stack)
1659 return 0;
1660
1661 return get_frame_block (get_selected_frame (NULL), addr_in_block);
1662 }
1663
1664 /* Find a frame a certain number of levels away from FRAME.
1665 LEVEL_OFFSET_PTR points to an int containing the number of levels.
1666 Positive means go to earlier frames (up); negative, the reverse.
1667 The int that contains the number of levels is counted toward
1668 zero as the frames for those levels are found.
1669 If the top or bottom frame is reached, that frame is returned,
1670 but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
1671 how much farther the original request asked to go. */
1672
1673 struct frame_info *
1674 find_relative_frame (struct frame_info *frame, int *level_offset_ptr)
1675 {
1676 /* Going up is simple: just call get_prev_frame enough times or
1677 until the initial frame is reached. */
1678 while (*level_offset_ptr > 0)
1679 {
1680 struct frame_info *prev = get_prev_frame (frame);
1681 if (!prev)
1682 break;
1683 (*level_offset_ptr)--;
1684 frame = prev;
1685 }
1686
1687 /* Going down is just as simple. */
1688 while (*level_offset_ptr < 0)
1689 {
1690 struct frame_info *next = get_next_frame (frame);
1691 if (!next)
1692 break;
1693 (*level_offset_ptr)++;
1694 frame = next;
1695 }
1696
1697 return frame;
1698 }
1699
1700 /* The "select_frame" command. With no argument this is a NOP.
1701 Select the frame at level LEVEL_EXP if it is a valid level.
1702 Otherwise, treat LEVEL_EXP as an address expression and select it.
1703
1704 See parse_frame_specification for more info on proper frame
1705 expressions. */
1706
1707 void
1708 select_frame_command (char *level_exp, int from_tty)
1709 {
1710 select_frame (parse_frame_specification_1 (level_exp, "No stack.", NULL));
1711 }
1712
1713 /* The "frame" command. With no argument, print the selected frame
1714 briefly. With an argument, behave like select_frame and then print
1715 the selected frame. */
1716
1717 static void
1718 frame_command (char *level_exp, int from_tty)
1719 {
1720 select_frame_command (level_exp, from_tty);
1721 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1722 }
1723
1724 /* The XDB Compatibility command to print the current frame. */
1725
1726 static void
1727 current_frame_command (char *level_exp, int from_tty)
1728 {
1729 print_stack_frame (get_selected_frame (_("No stack.")), 1, SRC_AND_LOC);
1730 }
1731
1732 /* Select the frame up one or COUNT_EXP stack levels from the
1733 previously selected frame, and print it briefly. */
1734
1735 static void
1736 up_silently_base (char *count_exp)
1737 {
1738 struct frame_info *frame;
1739 int count = 1;
1740
1741 if (count_exp)
1742 count = parse_and_eval_long (count_exp);
1743
1744 frame = find_relative_frame (get_selected_frame ("No stack."), &count);
1745 if (count != 0 && count_exp == NULL)
1746 error (_("Initial frame selected; you cannot go up."));
1747 select_frame (frame);
1748 }
1749
1750 static void
1751 up_silently_command (char *count_exp, int from_tty)
1752 {
1753 up_silently_base (count_exp);
1754 }
1755
1756 static void
1757 up_command (char *count_exp, int from_tty)
1758 {
1759 up_silently_base (count_exp);
1760 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1761 }
1762
1763 /* Select the frame down one or COUNT_EXP stack levels from the previously
1764 selected frame, and print it briefly. */
1765
1766 static void
1767 down_silently_base (char *count_exp)
1768 {
1769 struct frame_info *frame;
1770 int count = -1;
1771 if (count_exp)
1772 count = -parse_and_eval_long (count_exp);
1773
1774 frame = find_relative_frame (get_selected_frame ("No stack."), &count);
1775 if (count != 0 && count_exp == NULL)
1776 {
1777 /* We only do this if COUNT_EXP is not specified. That way
1778 "down" means to really go down (and let me know if that is
1779 impossible), but "down 9999" can be used to mean go all the
1780 way down without getting an error. */
1781
1782 error (_("Bottom (innermost) frame selected; you cannot go down."));
1783 }
1784
1785 select_frame (frame);
1786 }
1787
1788 static void
1789 down_silently_command (char *count_exp, int from_tty)
1790 {
1791 down_silently_base (count_exp);
1792 }
1793
1794 static void
1795 down_command (char *count_exp, int from_tty)
1796 {
1797 down_silently_base (count_exp);
1798 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1799 }
1800 \f
1801
1802 void
1803 return_command (char *retval_exp, int from_tty)
1804 {
1805 struct symbol *thisfun;
1806 struct value *return_value = NULL;
1807 const char *query_prefix = "";
1808
1809 thisfun = get_frame_function (get_selected_frame ("No selected frame."));
1810
1811 /* Compute the return value. If the computation triggers an error,
1812 let it bail. If the return type can't be handled, set
1813 RETURN_VALUE to NULL, and QUERY_PREFIX to an informational
1814 message. */
1815 if (retval_exp)
1816 {
1817 struct type *return_type = NULL;
1818
1819 /* Compute the return value. Should the computation fail, this
1820 call throws an error. */
1821 return_value = parse_and_eval (retval_exp);
1822
1823 /* Cast return value to the return type of the function. Should
1824 the cast fail, this call throws an error. */
1825 if (thisfun != NULL)
1826 return_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (thisfun));
1827 if (return_type == NULL)
1828 return_type = builtin_type_int;
1829 CHECK_TYPEDEF (return_type);
1830 return_value = value_cast (return_type, return_value);
1831
1832 /* Make sure the value is fully evaluated. It may live in the
1833 stack frame we're about to pop. */
1834 if (value_lazy (return_value))
1835 value_fetch_lazy (return_value);
1836
1837 if (TYPE_CODE (return_type) == TYPE_CODE_VOID)
1838 /* If the return-type is "void", don't try to find the
1839 return-value's location. However, do still evaluate the
1840 return expression so that, even when the expression result
1841 is discarded, side effects such as "return i++" still
1842 occur. */
1843 return_value = NULL;
1844 else if (using_struct_return (return_type))
1845 {
1846 query_prefix = "\
1847 The location at which to store the function's return value is unknown.\n\
1848 If you continue, the return value that you specified will be ignored.\n";
1849 return_value = NULL;
1850 }
1851 }
1852
1853 /* Does an interactive user really want to do this? Include
1854 information, such as how well GDB can handle the return value, in
1855 the query message. */
1856 if (from_tty)
1857 {
1858 int confirmed;
1859 if (thisfun == NULL)
1860 confirmed = query (_("%sMake selected stack frame return now? "),
1861 query_prefix);
1862 else
1863 confirmed = query (_("%sMake %s return now? "), query_prefix,
1864 SYMBOL_PRINT_NAME (thisfun));
1865 if (!confirmed)
1866 error (_("Not confirmed"));
1867 }
1868
1869 /* NOTE: cagney/2003-01-18: Is this silly? Rather than pop each
1870 frame in turn, should this code just go straight to the relevant
1871 frame and pop that? */
1872
1873 /* First discard all frames inner-to the selected frame (making the
1874 selected frame current). */
1875 {
1876 struct frame_id selected_id = get_frame_id (get_selected_frame (NULL));
1877 while (!frame_id_eq (selected_id, get_frame_id (get_current_frame ())))
1878 {
1879 struct frame_info *frame = get_current_frame ();
1880 if (frame_id_inner (get_frame_arch (frame), selected_id,
1881 get_frame_id (frame)))
1882 /* Caught in the safety net, oops! We've gone way past the
1883 selected frame. */
1884 error (_("Problem while popping stack frames (corrupt stack?)"));
1885 frame_pop (get_current_frame ());
1886 }
1887 }
1888
1889 /* Second discard the selected frame (which is now also the current
1890 frame). */
1891 frame_pop (get_current_frame ());
1892
1893 /* Store RETURN_VALUE in the just-returned register set. */
1894 if (return_value != NULL)
1895 {
1896 struct type *return_type = value_type (return_value);
1897 struct gdbarch *gdbarch = get_regcache_arch (get_current_regcache ());
1898 gdb_assert (gdbarch_return_value (gdbarch, return_type, NULL, NULL, NULL)
1899 == RETURN_VALUE_REGISTER_CONVENTION);
1900 gdbarch_return_value (gdbarch, return_type,
1901 get_current_regcache (), NULL /*read*/,
1902 value_contents (return_value) /*write*/);
1903 }
1904
1905 /* If we are at the end of a call dummy now, pop the dummy frame
1906 too. */
1907 if (get_frame_type (get_current_frame ()) == DUMMY_FRAME)
1908 frame_pop (get_current_frame ());
1909
1910 /* If interactive, print the frame that is now current. */
1911 if (from_tty)
1912 frame_command ("0", 1);
1913 else
1914 select_frame_command ("0", 0);
1915 }
1916
1917 /* Sets the scope to input function name, provided that the function
1918 is within the current stack frame */
1919
1920 struct function_bounds
1921 {
1922 CORE_ADDR low, high;
1923 };
1924
1925 static void
1926 func_command (char *arg, int from_tty)
1927 {
1928 struct frame_info *frame;
1929 int found = 0;
1930 struct symtabs_and_lines sals;
1931 int i;
1932 int level = 1;
1933 struct function_bounds *func_bounds = NULL;
1934
1935 if (arg != NULL)
1936 return;
1937
1938 frame = parse_frame_specification ("0");
1939 sals = decode_line_spec (arg, 1);
1940 func_bounds = (struct function_bounds *) xmalloc (
1941 sizeof (struct function_bounds) * sals.nelts);
1942 for (i = 0; (i < sals.nelts && !found); i++)
1943 {
1944 if (sals.sals[i].pc == 0
1945 || find_pc_partial_function (sals.sals[i].pc, NULL,
1946 &func_bounds[i].low,
1947 &func_bounds[i].high) == 0)
1948 {
1949 func_bounds[i].low = func_bounds[i].high = 0;
1950 }
1951 }
1952
1953 do
1954 {
1955 for (i = 0; (i < sals.nelts && !found); i++)
1956 found = (get_frame_pc (frame) >= func_bounds[i].low
1957 && get_frame_pc (frame) < func_bounds[i].high);
1958 if (!found)
1959 {
1960 level = 1;
1961 frame = find_relative_frame (frame, &level);
1962 }
1963 }
1964 while (!found && level == 0);
1965
1966 if (func_bounds)
1967 xfree (func_bounds);
1968
1969 if (!found)
1970 printf_filtered (_("'%s' not within current stack frame.\n"), arg);
1971 else if (frame != get_selected_frame (NULL))
1972 select_and_print_frame (frame);
1973 }
1974
1975 /* Gets the language of the current frame. */
1976
1977 enum language
1978 get_frame_language (void)
1979 {
1980 struct frame_info *frame = deprecated_safe_get_selected_frame ();
1981
1982 if (frame)
1983 {
1984 /* We determine the current frame language by looking up its
1985 associated symtab. To retrieve this symtab, we use the frame
1986 PC. However we cannot use the frame PC as is, because it
1987 usually points to the instruction following the "call", which
1988 is sometimes the first instruction of another function. So
1989 we rely on get_frame_address_in_block(), it provides us with
1990 a PC that is guaranteed to be inside the frame's code
1991 block. */
1992 CORE_ADDR pc = get_frame_address_in_block (frame);
1993 struct symtab *s = find_pc_symtab (pc);
1994
1995 if (s)
1996 return s->language;
1997 }
1998
1999 return language_unknown;
2000 }
2001 \f
2002
2003 /* Provide a prototype to silence -Wmissing-prototypes. */
2004 void _initialize_stack (void);
2005
2006 void
2007 _initialize_stack (void)
2008 {
2009 #if 0
2010 backtrace_limit = 30;
2011 #endif
2012
2013 add_com ("return", class_stack, return_command, _("\
2014 Make selected stack frame return to its caller.\n\
2015 Control remains in the debugger, but when you continue\n\
2016 execution will resume in the frame above the one now selected.\n\
2017 If an argument is given, it is an expression for the value to return."));
2018
2019 add_com ("up", class_stack, up_command, _("\
2020 Select and print stack frame that called this one.\n\
2021 An argument says how many frames up to go."));
2022 add_com ("up-silently", class_support, up_silently_command, _("\
2023 Same as the `up' command, but does not print anything.\n\
2024 This is useful in command scripts."));
2025
2026 add_com ("down", class_stack, down_command, _("\
2027 Select and print stack frame called by this one.\n\
2028 An argument says how many frames down to go."));
2029 add_com_alias ("do", "down", class_stack, 1);
2030 add_com_alias ("dow", "down", class_stack, 1);
2031 add_com ("down-silently", class_support, down_silently_command, _("\
2032 Same as the `down' command, but does not print anything.\n\
2033 This is useful in command scripts."));
2034
2035 add_com ("frame", class_stack, frame_command, _("\
2036 Select and print a stack frame.\n\
2037 With no argument, print the selected stack frame. (See also \"info frame\").\n\
2038 An argument specifies the frame to select.\n\
2039 It can be a stack frame number or the address of the frame.\n\
2040 With argument, nothing is printed if input is coming from\n\
2041 a command file or a user-defined command."));
2042
2043 add_com_alias ("f", "frame", class_stack, 1);
2044
2045 if (xdb_commands)
2046 {
2047 add_com ("L", class_stack, current_frame_command,
2048 _("Print the current stack frame.\n"));
2049 add_com_alias ("V", "frame", class_stack, 1);
2050 }
2051 add_com ("select-frame", class_stack, select_frame_command, _("\
2052 Select a stack frame without printing anything.\n\
2053 An argument specifies the frame to select.\n\
2054 It can be a stack frame number or the address of the frame.\n"));
2055
2056 add_com ("backtrace", class_stack, backtrace_command, _("\
2057 Print backtrace of all stack frames, or innermost COUNT frames.\n\
2058 With a negative argument, print outermost -COUNT frames.\n\
2059 Use of the 'full' qualifier also prints the values of the local variables.\n"));
2060 add_com_alias ("bt", "backtrace", class_stack, 0);
2061 if (xdb_commands)
2062 {
2063 add_com_alias ("t", "backtrace", class_stack, 0);
2064 add_com ("T", class_stack, backtrace_full_command, _("\
2065 Print backtrace of all stack frames, or innermost COUNT frames \n\
2066 and the values of the local variables.\n\
2067 With a negative argument, print outermost -COUNT frames.\n\
2068 Usage: T <count>\n"));
2069 }
2070
2071 add_com_alias ("where", "backtrace", class_alias, 0);
2072 add_info ("stack", backtrace_command,
2073 _("Backtrace of the stack, or innermost COUNT frames."));
2074 add_info_alias ("s", "stack", 1);
2075 add_info ("frame", frame_info,
2076 _("All about selected stack frame, or frame at ADDR."));
2077 add_info_alias ("f", "frame", 1);
2078 add_info ("locals", locals_info,
2079 _("Local variables of current stack frame."));
2080 add_info ("args", args_info,
2081 _("Argument variables of current stack frame."));
2082 if (xdb_commands)
2083 add_com ("l", class_info, args_plus_locals_info,
2084 _("Argument and local variables of current stack frame."));
2085
2086 if (dbx_commands)
2087 add_com ("func", class_stack, func_command, _("\
2088 Select the stack frame that contains <func>.\n\
2089 Usage: func <name>\n"));
2090
2091 add_info ("catch", catch_info,
2092 _("Exceptions that can be caught in the current stack frame."));
2093
2094 add_setshow_enum_cmd ("frame-arguments", class_stack,
2095 print_frame_arguments_choices, &print_frame_arguments,
2096 _("Set printing of non-scalar frame arguments"),
2097 _("Show printing of non-scalar frame arguments"),
2098 NULL, NULL, NULL, &setprintlist, &showprintlist);
2099
2100 #if 0
2101 add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command, _(\
2102 "Specify maximum number of frames for \"backtrace\" to print by default."),
2103 &setlist);
2104 add_info ("backtrace-limit", backtrace_limit_info, _("\
2105 The maximum number of frames for \"backtrace\" to print by default."));
2106 #endif
2107 }