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