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