home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / gdb / stack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  30.8 KB  |  1,172 lines

  1. /* Print and select stack frames for GDB, the GNU debugger.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21.  
  22. #include "defs.h"
  23. #include "param.h"
  24. #include "symtab.h"
  25. #include "frame.h"
  26. #include "gdbcmd.h"
  27. #include "value.h"
  28. #include "gdbcore.h"
  29. #include "target.h"
  30. #include "breakpoint.h"
  31.  
  32. extern int addressprint;    /* Print addresses, or stay symbolic only? */
  33. extern int info_verbose;    /* Verbosity of symbol reading msgs */
  34. extern char *reg_names[];    /* Names of registers */
  35.  
  36. /* Thie "selected" stack frame is used by default for local and arg access.
  37.    May be zero, for no selected frame.  */
  38.  
  39. FRAME selected_frame;
  40.  
  41. /* Level of the selected frame:
  42.    0 for innermost, 1 for its caller, ...
  43.    or -1 for frame specified by address with no defined level.  */
  44.  
  45. int selected_frame_level;
  46.  
  47. /* Nonzero means print the full filename and linenumber
  48.    when a frame is printed, and do so in a format programs can parse.  */
  49.  
  50. int frame_file_full_name = 0;
  51.  
  52. void print_frame_info ();
  53.  
  54. /* Print a stack frame briefly.  FRAME should be the frame id
  55.    and LEVEL should be its level in the stack (or -1 for level not defined).
  56.    This prints the level, the function executing, the arguments,
  57.    and the file name and line number.
  58.    If the pc is not at the beginning of the source line,
  59.    the actual pc is printed at the beginning.
  60.  
  61.    If SOURCE is 1, print the source line as well.
  62.    If SOURCE is -1, print ONLY the source line.  */
  63.  
  64. static void
  65. print_stack_frame (frame, level, source)
  66.      FRAME frame;
  67.      int level;
  68.      int source;
  69. {
  70.   struct frame_info *fi;
  71.  
  72.   fi = get_frame_info (frame);
  73.  
  74.   print_frame_info (fi, level, source, 1);
  75. }
  76.  
  77. void
  78. print_frame_info (fi, level, source, args)
  79.      struct frame_info *fi;
  80.      register int level;
  81.      int source;
  82.      int args;
  83. {
  84.   struct symtab_and_line sal;
  85.   struct symbol *func;
  86.   register char *funname = 0;
  87.   int numargs;
  88.  
  89. #if 0    /* Symbol reading is fast enough now */
  90.   struct partial_symtab *pst;
  91.  
  92.   /* Don't give very much information if we haven't readin the
  93.      symbol table yet.  */
  94.   pst = find_pc_psymtab (fi->pc);
  95.   if (pst && !pst->readin)
  96.     {
  97.       /* Abbreviated information.  */
  98.       char *fname;
  99.  
  100.       if (!find_pc_partial_function (fi->pc, &fname, 0))
  101.     fname = "??";
  102.     
  103.       printf_filtered ("#%-2d ", level);
  104.       if (addressprint)
  105.         printf_filtered ("0x%x in ", fi->pc);
  106.  
  107.       fputs_demangled (fname, stdout, -1);
  108.       fputs_filtered (" (...)\n", stdout);
  109.       
  110.       return;
  111.     }
  112. #endif
  113.  
  114.   sal = find_pc_line (fi->pc, fi->next_frame);
  115.   func = find_pc_function (fi->pc);
  116.   if (func)
  117.     {
  118.       /* In certain pathological cases, the symtabs give the wrong
  119.      function (when we are in the first function in a file which
  120.      is compiled without debugging symbols, the previous function
  121.      is compiled with debugging symbols, and the "foo.o" symbol
  122.      that is supposed to tell us where the file with debugging symbols
  123.      ends has been truncated by ar because it is longer than 15
  124.      characters).
  125.  
  126.      So look in the misc_function_vector as well, and if it comes
  127.      up with a larger address for the function use that instead.
  128.      I don't think this can ever cause any problems;
  129.      there shouldn't be any
  130.      misc_function_vector symbols in the middle of a function.  */
  131.       int misc_index = find_pc_misc_function (fi->pc);
  132.       if (misc_index >= 0
  133.       && (misc_function_vector[misc_index].address
  134.           > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
  135.     {
  136.       /* In this case we have no way of knowing the source file
  137.          and line number, so don't print them.  */
  138.       sal.symtab = 0;
  139.       /* We also don't know anything about the function besides
  140.          its address and name.  */
  141.       func = 0;
  142.       funname = misc_function_vector[misc_index].name;
  143.     }
  144.       else
  145.     funname = SYMBOL_NAME (func);
  146.     }
  147.   else
  148.     {
  149.       register int misc_index = find_pc_misc_function (fi->pc);
  150.       if (misc_index >= 0)
  151.     funname = misc_function_vector[misc_index].name;
  152.     }
  153.  
  154.   if (source >= 0 || !sal.symtab)
  155.     {
  156.       if (level >= 0)
  157.     printf_filtered ("#%-2d ", level);
  158.       if (addressprint)
  159.     if (fi->pc != sal.pc || !sal.symtab)
  160.       printf_filtered ("0x%x in ", fi->pc);
  161.       fputs_demangled (funname ? funname : "??", stdout, -1);
  162.       wrap_here ("   ");
  163.       fputs_filtered (" (", stdout);
  164.       if (args)
  165.     {
  166.       FRAME_NUM_ARGS (numargs, fi);
  167.       print_frame_args (func, fi, numargs, stdout);
  168.     }
  169.       printf_filtered (")");
  170.       if (sal.symtab && sal.symtab->filename)
  171.     {
  172.           wrap_here ("   ");
  173.       printf_filtered (" at %s:%d", sal.symtab->filename, sal.line);
  174.     }
  175.       printf_filtered ("\n");
  176.     }
  177.  
  178.   if ((source != 0) && sal.symtab)
  179.     {
  180.       int done = 0;
  181.       int mid_statement = source < 0 && fi->pc != sal.pc;
  182.       if (frame_file_full_name)
  183.     done = identify_source_line (sal.symtab, sal.line, mid_statement);
  184.       if (!done)
  185.     {
  186.       if (addressprint && mid_statement)
  187.         printf_filtered ("0x%x\t", fi->pc);
  188.       print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
  189.     }
  190.       current_source_line = max (sal.line - lines_to_list () / 2, 1);
  191.     }
  192.   if (source != 0)
  193.     set_default_breakpoint (1, fi->pc, sal.symtab, sal.line);
  194.  
  195.   fflush (stdout);
  196. }
  197.  
  198. /* Call here to print info on selected frame, after a trap.  */
  199.  
  200. void
  201. print_sel_frame (just_source)
  202.      int just_source;
  203. {
  204.   print_stack_frame (selected_frame, -1, just_source ? -1 : 1);
  205. }
  206.  
  207. /* Print info on the selected frame, including level number
  208.    but not source.  */
  209.  
  210. void
  211. print_selected_frame ()
  212. {
  213.   print_stack_frame (selected_frame, selected_frame_level, 0);
  214. }
  215.  
  216. void flush_cached_frames ();
  217.  
  218. #ifdef FRAME_SPECIFICATION_DYADIC
  219. extern FRAME setup_arbitrary_frame ();
  220. #endif
  221.  
  222. /*
  223.  * Read a frame specification in whatever the appropriate format is.
  224.  * Call error() if the specification is in any way invalid (i.e.
  225.  * this function never returns NULL).
  226.  */
  227. static FRAME
  228. parse_frame_specification (frame_exp)
  229.      char *frame_exp;
  230. {
  231.   int numargs = 0;
  232.   int arg1, arg2;
  233.   
  234.   if (frame_exp)
  235.     {
  236.       char *addr_string, *p;
  237.       struct cleanup *tmp_cleanup;
  238.  
  239.       while (*frame_exp == ' ') frame_exp++;
  240.       for (p = frame_exp; *p && *p != ' '; p++)
  241.     ;
  242.  
  243.       if (*frame_exp)
  244.     {
  245.       numargs = 1;
  246.       addr_string = savestring(frame_exp, p - frame_exp);
  247.  
  248.       {
  249.         tmp_cleanup = make_cleanup (free, addr_string);
  250.         arg1 = parse_and_eval_address (addr_string);
  251.         do_cleanups (tmp_cleanup);
  252.       }
  253.  
  254.       while (*p == ' ') p++;
  255.       
  256.       if (*p)
  257.         {
  258.           numargs = 2;
  259.           arg2 = parse_and_eval_address (p);
  260.         }
  261.     }
  262.     }
  263.  
  264.   switch (numargs)
  265.     {
  266.     case 0:
  267.       if (selected_frame == NULL)
  268.     error ("No selected frame.");
  269.       return selected_frame;
  270.       /* NOTREACHED */
  271.     case 1:
  272.       {
  273.     int level = arg1;
  274.     FRAME fid = find_relative_frame (get_current_frame (), &level);
  275.     FRAME tfid;
  276.  
  277.     if (level == 0)
  278.       /* find_relative_frame was successful */
  279.       return fid;
  280.  
  281.     /* If (s)he specifies the frame with an address, he deserves what
  282.        (s)he gets.  Still, give the highest one that matches.  */
  283.  
  284.     for (fid = get_current_frame ();
  285.          fid && FRAME_FP (fid) != arg1;
  286.          fid = get_prev_frame (fid))
  287.       ;
  288.  
  289.     if (fid)
  290.       while ((tfid = get_prev_frame (fid)) &&
  291.          (FRAME_FP (tfid) == arg1))
  292.         fid = tfid;
  293.       
  294. #ifdef FRAME_SPECIFICATION_DYADIC
  295.     if (!fid)
  296.       error ("Incorrect number of args in frame specification");
  297.  
  298.     return fid;
  299. #else
  300.     return create_new_frame (arg1, 0);
  301. #endif
  302.       }
  303.       /* NOTREACHED */
  304.     case 2:
  305.       /* Must be addresses */
  306. #ifndef FRAME_SPECIFICATION_DYADIC
  307.       error ("Incorrect number of args in frame specification");
  308. #else
  309.       return setup_arbitrary_frame (arg1, arg2);
  310. #endif
  311.       /* NOTREACHED */
  312.     }
  313.   fatal ("Internal: Error in parsing in parse_frame_specification");
  314.   /* NOTREACHED */
  315. }
  316.  
  317. /* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except
  318.    that if it is unsure about the answer, it returns 0
  319.    instead of guessing (this happens on the VAX and i960, for example).
  320.  
  321.    On most machines, we never have to guess about the args address,
  322.    so FRAME_ARGS_ADDRESS{,_CORRECT} are the same.  */
  323. #if !defined (FRAME_ARGS_ADDRESS_CORRECT)
  324. #define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS
  325. #endif
  326.  
  327. /* Print verbosely the selected frame or the frame at address ADDR.
  328.    This means absolutely all information in the frame is printed.  */
  329.  
  330. static void
  331. frame_info (addr_exp)
  332.      char *addr_exp;
  333. {
  334.   FRAME frame;
  335.   struct frame_info *fi;
  336.   struct frame_saved_regs fsr;
  337.   struct symtab_and_line sal;
  338.   struct symbol *func;
  339.   FRAME calling_frame;
  340.   int i, count;
  341.   char *funname = 0;
  342.  
  343.   if (!target_has_stack)
  344.     error ("No inferior or core file.");
  345.  
  346.   frame = parse_frame_specification (addr_exp);
  347.   if (!frame)
  348.     error ("Invalid frame specified.");
  349.  
  350.   fi = get_frame_info (frame);
  351.   sal = find_pc_line (fi->pc, fi->next_frame);
  352.   func = get_frame_function (frame);
  353.   if (func)
  354.     funname = SYMBOL_NAME (func);
  355.   else
  356.     {
  357.       register int misc_index = find_pc_misc_function (fi->pc);
  358.       if (misc_index >= 0)
  359.     funname = misc_function_vector[misc_index].name;
  360.     }
  361.   calling_frame = get_prev_frame (frame);
  362.  
  363.   if (!addr_exp && selected_frame_level >= 0)
  364.     printf_filtered ("Stack level %d, frame at 0x%x:\n %s = 0x%x",
  365.         selected_frame_level, FRAME_FP(frame),
  366.         reg_names[PC_REGNUM], fi->pc);
  367.   else
  368.     printf_filtered ("Stack frame at 0x%x:\n %s = 0x%x",
  369.         FRAME_FP(frame), reg_names[PC_REGNUM], fi->pc);
  370.  
  371.   wrap_here ("   ");
  372.   if (funname)
  373.     printf_filtered (" in %s", funname);
  374.   wrap_here ("   ");
  375.   if (sal.symtab)
  376.     printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
  377.   puts_filtered ("; ");
  378.   wrap_here ("    ");
  379.   printf_filtered ("saved %s 0x%x\n", reg_names[PC_REGNUM],
  380.                       FRAME_SAVED_PC (frame));
  381.   if (calling_frame)
  382.     printf_filtered (" called by frame at 0x%x", FRAME_FP (calling_frame));
  383.   if (fi->next_frame && calling_frame)
  384.     puts_filtered (",");
  385.   wrap_here ("   ");
  386.   if (fi->next_frame)
  387.     printf_filtered (" caller of frame at 0x%x", fi->next_frame);
  388.   if (fi->next_frame || calling_frame)
  389.     puts_filtered ("\n");
  390.  
  391.   {
  392.     /* Address of the argument list for this frame, or 0.  */
  393.     CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
  394.     /* Number of args for this frame, or -1 if unknown.  */
  395.     int numargs;
  396.  
  397.     if (arg_list == 0)
  398.     printf_filtered (" Arglist at unknown address.\n");
  399.     else
  400.       {
  401.     printf_filtered (" Arglist at 0x%x,", arg_list);
  402.  
  403.     FRAME_NUM_ARGS (numargs, fi);
  404.     if (numargs < 0)
  405.       puts_filtered (" args: ");
  406.     else if (numargs == 0)
  407.       puts_filtered (" no args.");
  408.     else if (numargs == 1)
  409.       puts_filtered (" 1 arg: ");
  410.     else
  411.       printf_filtered (" %d args: ", numargs);
  412.     print_frame_args (func, fi, numargs, stdout);
  413.     puts_filtered ("\n");
  414.       }
  415.   }
  416.  
  417. #if defined (FRAME_FIND_SAVED_REGS)  
  418.   get_frame_saved_regs (fi, &fsr);
  419.   /* The sp is special; what's returned isn't the save address, but
  420.      actually the value of the previous frame's sp.  */
  421.   printf_filtered (" Previous frame's sp is 0x%x\n", fsr.regs[SP_REGNUM]);
  422.   count = 0;
  423.   for (i = 0; i < NUM_REGS; i++)
  424.     if (fsr.regs[i] && i != SP_REGNUM)
  425.       {
  426.     if (count == 0)
  427.       puts_filtered (" Saved registers:\n ");
  428.     else
  429.       puts_filtered (",");
  430.     wrap_here (" ");
  431.     printf_filtered (" %s at 0x%x", reg_names[i], fsr.regs[i]);
  432.     count++;
  433.       }
  434.   if (count)
  435.     puts_filtered ("\n");
  436. #endif /* Have FRAME_FIND_SAVED_REGS.  */
  437. }
  438.  
  439. #if 0
  440. /* Set a limit on the number of frames printed by default in a
  441.    backtrace.  */
  442.  
  443. static int backtrace_limit;
  444.  
  445. static void
  446. set_backtrace_limit_command (count_exp, from_tty)
  447.      char *count_exp;
  448.      int from_tty;
  449. {
  450.   int count = parse_and_eval_address (count_exp);
  451.  
  452.   if (count < 0)
  453.     error ("Negative argument not meaningful as backtrace limit.");
  454.  
  455.   backtrace_limit = count;
  456. }
  457.  
  458. static void
  459. backtrace_limit_info (arg, from_tty)
  460.      char *arg;
  461.      int from_tty;
  462. {
  463.   if (arg)
  464.     error ("\"Info backtrace-limit\" takes no arguments.");
  465.  
  466.   printf ("Backtrace limit: %d.\n", backtrace_limit);
  467. }
  468. #endif
  469.  
  470. /* Print briefly all stack frames or just the innermost COUNT frames.  */
  471.  
  472. static void
  473. backtrace_command (count_exp, from_tty)
  474.      char *count_exp;
  475.      int from_tty;
  476. {
  477.   struct frame_info *fi;
  478.   register int count;
  479.   register FRAME frame;
  480.   register int i;
  481.   register FRAME trailing;
  482.   register int trailing_level;
  483.  
  484.   if (!target_has_stack)
  485.     error ("No stack.");
  486.  
  487.   /* The following code must do two things.  First, it must
  488.      set the variable TRAILING to the frame from which we should start
  489.      printing.  Second, it must set the variable count to the number
  490.      of frames which we should print, or -1 if all of them.  */
  491.   trailing = get_current_frame ();
  492.   trailing_level = 0;
  493.   if (count_exp)
  494.     {
  495.       count = parse_and_eval_address (count_exp);
  496.       if (count < 0)
  497.     {
  498.       FRAME current;
  499.  
  500.       count = -count;
  501.  
  502.       current = trailing;
  503.       while (current && count--)
  504.         {
  505.           QUIT;
  506.           current = get_prev_frame (current);
  507.         }
  508.       
  509.       /* Will stop when CURRENT reaches the top of the stack.  TRAILING
  510.          will be COUNT below it.  */
  511.       while (current)
  512.         {
  513.           QUIT;
  514.           trailing = get_prev_frame (trailing);
  515.           current = get_prev_frame (current);
  516.           trailing_level++;
  517.         }
  518.       
  519.       count = -1;
  520.     }
  521.     }
  522.   else
  523.     count = -1;
  524.  
  525.   if (info_verbose)
  526.     {
  527.       struct partial_symtab *ps;
  528.       
  529.       /* Read in symbols for all of the frames.  Need to do this in
  530.      a separate pass so that "Reading in symbols for xxx" messages
  531.      don't screw up the appearance of the backtrace.  Also
  532.      if people have strong opinions against reading symbols for
  533.      backtrace this may have to be an option.  */
  534.       i = count;
  535.       for (frame = trailing;
  536.        frame != NULL && i--;
  537.        frame = get_prev_frame (frame))
  538.     {
  539.       QUIT;
  540.       fi = get_frame_info (frame);
  541.       ps = find_pc_psymtab (fi->pc);
  542.       if (ps)
  543.         (void) PSYMTAB_TO_SYMTAB (ps);    /* Force syms to come in */
  544.     }
  545.     }
  546.  
  547.   for (i = 0, frame = trailing;
  548.        frame && count--;
  549.        i++, frame = get_prev_frame (frame))
  550.     {
  551.       QUIT;
  552.       fi = get_frame_info (frame);
  553.       print_frame_info (fi, trailing_level + i, 0, 1);
  554.     }
  555.  
  556.   /* If we've stopped before the end, mention that.  */
  557.   if (frame && from_tty)
  558.     printf_filtered ("(More stack frames follow...)\n");
  559. }
  560.  
  561. /* Print the local variables of a block B active in FRAME.
  562.    Return 1 if any variables were printed; 0 otherwise.  */
  563.  
  564. static int
  565. print_block_frame_locals (b, frame, stream)
  566.      struct block *b;
  567.      register FRAME frame;
  568.      register FILE *stream;
  569. {
  570.   int nsyms;
  571.   register int i;
  572.   register struct symbol *sym;
  573.   register int values_printed = 0;
  574.  
  575.   nsyms = BLOCK_NSYMS (b);
  576.  
  577.   for (i = 0; i < nsyms; i++)
  578.     {
  579.       sym = BLOCK_SYM (b, i);
  580.       if (SYMBOL_CLASS (sym) == LOC_LOCAL
  581.       || SYMBOL_CLASS (sym) == LOC_REGISTER
  582.       || SYMBOL_CLASS (sym) == LOC_STATIC)
  583.     {
  584.       values_printed = 1;
  585.       fprint_symbol (stream, SYMBOL_NAME (sym));
  586.       fputs_filtered (" = ", stream);
  587.       print_variable_value (sym, frame, stream);
  588.       fprintf_filtered (stream, "\n");
  589.       fflush (stream);
  590.     }
  591.     }
  592.   return values_printed;
  593. }
  594.  
  595. /* Same, but print labels.  */
  596.  
  597. static int
  598. print_block_frame_labels (b, have_default, stream)
  599.      struct block *b;
  600.      int *have_default;
  601.      register FILE *stream;
  602. {
  603.   int nsyms;
  604.   register int i;
  605.   register struct symbol *sym;
  606.   register int values_printed = 0;
  607.  
  608.   nsyms = BLOCK_NSYMS (b);
  609.  
  610.   for (i = 0; i < nsyms; i++)
  611.     {
  612.       sym = BLOCK_SYM (b, i);
  613.       if (! strcmp (SYMBOL_NAME (sym), "default"))
  614.     {
  615.       if (*have_default)
  616.         continue;
  617.       *have_default = 1;
  618.     }
  619.       if (SYMBOL_CLASS (sym) == LOC_LABEL)
  620.     {
  621.       struct symtab_and_line sal;
  622.       sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
  623.       values_printed = 1;
  624.       fputs_demangled (SYMBOL_NAME (sym), stream, 1);
  625.       if (addressprint)
  626.         fprintf_filtered (stream, " 0x%x", SYMBOL_VALUE_ADDRESS (sym));
  627.       fprintf_filtered (stream, " in file %s, line %d\n",
  628.                 sal.symtab->filename, sal.line);
  629.       fflush (stream);
  630.     }
  631.     }
  632.   return values_printed;
  633. }
  634.  
  635. /* Print on STREAM all the local variables in frame FRAME,
  636.    including all the blocks active in that frame
  637.    at its current pc.
  638.  
  639.    Returns 1 if the job was done,
  640.    or 0 if nothing was printed because we have no info
  641.    on the function running in FRAME.  */
  642.  
  643. static int
  644. print_frame_local_vars (frame, stream)
  645.      register FRAME frame;
  646.      register FILE *stream;
  647. {
  648.   register struct block *block = get_frame_block (frame);
  649.   register int values_printed = 0;
  650.  
  651.   if (block == 0)
  652.     {
  653.       fprintf_filtered (stream, "No symbol table info available.\n");
  654.       fflush (stream);
  655.       return 0;
  656.     }
  657.   
  658.   while (block != 0)
  659.     {
  660.       if (print_block_frame_locals (block, frame, stream))
  661.     values_printed = 1;
  662.       /* After handling the function's top-level block, stop.
  663.      Don't continue to its superblock, the block of
  664.      per-file symbols.  */
  665.       if (BLOCK_FUNCTION (block))
  666.     break;
  667.       block = BLOCK_SUPERBLOCK (block);
  668.     }
  669.  
  670.   if (!values_printed)
  671.     {
  672.       fprintf_filtered (stream, "No locals.\n");
  673.       fflush (stream);
  674.     }
  675.   
  676.   return 1;
  677. }
  678.  
  679. /* Same, but print labels.  */
  680.  
  681. static int
  682. print_frame_label_vars (frame, this_level_only, stream)
  683.      register FRAME frame;
  684.      int this_level_only;
  685.      register FILE *stream;
  686. {
  687.   extern struct blockvector *blockvector_for_pc ();
  688.   register struct blockvector *bl;
  689.   register struct block *block = get_frame_block (frame);
  690.   register int values_printed = 0;
  691.   int index, have_default = 0;
  692.   char *blocks_printed;
  693.   struct frame_info *fi = get_frame_info (frame);
  694.   CORE_ADDR pc = fi->pc;
  695.  
  696.   if (block == 0)
  697.     {
  698.       fprintf_filtered (stream, "No symbol table info available.\n");
  699.       fflush (stream);
  700.       return 0;
  701.     }
  702.  
  703.   bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
  704.   blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
  705.   bzero (blocks_printed, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
  706.  
  707.   while (block != 0)
  708.     {
  709.       CORE_ADDR end = BLOCK_END (block) - 4;
  710.       int last_index;
  711.  
  712.       if (bl != blockvector_for_pc (end, &index))
  713.     error ("blockvector blotch");
  714.       if (BLOCKVECTOR_BLOCK (bl, index) != block)
  715.     error ("blockvector botch");
  716.       last_index = BLOCKVECTOR_NBLOCKS (bl);
  717.       index += 1;
  718.  
  719.       /* Don't print out blocks that have gone by.  */
  720.       while (index < last_index
  721.          && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
  722.     index++;
  723.  
  724.       while (index < last_index
  725.          && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
  726.     {
  727.       if (blocks_printed[index] == 0)
  728.         {
  729.           if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
  730.         values_printed = 1;
  731.           blocks_printed[index] = 1;
  732.         }
  733.       index++;
  734.     }
  735.       if (have_default)
  736.     return 1;
  737.       if (values_printed && this_level_only)
  738.     return 1;
  739.  
  740.       /* After handling the function's top-level block, stop.
  741.      Don't continue to its superblock, the block of
  742.      per-file symbols.  */
  743.       if (BLOCK_FUNCTION (block))
  744.     break;
  745.       block = BLOCK_SUPERBLOCK (block);
  746.     }
  747.  
  748.   if (!values_printed && !this_level_only)
  749.     {
  750.       fprintf_filtered (stream, "No catches.\n");
  751.       fflush (stream);
  752.     }
  753.   
  754.   return values_printed;
  755. }
  756.  
  757. /* ARGSUSED */
  758. static void
  759. locals_info (args, from_tty)
  760.      char *args;
  761.      int from_tty;
  762. {
  763.   if (!selected_frame)
  764.     error ("No frame selected.");
  765.   print_frame_local_vars (selected_frame, stdout);
  766. }
  767.  
  768. static void
  769. catch_info ()
  770. {
  771.   if (!selected_frame)
  772.     error ("No frame selected.");
  773.   print_frame_label_vars (selected_frame, 0, stdout);
  774. }
  775.  
  776. static int
  777. print_frame_arg_vars (frame, stream)
  778.      register FRAME frame;
  779.      register FILE *stream;
  780. {
  781.   struct symbol *func = get_frame_function (frame);
  782.   register struct block *b;
  783.   int nsyms;
  784.   register int i;
  785.   register struct symbol *sym, *sym2;
  786.   register int values_printed = 0;
  787.  
  788.   if (func == 0)
  789.     {
  790.       fprintf_filtered (stream, "No symbol table info available.\n");
  791.       fflush (stream);
  792.       return 0;
  793.     }
  794.  
  795.   b = SYMBOL_BLOCK_VALUE (func);
  796.   nsyms = BLOCK_NSYMS (b);
  797.  
  798.   for (i = 0; i < nsyms; i++)
  799.     {
  800.       sym = BLOCK_SYM (b, i);
  801.       if (SYMBOL_CLASS (sym) == LOC_ARG
  802.       || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
  803.       || SYMBOL_CLASS (sym) == LOC_REF_ARG
  804.       || SYMBOL_CLASS (sym) == LOC_REGPARM)
  805.     {
  806.       values_printed = 1;
  807.       fprint_symbol (stream, SYMBOL_NAME (sym));
  808.       fputs_filtered (" = ", stream);
  809.       /* We have to look up the symbol because arguments often have
  810.          two entries (one a parameter, one a register) and the one
  811.          we want is the register, which lookup_symbol will find for
  812.          us.  */
  813.       sym2 = lookup_symbol (SYMBOL_NAME (sym),
  814.             b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL);
  815.       print_variable_value (sym2, frame, stream);
  816.       fprintf_filtered (stream, "\n");
  817.       fflush (stream);
  818.     }
  819.     }
  820.  
  821.   if (!values_printed)
  822.     {
  823.       fprintf_filtered (stream, "No arguments.\n");
  824.       fflush (stream);
  825.     }
  826.  
  827.   return 1;
  828. }
  829.  
  830. static void
  831. args_info ()
  832. {
  833.   if (!selected_frame)
  834.     error ("No frame selected.");
  835.   print_frame_arg_vars (selected_frame, stdout);
  836. }
  837.  
  838. /* Select frame FRAME, and note that its stack level is LEVEL.
  839.    LEVEL may be -1 if an actual level number is not known.  */
  840.  
  841. void
  842. select_frame (frame, level)
  843.      FRAME frame;
  844.      int level;
  845. {
  846.   selected_frame = frame;
  847.   selected_frame_level = level;
  848.   /* Ensure that symbols for this frame are readin.  */
  849.   if (frame)
  850.     find_pc_symtab (get_frame_info (frame)->pc);
  851. }
  852.  
  853. /* Store the selected frame and its level into *FRAMEP and *LEVELP.
  854.    If there is no selected frame, *FRAMEP is set to NULL.  */
  855.  
  856. void
  857. record_selected_frame (frameaddrp, levelp)
  858.      FRAME_ADDR *frameaddrp;
  859.      int *levelp;
  860. {
  861.   *frameaddrp = selected_frame ? FRAME_FP (selected_frame) : NULL;
  862.   *levelp = selected_frame_level;
  863. }
  864.  
  865. /* Return the symbol-block in which the selected frame is executing.
  866.    Can return zero under various legitimate circumstances.  */
  867.  
  868. struct block *
  869. get_selected_block ()
  870. {
  871.   if (!target_has_stack)
  872.     return 0;
  873.  
  874.   if (!selected_frame)
  875.     return get_current_block ();
  876.   return get_frame_block (selected_frame);
  877. }
  878.  
  879. /* Find a frame a certain number of levels away from FRAME.
  880.    LEVEL_OFFSET_PTR points to an int containing the number of levels.
  881.    Positive means go to earlier frames (up); negative, the reverse.
  882.    The int that contains the number of levels is counted toward
  883.    zero as the frames for those levels are found.
  884.    If the top or bottom frame is reached, that frame is returned,
  885.    but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
  886.    how much farther the original request asked to go.  */
  887.  
  888. FRAME
  889. find_relative_frame (frame, level_offset_ptr)
  890.      register FRAME frame;
  891.      register int* level_offset_ptr;
  892. {
  893.   register FRAME prev;
  894.   register FRAME frame1, frame2;
  895.  
  896.   /* Going up is simple: just do get_prev_frame enough times
  897.      or until initial frame is reached.  */
  898.   while (*level_offset_ptr > 0)
  899.     {
  900.       prev = get_prev_frame (frame);
  901.       if (prev == 0)
  902.     break;
  903.       (*level_offset_ptr)--;
  904.       frame = prev;
  905.     }
  906.   /* Going down could be done by iterating get_frame_info to
  907.      find the next frame, but that would be quadratic
  908.      since get_frame_info must scan all the way from the current frame.
  909.      The following algorithm is linear.  */
  910.   if (*level_offset_ptr < 0)
  911.     {
  912. #if 0
  913. /* This is ancient and unnecessary?             -- gnu@cygnus.com 
  914.    It also loops forever if frame #0 is not current_frame (e.g. when we have
  915.    used the "frame" command after the stack was invalid).  */
  916.  
  917.       /* First put frame1 at innermost frame
  918.      and frame2 N levels up from there.  */
  919.       frame1 = get_current_frame ();
  920.       frame2 = frame1;
  921.       while (*level_offset_ptr < 0 && frame2 != frame)
  922.     {
  923.       frame2 = get_prev_frame (frame2);
  924.       (*level_offset_ptr) ++;
  925.     }
  926.       /* Then slide frame1 and frame2 up in synchrony
  927.      and when frame2 reaches our starting point
  928.      frame1 must be N levels down from there.  */
  929.       while (frame2 != frame)
  930.     {
  931.       frame1 = get_prev_frame (frame1);
  932.       frame2 = get_prev_frame (frame2);
  933.     }
  934.       return frame1;
  935. #else
  936.       while (*level_offset_ptr < 0) {
  937.     frame1 = get_next_frame (frame);
  938.     if (!frame1)
  939.       break;
  940.     frame = frame1;
  941.     (*level_offset_ptr)++;
  942.       }
  943. #endif
  944.     }
  945.   return frame;
  946. }
  947.  
  948. /* The "frame" command.  With no arg, print selected frame briefly.
  949.    With arg LEVEL_EXP, select the frame at level LEVEL if it is a
  950.    valid level.  Otherwise, treat level_exp as an address expression
  951.    and print it.  See parse_frame_specification for more info on proper
  952.    frame expressions. */
  953.  
  954. static void
  955. frame_command (level_exp, from_tty)
  956.      char *level_exp;
  957.      int from_tty;
  958. {
  959.   register FRAME frame, frame1;
  960.   unsigned int level = 0;
  961.  
  962.   if (!target_has_stack)
  963.     error ("No stack.");
  964.  
  965.   frame = parse_frame_specification (level_exp);
  966.  
  967.   /* Try to figure out what level this frame is.  But if there is
  968.      no current stack, don't error out -- let the user set one.  */
  969.   frame1 = 0;
  970.   if (get_current_frame()) {
  971.     for (frame1 = get_prev_frame (0);
  972.      frame1 && frame1 != frame;
  973.      frame1 = get_prev_frame (frame1))
  974.       level++;
  975.   }
  976.  
  977.   if (!frame1)
  978.     level = 0;
  979.  
  980.   select_frame (frame, level);
  981.  
  982.   if (!from_tty)
  983.     return;
  984.  
  985.   print_stack_frame (selected_frame, selected_frame_level, 1);
  986. }
  987.  
  988. /* Select the frame up one or COUNT stack levels
  989.    from the previously selected frame, and print it briefly.  */
  990.  
  991. /* ARGSUSED */
  992. static void
  993. up_silently_command (count_exp, from_tty)
  994.      char *count_exp;
  995.      int from_tty;
  996. {
  997.   register FRAME frame;
  998.   int count = 1, count1;
  999.   if (count_exp)
  1000.     count = parse_and_eval_address (count_exp);
  1001.   count1 = count;
  1002.   
  1003.   if (!target_has_stack)
  1004.     error ("No stack.");
  1005.  
  1006.   frame = find_relative_frame (selected_frame, &count1);
  1007.   if (count1 != 0 && count_exp == 0)
  1008.     error ("Initial frame selected; you cannot go up.");
  1009.   select_frame (frame, selected_frame_level + count - count1);
  1010. }
  1011.  
  1012. static void
  1013. up_command (count_exp, from_tty)
  1014.      char *count_exp;
  1015.      int from_tty;
  1016. {
  1017.   up_silently_command (count_exp, from_tty);
  1018.   print_stack_frame (selected_frame, selected_frame_level, 1);
  1019. }
  1020.  
  1021. /* Select the frame down one or COUNT stack levels
  1022.    from the previously selected frame, and print it briefly.  */
  1023.  
  1024. /* ARGSUSED */
  1025. static void
  1026. down_silently_command (count_exp, from_tty)
  1027.      char *count_exp;
  1028.      int from_tty;
  1029. {
  1030.   register FRAME frame;
  1031.   int count = -1, count1;
  1032.   if (count_exp)
  1033.     count = - parse_and_eval_address (count_exp);
  1034.   count1 = count;
  1035.   
  1036.   frame = find_relative_frame (selected_frame, &count1);
  1037.   if (count1 != 0 && count_exp == 0)
  1038.     error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
  1039.   select_frame (frame, selected_frame_level + count - count1);
  1040. }
  1041.  
  1042.  
  1043. static void
  1044. down_command (count_exp, from_tty)
  1045.      char *count_exp;
  1046.      int from_tty;
  1047. {
  1048.   down_silently_command (count_exp, from_tty);
  1049.   print_stack_frame (selected_frame, selected_frame_level, 1);
  1050. }
  1051.  
  1052. static void
  1053. return_command (retval_exp, from_tty)
  1054.      char *retval_exp;
  1055.      int from_tty;
  1056. {
  1057.   struct symbol *thisfun;
  1058.   FRAME_ADDR selected_frame_addr;
  1059.   CORE_ADDR selected_frame_pc;
  1060.   FRAME frame;
  1061.  
  1062.   if (selected_frame == NULL)
  1063.     error ("No selected frame.");
  1064.   thisfun = get_frame_function (selected_frame);
  1065.   selected_frame_addr = FRAME_FP (selected_frame);
  1066.   selected_frame_pc = (get_frame_info (selected_frame))->pc;
  1067.  
  1068.   /* If interactive, require confirmation.  */
  1069.  
  1070.   if (from_tty)
  1071.     {
  1072.       if (thisfun != 0)
  1073.     {
  1074.       if (!query ("Make %s return now? ", SYMBOL_NAME (thisfun)))
  1075.         error ("Not confirmed.");
  1076.     }
  1077.       else
  1078.     if (!query ("Make selected stack frame return now? "))
  1079.       error ("Not confirmed.");
  1080.     }
  1081.  
  1082.   /* Do the real work.  Pop until the specified frame is current.  We
  1083.      use this method because the selected_frame is not valid after
  1084.      a POP_FRAME.  The pc comparison makes this work even if the
  1085.      selected frame shares its fp with another frame.  */
  1086.  
  1087.   while ( selected_frame_addr != FRAME_FP (frame = get_current_frame())
  1088.        || selected_frame_pc   != (get_frame_info (frame))->pc  )
  1089.     POP_FRAME;
  1090.  
  1091.   /* Then pop that frame.  */
  1092.  
  1093.   POP_FRAME;
  1094.  
  1095.   /* Compute the return value (if any) and store in the place
  1096.      for return values.  */
  1097.  
  1098.   if (retval_exp)
  1099.     set_return_value (parse_and_eval (retval_exp));
  1100.  
  1101.   /* If interactive, print the frame that is now current.  */
  1102.  
  1103.   if (from_tty)
  1104.     frame_command ("0", 1);
  1105. }
  1106.  
  1107. void
  1108. _initialize_stack ()
  1109. {
  1110. #if 0  
  1111.   backtrace_limit = 30;
  1112. #endif
  1113.  
  1114.   add_com ("return", class_stack, return_command,
  1115.        "Make selected stack frame return to its caller.\n\
  1116. Control remains in the debugger, but when you continue\n\
  1117. execution will resume in the frame above the one now selected.\n\
  1118. If an argument is given, it is an expression for the value to return.");
  1119.  
  1120.   add_com ("up", class_stack, up_command,
  1121.        "Select and print stack frame that called this one.\n\
  1122. An argument says how many frames up to go.");
  1123.   add_com ("up-silently", class_support, up_silently_command,
  1124.        "Same as the `up' command, but does not print anything.\n\
  1125. This is useful in command scripts.");
  1126.  
  1127.   add_com ("down", class_stack, down_command,
  1128.        "Select and print stack frame called by this one.\n\
  1129. An argument says how many frames down to go.");
  1130.   add_com_alias ("do", "down", class_stack, 1);
  1131.   add_com ("down-silently", class_support, down_silently_command,
  1132.        "Same as the `down' command, but does not print anything.\n\
  1133. This is useful in command scripts.");
  1134.  
  1135.   add_com ("frame", class_stack, frame_command,
  1136.        "Select and print a stack frame.\n\
  1137. With no argument, print the selected stack frame.  (See also \"info frame\").\n\
  1138. An argument specifies the frame to select.\n\
  1139. It can be a stack frame number or the address of the frame.\n\
  1140. With argument, nothing is printed if input is coming from\n\
  1141. a command file or a user-defined command.");
  1142.  
  1143.   add_com_alias ("f", "frame", class_stack, 1);
  1144.  
  1145.   add_com ("backtrace", class_stack, backtrace_command,
  1146.        "Print backtrace of all stack frames, or innermost COUNT frames.\n\
  1147. With a negative argument, print outermost -COUNT frames.");
  1148.   add_com_alias ("bt", "backtrace", class_stack, 0);
  1149.   add_com_alias ("where", "backtrace", class_alias, 0);
  1150.   add_info ("stack", backtrace_command,
  1151.         "Backtrace of the stack, or innermost COUNT frames.");
  1152.   add_info_alias ("s", "stack", 1);
  1153.   add_info ("frame", frame_info,
  1154.         "All about selected stack frame, or frame at ADDR.");
  1155.   add_info_alias ("f", "frame", 1);
  1156.   add_info ("locals", locals_info,
  1157.         "Local variables of current stack frame.");
  1158.   add_info ("args", args_info,
  1159.         "Argument variables of current stack frame.");
  1160.   add_info ("catch", catch_info,
  1161.         "Exceptions that can be caught in the current stack frame.");
  1162.  
  1163. #if 0
  1164.   add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command, 
  1165.        "Specify maximum number of frames for \"backtrace\" to print by default.",
  1166.        &setlist);
  1167.   add_info ("backtrace-limit", backtrace_limit_info,
  1168.         "The maximum number of frames for \"backtrace\" to print by default.");
  1169. #endif
  1170. }
  1171.  
  1172.