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