home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / stack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-18  |  40.6 KB  |  1,481 lines

  1. /* Print and select stack frames for GDB, the GNU debugger.
  2.    Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994
  3.    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 2 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, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "value.h"
  23. #include "symtab.h"
  24. #include "gdbtypes.h"
  25. #include "expression.h"
  26. #include "language.h"
  27. #include "frame.h"
  28. #include "gdbcmd.h"
  29. #include "gdbcore.h"
  30. #include "target.h"
  31. #include "breakpoint.h"
  32. #include "demangle.h"
  33. #include "inferior.h"
  34. #include "annotate.h"
  35.  
  36. static void return_command PARAMS ((char *, int));
  37.  
  38. static void down_command PARAMS ((char *, int));
  39.  
  40. static void down_silently_command PARAMS ((char *, int));
  41.  
  42. static void up_command PARAMS ((char *, int));
  43.  
  44. static void up_silently_command PARAMS ((char *, int));
  45.  
  46. static void frame_command PARAMS ((char *, int));
  47.  
  48. static void select_frame_command PARAMS ((char *, int));
  49.  
  50. static void args_info PARAMS ((char *, int));
  51.  
  52. static void print_frame_arg_vars PARAMS ((struct frame_info *, GDB_FILE *));
  53.  
  54. static void catch_info PARAMS ((char *, int));
  55.  
  56. static void locals_info PARAMS ((char *, int));
  57.  
  58. static void print_frame_label_vars PARAMS ((struct frame_info *, int,
  59.                         GDB_FILE *));
  60.  
  61. static void print_frame_local_vars PARAMS ((struct frame_info *, GDB_FILE *));
  62.  
  63. static int print_block_frame_labels PARAMS ((struct block *, int *,
  64.                          GDB_FILE *));
  65.  
  66. static int print_block_frame_locals PARAMS ((struct block *,
  67.                          struct frame_info *,
  68.                          GDB_FILE *));
  69.  
  70. static void backtrace_command PARAMS ((char *, int));
  71.  
  72. static struct frame_info *parse_frame_specification PARAMS ((char *));
  73.  
  74. static void frame_info PARAMS ((char *, int));
  75.  
  76. extern int addressprint;    /* Print addresses, or stay symbolic only? */
  77. extern int info_verbose;    /* Verbosity of symbol reading msgs */
  78. extern int lines_to_list;    /* # of lines "list" command shows by default */
  79.  
  80. /* The "selected" stack frame is used by default for local and arg access.
  81.    May be zero, for no selected frame.  */
  82.  
  83. struct frame_info *selected_frame;
  84.  
  85. /* Level of the selected frame:
  86.    0 for innermost, 1 for its caller, ...
  87.    or -1 for frame specified by address with no defined level.  */
  88.  
  89. int selected_frame_level;
  90.  
  91. /* Zero means do things normally; we are interacting directly with the
  92.    user.  One means print the full filename and linenumber when a
  93.    frame is printed, and do so in a format emacs18/emacs19.22 can
  94.    parse.  Two means print similar annotations, but in many more
  95.    cases and in a slightly different syntax.  */
  96.  
  97. int annotation_level = 0;
  98.  
  99.  
  100. struct print_stack_frame_args {
  101.   struct frame_info *fi;
  102.   int level;
  103.   int source;
  104.   int args;
  105. };
  106.  
  107. static int print_stack_frame_stub PARAMS ((char *));
  108.  
  109. /* Pass the args the way catch_errors wants them.  */
  110. static int
  111. print_stack_frame_stub (args)
  112.      char *args;
  113. {
  114.   struct print_stack_frame_args *p = (struct print_stack_frame_args *)args;
  115.  
  116.   print_frame_info (p->fi, p->level, p->source, p->args);
  117.   return 0;
  118. }
  119.  
  120. /* Print a stack frame briefly.  FRAME_INFI should be the frame info
  121.    and LEVEL should be its level in the stack (or -1 for level not defined).
  122.    This prints the level, the function executing, the arguments,
  123.    and the file name and line number.
  124.    If the pc is not at the beginning of the source line,
  125.    the actual pc is printed at the beginning.
  126.  
  127.    If SOURCE is 1, print the source line as well.
  128.    If SOURCE is -1, print ONLY the source line.  */
  129.  
  130. void
  131. print_stack_frame (fi, level, source)
  132.      struct frame_info *fi;
  133.      int level;
  134.      int source;
  135. {
  136.   struct print_stack_frame_args args;
  137.  
  138.   args.fi = fi;
  139.   args.level = level;
  140.   args.source = source;
  141.   args.args = 1;
  142.  
  143.   catch_errors (print_stack_frame_stub, (char *)&args, "", RETURN_MASK_ERROR);
  144. }
  145.  
  146. struct print_args_args {
  147.   struct symbol *func;
  148.   struct frame_info *fi;
  149. };
  150.  
  151. static int print_args_stub PARAMS ((char *));
  152.  
  153. /* Pass the args the way catch_errors wants them.  */
  154.  
  155. static int
  156. print_args_stub (args)
  157.      char *args;
  158. {
  159.   int numargs;
  160.   struct print_args_args *p = (struct print_args_args *)args;
  161.  
  162.   FRAME_NUM_ARGS (numargs, (p->fi));
  163.   print_frame_args (p->func, p->fi, numargs, gdb_stdout);
  164.   return 0;
  165. }
  166.  
  167. /* LEVEL is the level of the frame, or -1 if it is the innermost frame
  168.    but we don't want to print the level.  */
  169.  
  170. void
  171. print_frame_info (fi, level, source, args)
  172.      struct frame_info *fi;
  173.      register int level;
  174.      int source;
  175.      int args;
  176. {
  177.   struct symtab_and_line sal;
  178.   struct symbol *func;
  179.   register char *funname = 0;
  180.   enum language funlang = language_unknown;
  181.  
  182. #if 0
  183.   char buf[MAX_REGISTER_RAW_SIZE];
  184.   CORE_ADDR sp;
  185.  
  186.   /* On the 68k, this spends too much time in m68k_find_saved_regs.  */
  187.  
  188.   /* Get the value of SP_REGNUM relative to the frame.  */
  189.   get_saved_register (buf, (int *)NULL, (CORE_ADDR *)NULL,
  190.               FRAME_INFO_ID (fi), SP_REGNUM, (enum lval_type *)NULL);
  191.   sp = extract_address (buf, REGISTER_RAW_SIZE (SP_REGNUM));
  192.  
  193.   /* This is not a perfect test, because if a function alloca's some
  194.      memory, puts some code there, and then jumps into it, then the test
  195.      will succeed even though there is no call dummy.  Probably best is
  196.      to check for a bp_call_dummy breakpoint.  */
  197.   if (PC_IN_CALL_DUMMY (fi->pc, sp, fi->frame))
  198. #else
  199.   if (frame_in_dummy (fi))
  200. #endif
  201.     {
  202.       annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
  203.  
  204.       /* Do this regardless of SOURCE because we don't have any source
  205.      to list for this frame.  */
  206.       if (level >= 0)
  207.     printf_filtered ("#%-2d ", level);
  208.       annotate_function_call ();
  209.       printf_filtered ("<function called from gdb>\n");
  210.       annotate_frame_end ();
  211.       return;
  212.     }
  213.   if (fi->signal_handler_caller)
  214.     {
  215.       annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
  216.  
  217.       /* Do this regardless of SOURCE because we don't have any source
  218.      to list for this frame.  */
  219.       if (level >= 0)
  220.     printf_filtered ("#%-2d ", level);
  221.       annotate_signal_handler_caller ();
  222.       printf_filtered ("<signal handler called>\n");
  223.       annotate_frame_end ();
  224.       return;
  225.     }
  226.  
  227.   /* If fi is not the innermost frame, that normally means that fi->pc
  228.      points to *after* the call instruction, and we want to get the line
  229.      containing the call, never the next line.  But if the next frame is
  230.      a signal_handler_caller or a dummy frame, then the next frame was
  231.      not entered as the result of a call, and we want to get the line
  232.      containing fi->pc.  */
  233.   sal =
  234.     find_pc_line (fi->pc,
  235.           fi->next != NULL
  236.           && !fi->next->signal_handler_caller
  237.           && !frame_in_dummy (fi->next));
  238.  
  239.   func = find_pc_function (fi->pc);
  240.   if (func)
  241.     {
  242.       /* In certain pathological cases, the symtabs give the wrong
  243.      function (when we are in the first function in a file which
  244.      is compiled without debugging symbols, the previous function
  245.      is compiled with debugging symbols, and the "foo.o" symbol
  246.      that is supposed to tell us where the file with debugging symbols
  247.      ends has been truncated by ar because it is longer than 15
  248.      characters).  This also occurs if the user uses asm() to create
  249.      a function but not stabs for it (in a file compiled -g).
  250.  
  251.      So look in the minimal symbol tables as well, and if it comes
  252.      up with a larger address for the function use that instead.
  253.      I don't think this can ever cause any problems; there shouldn't
  254.      be any minimal symbols in the middle of a function; if this is
  255.      ever changed many parts of GDB will need to be changed (and we'll
  256.      create a find_pc_minimal_function or some such).  */
  257.  
  258.       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
  259.       if (msymbol != NULL
  260.       && (SYMBOL_VALUE_ADDRESS (msymbol) 
  261.           > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
  262.     {
  263. #if 0
  264.       /* There is no particular reason to think the line number
  265.          information is wrong.  Someone might have just put in
  266.          a label with asm() but left the line numbers alone.  */
  267.       /* In this case we have no way of knowing the source file
  268.          and line number, so don't print them.  */
  269.       sal.symtab = 0;
  270. #endif
  271.       /* We also don't know anything about the function besides
  272.          its address and name.  */
  273.       func = 0;
  274.       funname = SYMBOL_NAME (msymbol);
  275.       funlang = SYMBOL_LANGUAGE (msymbol);
  276.     }
  277.       else
  278.     {
  279.       funname = SYMBOL_NAME (func);
  280.       funlang = SYMBOL_LANGUAGE (func);
  281.     }
  282.     }
  283.   else
  284.     {
  285.       register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
  286.       if (msymbol != NULL)
  287.     {
  288.       funname = SYMBOL_NAME (msymbol);
  289.       funlang = SYMBOL_LANGUAGE (msymbol);
  290.     }
  291.     }
  292.  
  293.   if (source >= 0 || !sal.symtab)
  294.     {
  295.       annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
  296.  
  297.       if (level >= 0)
  298.     printf_filtered ("#%-2d ", level);
  299.       if (addressprint)
  300.     if (fi->pc != sal.pc || !sal.symtab)
  301.       {
  302.         annotate_frame_address ();
  303.         print_address_numeric (fi->pc, 1, gdb_stdout);
  304.         annotate_frame_address_end ();
  305.         printf_filtered (" in ");
  306.       }
  307.       annotate_frame_function_name ();
  308.       fprintf_symbol_filtered (gdb_stdout, funname ? funname : "??", funlang,
  309.                    DMGL_ANSI);
  310.       wrap_here ("   ");
  311.       annotate_frame_args ();
  312.       fputs_filtered (" (", gdb_stdout);
  313.       if (args)
  314.     {
  315.       struct print_args_args args;
  316.       args.fi = fi;
  317.       args.func = func;
  318.       catch_errors (print_args_stub, (char *)&args, "", RETURN_MASK_ERROR);
  319.     }
  320.       printf_filtered (")");
  321.       if (sal.symtab && sal.symtab->filename)
  322.     {
  323.       annotate_frame_source_begin ();
  324.           wrap_here ("   ");
  325.       printf_filtered (" at ");
  326.       annotate_frame_source_file ();
  327.       printf_filtered ("%s", sal.symtab->filename);
  328.       annotate_frame_source_file_end ();
  329.       printf_filtered (":");
  330.       annotate_frame_source_line ();
  331.       printf_filtered ("%d", sal.line);
  332.       annotate_frame_source_end ();
  333.     }
  334.  
  335. #ifdef PC_LOAD_SEGMENT
  336.      /* If we couldn't print out function name but if can figure out what
  337.         load segment this pc value is from, at least print out some info
  338.     about its load segment. */
  339.       if (!funname)
  340.     {
  341.       annotate_frame_where ();
  342.       wrap_here ("  ");
  343.       printf_filtered (" from %s", PC_LOAD_SEGMENT (fi->pc));
  344.     }
  345. #endif
  346.       printf_filtered ("\n");
  347.     }
  348.  
  349.   if ((source != 0) && sal.symtab)
  350.     {
  351.       int done = 0;
  352.       int mid_statement = source < 0 && fi->pc != sal.pc;
  353.       if (annotation_level)
  354.     done = identify_source_line (sal.symtab, sal.line, mid_statement,
  355.                      fi->pc);
  356.       if (!done)
  357.     {
  358.       if (addressprint && mid_statement)
  359.         {
  360.           print_address_numeric (fi->pc, 1, gdb_stdout);
  361.           printf_filtered ("\t");
  362.         }
  363.       if (print_frame_info_listing_hook)
  364.         print_frame_info_listing_hook (sal.symtab, sal.line, sal.line + 1, 0);
  365.       else
  366.         print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
  367.     }
  368.       current_source_line = max (sal.line - lines_to_list/2, 1);
  369.     }
  370.   if (source != 0)
  371.     set_default_breakpoint (1, fi->pc, sal.symtab, sal.line);
  372.  
  373.   annotate_frame_end ();
  374.  
  375.   gdb_flush (gdb_stdout);
  376. }
  377.  
  378. /* Read a frame specification in whatever the appropriate format is.
  379.    Call error() if the specification is in any way invalid (i.e.
  380.    this function never returns NULL).  */
  381.  
  382. static struct frame_info *
  383. parse_frame_specification (frame_exp)
  384.      char *frame_exp;
  385. {
  386.   int numargs = 0;
  387. #define    MAXARGS    4
  388.   CORE_ADDR args[MAXARGS];
  389.   
  390.   if (frame_exp)
  391.     {
  392.       char *addr_string, *p;
  393.       struct cleanup *tmp_cleanup;
  394.  
  395.       while (*frame_exp == ' ') frame_exp++;
  396.  
  397.       while (*frame_exp)
  398.     {
  399.       if (numargs > MAXARGS)
  400.         error ("Too many args in frame specification");
  401.       /* Parse an argument.  */
  402.           for (p = frame_exp; *p && *p != ' '; p++)
  403.         ;
  404.       addr_string = savestring(frame_exp, p - frame_exp);
  405.  
  406.       {
  407.         tmp_cleanup = make_cleanup (free, addr_string);
  408.         args[numargs++] = parse_and_eval_address (addr_string);
  409.         do_cleanups (tmp_cleanup);
  410.       }
  411.  
  412.       /* Skip spaces, move to possible next arg.  */
  413.       while (*p == ' ') p++;
  414.       frame_exp = p;
  415.     }
  416.     }
  417.  
  418.   switch (numargs)
  419.     {
  420.     case 0:
  421.       if (selected_frame == NULL)
  422.     error ("No selected frame.");
  423.       return selected_frame;
  424.       /* NOTREACHED */
  425.     case 1:
  426.       {
  427.     int level = args[0];
  428.     struct frame_info *fid =
  429.       find_relative_frame (get_current_frame (), &level);
  430.     struct frame_info *tfid;
  431.  
  432.     if (level == 0)
  433.       /* find_relative_frame was successful */
  434.       return fid;
  435.  
  436.     /* If SETUP_ARBITRARY_FRAME is defined, then frame specifications
  437.        take at least 2 addresses.  It is important to detect this case
  438.        here so that "frame 100" does not give a confusing error message
  439.        like "frame specification requires two addresses".  This of course
  440.        does not solve the "frame 100" problem for machines on which
  441.        a frame specification can be made with one address.  To solve
  442.        that, we need a new syntax for a specifying a frame by address.
  443.        I think the cleanest syntax is $frame(0x45) ($frame(0x23,0x45) for
  444.        two args, etc.), but people might think that is too much typing,
  445.        so I guess *0x23,0x45 would be a possible alternative (commas
  446.        really should be used instead of spaces to delimit; using spaces
  447.        normally works in an expression).  */
  448. #ifdef SETUP_ARBITRARY_FRAME
  449.     error ("No frame %d", args[0]);
  450. #endif
  451.  
  452.     /* If (s)he specifies the frame with an address, he deserves what
  453.        (s)he gets.  Still, give the highest one that matches.  */
  454.  
  455.     for (fid = get_current_frame ();
  456.          fid && fid->frame != args[0];
  457.          fid = get_prev_frame (fid))
  458.       ;
  459.  
  460.     if (fid)
  461.       while ((tfid = get_prev_frame (fid)) &&
  462.          (tfid->frame == args[0]))
  463.         fid = tfid;
  464.       
  465.     /* We couldn't identify the frame as an existing frame, but
  466.        perhaps we can create one with a single argument.  */
  467.       }
  468.  
  469.      default:
  470. #ifdef SETUP_ARBITRARY_FRAME
  471.       return SETUP_ARBITRARY_FRAME (numargs, args);
  472. #else
  473.       /* Usual case.  Do it here rather than have everyone supply
  474.      a SETUP_ARBITRARY_FRAME that does this.  */
  475.       if (numargs == 1)
  476.     return create_new_frame (args[0], 0);
  477.       error ("Too many args in frame specification");
  478. #endif
  479.       /* NOTREACHED */
  480.     }
  481.   /* NOTREACHED */
  482. }
  483.  
  484. /* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except
  485.    that if it is unsure about the answer, it returns 0
  486.    instead of guessing (this happens on the VAX and i960, for example).
  487.  
  488.    On most machines, we never have to guess about the args address,
  489.    so FRAME_ARGS_ADDRESS{,_CORRECT} are the same.  */
  490. #if !defined (FRAME_ARGS_ADDRESS_CORRECT)
  491. #define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS
  492. #endif
  493.  
  494. /* Print verbosely the selected frame or the frame at address ADDR.
  495.    This means absolutely all information in the frame is printed.  */
  496.  
  497. static void
  498. frame_info (addr_exp, from_tty)
  499.      char *addr_exp;
  500.      int from_tty;
  501. {
  502.   struct frame_info *fi;
  503.   struct frame_saved_regs fsr;
  504.   struct symtab_and_line sal;
  505.   struct symbol *func;
  506.   struct symtab *s;
  507.   struct frame_info *calling_frame_info;
  508.   int i, count, numregs;
  509.   char *funname = 0;
  510.   enum language funlang = language_unknown;
  511.  
  512.   if (!target_has_stack)
  513.     error ("No stack.");
  514.  
  515.   fi = parse_frame_specification (addr_exp);
  516.   if (fi == NULL)
  517.     error ("Invalid frame specified.");
  518.  
  519.   sal = find_pc_line (fi->pc,
  520.               fi->next != NULL
  521.               && !fi->next->signal_handler_caller
  522.               && !frame_in_dummy (fi->next));
  523.   func = get_frame_function (fi);
  524.   s = find_pc_symtab(fi->pc);
  525.   if (func)
  526.     {
  527.       funname = SYMBOL_NAME (func);
  528.       funlang = SYMBOL_LANGUAGE (func);
  529.     }
  530.   else
  531.     {
  532.       register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
  533.       if (msymbol != NULL)
  534.     {
  535.       funname = SYMBOL_NAME (msymbol);
  536.       funlang = SYMBOL_LANGUAGE (msymbol);
  537.     }
  538.     }
  539.   calling_frame_info = get_prev_frame (fi);
  540.  
  541.   if (!addr_exp && selected_frame_level >= 0)
  542.     {
  543.       printf_filtered ("Stack level %d, frame at ", selected_frame_level);
  544.       print_address_numeric (fi->frame, 1, gdb_stdout);
  545.       printf_filtered (":\n");
  546.     }
  547.   else
  548.     {
  549.       printf_filtered ("Stack frame at ");
  550.       print_address_numeric (fi->frame, 1, gdb_stdout);
  551.       printf_filtered (":\n");
  552.     }
  553.   printf_filtered (" %s = ", reg_names[PC_REGNUM]);
  554.   print_address_numeric (fi->pc, 1, gdb_stdout);
  555.  
  556.   wrap_here ("   ");
  557.   if (funname)
  558.     {
  559.       printf_filtered (" in ");
  560.       fprintf_symbol_filtered (gdb_stdout, funname, funlang,
  561.                    DMGL_ANSI | DMGL_PARAMS);
  562.     }
  563.   wrap_here ("   ");
  564.   if (sal.symtab)
  565.     printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
  566.   puts_filtered ("; ");
  567.   wrap_here ("    ");
  568.   printf_filtered ("saved %s ", reg_names[PC_REGNUM]);
  569.   print_address_numeric (FRAME_SAVED_PC (fi), 1, gdb_stdout);
  570.   printf_filtered ("\n");
  571.  
  572.   {
  573.     int frameless = 0;
  574. #ifdef FRAMELESS_FUNCTION_INVOCATION
  575.     FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
  576. #endif
  577.     if (frameless)
  578.       printf_filtered (" (FRAMELESS),");
  579.   }
  580.  
  581.   if (calling_frame_info)
  582.     {
  583.       printf_filtered (" called by frame at ");
  584.       print_address_numeric (calling_frame_info->frame, 1, gdb_stdout);
  585.     }
  586.   if (fi->next && calling_frame_info)
  587.     puts_filtered (",");
  588.   wrap_here ("   ");
  589.   if (fi->next)
  590.     {
  591.       printf_filtered (" caller of frame at ");
  592.       print_address_numeric (fi->next->frame, 1, gdb_stdout);
  593.     }
  594.   if (fi->next || calling_frame_info)
  595.     puts_filtered ("\n");
  596.   if (s)
  597.     printf_filtered (" source language %s.\n", language_str (s->language));
  598.  
  599. #ifdef PRINT_EXTRA_FRAME_INFO
  600.   PRINT_EXTRA_FRAME_INFO (fi);
  601. #endif
  602.  
  603.   {
  604.     /* Address of the argument list for this frame, or 0.  */
  605.     CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
  606.     /* Number of args for this frame, or -1 if unknown.  */
  607.     int numargs;
  608.  
  609.     if (arg_list == 0)
  610.       printf_filtered (" Arglist at unknown address.\n");
  611.     else
  612.       {
  613.     printf_filtered (" Arglist at ");
  614.     print_address_numeric (arg_list, 1, gdb_stdout);
  615.     printf_filtered (",");
  616.  
  617.     FRAME_NUM_ARGS (numargs, fi);
  618.     if (numargs < 0)
  619.       puts_filtered (" args: ");
  620.     else if (numargs == 0)
  621.       puts_filtered (" no args.");
  622.     else if (numargs == 1)
  623.       puts_filtered (" 1 arg: ");
  624.     else
  625.       printf_filtered (" %d args: ", numargs);
  626.     print_frame_args (func, fi, numargs, gdb_stdout);
  627.     puts_filtered ("\n");
  628.       }
  629.   }
  630.   {
  631.     /* Address of the local variables for this frame, or 0.  */
  632.     CORE_ADDR arg_list = FRAME_LOCALS_ADDRESS (fi);
  633.  
  634.     if (arg_list == 0)
  635.       printf_filtered (" Locals at unknown address,");
  636.     else
  637.       {
  638.     printf_filtered (" Locals at ");
  639.     print_address_numeric (arg_list, 1, gdb_stdout);
  640.     printf_filtered (",");
  641.       }
  642.   }
  643.  
  644. #if defined (FRAME_FIND_SAVED_REGS)  
  645.   get_frame_saved_regs (fi, &fsr);
  646.   /* The sp is special; what's returned isn't the save address, but
  647.      actually the value of the previous frame's sp.  */
  648.   printf_filtered (" Previous frame's sp is ");
  649.   print_address_numeric (fsr.regs[SP_REGNUM], 1, gdb_stdout);
  650.   printf_filtered ("\n");
  651.   count = 0;
  652.   numregs = ARCH_NUM_REGS;
  653.   for (i = 0; i < numregs; i++)
  654.     if (fsr.regs[i] && i != SP_REGNUM)
  655.       {
  656.     if (count == 0)
  657.       puts_filtered (" Saved registers:\n ");
  658.     else
  659.       puts_filtered (",");
  660.     wrap_here (" ");
  661.     printf_filtered (" %s at ", reg_names[i]);
  662.     print_address_numeric (fsr.regs[i], 1, gdb_stdout);
  663.     count++;
  664.       }
  665.   if (count)
  666.     puts_filtered ("\n");
  667. #else  /* Have FRAME_FIND_SAVED_REGS.  */
  668.   /* We could get some information about saved registers by calling
  669.      get_saved_register on each register.  Which info goes with which frame
  670.      is necessarily lost, however, and I suspect that the users don't care
  671.      whether they get the info.  */
  672.   puts_filtered ("\n");
  673. #endif /* Have FRAME_FIND_SAVED_REGS.  */
  674. }
  675.  
  676. #if 0
  677. /* Set a limit on the number of frames printed by default in a
  678.    backtrace.  */
  679.  
  680. static int backtrace_limit;
  681.  
  682. static void
  683. set_backtrace_limit_command (count_exp, from_tty)
  684.      char *count_exp;
  685.      int from_tty;
  686. {
  687.   int count = parse_and_eval_address (count_exp);
  688.  
  689.   if (count < 0)
  690.     error ("Negative argument not meaningful as backtrace limit.");
  691.  
  692.   backtrace_limit = count;
  693. }
  694.  
  695. static void
  696. backtrace_limit_info (arg, from_tty)
  697.      char *arg;
  698.      int from_tty;
  699. {
  700.   if (arg)
  701.     error ("\"Info backtrace-limit\" takes no arguments.");
  702.  
  703.   printf_unfiltered ("Backtrace limit: %d.\n", backtrace_limit);
  704. }
  705. #endif
  706.  
  707. /* Print briefly all stack frames or just the innermost COUNT frames.  */
  708.  
  709. static void
  710. backtrace_command (count_exp, from_tty)
  711.      char *count_exp;
  712.      int from_tty;
  713. {
  714.   struct frame_info *fi;
  715.   register int count;
  716.   register int i;
  717.   register struct frame_info *trailing;
  718.   register int trailing_level;
  719.  
  720.   if (!target_has_stack)
  721.     error ("No stack.");
  722.  
  723.   /* The following code must do two things.  First, it must
  724.      set the variable TRAILING to the frame from which we should start
  725.      printing.  Second, it must set the variable count to the number
  726.      of frames which we should print, or -1 if all of them.  */
  727.   trailing = get_current_frame ();
  728.   trailing_level = 0;
  729.   if (count_exp)
  730.     {
  731.       count = parse_and_eval_address (count_exp);
  732.       if (count < 0)
  733.     {
  734.       struct frame_info *current;
  735.  
  736.       count = -count;
  737.  
  738.       current = trailing;
  739.       while (current && count--)
  740.         {
  741.           QUIT;
  742.           current = get_prev_frame (current);
  743.         }
  744.       
  745.       /* Will stop when CURRENT reaches the top of the stack.  TRAILING
  746.          will be COUNT below it.  */
  747.       while (current)
  748.         {
  749.           QUIT;
  750.           trailing = get_prev_frame (trailing);
  751.           current = get_prev_frame (current);
  752.           trailing_level++;
  753.         }
  754.       
  755.       count = -1;
  756.     }
  757.     }
  758.   else
  759.     count = -1;
  760.  
  761.   if (info_verbose)
  762.     {
  763.       struct partial_symtab *ps;
  764.       
  765.       /* Read in symbols for all of the frames.  Need to do this in
  766.      a separate pass so that "Reading in symbols for xxx" messages
  767.      don't screw up the appearance of the backtrace.  Also
  768.      if people have strong opinions against reading symbols for
  769.      backtrace this may have to be an option.  */
  770.       i = count;
  771.       for (fi = trailing;
  772.        fi != NULL && i--;
  773.        fi = get_prev_frame (fi))
  774.     {
  775.       QUIT;
  776.       ps = find_pc_psymtab (fi->pc);
  777.       if (ps)
  778.         PSYMTAB_TO_SYMTAB (ps);    /* Force syms to come in */
  779.     }
  780.     }
  781.  
  782.   for (i = 0, fi = trailing;
  783.        fi && count--;
  784.        i++, fi = get_prev_frame (fi))
  785.     {
  786.       QUIT;
  787.  
  788.       /* Don't use print_stack_frame; if an error() occurs it probably
  789.      means further attempts to backtrace would fail (on the other
  790.      hand, perhaps the code does or could be fixed to make sure
  791.      the frame->prev field gets set to NULL in that case).  */
  792.       print_frame_info (fi, trailing_level + i, 0, 1);
  793.     }
  794.  
  795.   /* If we've stopped before the end, mention that.  */
  796.   if (fi && from_tty)
  797.     printf_filtered ("(More stack frames follow...)\n");
  798. }
  799.  
  800. /* Print the local variables of a block B active in FRAME.
  801.    Return 1 if any variables were printed; 0 otherwise.  */
  802.  
  803. static int
  804. print_block_frame_locals (b, fi, stream)
  805.      struct block *b;
  806.      register struct frame_info *fi;
  807.      register GDB_FILE *stream;
  808. {
  809.   int nsyms;
  810.   register int i;
  811.   register struct symbol *sym;
  812.   register int values_printed = 0;
  813.  
  814.   nsyms = BLOCK_NSYMS (b);
  815.  
  816.   for (i = 0; i < nsyms; i++)
  817.     {
  818.       sym = BLOCK_SYM (b, i);
  819.       switch (SYMBOL_CLASS (sym))
  820.     {
  821.     case LOC_LOCAL:
  822.     case LOC_REGISTER:
  823.     case LOC_STATIC:
  824.     case LOC_BASEREG:
  825.       values_printed = 1;
  826.       fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
  827.       fputs_filtered (" = ", stream);
  828.       print_variable_value (sym, fi, stream);
  829.       fprintf_filtered (stream, "\n");
  830.       break;
  831.  
  832.     default:
  833.       /* Ignore symbols which are not locals.  */
  834.       break;
  835.     }
  836.     }
  837.   return values_printed;
  838. }
  839.  
  840. /* Same, but print labels.  */
  841.  
  842. static int
  843. print_block_frame_labels (b, have_default, stream)
  844.      struct block *b;
  845.      int *have_default;
  846.      register GDB_FILE *stream;
  847. {
  848.   int nsyms;
  849.   register int i;
  850.   register struct symbol *sym;
  851.   register int values_printed = 0;
  852.  
  853.   nsyms = BLOCK_NSYMS (b);
  854.  
  855.   for (i = 0; i < nsyms; i++)
  856.     {
  857.       sym = BLOCK_SYM (b, i);
  858.       if (STREQ (SYMBOL_NAME (sym), "default"))
  859.     {
  860.       if (*have_default)
  861.         continue;
  862.       *have_default = 1;
  863.     }
  864.       if (SYMBOL_CLASS (sym) == LOC_LABEL)
  865.     {
  866.       struct symtab_and_line sal;
  867.       sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
  868.       values_printed = 1;
  869.       fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
  870.       if (addressprint)
  871.         {
  872.           fprintf_filtered (stream, " ");
  873.           print_address_numeric (SYMBOL_VALUE_ADDRESS (sym), 1, stream);
  874.         }
  875.       fprintf_filtered (stream, " in file %s, line %d\n",
  876.                 sal.symtab->filename, sal.line);
  877.     }
  878.     }
  879.   return values_printed;
  880. }
  881.  
  882. /* Print on STREAM all the local variables in frame FRAME,
  883.    including all the blocks active in that frame
  884.    at its current pc.
  885.  
  886.    Returns 1 if the job was done,
  887.    or 0 if nothing was printed because we have no info
  888.    on the function running in FRAME.  */
  889.  
  890. static void
  891. print_frame_local_vars (fi, stream)
  892.      register struct frame_info *fi;
  893.      register GDB_FILE *stream;
  894. {
  895.   register struct block *block = get_frame_block (fi);
  896.   register int values_printed = 0;
  897.  
  898.   if (block == 0)
  899.     {
  900.       fprintf_filtered (stream, "No symbol table info available.\n");
  901.       return;
  902.     }
  903.   
  904.   while (block != 0)
  905.     {
  906.       if (print_block_frame_locals (block, fi, stream))
  907.     values_printed = 1;
  908.       /* After handling the function's top-level block, stop.
  909.      Don't continue to its superblock, the block of
  910.      per-file symbols.  */
  911.       if (BLOCK_FUNCTION (block))
  912.     break;
  913.       block = BLOCK_SUPERBLOCK (block);
  914.     }
  915.  
  916.   if (!values_printed)
  917.     {
  918.       fprintf_filtered (stream, "No locals.\n");
  919.     }
  920. }
  921.  
  922. /* Same, but print labels.  */
  923.  
  924. static void
  925. print_frame_label_vars (fi, this_level_only, stream)
  926.      register struct frame_info *fi;
  927.      int this_level_only;
  928.      register GDB_FILE *stream;
  929. {
  930.   register struct blockvector *bl;
  931.   register struct block *block = get_frame_block (fi);
  932.   register int values_printed = 0;
  933.   int index, have_default = 0;
  934.   char *blocks_printed;
  935.   CORE_ADDR pc = fi->pc;
  936.  
  937.   if (block == 0)
  938.     {
  939.       fprintf_filtered (stream, "No symbol table info available.\n");
  940.       return;
  941.     }
  942.  
  943.   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
  944.   blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
  945.   memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
  946.  
  947.   while (block != 0)
  948.     {
  949.       CORE_ADDR end = BLOCK_END (block) - 4;
  950.       int last_index;
  951.  
  952.       if (bl != blockvector_for_pc (end, &index))
  953.     error ("blockvector blotch");
  954.       if (BLOCKVECTOR_BLOCK (bl, index) != block)
  955.     error ("blockvector botch");
  956.       last_index = BLOCKVECTOR_NBLOCKS (bl);
  957.       index += 1;
  958.  
  959.       /* Don't print out blocks that have gone by.  */
  960.       while (index < last_index
  961.          && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
  962.     index++;
  963.  
  964.       while (index < last_index
  965.          && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
  966.     {
  967.       if (blocks_printed[index] == 0)
  968.         {
  969.           if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
  970.         values_printed = 1;
  971.           blocks_printed[index] = 1;
  972.         }
  973.       index++;
  974.     }
  975.       if (have_default)
  976.     return;
  977.       if (values_printed && this_level_only)
  978.     return;
  979.  
  980.       /* After handling the function's top-level block, stop.
  981.      Don't continue to its superblock, the block of
  982.      per-file symbols.  */
  983.       if (BLOCK_FUNCTION (block))
  984.     break;
  985.       block = BLOCK_SUPERBLOCK (block);
  986.     }
  987.  
  988.   if (!values_printed && !this_level_only)
  989.     {
  990.       fprintf_filtered (stream, "No catches.\n");
  991.     }
  992. }
  993.  
  994. /* ARGSUSED */
  995. static void
  996. locals_info (args, from_tty)
  997.      char *args;
  998.      int from_tty;
  999. {
  1000.   if (!selected_frame)
  1001.     error ("No frame selected.");
  1002.   print_frame_local_vars (selected_frame, gdb_stdout);
  1003. }
  1004.  
  1005. static void
  1006. catch_info (ignore, from_tty)
  1007.      char *ignore;
  1008.      int from_tty;
  1009. {
  1010.   if (!selected_frame)
  1011.     error ("No frame selected.");
  1012.   print_frame_label_vars (selected_frame, 0, gdb_stdout);
  1013. }
  1014.  
  1015. static void
  1016. print_frame_arg_vars (fi, stream)
  1017.      register struct frame_info *fi;
  1018.      register GDB_FILE *stream;
  1019. {
  1020.   struct symbol *func = get_frame_function (fi);
  1021.   register struct block *b;
  1022.   int nsyms;
  1023.   register int i;
  1024.   register struct symbol *sym, *sym2;
  1025.   register int values_printed = 0;
  1026.  
  1027.   if (func == 0)
  1028.     {
  1029.       fprintf_filtered (stream, "No symbol table info available.\n");
  1030.       return;
  1031.     }
  1032.  
  1033.   b = SYMBOL_BLOCK_VALUE (func);
  1034.   nsyms = BLOCK_NSYMS (b);
  1035.  
  1036.   for (i = 0; i < nsyms; i++)
  1037.     {
  1038.       sym = BLOCK_SYM (b, i);
  1039.       switch (SYMBOL_CLASS (sym))
  1040.     {
  1041.     case LOC_ARG:
  1042.     case LOC_LOCAL_ARG:
  1043.     case LOC_REF_ARG:
  1044.     case LOC_REGPARM:
  1045.     case LOC_REGPARM_ADDR:
  1046.     case LOC_BASEREG_ARG:
  1047.       values_printed = 1;
  1048.       fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
  1049.       fputs_filtered (" = ", stream);
  1050.  
  1051.       /* We have to look up the symbol because arguments can have
  1052.          two entries (one a parameter, one a local) and the one we
  1053.          want is the local, which lookup_symbol will find for us.
  1054.          This includes gcc1 (not gcc2) on the sparc when passing a
  1055.          small structure and gcc2 when the argument type is float
  1056.          and it is passed as a double and converted to float by
  1057.          the prologue (in the latter case the type of the LOC_ARG
  1058.          symbol is double and the type of the LOC_LOCAL symbol is
  1059.          float).  There are also LOC_ARG/LOC_REGISTER pairs which
  1060.          are not combined in symbol-reading.  */
  1061.  
  1062.       sym2 = lookup_symbol (SYMBOL_NAME (sym),
  1063.             b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL);
  1064.       print_variable_value (sym2, fi, stream);
  1065.       fprintf_filtered (stream, "\n");
  1066.       break;
  1067.  
  1068.     default:
  1069.       /* Don't worry about things which aren't arguments.  */
  1070.       break;
  1071.     }
  1072.     }
  1073.  
  1074.   if (!values_printed)
  1075.     {
  1076.       fprintf_filtered (stream, "No arguments.\n");
  1077.     }
  1078. }
  1079.  
  1080. static void
  1081. args_info (ignore, from_tty)
  1082.      char *ignore;
  1083.      int from_tty;
  1084. {
  1085.   if (!selected_frame)
  1086.     error ("No frame selected.");
  1087.   print_frame_arg_vars (selected_frame, gdb_stdout);
  1088. }
  1089.  
  1090. /* Select frame FI, and note that its stack level is LEVEL.
  1091.    LEVEL may be -1 if an actual level number is not known.  */
  1092.  
  1093. void
  1094. select_frame (fi, level)
  1095.      struct frame_info *fi;
  1096.      int level;
  1097. {
  1098.   register struct symtab *s;
  1099.  
  1100.   selected_frame = fi;
  1101.   selected_frame_level = level;
  1102.  
  1103.   /* Ensure that symbols for this frame are read in.  Also, determine the
  1104.      source language of this frame, and switch to it if desired.  */
  1105.   if (fi)
  1106.   {
  1107.     s = find_pc_symtab (fi->pc);
  1108.     if (s 
  1109.     && s->language != current_language->la_language
  1110.     && s->language != language_unknown
  1111.     && language_mode == language_mode_auto) {
  1112.       set_language(s->language);
  1113.     }
  1114.   }
  1115. }
  1116.  
  1117. /* Store the selected frame and its level into *FRAMEP and *LEVELP.
  1118.    If there is no selected frame, *FRAMEP is set to NULL.  */
  1119.  
  1120. void
  1121. record_selected_frame (frameaddrp, levelp)
  1122.      CORE_ADDR *frameaddrp;
  1123.      int *levelp;
  1124. {
  1125.   *frameaddrp = selected_frame ? selected_frame->frame : 0;
  1126.   *levelp = selected_frame_level;
  1127. }
  1128.  
  1129. /* Return the symbol-block in which the selected frame is executing.
  1130.    Can return zero under various legitimate circumstances.  */
  1131.  
  1132. struct block *
  1133. get_selected_block ()
  1134. {
  1135.   if (!target_has_stack)
  1136.     return 0;
  1137.  
  1138.   if (!selected_frame)
  1139.     return get_current_block ();
  1140.   return get_frame_block (selected_frame);
  1141. }
  1142.  
  1143. /* Find a frame a certain number of levels away from FRAME.
  1144.    LEVEL_OFFSET_PTR points to an int containing the number of levels.
  1145.    Positive means go to earlier frames (up); negative, the reverse.
  1146.    The int that contains the number of levels is counted toward
  1147.    zero as the frames for those levels are found.
  1148.    If the top or bottom frame is reached, that frame is returned,
  1149.    but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
  1150.    how much farther the original request asked to go.  */
  1151.  
  1152. struct frame_info *
  1153. find_relative_frame (frame, level_offset_ptr)
  1154.      register struct frame_info *frame;
  1155.      register int *level_offset_ptr;
  1156. {
  1157.   register struct frame_info *prev;
  1158.   register struct frame_info *frame1;
  1159.  
  1160.   /* Going up is simple: just do get_prev_frame enough times
  1161.      or until initial frame is reached.  */
  1162.   while (*level_offset_ptr > 0)
  1163.     {
  1164.       prev = get_prev_frame (frame);
  1165.       if (prev == 0)
  1166.     break;
  1167.       (*level_offset_ptr)--;
  1168.       frame = prev;
  1169.     }
  1170.   /* Going down is just as simple.  */
  1171.   if (*level_offset_ptr < 0)
  1172.     {
  1173.       while (*level_offset_ptr < 0) {
  1174.     frame1 = get_next_frame (frame);
  1175.     if (!frame1)
  1176.       break;
  1177.     frame = frame1;
  1178.     (*level_offset_ptr)++;
  1179.       }
  1180.     }
  1181.   return frame;
  1182. }
  1183.  
  1184. /* The "select_frame" command.  With no arg, NOP.
  1185.    With arg LEVEL_EXP, select the frame at level LEVEL if it is a
  1186.    valid level.  Otherwise, treat level_exp as an address expression
  1187.    and select it.  See parse_frame_specification for more info on proper
  1188.    frame expressions. */
  1189.  
  1190. /* ARGSUSED */
  1191. static void
  1192. select_frame_command (level_exp, from_tty)
  1193.      char *level_exp;
  1194.      int from_tty;
  1195. {
  1196.   register struct frame_info *frame, *frame1;
  1197.   unsigned int level = 0;
  1198.  
  1199.   if (!target_has_stack)
  1200.     error ("No stack.");
  1201.  
  1202.   frame = parse_frame_specification (level_exp);
  1203.  
  1204.   /* Try to figure out what level this frame is.  But if there is
  1205.      no current stack, don't error out -- let the user set one.  */
  1206.   frame1 = 0;
  1207.   if (get_current_frame()) {
  1208.     for (frame1 = get_prev_frame (0);
  1209.      frame1 && frame1 != frame;
  1210.      frame1 = get_prev_frame (frame1))
  1211.       level++;
  1212.   }
  1213.  
  1214.   if (!frame1)
  1215.     level = 0;
  1216.  
  1217.   select_frame (frame, level);
  1218. }
  1219.  
  1220. /* The "frame" command.  With no arg, print selected frame briefly.
  1221.    With arg, behaves like select_frame and then prints the selected
  1222.    frame.  */
  1223.  
  1224. static void
  1225. frame_command (level_exp, from_tty)
  1226.      char *level_exp;
  1227.      int from_tty;
  1228. {
  1229.   select_frame_command (level_exp, from_tty);
  1230.   print_stack_frame (selected_frame, selected_frame_level, 1);
  1231. }
  1232.  
  1233. /* Select the frame up one or COUNT stack levels
  1234.    from the previously selected frame, and print it briefly.  */
  1235.  
  1236. /* ARGSUSED */
  1237. static void
  1238. up_silently_command (count_exp, from_tty)
  1239.      char *count_exp;
  1240.      int from_tty;
  1241. {
  1242.   register struct frame_info *fi;
  1243.   int count = 1, count1;
  1244.   if (count_exp)
  1245.     count = parse_and_eval_address (count_exp);
  1246.   count1 = count;
  1247.   
  1248.   if (target_has_stack == 0 || selected_frame == 0)
  1249.     error ("No stack.");
  1250.  
  1251.   fi = find_relative_frame (selected_frame, &count1);
  1252.   if (count1 != 0 && count_exp == 0)
  1253.     error ("Initial frame selected; you cannot go up.");
  1254.   select_frame (fi, selected_frame_level + count - count1);
  1255. }
  1256.  
  1257. static void
  1258. up_command (count_exp, from_tty)
  1259.      char *count_exp;
  1260.      int from_tty;
  1261. {
  1262.   up_silently_command (count_exp, from_tty);
  1263.   print_stack_frame (selected_frame, selected_frame_level, 1);
  1264. }
  1265.  
  1266. /* Select the frame down one or COUNT stack levels
  1267.    from the previously selected frame, and print it briefly.  */
  1268.  
  1269. /* ARGSUSED */
  1270. static void
  1271. down_silently_command (count_exp, from_tty)
  1272.      char *count_exp;
  1273.      int from_tty;
  1274. {
  1275.   register struct frame_info *frame;
  1276.   int count = -1, count1;
  1277.   if (count_exp)
  1278.     count = - parse_and_eval_address (count_exp);
  1279.   count1 = count;
  1280.   
  1281.   if (target_has_stack == 0 || selected_frame == 0)
  1282.     error ("No stack.");
  1283.  
  1284.   frame = find_relative_frame (selected_frame, &count1);
  1285.   if (count1 != 0 && count_exp == 0)
  1286.     {
  1287.  
  1288.       /* We only do this if count_exp is not specified.  That way "down"
  1289.      means to really go down (and let me know if that is
  1290.      impossible), but "down 9999" can be used to mean go all the way
  1291.      down without getting an error.  */
  1292.  
  1293.       error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
  1294.     }
  1295.  
  1296.   select_frame (frame, selected_frame_level + count - count1);
  1297. }
  1298.  
  1299.  
  1300. static void
  1301. down_command (count_exp, from_tty)
  1302.      char *count_exp;
  1303.      int from_tty;
  1304. {
  1305.   down_silently_command (count_exp, from_tty);
  1306.   print_stack_frame (selected_frame, selected_frame_level, 1);
  1307. }
  1308.  
  1309. static void
  1310. return_command (retval_exp, from_tty)
  1311.      char *retval_exp;
  1312.      int from_tty;
  1313. {
  1314.   struct symbol *thisfun;
  1315.   CORE_ADDR selected_frame_addr;
  1316.   CORE_ADDR selected_frame_pc;
  1317.   struct frame_info *frame;
  1318.   value_ptr return_value = NULL;
  1319.  
  1320.   if (selected_frame == NULL)
  1321.     error ("No selected frame.");
  1322.   thisfun = get_frame_function (selected_frame);
  1323.   selected_frame_addr = FRAME_FP (selected_frame);
  1324.   selected_frame_pc = selected_frame->pc;
  1325.  
  1326.   /* Compute the return value (if any -- possibly getting errors here).  */
  1327.  
  1328.   if (retval_exp)
  1329.     {
  1330.       struct type *return_type = NULL;
  1331.  
  1332.       return_value = parse_and_eval (retval_exp);
  1333.  
  1334.       /* Cast return value to the return type of the function.  */
  1335.       if (thisfun != NULL)
  1336.     return_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (thisfun));
  1337.       if (return_type == NULL)
  1338.     return_type = builtin_type_int;
  1339.       return_value = value_cast (return_type, return_value);
  1340.  
  1341.       /* Make sure we have fully evaluated it, since
  1342.      it might live in the stack frame we're about to pop.  */
  1343.       if (VALUE_LAZY (return_value))
  1344.     value_fetch_lazy (return_value);
  1345.     }
  1346.  
  1347.   /* If interactive, require confirmation.  */
  1348.  
  1349.   if (from_tty)
  1350.     {
  1351.       if (thisfun != 0)
  1352.     {
  1353.       if (!query ("Make %s return now? ", SYMBOL_SOURCE_NAME (thisfun)))
  1354.         {
  1355.           error ("Not confirmed.");
  1356.           /* NOTREACHED */
  1357.         }
  1358.     }
  1359.       else
  1360.     if (!query ("Make selected stack frame return now? "))
  1361.       error ("Not confirmed.");
  1362.     }
  1363.  
  1364.   /* Do the real work.  Pop until the specified frame is current.  We
  1365.      use this method because the selected_frame is not valid after
  1366.      a POP_FRAME.  The pc comparison makes this work even if the
  1367.      selected frame shares its fp with another frame.  */
  1368.  
  1369.   while (selected_frame_addr != (frame = get_current_frame())->frame
  1370.      || selected_frame_pc != frame->pc)
  1371.     POP_FRAME;
  1372.  
  1373.   /* Then pop that frame.  */
  1374.  
  1375.   POP_FRAME;
  1376.  
  1377.   /* Compute the return value (if any) and store in the place
  1378.      for return values.  */
  1379.  
  1380.   if (retval_exp)
  1381.     set_return_value (return_value);
  1382.  
  1383.   /* If interactive, print the frame that is now current.  */
  1384.  
  1385.   if (from_tty)
  1386.     frame_command ("0", 1);
  1387. }
  1388.  
  1389. /* Gets the language of the current frame.  */
  1390.  
  1391. enum language
  1392. get_frame_language()
  1393. {
  1394.   register struct symtab *s;
  1395.   enum language flang;        /* The language of the current frame */
  1396.    
  1397.   if (selected_frame)
  1398.     {
  1399.       s = find_pc_symtab(selected_frame->pc);
  1400.       if (s)
  1401.     flang = s->language;
  1402.       else
  1403.     flang = language_unknown;
  1404.     }
  1405.   else
  1406.     flang = language_unknown;
  1407.  
  1408.   return flang;
  1409. }
  1410.  
  1411. void
  1412. _initialize_stack ()
  1413. {
  1414. #if 0  
  1415.   backtrace_limit = 30;
  1416. #endif
  1417.  
  1418.   add_com ("return", class_stack, return_command,
  1419.        "Make selected stack frame return to its caller.\n\
  1420. Control remains in the debugger, but when you continue\n\
  1421. execution will resume in the frame above the one now selected.\n\
  1422. If an argument is given, it is an expression for the value to return.");
  1423.  
  1424.   add_com ("up", class_stack, up_command,
  1425.        "Select and print stack frame that called this one.\n\
  1426. An argument says how many frames up to go.");
  1427.   add_com ("up-silently", class_support, up_silently_command,
  1428.        "Same as the `up' command, but does not print anything.\n\
  1429. This is useful in command scripts.");
  1430.  
  1431.   add_com ("down", class_stack, down_command,
  1432.        "Select and print stack frame called by this one.\n\
  1433. An argument says how many frames down to go.");
  1434.   add_com_alias ("do", "down", class_stack, 1);
  1435.   add_com_alias ("dow", "down", class_stack, 1);
  1436.   add_com ("down-silently", class_support, down_silently_command,
  1437.        "Same as the `down' command, but does not print anything.\n\
  1438. This is useful in command scripts.");
  1439.  
  1440.   add_com ("frame", class_stack, frame_command,
  1441.        "Select and print a stack frame.\n\
  1442. With no argument, print the selected stack frame.  (See also \"info frame\").\n\
  1443. An argument specifies the frame to select.\n\
  1444. It can be a stack frame number or the address of the frame.\n\
  1445. With argument, nothing is printed if input is coming from\n\
  1446. a command file or a user-defined command.");
  1447.  
  1448.   add_com_alias ("f", "frame", class_stack, 1);
  1449.  
  1450.   add_com ("select-frame", class_stack, select_frame_command,
  1451.        "Select a stack frame without printing anything.\n\
  1452. An argument specifies the frame to select.\n\
  1453. It can be a stack frame number or the address of the frame.\n");
  1454.  
  1455.   add_com ("backtrace", class_stack, backtrace_command,
  1456.        "Print backtrace of all stack frames, or innermost COUNT frames.\n\
  1457. With a negative argument, print outermost -COUNT frames.");
  1458.   add_com_alias ("bt", "backtrace", class_stack, 0);
  1459.   add_com_alias ("where", "backtrace", class_alias, 0);
  1460.   add_info ("stack", backtrace_command,
  1461.         "Backtrace of the stack, or innermost COUNT frames.");
  1462.   add_info_alias ("s", "stack", 1);
  1463.   add_info ("frame", frame_info,
  1464.         "All about selected stack frame, or frame at ADDR.");
  1465.   add_info_alias ("f", "frame", 1);
  1466.   add_info ("locals", locals_info,
  1467.         "Local variables of current stack frame.");
  1468.   add_info ("args", args_info,
  1469.         "Argument variables of current stack frame.");
  1470.   add_info ("catch", catch_info,
  1471.         "Exceptions that can be caught in the current stack frame.");
  1472.  
  1473. #if 0
  1474.   add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command, 
  1475.        "Specify maximum number of frames for \"backtrace\" to print by default.",
  1476.        &setlist);
  1477.   add_info ("backtrace-limit", backtrace_limit_info,
  1478.         "The maximum number of frames for \"backtrace\" to print by default.");
  1479. #endif
  1480. }
  1481.