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