wg.conf.5: Fix a typo (in-inline comments are *not* allowed)
[dragonfly.git] / contrib / gdb-7 / gdb / stack.c
blobea5a306a393a5579e4dfb77421c58cad79be2083
1 /* Print and select stack frames for GDB, the GNU debugger.
3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "value.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "language.h"
26 #include "frame.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "target.h"
30 #include "source.h"
31 #include "breakpoint.h"
32 #include "demangle.h"
33 #include "inferior.h"
34 #include "annotate.h"
35 #include "ui-out.h"
36 #include "block.h"
37 #include "stack.h"
38 #include "dictionary.h"
39 #include "exceptions.h"
40 #include "reggroups.h"
41 #include "regcache.h"
42 #include "solib.h"
43 #include "valprint.h"
44 #include "gdbthread.h"
45 #include "cp-support.h"
46 #include "disasm.h"
47 #include "inline-frame.h"
48 #include "linespec.h"
49 #include "cli/cli-utils.h"
51 #include "gdb_assert.h"
52 #include <ctype.h>
53 #include "gdb_string.h"
55 #include "psymtab.h"
56 #include "symfile.h"
58 void (*deprecated_selected_frame_level_changed_hook) (int);
60 /* The possible choices of "set print frame-arguments", and the value
61 of this setting. */
63 static const char *const print_frame_arguments_choices[] =
64 {"all", "scalars", "none", NULL};
65 static const char *print_frame_arguments = "scalars";
67 /* The possible choices of "set print entry-values", and the value
68 of this setting. */
70 const char print_entry_values_no[] = "no";
71 const char print_entry_values_only[] = "only";
72 const char print_entry_values_preferred[] = "preferred";
73 const char print_entry_values_if_needed[] = "if-needed";
74 const char print_entry_values_both[] = "both";
75 const char print_entry_values_compact[] = "compact";
76 const char print_entry_values_default[] = "default";
77 static const char *const print_entry_values_choices[] =
79 print_entry_values_no,
80 print_entry_values_only,
81 print_entry_values_preferred,
82 print_entry_values_if_needed,
83 print_entry_values_both,
84 print_entry_values_compact,
85 print_entry_values_default,
86 NULL
88 const char *print_entry_values = print_entry_values_default;
90 /* Prototypes for local functions. */
92 static void print_frame_local_vars (struct frame_info *, int,
93 struct ui_file *);
95 static void print_frame (struct frame_info *frame, int print_level,
96 enum print_what print_what, int print_args,
97 struct symtab_and_line sal);
99 static void set_last_displayed_sal (int valid,
100 struct program_space *pspace,
101 CORE_ADDR addr,
102 struct symtab *symtab,
103 int line);
105 /* Zero means do things normally; we are interacting directly with the
106 user. One means print the full filename and linenumber when a
107 frame is printed, and do so in a format emacs18/emacs19.22 can
108 parse. Two means print similar annotations, but in many more
109 cases and in a slightly different syntax. */
111 int annotation_level = 0;
113 /* These variables hold the last symtab and line we displayed to the user.
114 * This is where we insert a breakpoint or a skiplist entry by default. */
115 static int last_displayed_sal_valid = 0;
116 static struct program_space *last_displayed_pspace = 0;
117 static CORE_ADDR last_displayed_addr = 0;
118 static struct symtab *last_displayed_symtab = 0;
119 static int last_displayed_line = 0;
122 /* Return 1 if we should display the address in addition to the location,
123 because we are in the middle of a statement. */
125 static int
126 frame_show_address (struct frame_info *frame,
127 struct symtab_and_line sal)
129 /* If there is a line number, but no PC, then there is no location
130 information associated with this sal. The only way that should
131 happen is for the call sites of inlined functions (SAL comes from
132 find_frame_sal). Otherwise, we would have some PC range if the
133 SAL came from a line table. */
134 if (sal.line != 0 && sal.pc == 0 && sal.end == 0)
136 if (get_next_frame (frame) == NULL)
137 gdb_assert (inline_skipped_frames (inferior_ptid) > 0);
138 else
139 gdb_assert (get_frame_type (get_next_frame (frame)) == INLINE_FRAME);
140 return 0;
143 return get_frame_pc (frame) != sal.pc;
146 /* Show or print a stack frame FRAME briefly. The output is format
147 according to PRINT_LEVEL and PRINT_WHAT printing the frame's
148 relative level, function name, argument list, and file name and
149 line number. If the frame's PC is not at the beginning of the
150 source line, the actual PC is printed at the beginning. */
152 void
153 print_stack_frame (struct frame_info *frame, int print_level,
154 enum print_what print_what)
156 volatile struct gdb_exception e;
158 /* For mi, alway print location and address. */
159 if (ui_out_is_mi_like_p (current_uiout))
160 print_what = LOC_AND_ADDRESS;
162 TRY_CATCH (e, RETURN_MASK_ERROR)
164 int center = (print_what == SRC_LINE || print_what == SRC_AND_LOC);
166 print_frame_info (frame, print_level, print_what, 1 /* print_args */);
167 set_current_sal_from_frame (frame, center);
171 /* Print nameless arguments of frame FRAME on STREAM, where START is
172 the offset of the first nameless argument, and NUM is the number of
173 nameless arguments to print. FIRST is nonzero if this is the first
174 argument (not just the first nameless argument). */
176 static void
177 print_frame_nameless_args (struct frame_info *frame, long start, int num,
178 int first, struct ui_file *stream)
180 struct gdbarch *gdbarch = get_frame_arch (frame);
181 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
182 int i;
183 CORE_ADDR argsaddr;
184 long arg_value;
186 for (i = 0; i < num; i++)
188 QUIT;
189 argsaddr = get_frame_args_address (frame);
190 if (!argsaddr)
191 return;
192 arg_value = read_memory_integer (argsaddr + start,
193 sizeof (int), byte_order);
194 if (!first)
195 fprintf_filtered (stream, ", ");
196 fprintf_filtered (stream, "%ld", arg_value);
197 first = 0;
198 start += sizeof (int);
202 /* Print single argument of inferior function. ARG must be already
203 read in.
205 Errors are printed as if they would be the parameter value. Use zeroed ARG
206 iff it should not be printed accoring to user settings. */
208 static void
209 print_frame_arg (const struct frame_arg *arg)
211 struct ui_out *uiout = current_uiout;
212 volatile struct gdb_exception except;
213 struct cleanup *old_chain;
214 struct ui_file *stb;
216 stb = mem_fileopen ();
217 old_chain = make_cleanup_ui_file_delete (stb);
219 gdb_assert (!arg->val || !arg->error);
220 gdb_assert (arg->entry_kind == print_entry_values_no
221 || arg->entry_kind == print_entry_values_only
222 || (!ui_out_is_mi_like_p (uiout)
223 && arg->entry_kind == print_entry_values_compact));
225 annotate_arg_begin ();
227 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
228 fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (arg->sym),
229 SYMBOL_LANGUAGE (arg->sym), DMGL_PARAMS | DMGL_ANSI);
230 if (arg->entry_kind == print_entry_values_compact)
232 /* It is OK to provide invalid MI-like stream as with
233 PRINT_ENTRY_VALUE_COMPACT we never use MI. */
234 fputs_filtered ("=", stb);
236 fprintf_symbol_filtered (stb, SYMBOL_PRINT_NAME (arg->sym),
237 SYMBOL_LANGUAGE (arg->sym),
238 DMGL_PARAMS | DMGL_ANSI);
240 if (arg->entry_kind == print_entry_values_only
241 || arg->entry_kind == print_entry_values_compact)
242 fputs_filtered ("@entry", stb);
243 ui_out_field_stream (uiout, "name", stb);
244 annotate_arg_name_end ();
245 ui_out_text (uiout, "=");
247 if (!arg->val && !arg->error)
248 ui_out_text (uiout, "...");
249 else
251 if (arg->error)
252 except.message = arg->error;
253 else
255 /* TRY_CATCH has two statements, wrap it in a block. */
257 TRY_CATCH (except, RETURN_MASK_ERROR)
259 const struct language_defn *language;
260 struct value_print_options opts;
262 /* Avoid value_print because it will deref ref parameters. We
263 just want to print their addresses. Print ??? for args whose
264 address we do not know. We pass 2 as "recurse" to val_print
265 because our standard indentation here is 4 spaces, and
266 val_print indents 2 for each recurse. */
268 annotate_arg_value (value_type (arg->val));
270 /* Use the appropriate language to display our symbol, unless the
271 user forced the language to a specific language. */
272 if (language_mode == language_mode_auto)
273 language = language_def (SYMBOL_LANGUAGE (arg->sym));
274 else
275 language = current_language;
277 get_raw_print_options (&opts);
278 opts.deref_ref = 1;
280 /* True in "summary" mode, false otherwise. */
281 opts.summary = !strcmp (print_frame_arguments, "scalars");
283 common_val_print (arg->val, stb, 2, &opts, language);
286 if (except.message)
287 fprintf_filtered (stb, _("<error reading variable: %s>"),
288 except.message);
291 ui_out_field_stream (uiout, "value", stb);
293 /* Also invoke ui_out_tuple_end. */
294 do_cleanups (old_chain);
296 annotate_arg_end ();
299 /* Read in inferior function local SYM at FRAME into ARGP. Caller is
300 responsible for xfree of ARGP->ERROR. This function never throws an
301 exception. */
303 void
304 read_frame_local (struct symbol *sym, struct frame_info *frame,
305 struct frame_arg *argp)
307 volatile struct gdb_exception except;
308 struct value *val = NULL;
310 TRY_CATCH (except, RETURN_MASK_ERROR)
312 val = read_var_value (sym, frame);
315 argp->error = (val == NULL) ? xstrdup (except.message) : NULL;
316 argp->sym = sym;
317 argp->val = val;
320 /* Read in inferior function parameter SYM at FRAME into ARGP. Caller is
321 responsible for xfree of ARGP->ERROR. This function never throws an
322 exception. */
324 void
325 read_frame_arg (struct symbol *sym, struct frame_info *frame,
326 struct frame_arg *argp, struct frame_arg *entryargp)
328 struct value *val = NULL, *entryval = NULL;
329 char *val_error = NULL, *entryval_error = NULL;
330 int val_equal = 0;
331 volatile struct gdb_exception except;
333 if (print_entry_values != print_entry_values_only
334 && print_entry_values != print_entry_values_preferred)
336 TRY_CATCH (except, RETURN_MASK_ERROR)
338 val = read_var_value (sym, frame);
340 if (!val)
342 val_error = alloca (strlen (except.message) + 1);
343 strcpy (val_error, except.message);
347 if (SYMBOL_CLASS (sym) == LOC_COMPUTED
348 && print_entry_values != print_entry_values_no
349 && (print_entry_values != print_entry_values_if_needed
350 || !val || value_optimized_out (val)))
352 TRY_CATCH (except, RETURN_MASK_ERROR)
354 const struct symbol_computed_ops *ops;
356 ops = SYMBOL_COMPUTED_OPS (sym);
357 entryval = ops->read_variable_at_entry (sym, frame);
359 if (!entryval)
361 entryval_error = alloca (strlen (except.message) + 1);
362 strcpy (entryval_error, except.message);
365 if (except.error == NO_ENTRY_VALUE_ERROR
366 || (entryval && value_optimized_out (entryval)))
368 entryval = NULL;
369 entryval_error = NULL;
372 if (print_entry_values == print_entry_values_compact
373 || print_entry_values == print_entry_values_default)
375 /* For MI do not try to use print_entry_values_compact for ARGP. */
377 if (val && entryval && !ui_out_is_mi_like_p (current_uiout))
379 struct type *type = value_type (val);
381 if (!value_optimized_out (val) && value_lazy (val))
382 value_fetch_lazy (val);
383 if (!value_optimized_out (val) && value_lazy (entryval))
384 value_fetch_lazy (entryval);
385 if (!value_optimized_out (val)
386 && value_available_contents_eq (val, 0, entryval, 0,
387 TYPE_LENGTH (type)))
389 /* Initialize it just to avoid a GCC false warning. */
390 struct value *val_deref = NULL, *entryval_deref;
392 /* DW_AT_GNU_call_site_value does match with the current
393 value. If it is a reference still try to verify if
394 dereferenced DW_AT_GNU_call_site_data_value does not
395 differ. */
397 TRY_CATCH (except, RETURN_MASK_ERROR)
399 struct type *type_deref;
401 val_deref = coerce_ref (val);
402 if (value_lazy (val_deref))
403 value_fetch_lazy (val_deref);
404 type_deref = value_type (val_deref);
406 entryval_deref = coerce_ref (entryval);
407 if (value_lazy (entryval_deref))
408 value_fetch_lazy (entryval_deref);
410 /* If the reference addresses match but dereferenced
411 content does not match print them. */
412 if (val != val_deref
413 && value_available_contents_eq (val_deref, 0,
414 entryval_deref, 0,
415 TYPE_LENGTH (type_deref)))
416 val_equal = 1;
419 /* Value was not a reference; and its content matches. */
420 if (val == val_deref)
421 val_equal = 1;
422 /* If the dereferenced content could not be fetched do not
423 display anything. */
424 else if (except.error == NO_ENTRY_VALUE_ERROR)
425 val_equal = 1;
426 else if (except.message)
428 entryval_error = alloca (strlen (except.message) + 1);
429 strcpy (entryval_error, except.message);
432 if (val_equal)
433 entryval = NULL;
437 /* Try to remove possibly duplicate error message for ENTRYARGP even
438 in MI mode. */
440 if (val_error && entryval_error
441 && strcmp (val_error, entryval_error) == 0)
443 entryval_error = NULL;
445 /* Do not se VAL_EQUAL as the same error message may be shown for
446 the entry value even if no entry values are present in the
447 inferior. */
452 if (entryval == NULL)
454 if (print_entry_values == print_entry_values_preferred)
456 TRY_CATCH (except, RETURN_MASK_ERROR)
458 val = read_var_value (sym, frame);
460 if (!val)
462 val_error = alloca (strlen (except.message) + 1);
463 strcpy (val_error, except.message);
466 if (print_entry_values == print_entry_values_only
467 || print_entry_values == print_entry_values_both
468 || (print_entry_values == print_entry_values_preferred
469 && (!val || value_optimized_out (val))))
470 entryval = allocate_optimized_out_value (SYMBOL_TYPE (sym));
472 if ((print_entry_values == print_entry_values_compact
473 || print_entry_values == print_entry_values_if_needed
474 || print_entry_values == print_entry_values_preferred)
475 && (!val || value_optimized_out (val)) && entryval != NULL)
477 val = NULL;
478 val_error = NULL;
481 argp->sym = sym;
482 argp->val = val;
483 argp->error = val_error ? xstrdup (val_error) : NULL;
484 if (!val && !val_error)
485 argp->entry_kind = print_entry_values_only;
486 else if ((print_entry_values == print_entry_values_compact
487 || print_entry_values == print_entry_values_default) && val_equal)
489 argp->entry_kind = print_entry_values_compact;
490 gdb_assert (!ui_out_is_mi_like_p (current_uiout));
492 else
493 argp->entry_kind = print_entry_values_no;
495 entryargp->sym = sym;
496 entryargp->val = entryval;
497 entryargp->error = entryval_error ? xstrdup (entryval_error) : NULL;
498 if (!entryval && !entryval_error)
499 entryargp->entry_kind = print_entry_values_no;
500 else
501 entryargp->entry_kind = print_entry_values_only;
504 /* Print the arguments of frame FRAME on STREAM, given the function
505 FUNC running in that frame (as a symbol), where NUM is the number
506 of arguments according to the stack frame (or -1 if the number of
507 arguments is unknown). */
509 /* Note that currently the "number of arguments according to the
510 stack frame" is only known on VAX where i refers to the "number of
511 ints of arguments according to the stack frame". */
513 static void
514 print_frame_args (struct symbol *func, struct frame_info *frame,
515 int num, struct ui_file *stream)
517 struct ui_out *uiout = current_uiout;
518 int first = 1;
519 /* Offset of next stack argument beyond the one we have seen that is
520 at the highest offset, or -1 if we haven't come to a stack
521 argument yet. */
522 long highest_offset = -1;
523 /* Number of ints of arguments that we have printed so far. */
524 int args_printed = 0;
525 struct cleanup *old_chain;
526 struct ui_file *stb;
527 /* True if we should print arguments, false otherwise. */
528 int print_args = strcmp (print_frame_arguments, "none");
530 stb = mem_fileopen ();
531 old_chain = make_cleanup_ui_file_delete (stb);
533 if (func)
535 struct block *b = SYMBOL_BLOCK_VALUE (func);
536 struct block_iterator iter;
537 struct symbol *sym;
539 ALL_BLOCK_SYMBOLS (b, iter, sym)
541 struct frame_arg arg, entryarg;
543 QUIT;
545 /* Keep track of the highest stack argument offset seen, and
546 skip over any kinds of symbols we don't care about. */
548 if (!SYMBOL_IS_ARGUMENT (sym))
549 continue;
551 switch (SYMBOL_CLASS (sym))
553 case LOC_ARG:
554 case LOC_REF_ARG:
556 long current_offset = SYMBOL_VALUE (sym);
557 int arg_size = TYPE_LENGTH (SYMBOL_TYPE (sym));
559 /* Compute address of next argument by adding the size of
560 this argument and rounding to an int boundary. */
561 current_offset =
562 ((current_offset + arg_size + sizeof (int) - 1)
563 & ~(sizeof (int) - 1));
565 /* If this is the highest offset seen yet, set
566 highest_offset. */
567 if (highest_offset == -1
568 || (current_offset > highest_offset))
569 highest_offset = current_offset;
571 /* Add the number of ints we're about to print to
572 args_printed. */
573 args_printed += (arg_size + sizeof (int) - 1) / sizeof (int);
576 /* We care about types of symbols, but don't need to
577 keep track of stack offsets in them. */
578 case LOC_REGISTER:
579 case LOC_REGPARM_ADDR:
580 case LOC_COMPUTED:
581 case LOC_OPTIMIZED_OUT:
582 default:
583 break;
586 /* We have to look up the symbol because arguments can have
587 two entries (one a parameter, one a local) and the one we
588 want is the local, which lookup_symbol will find for us.
589 This includes gcc1 (not gcc2) on SPARC when passing a
590 small structure and gcc2 when the argument type is float
591 and it is passed as a double and converted to float by
592 the prologue (in the latter case the type of the LOC_ARG
593 symbol is double and the type of the LOC_LOCAL symbol is
594 float). */
595 /* But if the parameter name is null, don't try it. Null
596 parameter names occur on the RS/6000, for traceback
597 tables. FIXME, should we even print them? */
599 if (*SYMBOL_LINKAGE_NAME (sym))
601 struct symbol *nsym;
603 nsym = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
604 b, VAR_DOMAIN, NULL);
605 gdb_assert (nsym != NULL);
606 if (SYMBOL_CLASS (nsym) == LOC_REGISTER
607 && !SYMBOL_IS_ARGUMENT (nsym))
609 /* There is a LOC_ARG/LOC_REGISTER pair. This means
610 that it was passed on the stack and loaded into a
611 register, or passed in a register and stored in a
612 stack slot. GDB 3.x used the LOC_ARG; GDB
613 4.0-4.11 used the LOC_REGISTER.
615 Reasons for using the LOC_ARG:
617 (1) Because find_saved_registers may be slow for
618 remote debugging.
620 (2) Because registers are often re-used and stack
621 slots rarely (never?) are. Therefore using
622 the stack slot is much less likely to print
623 garbage.
625 Reasons why we might want to use the LOC_REGISTER:
627 (1) So that the backtrace prints the same value
628 as "print foo". I see no compelling reason
629 why this needs to be the case; having the
630 backtrace print the value which was passed
631 in, and "print foo" print the value as
632 modified within the called function, makes
633 perfect sense to me.
635 Additional note: It might be nice if "info args"
636 displayed both values.
638 One more note: There is a case with SPARC
639 structure passing where we need to use the
640 LOC_REGISTER, but this is dealt with by creating
641 a single LOC_REGPARM in symbol reading. */
643 /* Leave sym (the LOC_ARG) alone. */
646 else
647 sym = nsym;
650 /* Print the current arg. */
651 if (!first)
652 ui_out_text (uiout, ", ");
653 ui_out_wrap_hint (uiout, " ");
655 if (!print_args)
657 memset (&arg, 0, sizeof (arg));
658 arg.sym = sym;
659 arg.entry_kind = print_entry_values_no;
660 memset (&entryarg, 0, sizeof (entryarg));
661 entryarg.sym = sym;
662 entryarg.entry_kind = print_entry_values_no;
664 else
665 read_frame_arg (sym, frame, &arg, &entryarg);
667 if (arg.entry_kind != print_entry_values_only)
668 print_frame_arg (&arg);
670 if (entryarg.entry_kind != print_entry_values_no)
672 if (arg.entry_kind != print_entry_values_only)
674 ui_out_text (uiout, ", ");
675 ui_out_wrap_hint (uiout, " ");
678 print_frame_arg (&entryarg);
681 xfree (arg.error);
682 xfree (entryarg.error);
684 first = 0;
688 /* Don't print nameless args in situations where we don't know
689 enough about the stack to find them. */
690 if (num != -1)
692 long start;
694 if (highest_offset == -1)
695 start = gdbarch_frame_args_skip (get_frame_arch (frame));
696 else
697 start = highest_offset;
699 print_frame_nameless_args (frame, start, num - args_printed,
700 first, stream);
703 do_cleanups (old_chain);
706 /* Set the current source and line to the location given by frame
707 FRAME, if possible. When CENTER is true, adjust so the relevant
708 line is in the center of the next 'list'. */
710 void
711 set_current_sal_from_frame (struct frame_info *frame, int center)
713 struct symtab_and_line sal;
715 find_frame_sal (frame, &sal);
716 if (sal.symtab)
718 if (center)
719 sal.line = max (sal.line - get_lines_to_list () / 2, 1);
720 set_current_source_symtab_and_line (&sal);
724 /* If ON, GDB will display disassembly of the next source line when
725 execution of the program being debugged stops.
726 If AUTO (which is the default), or there's no line info to determine
727 the source line of the next instruction, display disassembly of next
728 instruction instead. */
730 static enum auto_boolean disassemble_next_line;
732 static void
733 show_disassemble_next_line (struct ui_file *file, int from_tty,
734 struct cmd_list_element *c,
735 const char *value)
737 fprintf_filtered (file,
738 _("Debugger's willingness to use "
739 "disassemble-next-line is %s.\n"),
740 value);
743 /* Use TRY_CATCH to catch the exception from the gdb_disassembly
744 because it will be broken by filter sometime. */
746 static void
747 do_gdb_disassembly (struct gdbarch *gdbarch,
748 int how_many, CORE_ADDR low, CORE_ADDR high)
750 volatile struct gdb_exception exception;
752 TRY_CATCH (exception, RETURN_MASK_ERROR)
754 gdb_disassembly (gdbarch, current_uiout, 0,
755 DISASSEMBLY_RAW_INSN, how_many,
756 low, high);
758 if (exception.reason < 0)
760 /* If an exception was thrown while doing the disassembly, print
761 the error message, to give the user a clue of what happened. */
762 exception_print (gdb_stderr, exception);
766 /* Print information about frame FRAME. The output is format according
767 to PRINT_LEVEL and PRINT_WHAT and PRINT_ARGS. The meaning of
768 PRINT_WHAT is:
770 SRC_LINE: Print only source line.
771 LOCATION: Print only location.
772 LOC_AND_SRC: Print location and source line.
774 Used in "where" output, and to emit breakpoint or step
775 messages. */
777 void
778 print_frame_info (struct frame_info *frame, int print_level,
779 enum print_what print_what, int print_args)
781 struct gdbarch *gdbarch = get_frame_arch (frame);
782 struct symtab_and_line sal;
783 int source_print;
784 int location_print;
785 struct ui_out *uiout = current_uiout;
787 if (get_frame_type (frame) == DUMMY_FRAME
788 || get_frame_type (frame) == SIGTRAMP_FRAME
789 || get_frame_type (frame) == ARCH_FRAME)
791 struct cleanup *uiout_cleanup
792 = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
794 annotate_frame_begin (print_level ? frame_relative_level (frame) : 0,
795 gdbarch, get_frame_pc (frame));
797 /* Do this regardless of SOURCE because we don't have any source
798 to list for this frame. */
799 if (print_level)
801 ui_out_text (uiout, "#");
802 ui_out_field_fmt_int (uiout, 2, ui_left, "level",
803 frame_relative_level (frame));
805 if (ui_out_is_mi_like_p (uiout))
807 annotate_frame_address ();
808 ui_out_field_core_addr (uiout, "addr",
809 gdbarch, get_frame_pc (frame));
810 annotate_frame_address_end ();
813 if (get_frame_type (frame) == DUMMY_FRAME)
815 annotate_function_call ();
816 ui_out_field_string (uiout, "func", "<function called from gdb>");
818 else if (get_frame_type (frame) == SIGTRAMP_FRAME)
820 annotate_signal_handler_caller ();
821 ui_out_field_string (uiout, "func", "<signal handler called>");
823 else if (get_frame_type (frame) == ARCH_FRAME)
825 ui_out_field_string (uiout, "func", "<cross-architecture call>");
827 ui_out_text (uiout, "\n");
828 annotate_frame_end ();
830 do_cleanups (uiout_cleanup);
831 return;
834 /* If FRAME is not the innermost frame, that normally means that
835 FRAME->pc points to *after* the call instruction, and we want to
836 get the line containing the call, never the next line. But if
837 the next frame is a SIGTRAMP_FRAME or a DUMMY_FRAME, then the
838 next frame was not entered as the result of a call, and we want
839 to get the line containing FRAME->pc. */
840 find_frame_sal (frame, &sal);
842 location_print = (print_what == LOCATION
843 || print_what == LOC_AND_ADDRESS
844 || print_what == SRC_AND_LOC);
846 if (location_print || !sal.symtab)
847 print_frame (frame, print_level, print_what, print_args, sal);
849 source_print = (print_what == SRC_LINE || print_what == SRC_AND_LOC);
851 /* If disassemble-next-line is set to auto or on and doesn't have
852 the line debug messages for $pc, output the next instruction. */
853 if ((disassemble_next_line == AUTO_BOOLEAN_AUTO
854 || disassemble_next_line == AUTO_BOOLEAN_TRUE)
855 && source_print && !sal.symtab)
856 do_gdb_disassembly (get_frame_arch (frame), 1,
857 get_frame_pc (frame), get_frame_pc (frame) + 1);
859 if (source_print && sal.symtab)
861 int done = 0;
862 int mid_statement = ((print_what == SRC_LINE)
863 && frame_show_address (frame, sal));
865 if (annotation_level)
866 done = identify_source_line (sal.symtab, sal.line, mid_statement,
867 get_frame_pc (frame));
868 if (!done)
870 if (deprecated_print_frame_info_listing_hook)
871 deprecated_print_frame_info_listing_hook (sal.symtab,
872 sal.line,
873 sal.line + 1, 0);
874 else
876 struct value_print_options opts;
878 get_user_print_options (&opts);
879 /* We used to do this earlier, but that is clearly
880 wrong. This function is used by many different
881 parts of gdb, including normal_stop in infrun.c,
882 which uses this to print out the current PC
883 when we stepi/nexti into the middle of a source
884 line. Only the command line really wants this
885 behavior. Other UIs probably would like the
886 ability to decide for themselves if it is desired. */
887 if (opts.addressprint && mid_statement)
889 ui_out_field_core_addr (uiout, "addr",
890 gdbarch, get_frame_pc (frame));
891 ui_out_text (uiout, "\t");
894 print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
898 /* If disassemble-next-line is set to on and there is line debug
899 messages, output assembly codes for next line. */
900 if (disassemble_next_line == AUTO_BOOLEAN_TRUE)
901 do_gdb_disassembly (get_frame_arch (frame), -1, sal.pc, sal.end);
904 if (print_what != LOCATION)
906 CORE_ADDR pc;
908 if (get_frame_pc_if_available (frame, &pc))
909 set_last_displayed_sal (1, sal.pspace, pc, sal.symtab, sal.line);
910 else
911 set_last_displayed_sal (0, 0, 0, 0, 0);
914 annotate_frame_end ();
916 gdb_flush (gdb_stdout);
919 /* Remember the last symtab and line we displayed, which we use e.g.
920 * as the place to put a breakpoint when the `break' command is
921 * invoked with no arguments. */
923 static void
924 set_last_displayed_sal (int valid, struct program_space *pspace,
925 CORE_ADDR addr, struct symtab *symtab,
926 int line)
928 last_displayed_sal_valid = valid;
929 last_displayed_pspace = pspace;
930 last_displayed_addr = addr;
931 last_displayed_symtab = symtab;
932 last_displayed_line = line;
933 if (valid && pspace == NULL)
935 clear_last_displayed_sal ();
936 internal_error (__FILE__, __LINE__,
937 _("Trying to set NULL pspace."));
941 /* Forget the last sal we displayed. */
943 void
944 clear_last_displayed_sal (void)
946 last_displayed_sal_valid = 0;
947 last_displayed_pspace = 0;
948 last_displayed_addr = 0;
949 last_displayed_symtab = 0;
950 last_displayed_line = 0;
953 /* Is our record of the last sal we displayed valid? If not,
954 * the get_last_displayed_* functions will return NULL or 0, as
955 * appropriate. */
958 last_displayed_sal_is_valid (void)
960 return last_displayed_sal_valid;
963 /* Get the pspace of the last sal we displayed, if it's valid. */
965 struct program_space *
966 get_last_displayed_pspace (void)
968 if (last_displayed_sal_valid)
969 return last_displayed_pspace;
970 return 0;
973 /* Get the address of the last sal we displayed, if it's valid. */
975 CORE_ADDR
976 get_last_displayed_addr (void)
978 if (last_displayed_sal_valid)
979 return last_displayed_addr;
980 return 0;
983 /* Get the symtab of the last sal we displayed, if it's valid. */
985 struct symtab*
986 get_last_displayed_symtab (void)
988 if (last_displayed_sal_valid)
989 return last_displayed_symtab;
990 return 0;
993 /* Get the line of the last sal we displayed, if it's valid. */
996 get_last_displayed_line (void)
998 if (last_displayed_sal_valid)
999 return last_displayed_line;
1000 return 0;
1003 /* Get the last sal we displayed, if it's valid. */
1005 void
1006 get_last_displayed_sal (struct symtab_and_line *sal)
1008 if (last_displayed_sal_valid)
1010 sal->pspace = last_displayed_pspace;
1011 sal->pc = last_displayed_addr;
1012 sal->symtab = last_displayed_symtab;
1013 sal->line = last_displayed_line;
1015 else
1017 sal->pspace = 0;
1018 sal->pc = 0;
1019 sal->symtab = 0;
1020 sal->line = 0;
1025 /* Attempt to obtain the FUNNAME, FUNLANG and optionally FUNCP of the function
1026 corresponding to FRAME. */
1028 void
1029 find_frame_funname (struct frame_info *frame, const char **funname,
1030 enum language *funlang, struct symbol **funcp)
1032 struct symbol *func;
1034 *funname = NULL;
1035 *funlang = language_unknown;
1036 if (funcp)
1037 *funcp = NULL;
1039 func = get_frame_function (frame);
1040 if (func)
1042 /* In certain pathological cases, the symtabs give the wrong
1043 function (when we are in the first function in a file which
1044 is compiled without debugging symbols, the previous function
1045 is compiled with debugging symbols, and the "foo.o" symbol
1046 that is supposed to tell us where the file with debugging
1047 symbols ends has been truncated by ar because it is longer
1048 than 15 characters). This also occurs if the user uses asm()
1049 to create a function but not stabs for it (in a file compiled
1050 with -g).
1052 So look in the minimal symbol tables as well, and if it comes
1053 up with a larger address for the function use that instead.
1054 I don't think this can ever cause any problems; there
1055 shouldn't be any minimal symbols in the middle of a function;
1056 if this is ever changed many parts of GDB will need to be
1057 changed (and we'll create a find_pc_minimal_function or some
1058 such). */
1060 struct minimal_symbol *msymbol = NULL;
1062 /* Don't attempt to do this for inlined functions, which do not
1063 have a corresponding minimal symbol. */
1064 if (!block_inlined_p (SYMBOL_BLOCK_VALUE (func)))
1065 msymbol
1066 = lookup_minimal_symbol_by_pc (get_frame_address_in_block (frame));
1068 if (msymbol != NULL
1069 && (SYMBOL_VALUE_ADDRESS (msymbol)
1070 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
1072 /* We also don't know anything about the function besides
1073 its address and name. */
1074 func = 0;
1075 *funname = SYMBOL_PRINT_NAME (msymbol);
1076 *funlang = SYMBOL_LANGUAGE (msymbol);
1078 else
1080 *funname = SYMBOL_PRINT_NAME (func);
1081 *funlang = SYMBOL_LANGUAGE (func);
1082 if (funcp)
1083 *funcp = func;
1084 if (*funlang == language_cplus)
1086 /* It seems appropriate to use SYMBOL_PRINT_NAME() here,
1087 to display the demangled name that we already have
1088 stored in the symbol table, but we stored a version
1089 with DMGL_PARAMS turned on, and here we don't want to
1090 display parameters. So remove the parameters. */
1091 char *func_only = cp_remove_params (*funname);
1093 if (func_only)
1095 *funname = func_only;
1096 make_cleanup (xfree, func_only);
1101 else
1103 struct minimal_symbol *msymbol;
1104 CORE_ADDR pc;
1106 if (!get_frame_address_in_block_if_available (frame, &pc))
1107 return;
1109 msymbol = lookup_minimal_symbol_by_pc (pc);
1110 if (msymbol != NULL)
1112 *funname = SYMBOL_PRINT_NAME (msymbol);
1113 *funlang = SYMBOL_LANGUAGE (msymbol);
1118 static void
1119 print_frame (struct frame_info *frame, int print_level,
1120 enum print_what print_what, int print_args,
1121 struct symtab_and_line sal)
1123 struct gdbarch *gdbarch = get_frame_arch (frame);
1124 struct ui_out *uiout = current_uiout;
1125 const char *funname = NULL;
1126 enum language funlang = language_unknown;
1127 struct ui_file *stb;
1128 struct cleanup *old_chain, *list_chain;
1129 struct value_print_options opts;
1130 struct symbol *func;
1131 CORE_ADDR pc = 0;
1132 int pc_p;
1134 pc_p = get_frame_pc_if_available (frame, &pc);
1136 stb = mem_fileopen ();
1137 old_chain = make_cleanup_ui_file_delete (stb);
1139 find_frame_funname (frame, &funname, &funlang, &func);
1141 annotate_frame_begin (print_level ? frame_relative_level (frame) : 0,
1142 gdbarch, pc);
1144 list_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "frame");
1146 if (print_level)
1148 ui_out_text (uiout, "#");
1149 ui_out_field_fmt_int (uiout, 2, ui_left, "level",
1150 frame_relative_level (frame));
1152 get_user_print_options (&opts);
1153 if (opts.addressprint)
1154 if (!sal.symtab
1155 || frame_show_address (frame, sal)
1156 || print_what == LOC_AND_ADDRESS)
1158 annotate_frame_address ();
1159 if (pc_p)
1160 ui_out_field_core_addr (uiout, "addr", gdbarch, pc);
1161 else
1162 ui_out_field_string (uiout, "addr", "<unavailable>");
1163 annotate_frame_address_end ();
1164 ui_out_text (uiout, " in ");
1166 annotate_frame_function_name ();
1167 fprintf_symbol_filtered (stb, funname ? funname : "??",
1168 funlang, DMGL_ANSI);
1169 ui_out_field_stream (uiout, "func", stb);
1170 ui_out_wrap_hint (uiout, " ");
1171 annotate_frame_args ();
1173 ui_out_text (uiout, " (");
1174 if (print_args)
1176 struct gdbarch *gdbarch = get_frame_arch (frame);
1177 int numargs;
1178 struct cleanup *args_list_chain;
1179 volatile struct gdb_exception e;
1181 if (gdbarch_frame_num_args_p (gdbarch))
1183 numargs = gdbarch_frame_num_args (gdbarch, frame);
1184 gdb_assert (numargs >= 0);
1186 else
1187 numargs = -1;
1189 args_list_chain = make_cleanup_ui_out_list_begin_end (uiout, "args");
1190 TRY_CATCH (e, RETURN_MASK_ERROR)
1192 print_frame_args (func, frame, numargs, gdb_stdout);
1194 /* FIXME: ARGS must be a list. If one argument is a string it
1195 will have " that will not be properly escaped. */
1196 /* Invoke ui_out_tuple_end. */
1197 do_cleanups (args_list_chain);
1198 QUIT;
1200 ui_out_text (uiout, ")");
1201 if (sal.symtab)
1203 const char *filename_display;
1205 filename_display = symtab_to_filename_for_display (sal.symtab);
1206 annotate_frame_source_begin ();
1207 ui_out_wrap_hint (uiout, " ");
1208 ui_out_text (uiout, " at ");
1209 annotate_frame_source_file ();
1210 ui_out_field_string (uiout, "file", filename_display);
1211 if (ui_out_is_mi_like_p (uiout))
1213 const char *fullname = symtab_to_fullname (sal.symtab);
1215 ui_out_field_string (uiout, "fullname", fullname);
1217 annotate_frame_source_file_end ();
1218 ui_out_text (uiout, ":");
1219 annotate_frame_source_line ();
1220 ui_out_field_int (uiout, "line", sal.line);
1221 annotate_frame_source_end ();
1224 if (pc_p && (funname == NULL || sal.symtab == NULL))
1226 #ifdef PC_SOLIB
1227 char *lib = PC_SOLIB (get_frame_pc (frame));
1228 #else
1229 char *lib = solib_name_from_address (get_frame_program_space (frame),
1230 get_frame_pc (frame));
1231 #endif
1232 if (lib)
1234 annotate_frame_where ();
1235 ui_out_wrap_hint (uiout, " ");
1236 ui_out_text (uiout, " from ");
1237 ui_out_field_string (uiout, "from", lib);
1241 /* do_cleanups will call ui_out_tuple_end() for us. */
1242 do_cleanups (list_chain);
1243 ui_out_text (uiout, "\n");
1244 do_cleanups (old_chain);
1248 /* Read a frame specification in whatever the appropriate format is
1249 from FRAME_EXP. Call error(), printing MESSAGE, if the
1250 specification is in any way invalid (so this function never returns
1251 NULL). When SEPECTED_P is non-NULL set its target to indicate that
1252 the default selected frame was used. */
1254 static struct frame_info *
1255 parse_frame_specification_1 (const char *frame_exp, const char *message,
1256 int *selected_frame_p)
1258 int numargs;
1259 struct value *args[4];
1260 CORE_ADDR addrs[ARRAY_SIZE (args)];
1262 if (frame_exp == NULL)
1263 numargs = 0;
1264 else
1266 numargs = 0;
1267 while (1)
1269 char *addr_string;
1270 struct cleanup *cleanup;
1271 const char *p;
1273 /* Skip leading white space, bail of EOL. */
1274 frame_exp = skip_spaces_const (frame_exp);
1275 if (!*frame_exp)
1276 break;
1278 /* Parse the argument, extract it, save it. */
1279 for (p = frame_exp;
1280 *p && !isspace (*p);
1281 p++);
1282 addr_string = savestring (frame_exp, p - frame_exp);
1283 frame_exp = p;
1284 cleanup = make_cleanup (xfree, addr_string);
1286 /* NOTE: Parse and evaluate expression, but do not use
1287 functions such as parse_and_eval_long or
1288 parse_and_eval_address to also extract the value.
1289 Instead value_as_long and value_as_address are used.
1290 This avoids problems with expressions that contain
1291 side-effects. */
1292 if (numargs >= ARRAY_SIZE (args))
1293 error (_("Too many args in frame specification"));
1294 args[numargs++] = parse_and_eval (addr_string);
1296 do_cleanups (cleanup);
1300 /* If no args, default to the selected frame. */
1301 if (numargs == 0)
1303 if (selected_frame_p != NULL)
1304 (*selected_frame_p) = 1;
1305 return get_selected_frame (message);
1308 /* None of the remaining use the selected frame. */
1309 if (selected_frame_p != NULL)
1310 (*selected_frame_p) = 0;
1312 /* Assume the single arg[0] is an integer, and try using that to
1313 select a frame relative to current. */
1314 if (numargs == 1)
1316 struct frame_info *fid;
1317 int level = value_as_long (args[0]);
1319 fid = find_relative_frame (get_current_frame (), &level);
1320 if (level == 0)
1321 /* find_relative_frame was successful. */
1322 return fid;
1325 /* Convert each value into a corresponding address. */
1327 int i;
1329 for (i = 0; i < numargs; i++)
1330 addrs[i] = value_as_address (args[i]);
1333 /* Assume that the single arg[0] is an address, use that to identify
1334 a frame with a matching ID. Should this also accept stack/pc or
1335 stack/pc/special. */
1336 if (numargs == 1)
1338 struct frame_id id = frame_id_build_wild (addrs[0]);
1339 struct frame_info *fid;
1341 /* If (s)he specifies the frame with an address, he deserves
1342 what (s)he gets. Still, give the highest one that matches.
1343 (NOTE: cagney/2004-10-29: Why highest, or outer-most, I don't
1344 know). */
1345 for (fid = get_current_frame ();
1346 fid != NULL;
1347 fid = get_prev_frame (fid))
1349 if (frame_id_eq (id, get_frame_id (fid)))
1351 struct frame_info *prev_frame;
1353 while (1)
1355 prev_frame = get_prev_frame (fid);
1356 if (!prev_frame
1357 || !frame_id_eq (id, get_frame_id (prev_frame)))
1358 break;
1359 fid = prev_frame;
1361 return fid;
1366 /* We couldn't identify the frame as an existing frame, but
1367 perhaps we can create one with a single argument. */
1368 if (numargs == 1)
1369 return create_new_frame (addrs[0], 0);
1370 else if (numargs == 2)
1371 return create_new_frame (addrs[0], addrs[1]);
1372 else
1373 error (_("Too many args in frame specification"));
1376 static struct frame_info *
1377 parse_frame_specification (char *frame_exp)
1379 return parse_frame_specification_1 (frame_exp, NULL, NULL);
1382 /* Print verbosely the selected frame or the frame at address
1383 ADDR_EXP. Absolutely all information in the frame is printed. */
1385 static void
1386 frame_info (char *addr_exp, int from_tty)
1388 struct frame_info *fi;
1389 struct symtab_and_line sal;
1390 struct symbol *func;
1391 struct symtab *s;
1392 struct frame_info *calling_frame_info;
1393 int numregs;
1394 const char *funname = 0;
1395 enum language funlang = language_unknown;
1396 const char *pc_regname;
1397 int selected_frame_p;
1398 struct gdbarch *gdbarch;
1399 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
1400 CORE_ADDR frame_pc;
1401 int frame_pc_p;
1402 CORE_ADDR caller_pc;
1404 fi = parse_frame_specification_1 (addr_exp, "No stack.", &selected_frame_p);
1405 gdbarch = get_frame_arch (fi);
1407 /* Name of the value returned by get_frame_pc(). Per comments, "pc"
1408 is not a good name. */
1409 if (gdbarch_pc_regnum (gdbarch) >= 0)
1410 /* OK, this is weird. The gdbarch_pc_regnum hardware register's value can
1411 easily not match that of the internal value returned by
1412 get_frame_pc(). */
1413 pc_regname = gdbarch_register_name (gdbarch, gdbarch_pc_regnum (gdbarch));
1414 else
1415 /* But then, this is weird to. Even without gdbarch_pc_regnum, an
1416 architectures will often have a hardware register called "pc",
1417 and that register's value, again, can easily not match
1418 get_frame_pc(). */
1419 pc_regname = "pc";
1421 frame_pc_p = get_frame_pc_if_available (fi, &frame_pc);
1422 find_frame_sal (fi, &sal);
1423 func = get_frame_function (fi);
1424 s = sal.symtab;
1425 if (func)
1427 funname = SYMBOL_PRINT_NAME (func);
1428 funlang = SYMBOL_LANGUAGE (func);
1429 if (funlang == language_cplus)
1431 /* It seems appropriate to use SYMBOL_PRINT_NAME() here,
1432 to display the demangled name that we already have
1433 stored in the symbol table, but we stored a version
1434 with DMGL_PARAMS turned on, and here we don't want to
1435 display parameters. So remove the parameters. */
1436 char *func_only = cp_remove_params (funname);
1438 if (func_only)
1440 funname = func_only;
1441 make_cleanup (xfree, func_only);
1445 else if (frame_pc_p)
1447 struct minimal_symbol *msymbol;
1449 msymbol = lookup_minimal_symbol_by_pc (frame_pc);
1450 if (msymbol != NULL)
1452 funname = SYMBOL_PRINT_NAME (msymbol);
1453 funlang = SYMBOL_LANGUAGE (msymbol);
1456 calling_frame_info = get_prev_frame (fi);
1458 if (selected_frame_p && frame_relative_level (fi) >= 0)
1460 printf_filtered (_("Stack level %d, frame at "),
1461 frame_relative_level (fi));
1463 else
1465 printf_filtered (_("Stack frame at "));
1467 fputs_filtered (paddress (gdbarch, get_frame_base (fi)), gdb_stdout);
1468 printf_filtered (":\n");
1469 printf_filtered (" %s = ", pc_regname);
1470 if (frame_pc_p)
1471 fputs_filtered (paddress (gdbarch, get_frame_pc (fi)), gdb_stdout);
1472 else
1473 fputs_filtered ("<unavailable>", gdb_stdout);
1475 wrap_here (" ");
1476 if (funname)
1478 printf_filtered (" in ");
1479 fprintf_symbol_filtered (gdb_stdout, funname, funlang,
1480 DMGL_ANSI | DMGL_PARAMS);
1482 wrap_here (" ");
1483 if (sal.symtab)
1484 printf_filtered (" (%s:%d)", symtab_to_filename_for_display (sal.symtab),
1485 sal.line);
1486 puts_filtered ("; ");
1487 wrap_here (" ");
1488 printf_filtered ("saved %s ", pc_regname);
1489 if (frame_unwind_caller_pc_if_available (fi, &caller_pc))
1490 fputs_filtered (paddress (gdbarch, caller_pc), gdb_stdout);
1491 else
1492 fputs_filtered ("<unavailable>", gdb_stdout);
1493 printf_filtered ("\n");
1495 if (calling_frame_info == NULL)
1497 enum unwind_stop_reason reason;
1499 reason = get_frame_unwind_stop_reason (fi);
1500 if (reason != UNWIND_NO_REASON)
1501 printf_filtered (_(" Outermost frame: %s\n"),
1502 frame_stop_reason_string (reason));
1504 else if (get_frame_type (fi) == TAILCALL_FRAME)
1505 puts_filtered (" tail call frame");
1506 else if (get_frame_type (fi) == INLINE_FRAME)
1507 printf_filtered (" inlined into frame %d",
1508 frame_relative_level (get_prev_frame (fi)));
1509 else
1511 printf_filtered (" called by frame at ");
1512 fputs_filtered (paddress (gdbarch, get_frame_base (calling_frame_info)),
1513 gdb_stdout);
1515 if (get_next_frame (fi) && calling_frame_info)
1516 puts_filtered (",");
1517 wrap_here (" ");
1518 if (get_next_frame (fi))
1520 printf_filtered (" caller of frame at ");
1521 fputs_filtered (paddress (gdbarch, get_frame_base (get_next_frame (fi))),
1522 gdb_stdout);
1524 if (get_next_frame (fi) || calling_frame_info)
1525 puts_filtered ("\n");
1527 if (s)
1528 printf_filtered (" source language %s.\n",
1529 language_str (s->language));
1532 /* Address of the argument list for this frame, or 0. */
1533 CORE_ADDR arg_list = get_frame_args_address (fi);
1534 /* Number of args for this frame, or -1 if unknown. */
1535 int numargs;
1537 if (arg_list == 0)
1538 printf_filtered (" Arglist at unknown address.\n");
1539 else
1541 printf_filtered (" Arglist at ");
1542 fputs_filtered (paddress (gdbarch, arg_list), gdb_stdout);
1543 printf_filtered (",");
1545 if (!gdbarch_frame_num_args_p (gdbarch))
1547 numargs = -1;
1548 puts_filtered (" args: ");
1550 else
1552 numargs = gdbarch_frame_num_args (gdbarch, fi);
1553 gdb_assert (numargs >= 0);
1554 if (numargs == 0)
1555 puts_filtered (" no args.");
1556 else if (numargs == 1)
1557 puts_filtered (" 1 arg: ");
1558 else
1559 printf_filtered (" %d args: ", numargs);
1561 print_frame_args (func, fi, numargs, gdb_stdout);
1562 puts_filtered ("\n");
1566 /* Address of the local variables for this frame, or 0. */
1567 CORE_ADDR arg_list = get_frame_locals_address (fi);
1569 if (arg_list == 0)
1570 printf_filtered (" Locals at unknown address,");
1571 else
1573 printf_filtered (" Locals at ");
1574 fputs_filtered (paddress (gdbarch, arg_list), gdb_stdout);
1575 printf_filtered (",");
1579 /* Print as much information as possible on the location of all the
1580 registers. */
1582 enum lval_type lval;
1583 int optimized;
1584 int unavailable;
1585 CORE_ADDR addr;
1586 int realnum;
1587 int count;
1588 int i;
1589 int need_nl = 1;
1591 /* The sp is special; what's displayed isn't the save address, but
1592 the value of the previous frame's sp. This is a legacy thing,
1593 at one stage the frame cached the previous frame's SP instead
1594 of its address, hence it was easiest to just display the cached
1595 value. */
1596 if (gdbarch_sp_regnum (gdbarch) >= 0)
1598 /* Find out the location of the saved stack pointer with out
1599 actually evaluating it. */
1600 frame_register_unwind (fi, gdbarch_sp_regnum (gdbarch),
1601 &optimized, &unavailable, &lval, &addr,
1602 &realnum, NULL);
1603 if (!optimized && !unavailable && lval == not_lval)
1605 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1606 int sp_size = register_size (gdbarch, gdbarch_sp_regnum (gdbarch));
1607 gdb_byte value[MAX_REGISTER_SIZE];
1608 CORE_ADDR sp;
1610 frame_register_unwind (fi, gdbarch_sp_regnum (gdbarch),
1611 &optimized, &unavailable, &lval, &addr,
1612 &realnum, value);
1613 /* NOTE: cagney/2003-05-22: This is assuming that the
1614 stack pointer was packed as an unsigned integer. That
1615 may or may not be valid. */
1616 sp = extract_unsigned_integer (value, sp_size, byte_order);
1617 printf_filtered (" Previous frame's sp is ");
1618 fputs_filtered (paddress (gdbarch, sp), gdb_stdout);
1619 printf_filtered ("\n");
1620 need_nl = 0;
1622 else if (!optimized && !unavailable && lval == lval_memory)
1624 printf_filtered (" Previous frame's sp at ");
1625 fputs_filtered (paddress (gdbarch, addr), gdb_stdout);
1626 printf_filtered ("\n");
1627 need_nl = 0;
1629 else if (!optimized && !unavailable && lval == lval_register)
1631 printf_filtered (" Previous frame's sp in %s\n",
1632 gdbarch_register_name (gdbarch, realnum));
1633 need_nl = 0;
1635 /* else keep quiet. */
1638 count = 0;
1639 numregs = gdbarch_num_regs (gdbarch)
1640 + gdbarch_num_pseudo_regs (gdbarch);
1641 for (i = 0; i < numregs; i++)
1642 if (i != gdbarch_sp_regnum (gdbarch)
1643 && gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
1645 /* Find out the location of the saved register without
1646 fetching the corresponding value. */
1647 frame_register_unwind (fi, i, &optimized, &unavailable,
1648 &lval, &addr, &realnum, NULL);
1649 /* For moment, only display registers that were saved on the
1650 stack. */
1651 if (!optimized && !unavailable && lval == lval_memory)
1653 if (count == 0)
1654 puts_filtered (" Saved registers:\n ");
1655 else
1656 puts_filtered (",");
1657 wrap_here (" ");
1658 printf_filtered (" %s at ",
1659 gdbarch_register_name (gdbarch, i));
1660 fputs_filtered (paddress (gdbarch, addr), gdb_stdout);
1661 count++;
1664 if (count || need_nl)
1665 puts_filtered ("\n");
1668 do_cleanups (back_to);
1671 /* Print briefly all stack frames or just the innermost COUNT_EXP
1672 frames. */
1674 static void
1675 backtrace_command_1 (char *count_exp, int show_locals, int from_tty)
1677 struct frame_info *fi;
1678 int count;
1679 int i;
1680 struct frame_info *trailing;
1681 int trailing_level;
1683 if (!target_has_stack)
1684 error (_("No stack."));
1686 /* The following code must do two things. First, it must set the
1687 variable TRAILING to the frame from which we should start
1688 printing. Second, it must set the variable count to the number
1689 of frames which we should print, or -1 if all of them. */
1690 trailing = get_current_frame ();
1692 trailing_level = 0;
1693 if (count_exp)
1695 count = parse_and_eval_long (count_exp);
1696 if (count < 0)
1698 struct frame_info *current;
1700 count = -count;
1702 current = trailing;
1703 while (current && count--)
1705 QUIT;
1706 current = get_prev_frame (current);
1709 /* Will stop when CURRENT reaches the top of the stack.
1710 TRAILING will be COUNT below it. */
1711 while (current)
1713 QUIT;
1714 trailing = get_prev_frame (trailing);
1715 current = get_prev_frame (current);
1716 trailing_level++;
1719 count = -1;
1722 else
1723 count = -1;
1725 if (info_verbose)
1727 /* Read in symbols for all of the frames. Need to do this in a
1728 separate pass so that "Reading in symbols for xxx" messages
1729 don't screw up the appearance of the backtrace. Also if
1730 people have strong opinions against reading symbols for
1731 backtrace this may have to be an option. */
1732 i = count;
1733 for (fi = trailing; fi != NULL && i--; fi = get_prev_frame (fi))
1735 CORE_ADDR pc;
1737 QUIT;
1738 pc = get_frame_address_in_block (fi);
1739 find_pc_sect_symtab_via_partial (pc, find_pc_mapped_section (pc));
1743 for (i = 0, fi = trailing; fi && count--; i++, fi = get_prev_frame (fi))
1745 QUIT;
1747 /* Don't use print_stack_frame; if an error() occurs it probably
1748 means further attempts to backtrace would fail (on the other
1749 hand, perhaps the code does or could be fixed to make sure
1750 the frame->prev field gets set to NULL in that case). */
1751 print_frame_info (fi, 1, LOCATION, 1);
1752 if (show_locals)
1754 struct frame_id frame_id = get_frame_id (fi);
1756 print_frame_local_vars (fi, 1, gdb_stdout);
1758 /* print_frame_local_vars invalidates FI. */
1759 fi = frame_find_by_id (frame_id);
1760 if (fi == NULL)
1762 trailing = NULL;
1763 warning (_("Unable to restore previously selected frame."));
1764 break;
1768 /* Save the last frame to check for error conditions. */
1769 trailing = fi;
1772 /* If we've stopped before the end, mention that. */
1773 if (fi && from_tty)
1774 printf_filtered (_("(More stack frames follow...)\n"));
1776 /* If we've run out of frames, and the reason appears to be an error
1777 condition, print it. */
1778 if (fi == NULL && trailing != NULL)
1780 enum unwind_stop_reason reason;
1782 reason = get_frame_unwind_stop_reason (trailing);
1783 if (reason >= UNWIND_FIRST_ERROR)
1784 printf_filtered (_("Backtrace stopped: %s\n"),
1785 frame_stop_reason_string (reason));
1789 static void
1790 backtrace_command (char *arg, int from_tty)
1792 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
1793 int fulltrace_arg = -1, arglen = 0, argc = 0;
1795 if (arg)
1797 char **argv;
1798 int i;
1800 argv = gdb_buildargv (arg);
1801 make_cleanup_freeargv (argv);
1802 argc = 0;
1803 for (i = 0; argv[i]; i++)
1805 unsigned int j;
1807 for (j = 0; j < strlen (argv[i]); j++)
1808 argv[i][j] = tolower (argv[i][j]);
1810 if (fulltrace_arg < 0 && subset_compare (argv[i], "full"))
1811 fulltrace_arg = argc;
1812 else
1814 arglen += strlen (argv[i]);
1815 argc++;
1818 arglen += argc;
1819 if (fulltrace_arg >= 0)
1821 if (arglen > 0)
1823 arg = xmalloc (arglen + 1);
1824 make_cleanup (xfree, arg);
1825 arg[0] = 0;
1826 for (i = 0; i < (argc + 1); i++)
1828 if (i != fulltrace_arg)
1830 strcat (arg, argv[i]);
1831 strcat (arg, " ");
1835 else
1836 arg = NULL;
1840 backtrace_command_1 (arg, fulltrace_arg >= 0 /* show_locals */, from_tty);
1842 do_cleanups (old_chain);
1845 static void
1846 backtrace_full_command (char *arg, int from_tty)
1848 backtrace_command_1 (arg, 1 /* show_locals */, from_tty);
1852 /* Iterate over the local variables of a block B, calling CB with
1853 CB_DATA. */
1855 static void
1856 iterate_over_block_locals (struct block *b,
1857 iterate_over_block_arg_local_vars_cb cb,
1858 void *cb_data)
1860 struct block_iterator iter;
1861 struct symbol *sym;
1863 ALL_BLOCK_SYMBOLS (b, iter, sym)
1865 switch (SYMBOL_CLASS (sym))
1867 case LOC_LOCAL:
1868 case LOC_REGISTER:
1869 case LOC_STATIC:
1870 case LOC_COMPUTED:
1871 if (SYMBOL_IS_ARGUMENT (sym))
1872 break;
1873 if (SYMBOL_DOMAIN (sym) == COMMON_BLOCK_DOMAIN)
1874 break;
1875 (*cb) (SYMBOL_PRINT_NAME (sym), sym, cb_data);
1876 break;
1878 default:
1879 /* Ignore symbols which are not locals. */
1880 break;
1886 /* Same, but print labels. */
1888 #if 0
1889 /* Commented out, as the code using this function has also been
1890 commented out. FIXME:brobecker/2009-01-13: Find out why the code
1891 was commented out in the first place. The discussion introducing
1892 this change (2007-12-04: Support lexical blocks and function bodies
1893 that occupy non-contiguous address ranges) did not explain why
1894 this change was made. */
1895 static int
1896 print_block_frame_labels (struct gdbarch *gdbarch, struct block *b,
1897 int *have_default, struct ui_file *stream)
1899 struct block_iterator iter;
1900 struct symbol *sym;
1901 int values_printed = 0;
1903 ALL_BLOCK_SYMBOLS (b, iter, sym)
1905 if (strcmp (SYMBOL_LINKAGE_NAME (sym), "default") == 0)
1907 if (*have_default)
1908 continue;
1909 *have_default = 1;
1911 if (SYMBOL_CLASS (sym) == LOC_LABEL)
1913 struct symtab_and_line sal;
1914 struct value_print_options opts;
1916 sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
1917 values_printed = 1;
1918 fputs_filtered (SYMBOL_PRINT_NAME (sym), stream);
1919 get_user_print_options (&opts);
1920 if (opts.addressprint)
1922 fprintf_filtered (stream, " ");
1923 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (sym)),
1924 stream);
1926 fprintf_filtered (stream, " in file %s, line %d\n",
1927 sal.symtab->filename, sal.line);
1931 return values_printed;
1933 #endif
1935 /* Iterate over all the local variables in block B, including all its
1936 superblocks, stopping when the top-level block is reached. */
1938 void
1939 iterate_over_block_local_vars (struct block *block,
1940 iterate_over_block_arg_local_vars_cb cb,
1941 void *cb_data)
1943 while (block)
1945 iterate_over_block_locals (block, cb, cb_data);
1946 /* After handling the function's top-level block, stop. Don't
1947 continue to its superblock, the block of per-file
1948 symbols. */
1949 if (BLOCK_FUNCTION (block))
1950 break;
1951 block = BLOCK_SUPERBLOCK (block);
1955 /* Data to be passed around in the calls to the locals and args
1956 iterators. */
1958 struct print_variable_and_value_data
1960 struct frame_id frame_id;
1961 int num_tabs;
1962 struct ui_file *stream;
1963 int values_printed;
1966 /* The callback for the locals and args iterators. */
1968 static void
1969 do_print_variable_and_value (const char *print_name,
1970 struct symbol *sym,
1971 void *cb_data)
1973 struct print_variable_and_value_data *p = cb_data;
1974 struct frame_info *frame;
1976 frame = frame_find_by_id (p->frame_id);
1977 if (frame == NULL)
1979 warning (_("Unable to restore previously selected frame."));
1980 return;
1983 print_variable_and_value (print_name, sym, frame, p->stream, p->num_tabs);
1985 /* print_variable_and_value invalidates FRAME. */
1986 frame = NULL;
1988 p->values_printed = 1;
1991 /* Print all variables from the innermost up to the function block of FRAME.
1992 Print them with values to STREAM indented by NUM_TABS.
1994 This function will invalidate FRAME. */
1996 static void
1997 print_frame_local_vars (struct frame_info *frame, int num_tabs,
1998 struct ui_file *stream)
2000 struct print_variable_and_value_data cb_data;
2001 struct block *block;
2002 CORE_ADDR pc;
2004 if (!get_frame_pc_if_available (frame, &pc))
2006 fprintf_filtered (stream,
2007 _("PC unavailable, cannot determine locals.\n"));
2008 return;
2011 block = get_frame_block (frame, 0);
2012 if (block == 0)
2014 fprintf_filtered (stream, "No symbol table info available.\n");
2015 return;
2018 cb_data.frame_id = get_frame_id (frame);
2019 cb_data.num_tabs = 4 * num_tabs;
2020 cb_data.stream = stream;
2021 cb_data.values_printed = 0;
2023 iterate_over_block_local_vars (block,
2024 do_print_variable_and_value,
2025 &cb_data);
2027 /* do_print_variable_and_value invalidates FRAME. */
2028 frame = NULL;
2030 if (!cb_data.values_printed)
2031 fprintf_filtered (stream, _("No locals.\n"));
2034 void
2035 locals_info (char *args, int from_tty)
2037 print_frame_local_vars (get_selected_frame (_("No frame selected.")),
2038 0, gdb_stdout);
2041 /* Iterate over all the argument variables in block B.
2043 Returns 1 if any argument was walked; 0 otherwise. */
2045 void
2046 iterate_over_block_arg_vars (struct block *b,
2047 iterate_over_block_arg_local_vars_cb cb,
2048 void *cb_data)
2050 struct block_iterator iter;
2051 struct symbol *sym, *sym2;
2053 ALL_BLOCK_SYMBOLS (b, iter, sym)
2055 /* Don't worry about things which aren't arguments. */
2056 if (SYMBOL_IS_ARGUMENT (sym))
2058 /* We have to look up the symbol because arguments can have
2059 two entries (one a parameter, one a local) and the one we
2060 want is the local, which lookup_symbol will find for us.
2061 This includes gcc1 (not gcc2) on the sparc when passing a
2062 small structure and gcc2 when the argument type is float
2063 and it is passed as a double and converted to float by
2064 the prologue (in the latter case the type of the LOC_ARG
2065 symbol is double and the type of the LOC_LOCAL symbol is
2066 float). There are also LOC_ARG/LOC_REGISTER pairs which
2067 are not combined in symbol-reading. */
2069 sym2 = lookup_symbol (SYMBOL_LINKAGE_NAME (sym),
2070 b, VAR_DOMAIN, NULL);
2071 (*cb) (SYMBOL_PRINT_NAME (sym), sym2, cb_data);
2076 /* Print all argument variables of the function of FRAME.
2077 Print them with values to STREAM.
2079 This function will invalidate FRAME. */
2081 static void
2082 print_frame_arg_vars (struct frame_info *frame, struct ui_file *stream)
2084 struct print_variable_and_value_data cb_data;
2085 struct symbol *func;
2086 CORE_ADDR pc;
2088 if (!get_frame_pc_if_available (frame, &pc))
2090 fprintf_filtered (stream, _("PC unavailable, cannot determine args.\n"));
2091 return;
2094 func = get_frame_function (frame);
2095 if (func == NULL)
2097 fprintf_filtered (stream, _("No symbol table info available.\n"));
2098 return;
2101 cb_data.frame_id = get_frame_id (frame);
2102 cb_data.num_tabs = 0;
2103 cb_data.stream = gdb_stdout;
2104 cb_data.values_printed = 0;
2106 iterate_over_block_arg_vars (SYMBOL_BLOCK_VALUE (func),
2107 do_print_variable_and_value, &cb_data);
2109 /* do_print_variable_and_value invalidates FRAME. */
2110 frame = NULL;
2112 if (!cb_data.values_printed)
2113 fprintf_filtered (stream, _("No arguments.\n"));
2116 void
2117 args_info (char *ignore, int from_tty)
2119 print_frame_arg_vars (get_selected_frame (_("No frame selected.")),
2120 gdb_stdout);
2124 static void
2125 args_plus_locals_info (char *ignore, int from_tty)
2127 args_info (ignore, from_tty);
2128 locals_info (ignore, from_tty);
2132 /* Select frame FRAME. Also print the stack frame and show the source
2133 if this is the tui version. */
2134 static void
2135 select_and_print_frame (struct frame_info *frame)
2137 select_frame (frame);
2138 if (frame)
2139 print_stack_frame (frame, 1, SRC_AND_LOC);
2142 /* Return the symbol-block in which the selected frame is executing.
2143 Can return zero under various legitimate circumstances.
2145 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the relevant
2146 code address within the block returned. We use this to decide
2147 which macros are in scope. */
2149 struct block *
2150 get_selected_block (CORE_ADDR *addr_in_block)
2152 if (!has_stack_frames ())
2153 return 0;
2155 return get_frame_block (get_selected_frame (NULL), addr_in_block);
2158 /* Find a frame a certain number of levels away from FRAME.
2159 LEVEL_OFFSET_PTR points to an int containing the number of levels.
2160 Positive means go to earlier frames (up); negative, the reverse.
2161 The int that contains the number of levels is counted toward
2162 zero as the frames for those levels are found.
2163 If the top or bottom frame is reached, that frame is returned,
2164 but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
2165 how much farther the original request asked to go. */
2167 struct frame_info *
2168 find_relative_frame (struct frame_info *frame, int *level_offset_ptr)
2170 /* Going up is simple: just call get_prev_frame enough times or
2171 until the initial frame is reached. */
2172 while (*level_offset_ptr > 0)
2174 struct frame_info *prev = get_prev_frame (frame);
2176 if (!prev)
2177 break;
2178 (*level_offset_ptr)--;
2179 frame = prev;
2182 /* Going down is just as simple. */
2183 while (*level_offset_ptr < 0)
2185 struct frame_info *next = get_next_frame (frame);
2187 if (!next)
2188 break;
2189 (*level_offset_ptr)++;
2190 frame = next;
2193 return frame;
2196 /* The "select_frame" command. With no argument this is a NOP.
2197 Select the frame at level LEVEL_EXP if it is a valid level.
2198 Otherwise, treat LEVEL_EXP as an address expression and select it.
2200 See parse_frame_specification for more info on proper frame
2201 expressions. */
2203 void
2204 select_frame_command (char *level_exp, int from_tty)
2206 select_frame (parse_frame_specification_1 (level_exp, "No stack.", NULL));
2209 /* The "frame" command. With no argument, print the selected frame
2210 briefly. With an argument, behave like select_frame and then print
2211 the selected frame. */
2213 static void
2214 frame_command (char *level_exp, int from_tty)
2216 select_frame_command (level_exp, from_tty);
2217 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2220 /* The XDB Compatibility command to print the current frame. */
2222 static void
2223 current_frame_command (char *level_exp, int from_tty)
2225 print_stack_frame (get_selected_frame (_("No stack.")), 1, SRC_AND_LOC);
2228 /* Select the frame up one or COUNT_EXP stack levels from the
2229 previously selected frame, and print it briefly. */
2231 static void
2232 up_silently_base (char *count_exp)
2234 struct frame_info *frame;
2235 int count = 1;
2237 if (count_exp)
2238 count = parse_and_eval_long (count_exp);
2240 frame = find_relative_frame (get_selected_frame ("No stack."), &count);
2241 if (count != 0 && count_exp == NULL)
2242 error (_("Initial frame selected; you cannot go up."));
2243 select_frame (frame);
2246 static void
2247 up_silently_command (char *count_exp, int from_tty)
2249 up_silently_base (count_exp);
2252 static void
2253 up_command (char *count_exp, int from_tty)
2255 up_silently_base (count_exp);
2256 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2259 /* Select the frame down one or COUNT_EXP stack levels from the previously
2260 selected frame, and print it briefly. */
2262 static void
2263 down_silently_base (char *count_exp)
2265 struct frame_info *frame;
2266 int count = -1;
2268 if (count_exp)
2269 count = -parse_and_eval_long (count_exp);
2271 frame = find_relative_frame (get_selected_frame ("No stack."), &count);
2272 if (count != 0 && count_exp == NULL)
2274 /* We only do this if COUNT_EXP is not specified. That way
2275 "down" means to really go down (and let me know if that is
2276 impossible), but "down 9999" can be used to mean go all the
2277 way down without getting an error. */
2279 error (_("Bottom (innermost) frame selected; you cannot go down."));
2282 select_frame (frame);
2285 static void
2286 down_silently_command (char *count_exp, int from_tty)
2288 down_silently_base (count_exp);
2291 static void
2292 down_command (char *count_exp, int from_tty)
2294 down_silently_base (count_exp);
2295 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2299 void
2300 return_command (char *retval_exp, int from_tty)
2302 /* Initialize it just to avoid a GCC false warning. */
2303 enum return_value_convention rv_conv = RETURN_VALUE_STRUCT_CONVENTION;
2304 struct frame_info *thisframe;
2305 struct gdbarch *gdbarch;
2306 struct symbol *thisfun;
2307 struct value *return_value = NULL;
2308 struct value *function = NULL;
2309 const char *query_prefix = "";
2311 thisframe = get_selected_frame ("No selected frame.");
2312 thisfun = get_frame_function (thisframe);
2313 gdbarch = get_frame_arch (thisframe);
2315 if (get_frame_type (get_current_frame ()) == INLINE_FRAME)
2316 error (_("Can not force return from an inlined function."));
2318 /* Compute the return value. If the computation triggers an error,
2319 let it bail. If the return type can't be handled, set
2320 RETURN_VALUE to NULL, and QUERY_PREFIX to an informational
2321 message. */
2322 if (retval_exp)
2324 struct expression *retval_expr = parse_expression (retval_exp);
2325 struct cleanup *old_chain = make_cleanup (xfree, retval_expr);
2326 struct type *return_type = NULL;
2328 /* Compute the return value. Should the computation fail, this
2329 call throws an error. */
2330 return_value = evaluate_expression (retval_expr);
2332 /* Cast return value to the return type of the function. Should
2333 the cast fail, this call throws an error. */
2334 if (thisfun != NULL)
2335 return_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (thisfun));
2336 if (return_type == NULL)
2338 if (retval_expr->elts[0].opcode != UNOP_CAST
2339 && retval_expr->elts[0].opcode != UNOP_CAST_TYPE)
2340 error (_("Return value type not available for selected "
2341 "stack frame.\n"
2342 "Please use an explicit cast of the value to return."));
2343 return_type = value_type (return_value);
2345 do_cleanups (old_chain);
2346 CHECK_TYPEDEF (return_type);
2347 return_value = value_cast (return_type, return_value);
2349 /* Make sure the value is fully evaluated. It may live in the
2350 stack frame we're about to pop. */
2351 if (value_lazy (return_value))
2352 value_fetch_lazy (return_value);
2354 if (thisfun != NULL)
2355 function = read_var_value (thisfun, thisframe);
2357 rv_conv = RETURN_VALUE_REGISTER_CONVENTION;
2358 if (TYPE_CODE (return_type) == TYPE_CODE_VOID)
2359 /* If the return-type is "void", don't try to find the
2360 return-value's location. However, do still evaluate the
2361 return expression so that, even when the expression result
2362 is discarded, side effects such as "return i++" still
2363 occur. */
2364 return_value = NULL;
2365 else if (thisfun != NULL)
2367 rv_conv = struct_return_convention (gdbarch, function, return_type);
2368 if (rv_conv == RETURN_VALUE_STRUCT_CONVENTION
2369 || rv_conv == RETURN_VALUE_ABI_RETURNS_ADDRESS)
2371 query_prefix = "The location at which to store the "
2372 "function's return value is unknown.\n"
2373 "If you continue, the return value "
2374 "that you specified will be ignored.\n";
2375 return_value = NULL;
2380 /* Does an interactive user really want to do this? Include
2381 information, such as how well GDB can handle the return value, in
2382 the query message. */
2383 if (from_tty)
2385 int confirmed;
2387 if (thisfun == NULL)
2388 confirmed = query (_("%sMake selected stack frame return now? "),
2389 query_prefix);
2390 else
2391 confirmed = query (_("%sMake %s return now? "), query_prefix,
2392 SYMBOL_PRINT_NAME (thisfun));
2393 if (!confirmed)
2394 error (_("Not confirmed"));
2397 /* Discard the selected frame and all frames inner-to it. */
2398 frame_pop (get_selected_frame (NULL));
2400 /* Store RETURN_VALUE in the just-returned register set. */
2401 if (return_value != NULL)
2403 struct type *return_type = value_type (return_value);
2404 struct gdbarch *gdbarch = get_regcache_arch (get_current_regcache ());
2406 gdb_assert (rv_conv != RETURN_VALUE_STRUCT_CONVENTION
2407 && rv_conv != RETURN_VALUE_ABI_RETURNS_ADDRESS);
2408 gdbarch_return_value (gdbarch, function, return_type,
2409 get_current_regcache (), NULL /*read*/,
2410 value_contents (return_value) /*write*/);
2413 /* If we are at the end of a call dummy now, pop the dummy frame
2414 too. */
2415 if (get_frame_type (get_current_frame ()) == DUMMY_FRAME)
2416 frame_pop (get_current_frame ());
2418 /* If interactive, print the frame that is now current. */
2419 if (from_tty)
2420 frame_command ("0", 1);
2421 else
2422 select_frame_command ("0", 0);
2425 /* Sets the scope to input function name, provided that the function
2426 is within the current stack frame. */
2428 struct function_bounds
2430 CORE_ADDR low, high;
2433 static void
2434 func_command (char *arg, int from_tty)
2436 struct frame_info *frame;
2437 int found = 0;
2438 struct symtabs_and_lines sals;
2439 int i;
2440 int level = 1;
2441 struct function_bounds *func_bounds = NULL;
2442 struct cleanup *cleanups;
2444 if (arg != NULL)
2445 return;
2447 frame = parse_frame_specification ("0");
2448 sals = decode_line_with_current_source (arg, DECODE_LINE_FUNFIRSTLINE);
2449 cleanups = make_cleanup (xfree, sals.sals);
2450 func_bounds = (struct function_bounds *) xmalloc (
2451 sizeof (struct function_bounds) * sals.nelts);
2452 make_cleanup (xfree, func_bounds);
2453 for (i = 0; (i < sals.nelts && !found); i++)
2455 if (sals.sals[i].pspace != current_program_space)
2456 func_bounds[i].low = func_bounds[i].high = 0;
2457 else if (sals.sals[i].pc == 0
2458 || find_pc_partial_function (sals.sals[i].pc, NULL,
2459 &func_bounds[i].low,
2460 &func_bounds[i].high) == 0)
2462 func_bounds[i].low = func_bounds[i].high = 0;
2468 for (i = 0; (i < sals.nelts && !found); i++)
2469 found = (get_frame_pc (frame) >= func_bounds[i].low
2470 && get_frame_pc (frame) < func_bounds[i].high);
2471 if (!found)
2473 level = 1;
2474 frame = find_relative_frame (frame, &level);
2477 while (!found && level == 0);
2479 do_cleanups (cleanups);
2481 if (!found)
2482 printf_filtered (_("'%s' not within current stack frame.\n"), arg);
2483 else if (frame != get_selected_frame (NULL))
2484 select_and_print_frame (frame);
2487 /* Gets the language of the current frame. */
2489 enum language
2490 get_frame_language (void)
2492 struct frame_info *frame = deprecated_safe_get_selected_frame ();
2494 if (frame)
2496 volatile struct gdb_exception ex;
2497 CORE_ADDR pc = 0;
2498 struct symtab *s;
2500 /* We determine the current frame language by looking up its
2501 associated symtab. To retrieve this symtab, we use the frame
2502 PC. However we cannot use the frame PC as is, because it
2503 usually points to the instruction following the "call", which
2504 is sometimes the first instruction of another function. So
2505 we rely on get_frame_address_in_block(), it provides us with
2506 a PC that is guaranteed to be inside the frame's code
2507 block. */
2509 TRY_CATCH (ex, RETURN_MASK_ERROR)
2511 pc = get_frame_address_in_block (frame);
2513 if (ex.reason < 0)
2515 if (ex.error != NOT_AVAILABLE_ERROR)
2516 throw_exception (ex);
2518 else
2520 s = find_pc_symtab (pc);
2521 if (s != NULL)
2522 return s->language;
2526 return language_unknown;
2530 /* Provide a prototype to silence -Wmissing-prototypes. */
2531 void _initialize_stack (void);
2533 void
2534 _initialize_stack (void)
2536 add_com ("return", class_stack, return_command, _("\
2537 Make selected stack frame return to its caller.\n\
2538 Control remains in the debugger, but when you continue\n\
2539 execution will resume in the frame above the one now selected.\n\
2540 If an argument is given, it is an expression for the value to return."));
2542 add_com ("up", class_stack, up_command, _("\
2543 Select and print stack frame that called this one.\n\
2544 An argument says how many frames up to go."));
2545 add_com ("up-silently", class_support, up_silently_command, _("\
2546 Same as the `up' command, but does not print anything.\n\
2547 This is useful in command scripts."));
2549 add_com ("down", class_stack, down_command, _("\
2550 Select and print stack frame called by this one.\n\
2551 An argument says how many frames down to go."));
2552 add_com_alias ("do", "down", class_stack, 1);
2553 add_com_alias ("dow", "down", class_stack, 1);
2554 add_com ("down-silently", class_support, down_silently_command, _("\
2555 Same as the `down' command, but does not print anything.\n\
2556 This is useful in command scripts."));
2558 add_com ("frame", class_stack, frame_command, _("\
2559 Select and print a stack frame.\nWith no argument, \
2560 print the selected stack frame. (See also \"info frame\").\n\
2561 An argument specifies the frame to select.\n\
2562 It can be a stack frame number or the address of the frame.\n\
2563 With argument, nothing is printed if input is coming from\n\
2564 a command file or a user-defined command."));
2566 add_com_alias ("f", "frame", class_stack, 1);
2568 if (xdb_commands)
2570 add_com ("L", class_stack, current_frame_command,
2571 _("Print the current stack frame.\n"));
2572 add_com_alias ("V", "frame", class_stack, 1);
2574 add_com ("select-frame", class_stack, select_frame_command, _("\
2575 Select a stack frame without printing anything.\n\
2576 An argument specifies the frame to select.\n\
2577 It can be a stack frame number or the address of the frame.\n"));
2579 add_com ("backtrace", class_stack, backtrace_command, _("\
2580 Print backtrace of all stack frames, or innermost COUNT frames.\n\
2581 With a negative argument, print outermost -COUNT frames.\nUse of the \
2582 'full' qualifier also prints the values of the local variables.\n"));
2583 add_com_alias ("bt", "backtrace", class_stack, 0);
2584 if (xdb_commands)
2586 add_com_alias ("t", "backtrace", class_stack, 0);
2587 add_com ("T", class_stack, backtrace_full_command, _("\
2588 Print backtrace of all stack frames, or innermost COUNT frames\n\
2589 and the values of the local variables.\n\
2590 With a negative argument, print outermost -COUNT frames.\n\
2591 Usage: T <count>\n"));
2594 add_com_alias ("where", "backtrace", class_alias, 0);
2595 add_info ("stack", backtrace_command,
2596 _("Backtrace of the stack, or innermost COUNT frames."));
2597 add_info_alias ("s", "stack", 1);
2598 add_info ("frame", frame_info,
2599 _("All about selected stack frame, or frame at ADDR."));
2600 add_info_alias ("f", "frame", 1);
2601 add_info ("locals", locals_info,
2602 _("Local variables of current stack frame."));
2603 add_info ("args", args_info,
2604 _("Argument variables of current stack frame."));
2605 if (xdb_commands)
2606 add_com ("l", class_info, args_plus_locals_info,
2607 _("Argument and local variables of current stack frame."));
2609 if (dbx_commands)
2610 add_com ("func", class_stack, func_command, _("\
2611 Select the stack frame that contains <func>.\n\
2612 Usage: func <name>\n"));
2614 add_setshow_enum_cmd ("frame-arguments", class_stack,
2615 print_frame_arguments_choices, &print_frame_arguments,
2616 _("Set printing of non-scalar frame arguments"),
2617 _("Show printing of non-scalar frame arguments"),
2618 NULL, NULL, NULL, &setprintlist, &showprintlist);
2620 add_setshow_auto_boolean_cmd ("disassemble-next-line", class_stack,
2621 &disassemble_next_line, _("\
2622 Set whether to disassemble next source line or insn when execution stops."),
2623 _("\
2624 Show whether to disassemble next source line or insn when execution stops."),
2625 _("\
2626 If ON, GDB will display disassembly of the next source line, in addition\n\
2627 to displaying the source line itself. If the next source line cannot\n\
2628 be displayed (e.g., source is unavailable or there's no line info), GDB\n\
2629 will display disassembly of next instruction instead of showing the\n\
2630 source line.\n\
2631 If AUTO, display disassembly of next instruction only if the source line\n\
2632 cannot be displayed.\n\
2633 If OFF (which is the default), never display the disassembly of the next\n\
2634 source line."),
2635 NULL,
2636 show_disassemble_next_line,
2637 &setlist, &showlist);
2638 disassemble_next_line = AUTO_BOOLEAN_FALSE;
2640 add_setshow_enum_cmd ("entry-values", class_stack,
2641 print_entry_values_choices, &print_entry_values,
2642 _("Set printing of function arguments at function "
2643 "entry"),
2644 _("Show printing of function arguments at function "
2645 "entry"),
2646 _("\
2647 GDB can sometimes determine the values of function arguments at entry,\n\
2648 in addition to their current values. This option tells GDB whether\n\
2649 to print the current value, the value at entry (marked as val@entry),\n\
2650 or both. Note that one or both of these values may be <optimized out>."),
2651 NULL, NULL, &setprintlist, &showprintlist);