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