home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / gdb / infrun.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  52.7 KB  |  1,770 lines

  1. /* Target-struct-independent code to start (run) and stop an inferior process.
  2.    Copyright 1986, 1987, 1988, 1989, 1991, 1992, 1993
  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. /* Notes on the algorithm used in wait_for_inferior to determine if we
  22.    just did a subroutine call when stepping.  We have the following
  23.    information at that point:
  24.  
  25.                   Current and previous (just before this step) pc.
  26.           Current and previous sp.
  27.           Current and previous start of current function.
  28.  
  29.    If the starts of the functions don't match, then
  30.  
  31.        a) We did a subroutine call.
  32.  
  33.    In this case, the pc will be at the beginning of a function.
  34.  
  35.     b) We did a subroutine return.
  36.  
  37.    Otherwise.
  38.  
  39.     c) We did a longjmp.
  40.  
  41.    If we did a longjump, we were doing "nexti", since a next would
  42.    have attempted to skip over the assembly language routine in which
  43.    the longjmp is coded and would have simply been the equivalent of a
  44.    continue.  I consider this ok behaivior.  We'd like one of two
  45.    things to happen if we are doing a nexti through the longjmp()
  46.    routine: 1) It behaves as a stepi, or 2) It acts like a continue as
  47.    above.  Given that this is a special case, and that anybody who
  48.    thinks that the concept of sub calls is meaningful in the context
  49.    of a longjmp, I'll take either one.  Let's see what happens.  
  50.  
  51.    Acts like a subroutine return.  I can handle that with no problem
  52.    at all.
  53.  
  54.    -->So: If the current and previous beginnings of the current
  55.    function don't match, *and* the pc is at the start of a function,
  56.    we've done a subroutine call.  If the pc is not at the start of a
  57.    function, we *didn't* do a subroutine call.  
  58.  
  59.    -->If the beginnings of the current and previous function do match,
  60.    either: 
  61.  
  62.        a) We just did a recursive call.
  63.  
  64.        In this case, we would be at the very beginning of a
  65.        function and 1) it will have a prologue (don't jump to
  66.        before prologue, or 2) (we assume here that it doesn't have
  67.        a prologue) there will have been a change in the stack
  68.        pointer over the last instruction.  (Ie. it's got to put
  69.        the saved pc somewhere.  The stack is the usual place.  In
  70.        a recursive call a register is only an option if there's a
  71.        prologue to do something with it.  This is even true on
  72.        register window machines; the prologue sets up the new
  73.        window.  It might not be true on a register window machine
  74.        where the call instruction moved the register window
  75.        itself.  Hmmm.  One would hope that the stack pointer would
  76.        also change.  If it doesn't, somebody send me a note, and
  77.        I'll work out a more general theory.
  78.        bug-gdb@prep.ai.mit.edu).  This is true (albeit slipperly
  79.        so) on all machines I'm aware of:
  80.  
  81.           m68k:    Call changes stack pointer.  Regular jumps don't.
  82.  
  83.           sparc:    Recursive calls must have frames and therefor,
  84.                     prologues.
  85.  
  86.           vax:    All calls have frames and hence change the
  87.                     stack pointer.
  88.  
  89.     b) We did a return from a recursive call.  I don't see that we
  90.        have either the ability or the need to distinguish this
  91.        from an ordinary jump.  The stack frame will be printed
  92.        when and if the frame pointer changes; if we are in a
  93.        function without a frame pointer, it's the users own
  94.        lookout.
  95.  
  96.     c) We did a jump within a function.  We assume that this is
  97.        true if we didn't do a recursive call.
  98.  
  99.     d) We are in no-man's land ("I see no symbols here").  We
  100.        don't worry about this; it will make calls look like simple
  101.        jumps (and the stack frames will be printed when the frame
  102.        pointer moves), which is a reasonably non-violent response.
  103. */
  104.  
  105. #include "defs.h"
  106. #include <string.h>
  107. #include <ctype.h>
  108. #include "symtab.h"
  109. #include "frame.h"
  110. #include "inferior.h"
  111. #include "breakpoint.h"
  112. #include "wait.h"
  113. #include "gdbcore.h"
  114. #include "gdbcmd.h"
  115. #include "target.h"
  116.  
  117. #include <signal.h>
  118.  
  119. /* unistd.h is needed to #define X_OK */
  120. #ifdef USG
  121. #include <unistd.h>
  122. #else
  123. #include <sys/file.h>
  124. #endif
  125.  
  126. /* Prototypes for local functions */
  127.  
  128. static void
  129. signals_info PARAMS ((char *, int));
  130.  
  131. static void
  132. handle_command PARAMS ((char *, int));
  133.  
  134. static void
  135. sig_print_info PARAMS ((int));
  136.  
  137. static void
  138. sig_print_header PARAMS ((void));
  139.  
  140. static void
  141. remove_step_breakpoint PARAMS ((void));
  142.  
  143. static void
  144. insert_step_breakpoint PARAMS ((void));
  145.  
  146. static void
  147. resume_cleanups PARAMS ((int));
  148.  
  149. static int
  150. hook_stop_stub PARAMS ((char *));
  151.  
  152. /* Sigtramp is a routine that the kernel calls (which then calls the
  153.    signal handler).  On most machines it is a library routine that
  154.    is linked into the executable.
  155.  
  156.    This macro, given a program counter value and the name of the
  157.    function in which that PC resides (which can be null if the
  158.    name is not known), returns nonzero if the PC and name show
  159.    that we are in sigtramp.
  160.  
  161.    On most machines just see if the name is sigtramp (and if we have
  162.    no name, assume we are not in sigtramp).  */
  163. #if !defined (IN_SIGTRAMP)
  164. #define IN_SIGTRAMP(pc, name) \
  165.   (name && STREQ ("_sigtramp", name))
  166. #endif
  167.  
  168. /* GET_LONGJMP_TARGET returns the PC at which longjmp() will resume the
  169.    program.  It needs to examine the jmp_buf argument and extract the PC
  170.    from it.  The return value is non-zero on success, zero otherwise. */
  171. #ifndef GET_LONGJMP_TARGET
  172. #define GET_LONGJMP_TARGET(PC_ADDR) 0
  173. #endif
  174.  
  175.  
  176. /* Some machines have trampoline code that sits between function callers
  177.    and the actual functions themselves.  If this machine doesn't have
  178.    such things, disable their processing.  */
  179. #ifndef SKIP_TRAMPOLINE_CODE
  180. #define    SKIP_TRAMPOLINE_CODE(pc)    0
  181. #endif
  182.  
  183. /* For SVR4 shared libraries, each call goes through a small piece of
  184.    trampoline code in the ".init" section.  IN_SOLIB_TRAMPOLINE evaluates
  185.    to nonzero if we are current stopped in one of these. */
  186. #ifndef IN_SOLIB_TRAMPOLINE
  187. #define IN_SOLIB_TRAMPOLINE(pc,name)    0
  188. #endif
  189.  
  190. /* On some systems, the PC may be left pointing at an instruction that  won't
  191.    actually be executed.  This is usually indicated by a bit in the PSW.  If
  192.    we find ourselves in such a state, then we step the target beyond the
  193.    nullified instruction before returning control to the user so as to avoid
  194.    confusion. */
  195.  
  196. #ifndef INSTRUCTION_NULLIFIED
  197. #define INSTRUCTION_NULLIFIED 0
  198. #endif
  199.  
  200. #ifdef TDESC
  201. #include "tdesc.h"
  202. int safe_to_init_tdesc_context = 0;
  203. extern dc_dcontext_t current_context;
  204. #endif
  205.  
  206. /* Tables of how to react to signals; the user sets them.  */
  207.  
  208. static unsigned char *signal_stop;
  209. static unsigned char *signal_print;
  210. static unsigned char *signal_program;
  211.  
  212. #define SET_SIGS(nsigs,sigs,flags) \
  213.   do { \
  214.     int signum = (nsigs); \
  215.     while (signum-- > 0) \
  216.       if ((sigs)[signum]) \
  217.     (flags)[signum] = 1; \
  218.   } while (0)
  219.  
  220. #define UNSET_SIGS(nsigs,sigs,flags) \
  221.   do { \
  222.     int signum = (nsigs); \
  223.     while (signum-- > 0) \
  224.       if ((sigs)[signum]) \
  225.     (flags)[signum] = 0; \
  226.   } while (0)
  227.  
  228.  
  229. /* Command list pointer for the "stop" placeholder.  */
  230.  
  231. static struct cmd_list_element *stop_command;
  232.  
  233. /* Nonzero if breakpoints are now inserted in the inferior.  */
  234.  
  235. static int breakpoints_inserted;
  236.  
  237. /* Function inferior was in as of last step command.  */
  238.  
  239. static struct symbol *step_start_function;
  240.  
  241. /* Nonzero => address for special breakpoint for resuming stepping.  */
  242.  
  243. static CORE_ADDR step_resume_break_address;
  244.  
  245. /* Pointer to orig contents of the byte where the special breakpoint is.  */
  246.  
  247. static char step_resume_break_shadow[BREAKPOINT_MAX];
  248.  
  249. /* Nonzero means the special breakpoint is a duplicate
  250.    so it has not itself been inserted.  */
  251.  
  252. static int step_resume_break_duplicate;
  253.  
  254. /* Nonzero if we are expecting a trace trap and should proceed from it.  */
  255.  
  256. static int trap_expected;
  257.  
  258. /* Nonzero if the next time we try to continue the inferior, it will
  259.    step one instruction and generate a spurious trace trap.
  260.    This is used to compensate for a bug in HP-UX.  */
  261.  
  262. static int trap_expected_after_continue;
  263.  
  264. /* Nonzero means expecting a trace trap
  265.    and should stop the inferior and return silently when it happens.  */
  266.  
  267. int stop_after_trap;
  268.  
  269. /* Nonzero means expecting a trap and caller will handle it themselves.
  270.    It is used after attach, due to attaching to a process;
  271.    when running in the shell before the child program has been exec'd;
  272.    and when running some kinds of remote stuff (FIXME?).  */
  273.  
  274. int stop_soon_quietly;
  275.  
  276. /* Nonzero if pc has been changed by the debugger
  277.    since the inferior stopped.  */
  278.  
  279. int pc_changed;
  280.  
  281. /* Nonzero if proceed is being used for a "finish" command or a similar
  282.    situation when stop_registers should be saved.  */
  283.  
  284. int proceed_to_finish;
  285.  
  286. /* Save register contents here when about to pop a stack dummy frame,
  287.    if-and-only-if proceed_to_finish is set.
  288.    Thus this contains the return value from the called function (assuming
  289.    values are returned in a register).  */
  290.  
  291. char stop_registers[REGISTER_BYTES];
  292.  
  293. /* Nonzero if program stopped due to error trying to insert breakpoints.  */
  294.  
  295. static int breakpoints_failed;
  296.  
  297. /* Nonzero after stop if current stack frame should be printed.  */
  298.  
  299. static int stop_print_frame;
  300.  
  301. #ifdef NO_SINGLE_STEP
  302. extern int one_stepped;        /* From machine dependent code */
  303. extern void single_step ();    /* Same. */
  304. #endif /* NO_SINGLE_STEP */
  305.  
  306.  
  307. /* Things to clean up if we QUIT out of resume ().  */
  308. /* ARGSUSED */
  309. static void
  310. resume_cleanups (arg)
  311.      int arg;
  312. {
  313.   normal_stop ();
  314. }
  315.  
  316. /* Resume the inferior, but allow a QUIT.  This is useful if the user
  317.    wants to interrupt some lengthy single-stepping operation
  318.    (for child processes, the SIGINT goes to the inferior, and so
  319.    we get a SIGINT random_signal, but for remote debugging and perhaps
  320.    other targets, that's not true).
  321.  
  322.    STEP nonzero if we should step (zero to continue instead).
  323.    SIG is the signal to give the inferior (zero for none).  */
  324. void
  325. resume (step, sig)
  326.      int step;
  327.      int sig;
  328. {
  329.   struct cleanup *old_cleanups = make_cleanup (resume_cleanups, 0);
  330.   QUIT;
  331.  
  332. #ifdef NO_SINGLE_STEP
  333.   if (step) {
  334.     single_step(sig);    /* Do it the hard way, w/temp breakpoints */
  335.     step = 0;        /* ...and don't ask hardware to do it.  */
  336.   }
  337. #endif
  338.  
  339.   /* Handle any optimized stores to the inferior NOW...  */
  340. #ifdef DO_DEFERRED_STORES
  341.   DO_DEFERRED_STORES;
  342. #endif
  343.  
  344.   target_resume (step, sig);
  345.   discard_cleanups (old_cleanups);
  346. }
  347.  
  348.  
  349. /* Clear out all variables saying what to do when inferior is continued.
  350.    First do this, then set the ones you want, then call `proceed'.  */
  351.  
  352. void
  353. clear_proceed_status ()
  354. {
  355.   trap_expected = 0;
  356.   step_range_start = 0;
  357.   step_range_end = 0;
  358.   step_frame_address = 0;
  359.   step_over_calls = -1;
  360.   step_resume_break_address = 0;
  361.   stop_after_trap = 0;
  362.   stop_soon_quietly = 0;
  363.   proceed_to_finish = 0;
  364.   breakpoint_proceeded = 1;    /* We're about to proceed... */
  365.  
  366.   /* Discard any remaining commands or status from previous stop.  */
  367.   bpstat_clear (&stop_bpstat);
  368. }
  369.  
  370. /* Basic routine for continuing the program in various fashions.
  371.  
  372.    ADDR is the address to resume at, or -1 for resume where stopped.
  373.    SIGGNAL is the signal to give it, or 0 for none,
  374.      or -1 for act according to how it stopped.
  375.    STEP is nonzero if should trap after one instruction.
  376.      -1 means return after that and print nothing.
  377.      You should probably set various step_... variables
  378.      before calling here, if you are stepping.
  379.  
  380.    You should call clear_proceed_status before calling proceed.  */
  381.  
  382. void
  383. proceed (addr, siggnal, step)
  384.      CORE_ADDR addr;
  385.      int siggnal;
  386.      int step;
  387. {
  388.   int oneproc = 0;
  389.  
  390.   if (step > 0)
  391.     step_start_function = find_pc_function (read_pc ());
  392.   if (step < 0)
  393.     stop_after_trap = 1;
  394.  
  395.   if (addr == (CORE_ADDR)-1)
  396.     {
  397.       /* If there is a breakpoint at the address we will resume at,
  398.      step one instruction before inserting breakpoints
  399.      so that we do not stop right away.  */
  400.  
  401.       if (!pc_changed && breakpoint_here_p (read_pc ()))
  402.     oneproc = 1;
  403.     }
  404.   else
  405.     write_pc (addr);
  406.  
  407.   if (trap_expected_after_continue)
  408.     {
  409.       /* If (step == 0), a trap will be automatically generated after
  410.      the first instruction is executed.  Force step one
  411.      instruction to clear this condition.  This should not occur
  412.      if step is nonzero, but it is harmless in that case.  */
  413.       oneproc = 1;
  414.       trap_expected_after_continue = 0;
  415.     }
  416.  
  417.   if (oneproc)
  418.     /* We will get a trace trap after one instruction.
  419.        Continue it automatically and insert breakpoints then.  */
  420.     trap_expected = 1;
  421.   else
  422.     {
  423.       int temp = insert_breakpoints ();
  424.       if (temp)
  425.     {
  426.       print_sys_errmsg ("ptrace", temp);
  427.       error ("Cannot insert breakpoints.\n\
  428. The same program may be running in another process.");
  429.     }
  430.       breakpoints_inserted = 1;
  431.     }
  432.  
  433.   /* Install inferior's terminal modes.  */
  434.   target_terminal_inferior ();
  435.  
  436.   if (siggnal >= 0)
  437.     stop_signal = siggnal;
  438.   /* If this signal should not be seen by program,
  439.      give it zero.  Used for debugging signals.  */
  440.   else if (stop_signal < NSIG && !signal_program[stop_signal])
  441.     stop_signal= 0;
  442.  
  443.   /* Resume inferior.  */
  444.   resume (oneproc || step || bpstat_should_step (), stop_signal);
  445.  
  446.   /* Wait for it to stop (if not standalone)
  447.      and in any case decode why it stopped, and act accordingly.  */
  448.  
  449.   wait_for_inferior ();
  450.   normal_stop ();
  451. }
  452.  
  453. /* Record the pc and sp of the program the last time it stopped.
  454.    These are just used internally by wait_for_inferior, but need
  455.    to be preserved over calls to it and cleared when the inferior
  456.    is started.  */
  457. static CORE_ADDR prev_pc;
  458. static CORE_ADDR prev_sp;
  459. static CORE_ADDR prev_func_start;
  460. static char *prev_func_name;
  461.  
  462.  
  463. /* Start remote-debugging of a machine over a serial link.  */
  464.  
  465. void
  466. start_remote ()
  467. {
  468.   init_wait_for_inferior ();
  469.   clear_proceed_status ();
  470.   stop_soon_quietly = 1;
  471.   trap_expected = 0;
  472.   wait_for_inferior ();
  473.   normal_stop ();
  474. }
  475.  
  476. /* Initialize static vars when a new inferior begins.  */
  477.  
  478. void
  479. init_wait_for_inferior ()
  480. {
  481.   /* These are meaningless until the first time through wait_for_inferior.  */
  482.   prev_pc = 0;
  483.   prev_sp = 0;
  484.   prev_func_start = 0;
  485.   prev_func_name = NULL;
  486.  
  487.   trap_expected_after_continue = 0;
  488.   breakpoints_inserted = 0;
  489.   mark_breakpoints_out ();
  490.   stop_signal = 0;        /* Don't confuse first call to proceed(). */
  491. }
  492.  
  493.  
  494.  
  495. /* Wait for control to return from inferior to debugger.
  496.    If inferior gets a signal, we may decide to start it up again
  497.    instead of returning.  That is why there is a loop in this function.
  498.    When this function actually returns it means the inferior
  499.    should be left stopped and GDB should read more commands.  */
  500.  
  501. void
  502. wait_for_inferior ()
  503. {
  504.   WAITTYPE w;
  505.   int another_trap;
  506.   int random_signal;
  507.   CORE_ADDR stop_sp;
  508.   CORE_ADDR stop_func_start;
  509.   char *stop_func_name;
  510.   CORE_ADDR prologue_pc, tmp;
  511.   int stop_step_resume_break;
  512.   struct symtab_and_line sal;
  513.   int remove_breakpoints_on_following_step = 0;
  514.   int current_line;
  515.   int handling_longjmp = 0;    /* FIXME */
  516.   struct symtab *symtab;
  517.  
  518.   sal = find_pc_line(prev_pc, 0);
  519.   current_line = sal.line;
  520.  
  521.   while (1)
  522.     {
  523.       /* Clean up saved state that will become invalid.  */
  524.       pc_changed = 0;
  525.       flush_cached_frames ();
  526.       registers_changed ();
  527.  
  528.       target_wait (&w);
  529.  
  530. #ifdef SIGTRAP_STOP_AFTER_LOAD
  531.  
  532.       /* Somebody called load(2), and it gave us a "trap signal after load".
  533.          Ignore it gracefully. */
  534.  
  535.       SIGTRAP_STOP_AFTER_LOAD (w);
  536. #endif
  537.  
  538.       /* See if the process still exists; clean up if it doesn't.  */
  539.       if (WIFEXITED (w))
  540.     {
  541.       target_terminal_ours ();    /* Must do this before mourn anyway */
  542.       if (WEXITSTATUS (w))
  543.         printf_filtered ("\nProgram exited with code 0%o.\n", 
  544.              (unsigned int)WEXITSTATUS (w));
  545.       else
  546.         if (!batch_mode())
  547.           printf_filtered ("\nProgram exited normally.\n");
  548.       fflush (stdout);
  549.       target_mourn_inferior ();
  550. #ifdef NO_SINGLE_STEP
  551.       one_stepped = 0;
  552. #endif
  553.       stop_print_frame = 0;
  554.       break;
  555.     }
  556.       else if (!WIFSTOPPED (w))
  557.     {
  558.       stop_print_frame = 0;
  559.       stop_signal = WTERMSIG (w);
  560.       target_terminal_ours ();    /* Must do this before mourn anyway */
  561.       target_kill ();        /* kill mourns as well */
  562. #ifdef PRINT_RANDOM_SIGNAL
  563.       printf_filtered ("\nProgram terminated: ");
  564.       PRINT_RANDOM_SIGNAL (stop_signal);
  565. #else
  566.       printf_filtered ("\nProgram terminated with signal %d, %s\n",
  567.                stop_signal, safe_strsignal (stop_signal));
  568. #endif
  569.       printf_filtered ("The inferior process no longer exists.\n");
  570.       fflush (stdout);
  571. #ifdef NO_SINGLE_STEP
  572.       one_stepped = 0;
  573. #endif
  574.       break;
  575.     }
  576.       
  577. #ifdef NO_SINGLE_STEP
  578.       if (one_stepped)
  579.     single_step (0);    /* This actually cleans up the ss */
  580. #endif /* NO_SINGLE_STEP */
  581.       
  582. /* If PC is pointing at a nullified instruction, then step beyond it so that
  583.    the user won't be confused when GDB appears to be ready to execute it. */
  584.  
  585.       if (INSTRUCTION_NULLIFIED)
  586.     {
  587.       resume (1, 0);
  588.       continue;
  589.     }
  590.  
  591.       stop_pc = read_pc ();
  592.       set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  593.                         read_pc ()));
  594.       
  595.       stop_frame_address = FRAME_FP (get_current_frame ());
  596.       stop_sp = read_register (SP_REGNUM);
  597. /* XXX - FIXME.  Need to figure out a better way to grab the stack seg reg. */
  598. #ifdef GDB_TARGET_IS_H8500
  599.       stop_sp |= read_register (SEG_T_REGNUM) << 16;
  600. #endif
  601.       stop_func_start = 0;
  602.       stop_func_name = 0;
  603.       /* Don't care about return value; stop_func_start and stop_func_name
  604.      will both be 0 if it doesn't work.  */
  605.       find_pc_partial_function (stop_pc, &stop_func_name, &stop_func_start);
  606.       stop_func_start += FUNCTION_START_OFFSET;
  607.       another_trap = 0;
  608.       bpstat_clear (&stop_bpstat);
  609.       stop_step = 0;
  610.       stop_stack_dummy = 0;
  611.       stop_print_frame = 1;
  612.       stop_step_resume_break = 0;
  613.       random_signal = 0;
  614.       stopped_by_random_signal = 0;
  615.       breakpoints_failed = 0;
  616.       
  617.       /* Look at the cause of the stop, and decide what to do.
  618.      The alternatives are:
  619.      1) break; to really stop and return to the debugger,
  620.      2) drop through to start up again
  621.      (set another_trap to 1 to single step once)
  622.      3) set random_signal to 1, and the decision between 1 and 2
  623.      will be made according to the signal handling tables.  */
  624.       
  625.       stop_signal = WSTOPSIG (w);
  626.       
  627.       /* First, distinguish signals caused by the debugger from signals
  628.      that have to do with the program's own actions.
  629.      Note that breakpoint insns may cause SIGTRAP or SIGILL
  630.      or SIGEMT, depending on the operating system version.
  631.      Here we detect when a SIGILL or SIGEMT is really a breakpoint
  632.      and change it to SIGTRAP.  */
  633.       
  634.       if (stop_signal == SIGTRAP
  635.       || (breakpoints_inserted &&
  636.           (stop_signal == SIGILL
  637. #ifdef SIGEMT
  638.            || stop_signal == SIGEMT
  639. #endif
  640.             ))
  641.       || stop_soon_quietly)
  642.     {
  643.       if (stop_signal == SIGTRAP && stop_after_trap)
  644.         {
  645.           stop_print_frame = 0;
  646.           break;
  647.         }
  648.       if (stop_soon_quietly)
  649.         break;
  650.  
  651.       /* Don't even think about breakpoints
  652.          if just proceeded over a breakpoint.
  653.  
  654.          However, if we are trying to proceed over a breakpoint
  655.          and end up in sigtramp, then step_resume_break_address
  656.          will be set and we should check whether we've hit the
  657.          step breakpoint.  */
  658.       if (stop_signal == SIGTRAP && trap_expected
  659.           && step_resume_break_address == 0)
  660.         bpstat_clear (&stop_bpstat);
  661.       else
  662.         {
  663.           /* See if there is a breakpoint at the current PC.  */
  664. #if DECR_PC_AFTER_BREAK
  665.           /* Notice the case of stepping through a jump
  666.          that lands just after a breakpoint.
  667.          Don't confuse that with hitting the breakpoint.
  668.          What we check for is that 1) stepping is going on
  669.          and 2) the pc before the last insn does not match
  670.          the address of the breakpoint before the current pc.  */
  671.           if (prev_pc == stop_pc - DECR_PC_AFTER_BREAK
  672.           || !step_range_end
  673.           || step_resume_break_address
  674.           || handling_longjmp /* FIXME */)
  675. #endif /* DECR_PC_AFTER_BREAK not zero */
  676.         {
  677.           /* See if we stopped at the special breakpoint for
  678.              stepping over a subroutine call.  If both are zero,
  679.              this wasn't the reason for the stop.  */
  680.           if (step_resume_break_address
  681.               && stop_pc - DECR_PC_AFTER_BREAK
  682.                  == step_resume_break_address)
  683.             {
  684.               stop_step_resume_break = 1;
  685.               if (DECR_PC_AFTER_BREAK)
  686.             {
  687.               stop_pc -= DECR_PC_AFTER_BREAK;
  688.               write_pc (stop_pc);
  689.             }
  690.             }
  691.           else
  692.             {
  693.               stop_bpstat =
  694.             bpstat_stop_status (&stop_pc, stop_frame_address);
  695.               /* Following in case break condition called a
  696.              function.  */
  697.               stop_print_frame = 1;
  698.             }
  699.         }
  700.         }
  701.       
  702.       if (stop_signal == SIGTRAP)
  703.         random_signal
  704.           = !(bpstat_explains_signal (stop_bpstat)
  705.           || trap_expected
  706.           || stop_step_resume_break
  707.           || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
  708.           || (step_range_end && !step_resume_break_address));
  709.       else
  710.         {
  711.           random_signal
  712.         = !(bpstat_explains_signal (stop_bpstat)
  713.             || stop_step_resume_break
  714.             /* End of a stack dummy.  Some systems (e.g. Sony
  715.                news) give another signal besides SIGTRAP,
  716.                so check here as well as above.  */
  717.             || PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address)
  718.             );
  719.           if (!random_signal)
  720.         stop_signal = SIGTRAP;
  721.         }
  722.     }
  723.       else
  724.     random_signal = 1;
  725.       
  726.       /* For the program's own signals, act according to
  727.      the signal handling tables.  */
  728.       
  729.       if (random_signal)
  730.     {
  731.       /* Signal not for debugging purposes.  */
  732.       int printed = 0;
  733.       
  734.       stopped_by_random_signal = 1;
  735.       
  736.       if (stop_signal >= NSIG
  737.           || signal_print[stop_signal])
  738.         {
  739.           printed = 1;
  740.           target_terminal_ours_for_output ();
  741. #ifdef PRINT_RANDOM_SIGNAL
  742.           PRINT_RANDOM_SIGNAL (stop_signal);
  743. #else
  744.           printf_filtered ("\nProgram received signal %d, %s\n",
  745.                    stop_signal, safe_strsignal (stop_signal));
  746. #endif /* PRINT_RANDOM_SIGNAL */
  747.           fflush (stdout);
  748.         }
  749.       if (stop_signal >= NSIG
  750.           || signal_stop[stop_signal])
  751.         break;
  752.       /* If not going to stop, give terminal back
  753.          if we took it away.  */
  754.       else if (printed)
  755.         target_terminal_inferior ();
  756.  
  757.       /* Note that virtually all the code below does `if !random_signal'.
  758.          Perhaps this code should end with a goto or continue.  At least
  759.          one (now fixed) bug was caused by this -- a !random_signal was
  760.          missing in one of the tests below.  */
  761.     }
  762.  
  763.       /* Handle cases caused by hitting a breakpoint.  */
  764.  
  765.       if (!random_signal)
  766.     {
  767.       CORE_ADDR jmp_buf_pc;
  768.       enum bpstat_what what = bpstat_what (stop_bpstat);
  769.  
  770.       switch (what)
  771.         {
  772.         case BPSTAT_WHAT_SET_LONGJMP_RESUME:
  773.           /* If we hit the breakpoint at longjmp, disable it for the
  774.          duration of this command.  Then, install a temporary
  775.          breakpoint at the target of the jmp_buf. */
  776.           disable_longjmp_breakpoint();
  777.           remove_breakpoints ();
  778.           breakpoints_inserted = 0;
  779.           if (!GET_LONGJMP_TARGET(&jmp_buf_pc)) goto keep_going;
  780.  
  781.           /* Need to blow away step-resume breakpoint, as it
  782.          interferes with us */
  783.           remove_step_breakpoint ();
  784.           step_resume_break_address = 0;
  785.           stop_step_resume_break = 0;
  786.  
  787. #if 0
  788.           /* FIXME - Need to implement nested temporary breakpoints */
  789.           if (step_over_calls > 0)
  790.         set_longjmp_resume_breakpoint(jmp_buf_pc,
  791.                           get_current_frame());
  792.           else
  793. #endif                /* 0 */
  794.         set_longjmp_resume_breakpoint(jmp_buf_pc, NULL);
  795.           handling_longjmp = 1; /* FIXME */
  796.           goto keep_going;
  797.  
  798.         case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME:
  799.         case BPSTAT_WHAT_CLEAR_LONGJMP_RESUME_SINGLE:
  800.           remove_breakpoints ();
  801.           breakpoints_inserted = 0;
  802. #if 0
  803.           /* FIXME - Need to implement nested temporary breakpoints */
  804.           if (step_over_calls
  805.           && (stop_frame_address
  806.               INNER_THAN step_frame_address))
  807.         {
  808.           another_trap = 1;
  809.           goto keep_going;
  810.         }
  811. #endif                /* 0 */
  812.           disable_longjmp_breakpoint();
  813.           handling_longjmp = 0; /* FIXME */
  814.           if (what == BPSTAT_WHAT_CLEAR_LONGJMP_RESUME)
  815.         break;
  816.           /* else fallthrough */
  817.  
  818.         case BPSTAT_WHAT_SINGLE:
  819.           if (breakpoints_inserted)
  820.         remove_breakpoints ();
  821.           remove_step_breakpoint ();
  822.           breakpoints_inserted = 0;
  823.           another_trap = 1;
  824.           /* Still need to check other stuff, at least the case
  825.          where we are stepping and step out of the right range.  */
  826.           break;
  827.           
  828.         case BPSTAT_WHAT_STOP_NOISY:
  829.           stop_print_frame = 1;
  830.           goto stop_stepping;
  831.           
  832.         case BPSTAT_WHAT_STOP_SILENT:
  833.           stop_print_frame = 0;
  834.           goto stop_stepping;
  835.           
  836.         case BPSTAT_WHAT_KEEP_CHECKING:
  837.           break;
  838.         }
  839.  
  840.       if (stop_step_resume_break)
  841.       {
  842.         /* But if we have hit the step-resumption breakpoint,
  843.            remove it.  It has done its job getting us here.
  844.            The sp test is to make sure that we don't get hung
  845.            up in recursive calls in functions without frame
  846.            pointers.  If the stack pointer isn't outside of
  847.            where the breakpoint was set (within a routine to be
  848.            stepped over), we're in the middle of a recursive
  849.            call. Not true for reg window machines (sparc)
  850.            because the must change frames to call things and
  851.            the stack pointer doesn't have to change if it
  852.            the bp was set in a routine without a frame (pc can
  853.            be stored in some other window).
  854.            
  855.            The removal of the sp test is to allow calls to
  856.            alloca.  Nasty things were happening.  Oh, well,
  857.            gdb can only handle one level deep of lack of
  858.            frame pointer. */
  859.  
  860.         /*
  861.           Disable test for step_frame_address match so that we always stop even if the
  862.           frames don't match.  Reason: if we hit the step_resume_breakpoint, there is
  863.           no way to temporarily disable it so that we can step past it.  If we leave
  864.           the breakpoint in, then we loop forever repeatedly hitting, but never
  865.           getting past the breakpoint.  This change keeps nexting over recursive
  866.           function calls from hanging gdb.
  867.           */
  868. #if 0
  869.         if (* step_frame_address == 0
  870.         || (step_frame_address == stop_frame_address))
  871. #endif
  872.           {
  873.         remove_step_breakpoint ();
  874.         step_resume_break_address = 0;
  875.  
  876.         /* If were waiting for a trap, hitting the step_resume_break
  877.            doesn't count as getting it.  */
  878.         if (trap_expected)
  879.           another_trap = 1;
  880.           }
  881.       }
  882.     }
  883.  
  884.       /* We come here if we hit a breakpoint but should not
  885.      stop for it.  Possibly we also were stepping
  886.      and should stop for that.  So fall through and
  887.      test for stepping.  But, if not stepping,
  888.      do not stop.  */
  889.  
  890.       /* If this is the breakpoint at the end of a stack dummy,
  891.      just stop silently.  */
  892.       if (!random_signal 
  893.      && PC_IN_CALL_DUMMY (stop_pc, stop_sp, stop_frame_address))
  894.       {
  895.         stop_print_frame = 0;
  896.         stop_stack_dummy = 1;
  897. #ifdef HP_OS_BUG
  898.         trap_expected_after_continue = 1;
  899. #endif
  900.         break;
  901.       }
  902.       
  903.       if (step_resume_break_address)
  904.     /* Having a step-resume breakpoint overrides anything
  905.        else having to do with stepping commands until
  906.        that breakpoint is reached.  */
  907.     ;
  908.       /* If stepping through a line, keep going if still within it.  */
  909.       else if (!random_signal
  910.            && step_range_end
  911.            && stop_pc >= step_range_start
  912.            && stop_pc < step_range_end
  913.            /* The step range might include the start of the
  914.           function, so if we are at the start of the
  915.           step range and either the stack or frame pointers
  916.           just changed, we've stepped outside */
  917.            && !(stop_pc == step_range_start
  918.             && stop_frame_address
  919.             && (stop_sp INNER_THAN prev_sp
  920.             || stop_frame_address != step_frame_address)))
  921.     {
  922.       ;
  923.     }
  924.       
  925.       /* We stepped out of the stepping range.  See if that was due
  926.      to a subroutine call that we should proceed to the end of.  */
  927.       else if (!random_signal && step_range_end)
  928.     {
  929.       if (stop_func_start)
  930.         {
  931.           prologue_pc = stop_func_start;
  932.           SKIP_PROLOGUE (prologue_pc);
  933.         }
  934.  
  935.       /* Did we just take a signal?  */
  936.       if (IN_SIGTRAMP (stop_pc, stop_func_name)
  937.           && !IN_SIGTRAMP (prev_pc, prev_func_name))
  938.         {
  939.           /* This code is needed at least in the following case:
  940.          The user types "next" and then a signal arrives (before
  941.          the "next" is done).  */
  942.           /* We've just taken a signal; go until we are back to
  943.          the point where we took it and one more.  */
  944.           step_resume_break_address = prev_pc;
  945.           step_resume_break_duplicate =
  946.         breakpoint_here_p (step_resume_break_address);
  947.           if (breakpoints_inserted)
  948.         insert_step_breakpoint ();
  949.           /* Make sure that the stepping range gets us past
  950.          that instruction.  */
  951.           if (step_range_end == 1)
  952.         step_range_end = (step_range_start = prev_pc) + 1;
  953.           remove_breakpoints_on_following_step = 1;
  954.           goto save_pc;
  955.         }
  956.  
  957.       /* ==> See comments at top of file on this algorithm.  <==*/
  958.       
  959.       if ((stop_pc == stop_func_start
  960.            || IN_SOLIB_TRAMPOLINE (stop_pc, stop_func_name))
  961.           && (stop_func_start != prev_func_start
  962.           || prologue_pc != stop_func_start
  963.           || stop_sp != prev_sp))
  964.         {
  965.           /* It's a subroutine call.
  966.          (0)  If we are not stepping over any calls ("stepi"), we
  967.               just stop.
  968.          (1)  If we're doing a "next", we want to continue through
  969.               the call ("step over the call").
  970.          (2)  If we are in a function-call trampoline (a stub between
  971.               the calling routine and the real function), locate
  972.               the real function and change stop_func_start.
  973.          (3)  If we're doing a "step", and there are no debug symbols
  974.               at the target of the call, we want to continue through
  975.               it ("step over the call").
  976.          (4)  Otherwise, we want to stop soon, after the function
  977.               prologue ("step into the call"). */
  978.  
  979.           if (step_over_calls == 0)
  980.         {
  981.           /* I presume that step_over_calls is only 0 when we're
  982.              supposed to be stepping at the assembly language level. */
  983.           stop_step = 1;
  984.           break;
  985.         }
  986.  
  987.           if (step_over_calls > 0)
  988.         goto step_over_function;
  989.  
  990.           tmp = SKIP_TRAMPOLINE_CODE (stop_pc);
  991.           if (tmp != 0)
  992.         stop_func_start = tmp;
  993.  
  994.           symtab = find_pc_symtab (stop_func_start);
  995.           if (symtab && LINETABLE (symtab))
  996.         goto step_into_function;
  997.  
  998. step_over_function:
  999.           /* A subroutine call has happened.  */
  1000.           /* Set a special breakpoint after the return */
  1001.           step_resume_break_address =
  1002.         ADDR_BITS_REMOVE
  1003.           (SAVED_PC_AFTER_CALL (get_current_frame ()));
  1004.           step_resume_break_duplicate
  1005.         = breakpoint_here_p (step_resume_break_address);
  1006.           if (breakpoints_inserted)
  1007.         insert_step_breakpoint ();
  1008.           goto save_pc;
  1009.  
  1010. step_into_function:
  1011.           /* Subroutine call with source code we should not step over.
  1012.          Do step to the first line of code in it.  */
  1013.           SKIP_PROLOGUE (stop_func_start);
  1014.           sal = find_pc_line (stop_func_start, 0);
  1015.           /* Use the step_resume_break to step until
  1016.          the end of the prologue, even if that involves jumps
  1017.          (as it seems to on the vax under 4.2).  */
  1018.           /* If the prologue ends in the middle of a source line,
  1019.          continue to the end of that source line.
  1020.          Otherwise, just go to end of prologue.  */
  1021. #ifdef PROLOGUE_FIRSTLINE_OVERLAP
  1022.           /* no, don't either.  It skips any code that's
  1023.          legitimately on the first line.  */
  1024. #else
  1025.           if (sal.end && sal.pc != stop_func_start)
  1026.         stop_func_start = sal.end;
  1027. #endif
  1028.  
  1029.           if (stop_func_start == stop_pc)
  1030.         {
  1031.           /* We are already there: stop now.  */
  1032.           stop_step = 1;
  1033.           break;
  1034.         }    
  1035.           else
  1036.         /* Put the step-breakpoint there and go until there. */
  1037.         {
  1038.           step_resume_break_address = stop_func_start;
  1039.           
  1040.           step_resume_break_duplicate
  1041.             = breakpoint_here_p (step_resume_break_address);
  1042.           if (breakpoints_inserted)
  1043.             insert_step_breakpoint ();
  1044.           /* Do not specify what the fp should be when we stop
  1045.              since on some machines the prologue
  1046.              is where the new fp value is established.  */
  1047.           step_frame_address = 0;
  1048.           /* And make sure stepping stops right away then.  */
  1049.           step_range_end = step_range_start;
  1050.         }
  1051.           goto save_pc;
  1052.         }
  1053.  
  1054.       /* We've wandered out of the step range (but haven't done a
  1055.          subroutine call or return).  */
  1056.  
  1057.       sal = find_pc_line(stop_pc, 0);
  1058.       
  1059.       if (step_range_end == 1 ||    /* stepi or nexti */
  1060.           sal.line == 0 ||        /* ...or no line # info */
  1061.           (stop_pc == sal.pc    /* ...or we're at the start */
  1062.            && current_line != sal.line)) {    /* of a different line */
  1063.         /* Stop because we're done stepping.  */
  1064.         stop_step = 1;
  1065.         break;
  1066.       } else {
  1067.         /* We aren't done stepping, and we have line number info for $pc.
  1068.            Optimize by setting the step_range for the line.  
  1069.            (We might not be in the original line, but if we entered a
  1070.            new line in mid-statement, we continue stepping.  This makes 
  1071.            things like for(;;) statements work better.)  */
  1072.         step_range_start = sal.pc;
  1073.         step_range_end = sal.end;
  1074.         goto save_pc;
  1075.       }
  1076.       /* We never fall through here */
  1077.     }
  1078.  
  1079.       if (trap_expected
  1080.       && IN_SIGTRAMP (stop_pc, stop_func_name)
  1081.       && !IN_SIGTRAMP (prev_pc, prev_func_name))
  1082.     {
  1083.       /* What has happened here is that we have just stepped the inferior
  1084.          with a signal (because it is a signal which shouldn't make
  1085.          us stop), thus stepping into sigtramp.
  1086.  
  1087.          So we need to set a step_resume_break_address breakpoint
  1088.          and continue until we hit it, and then step.  */
  1089.       step_resume_break_address = prev_pc;
  1090.       /* Always 1, I think, but it's probably easier to have
  1091.          the step_resume_break as usual rather than trying to
  1092.          re-use the breakpoint which is already there.  */
  1093.       step_resume_break_duplicate =
  1094.         breakpoint_here_p (step_resume_break_address);
  1095.       if (breakpoints_inserted)
  1096.         insert_step_breakpoint ();
  1097.       remove_breakpoints_on_following_step = 1;
  1098.       another_trap = 1;
  1099.     }
  1100.  
  1101. /* My apologies to the gods of structured programming. */
  1102. /* Come to this label when you need to resume the inferior.  It's really much
  1103.    cleaner at this time to do a goto than to try and figure out what the
  1104.    if-else chain ought to look like!! */
  1105.  
  1106.     keep_going:
  1107.  
  1108. save_pc:
  1109.       /* Save the pc before execution, to compare with pc after stop.  */
  1110.       prev_pc = read_pc ();    /* Might have been DECR_AFTER_BREAK */
  1111.       prev_func_start = stop_func_start; /* Ok, since if DECR_PC_AFTER
  1112.                       BREAK is defined, the
  1113.                       original pc would not have
  1114.                       been at the start of a
  1115.                       function. */
  1116.       prev_func_name = stop_func_name;
  1117.       prev_sp = stop_sp;
  1118.  
  1119.       /* If we did not do break;, it means we should keep
  1120.      running the inferior and not return to debugger.  */
  1121.  
  1122.       if (trap_expected && stop_signal != SIGTRAP)
  1123.     {
  1124.       /* We took a signal (which we are supposed to pass through to
  1125.          the inferior, else we'd have done a break above) and we
  1126.          haven't yet gotten our trap.  Simply continue.  */
  1127.       resume ((step_range_end && !step_resume_break_address)
  1128.           || (trap_expected && !step_resume_break_address)
  1129.           || bpstat_should_step (),
  1130.           stop_signal);
  1131.     }
  1132.       else
  1133.     {
  1134.       /* Either the trap was not expected, but we are continuing
  1135.          anyway (the user asked that this signal be passed to the
  1136.          child)
  1137.            -- or --
  1138.          The signal was SIGTRAP, e.g. it was our signal, but we
  1139.          decided we should resume from it.
  1140.  
  1141.          We're going to run this baby now!
  1142.  
  1143.          Insert breakpoints now, unless we are trying
  1144.          to one-proceed past a breakpoint.  */
  1145.       /* If we've just finished a special step resume and we don't
  1146.          want to hit a breakpoint, pull em out.  */
  1147.       if (!step_resume_break_address &&
  1148.           remove_breakpoints_on_following_step)
  1149.         {
  1150.           remove_breakpoints_on_following_step = 0;
  1151.           remove_breakpoints ();
  1152.           breakpoints_inserted = 0;
  1153.         }
  1154.       else if (!breakpoints_inserted &&
  1155.            (step_resume_break_address != 0 || !another_trap))
  1156.         {
  1157.           insert_step_breakpoint ();
  1158.           breakpoints_failed = insert_breakpoints ();
  1159.           if (breakpoints_failed)
  1160.         break;
  1161.           breakpoints_inserted = 1;
  1162.         }
  1163.  
  1164.       trap_expected = another_trap;
  1165.  
  1166.       if (stop_signal == SIGTRAP)
  1167.         stop_signal = 0;
  1168.  
  1169. #ifdef SHIFT_INST_REGS
  1170.       /* I'm not sure when this following segment applies.  I do know, now,
  1171.          that we shouldn't rewrite the regs when we were stopped by a
  1172.          random signal from the inferior process.  */
  1173.  
  1174.           if (!bpstat_explains_signal (stop_bpstat)
  1175.           && (stop_signal != SIGCLD) 
  1176.               && !stopped_by_random_signal)
  1177.             {
  1178.             CORE_ADDR pc_contents = read_register (PC_REGNUM);
  1179.             CORE_ADDR npc_contents = read_register (NPC_REGNUM);
  1180.             if (pc_contents != npc_contents)
  1181.               {
  1182.               write_register (NNPC_REGNUM, npc_contents);
  1183.               write_register (NPC_REGNUM, pc_contents);
  1184.           }
  1185.             }
  1186. #endif /* SHIFT_INST_REGS */
  1187.  
  1188.       resume ((!step_resume_break_address
  1189.            && !handling_longjmp
  1190.            && (step_range_end
  1191.                || trap_expected))
  1192.           || bpstat_should_step (),
  1193.           stop_signal);
  1194.     }
  1195.     }
  1196.  
  1197.  stop_stepping:
  1198.   if (target_has_execution)
  1199.     {
  1200.       /* Assuming the inferior still exists, set these up for next
  1201.      time, just like we did above if we didn't break out of the
  1202.      loop.  */
  1203.       prev_pc = read_pc ();
  1204.       prev_func_start = stop_func_start;
  1205.       prev_func_name = stop_func_name;
  1206.       prev_sp = stop_sp;
  1207.     }
  1208. }
  1209.  
  1210. /* Here to return control to GDB when the inferior stops for real.
  1211.    Print appropriate messages, remove breakpoints, give terminal our modes.
  1212.  
  1213.    STOP_PRINT_FRAME nonzero means print the executing frame
  1214.    (pc, function, args, file, line number and line text).
  1215.    BREAKPOINTS_FAILED nonzero means stop was due to error
  1216.    attempting to insert breakpoints.  */
  1217.  
  1218. void
  1219. normal_stop ()
  1220. {
  1221.   /* Make sure that the current_frame's pc is correct.  This
  1222.      is a correction for setting up the frame info before doing
  1223.      DECR_PC_AFTER_BREAK */
  1224.   if (target_has_execution)
  1225.     (get_current_frame ())->pc = read_pc ();
  1226.   
  1227.   if (breakpoints_failed)
  1228.     {
  1229.       target_terminal_ours_for_output ();
  1230.       print_sys_errmsg ("ptrace", breakpoints_failed);
  1231.       printf_filtered ("Stopped; cannot insert breakpoints.\n\
  1232. The same program may be running in another process.\n");
  1233.     }
  1234.  
  1235.   if (target_has_execution)
  1236.     remove_step_breakpoint ();
  1237.  
  1238.   if (target_has_execution && breakpoints_inserted)
  1239.     if (remove_breakpoints ())
  1240.       {
  1241.     target_terminal_ours_for_output ();
  1242.     printf_filtered ("Cannot remove breakpoints because program is no longer writable.\n\
  1243. It might be running in another process.\n\
  1244. Further execution is probably impossible.\n");
  1245.       }
  1246.  
  1247.   breakpoints_inserted = 0;
  1248.  
  1249.   /* Delete the breakpoint we stopped at, if it wants to be deleted.
  1250.      Delete any breakpoint that is to be deleted at the next stop.  */
  1251.  
  1252.   breakpoint_auto_delete (stop_bpstat);
  1253.  
  1254.   /* If an auto-display called a function and that got a signal,
  1255.      delete that auto-display to avoid an infinite recursion.  */
  1256.  
  1257.   if (stopped_by_random_signal)
  1258.     disable_current_display ();
  1259.  
  1260.   if (step_multi && stop_step)
  1261.     return;
  1262.  
  1263.   target_terminal_ours ();
  1264.  
  1265.   /* Look up the hook_stop and run it if it exists.  */
  1266.  
  1267.   if (stop_command->hook)
  1268.     {
  1269.       catch_errors (hook_stop_stub, (char *)stop_command->hook,
  1270.             "Error while running hook_stop:\n");
  1271.     }
  1272.  
  1273.   if (!target_has_stack)
  1274.     return;
  1275.  
  1276.   /* Select innermost stack frame except on return from a stack dummy routine,
  1277.      or if the program has exited.  Print it without a level number if
  1278.      we have changed functions or hit a breakpoint.  Print source line
  1279.      if we have one.  */
  1280.   if (!stop_stack_dummy)
  1281.     {
  1282.       select_frame (get_current_frame (), 0);
  1283.  
  1284.       if (stop_print_frame)
  1285.     {
  1286.       int source_only;
  1287.  
  1288.       source_only = bpstat_print (stop_bpstat);
  1289.       source_only = source_only ||
  1290.             (   stop_step
  1291.          && step_frame_address == stop_frame_address
  1292.          && step_start_function == find_pc_function (stop_pc));
  1293.  
  1294.           print_stack_frame (selected_frame, -1, source_only? -1: 1);
  1295.  
  1296.       /* Display the auto-display expressions.  */
  1297.       do_displays ();
  1298.     }
  1299.     }
  1300.  
  1301.   /* Save the function value return registers, if we care.
  1302.      We might be about to restore their previous contents.  */
  1303.   if (proceed_to_finish)
  1304.     read_register_bytes (0, stop_registers, REGISTER_BYTES);
  1305.  
  1306.   if (stop_stack_dummy)
  1307.     {
  1308.       /* Pop the empty frame that contains the stack dummy.
  1309.          POP_FRAME ends with a setting of the current frame, so we
  1310.      can use that next. */
  1311.       POP_FRAME;
  1312.       select_frame (get_current_frame (), 0);
  1313.     }
  1314. }
  1315.  
  1316. static int
  1317. hook_stop_stub (cmd)
  1318.      char *cmd;
  1319. {
  1320.   execute_user_command ((struct cmd_list_element *)cmd, 0);
  1321.   return (0);
  1322. }
  1323.  
  1324.  
  1325. static void
  1326. insert_step_breakpoint ()
  1327. {
  1328.   if (step_resume_break_address && !step_resume_break_duplicate)
  1329.     target_insert_breakpoint (step_resume_break_address,
  1330.                   step_resume_break_shadow);
  1331. }
  1332.  
  1333. static void
  1334. remove_step_breakpoint ()
  1335. {
  1336.   if (step_resume_break_address && !step_resume_break_duplicate)
  1337.     target_remove_breakpoint (step_resume_break_address,
  1338.                   step_resume_break_shadow);
  1339. }
  1340.  
  1341. int signal_stop_state (signo)
  1342.      int signo;
  1343. {
  1344.   return ((signo >= 0 && signo < NSIG) ? signal_stop[signo] : 0);
  1345. }
  1346.  
  1347. int signal_print_state (signo)
  1348.      int signo;
  1349. {
  1350.   return ((signo >= 0 && signo < NSIG) ? signal_print[signo] : 0);
  1351. }
  1352.  
  1353. int signal_pass_state (signo)
  1354.      int signo;
  1355. {
  1356.   return ((signo >= 0 && signo < NSIG) ? signal_program[signo] : 0);
  1357. }
  1358.  
  1359. static void
  1360. sig_print_header ()
  1361. {
  1362.   printf_filtered ("Signal\t\tStop\tPrint\tPass to program\tDescription\n");
  1363. }
  1364.  
  1365. static void
  1366. sig_print_info (number)
  1367.      int number;
  1368. {
  1369.   char *name;
  1370.  
  1371.   if ((name = strsigno (number)) == NULL)
  1372.     printf_filtered ("%d\t\t", number);
  1373.   else
  1374.     printf_filtered ("%s (%d)\t", name, number);
  1375.   printf_filtered ("%s\t", signal_stop[number] ? "Yes" : "No");
  1376.   printf_filtered ("%s\t", signal_print[number] ? "Yes" : "No");
  1377.   printf_filtered ("%s\t\t", signal_program[number] ? "Yes" : "No");
  1378.   printf_filtered ("%s\n", safe_strsignal (number));
  1379. }
  1380.  
  1381. /* Specify how various signals in the inferior should be handled.  */
  1382.  
  1383. static void
  1384. handle_command (args, from_tty)
  1385.      char *args;
  1386.      int from_tty;
  1387. {
  1388.   char **argv;
  1389.   int digits, wordlen;
  1390.   int sigfirst, signum, siglast;
  1391.   int allsigs;
  1392.   int nsigs;
  1393.   unsigned char *sigs;
  1394.   struct cleanup *old_chain;
  1395.  
  1396.   if (args == NULL)
  1397.     {
  1398.       error_no_arg ("signal to handle");
  1399.     }
  1400.  
  1401.   /* Allocate and zero an array of flags for which signals to handle. */
  1402.  
  1403.   nsigs = signo_max () + 1;
  1404.   sigs = (unsigned char *) alloca (nsigs);
  1405.   memset (sigs, 0, nsigs);
  1406.  
  1407.   /* Break the command line up into args. */
  1408.  
  1409.   argv = buildargv (args);
  1410.   if (argv == NULL)
  1411.     {
  1412.       nomem (0);
  1413.     }
  1414.   old_chain = make_cleanup (freeargv, (char *) argv);
  1415.  
  1416.   /* Walk through the args, looking for signal numbers, signal names, and
  1417.      actions.  Signal numbers and signal names may be interspersed with
  1418.      actions, with the actions being performed for all signals cumulatively
  1419.      specified.  Signal ranges can be specified as <LOW>-<HIGH>. */
  1420.  
  1421.   while (*argv != NULL)
  1422.     {
  1423.       wordlen = strlen (*argv);
  1424.       for (digits = 0; isdigit ((*argv)[digits]); digits++) {;}
  1425.       allsigs = 0;
  1426.       sigfirst = siglast = -1;
  1427.  
  1428.       if (wordlen >= 1 && !strncmp (*argv, "all", wordlen))
  1429.     {
  1430.       /* Apply action to all signals except those used by the
  1431.          debugger.  Silently skip those. */
  1432.       allsigs = 1;
  1433.       sigfirst = 0;
  1434.       siglast = nsigs - 1;
  1435.     }
  1436.       else if (wordlen >= 1 && !strncmp (*argv, "stop", wordlen))
  1437.     {
  1438.       SET_SIGS (nsigs, sigs, signal_stop);
  1439.       SET_SIGS (nsigs, sigs, signal_print);
  1440.     }
  1441.       else if (wordlen >= 1 && !strncmp (*argv, "ignore", wordlen))
  1442.     {
  1443.       UNSET_SIGS (nsigs, sigs, signal_program);
  1444.     }
  1445.       else if (wordlen >= 2 && !strncmp (*argv, "print", wordlen))
  1446.     {
  1447.       SET_SIGS (nsigs, sigs, signal_print);
  1448.     }
  1449.       else if (wordlen >= 2 && !strncmp (*argv, "pass", wordlen))
  1450.     {
  1451.       SET_SIGS (nsigs, sigs, signal_program);
  1452.     }
  1453.       else if (wordlen >= 3 && !strncmp (*argv, "nostop", wordlen))
  1454.     {
  1455.       UNSET_SIGS (nsigs, sigs, signal_stop);
  1456.     }
  1457.       else if (wordlen >= 3 && !strncmp (*argv, "noignore", wordlen))
  1458.     {
  1459.       SET_SIGS (nsigs, sigs, signal_program);
  1460.     }
  1461.       else if (wordlen >= 4 && !strncmp (*argv, "noprint", wordlen))
  1462.     {
  1463.       UNSET_SIGS (nsigs, sigs, signal_print);
  1464.       UNSET_SIGS (nsigs, sigs, signal_stop);
  1465.     }
  1466.       else if (wordlen >= 4 && !strncmp (*argv, "nopass", wordlen))
  1467.     {
  1468.       UNSET_SIGS (nsigs, sigs, signal_program);
  1469.     }
  1470.       else if (digits > 0)
  1471.     {
  1472.       sigfirst = siglast = atoi (*argv);
  1473.       if ((*argv)[digits] == '-')
  1474.         {
  1475.           siglast = atoi ((*argv) + digits + 1);
  1476.         }
  1477.       if (sigfirst > siglast)
  1478.         {
  1479.           /* Bet he didn't figure we'd think of this case... */
  1480.           signum = sigfirst;
  1481.           sigfirst = siglast;
  1482.           siglast = signum;
  1483.         }
  1484.       if (sigfirst < 0 || sigfirst >= nsigs)
  1485.         {
  1486.           error ("Signal %d not in range 0-%d", sigfirst, nsigs - 1);
  1487.         }
  1488.       if (siglast < 0 || siglast >= nsigs)
  1489.         {
  1490.           error ("Signal %d not in range 0-%d", siglast, nsigs - 1);
  1491.         }
  1492.     }
  1493.       else if ((signum = strtosigno (*argv)) != 0)
  1494.     {
  1495.       sigfirst = siglast = signum;
  1496.     }
  1497.       else
  1498.     {
  1499.       /* Not a number and not a recognized flag word => complain.  */
  1500.       error ("Unrecognized or ambiguous flag word: \"%s\".", *argv);
  1501.     }
  1502.  
  1503.       /* If any signal numbers or symbol names were found, set flags for
  1504.      which signals to apply actions to. */
  1505.  
  1506.       for (signum = sigfirst; signum >= 0 && signum <= siglast; signum++)
  1507.     {
  1508.       switch (signum)
  1509.         {
  1510.           case SIGTRAP:
  1511.           case SIGINT:
  1512.             if (!allsigs && !sigs[signum])
  1513.           {
  1514.             if (query ("%s is used by the debugger.\nAre you sure you want to change it? ", strsigno (signum)))
  1515.               {
  1516.             sigs[signum] = 1;
  1517.               }
  1518.             else
  1519.               {
  1520.             printf ("Not confirmed, unchanged.\n");
  1521.             fflush (stdout);
  1522.               }
  1523.           }
  1524.         break;
  1525.           default:
  1526.         sigs[signum] = 1;
  1527.         break;
  1528.         }
  1529.     }
  1530.  
  1531.       argv++;
  1532.     }
  1533.  
  1534.   target_notice_signals();
  1535.  
  1536.   if (from_tty)
  1537.     {
  1538.       /* Show the results.  */
  1539.       sig_print_header ();
  1540.       for (signum = 0; signum < nsigs; signum++)
  1541.     {
  1542.       if (sigs[signum])
  1543.         {
  1544.           sig_print_info (signum);
  1545.         }
  1546.     }
  1547.     }
  1548.  
  1549.   do_cleanups (old_chain);
  1550. }
  1551.  
  1552. /* Print current contents of the tables set by the handle command.  */
  1553.  
  1554. static void
  1555. signals_info (signum_exp, from_tty)
  1556.      char *signum_exp;
  1557.      int from_tty;
  1558. {
  1559.   register int i;
  1560.   sig_print_header ();
  1561.  
  1562.   if (signum_exp)
  1563.     {
  1564.       /* First see if this is a symbol name.  */
  1565.       i = strtosigno (signum_exp);
  1566.       if (i == 0)
  1567.     {
  1568.       /* Nope, maybe it's an address which evaluates to a signal
  1569.          number.  */
  1570.       i = parse_and_eval_address (signum_exp);
  1571.       if (i >= NSIG || i < 0)
  1572.         error ("Signal number out of bounds.");
  1573.     }
  1574.       sig_print_info (i);
  1575.       return;
  1576.     }
  1577.  
  1578.   printf_filtered ("\n");
  1579.   for (i = 0; i < NSIG; i++)
  1580.     {
  1581.       QUIT;
  1582.  
  1583.       sig_print_info (i);
  1584.     }
  1585.  
  1586.   printf_filtered ("\nUse the \"handle\" command to change these tables.\n");
  1587. }
  1588.  
  1589. /* Save all of the information associated with the inferior<==>gdb
  1590.    connection.  INF_STATUS is a pointer to a "struct inferior_status"
  1591.    (defined in inferior.h).  */
  1592.  
  1593. void
  1594. save_inferior_status (inf_status, restore_stack_info)
  1595.      struct inferior_status *inf_status;
  1596.      int restore_stack_info;
  1597. {
  1598.   inf_status->pc_changed = pc_changed;
  1599.   inf_status->stop_signal = stop_signal;
  1600.   inf_status->stop_pc = stop_pc;
  1601.   inf_status->stop_frame_address = stop_frame_address;
  1602.   inf_status->stop_step = stop_step;
  1603.   inf_status->stop_stack_dummy = stop_stack_dummy;
  1604.   inf_status->stopped_by_random_signal = stopped_by_random_signal;
  1605.   inf_status->trap_expected = trap_expected;
  1606.   inf_status->step_range_start = step_range_start;
  1607.   inf_status->step_range_end = step_range_end;
  1608.   inf_status->step_frame_address = step_frame_address;
  1609.   inf_status->step_over_calls = step_over_calls;
  1610.   inf_status->step_resume_break_address = step_resume_break_address;
  1611.   inf_status->stop_after_trap = stop_after_trap;
  1612.   inf_status->stop_soon_quietly = stop_soon_quietly;
  1613.   /* Save original bpstat chain here; replace it with copy of chain. 
  1614.      If caller's caller is walking the chain, they'll be happier if we
  1615.      hand them back the original chain when restore_i_s is called.  */
  1616.   inf_status->stop_bpstat = stop_bpstat;
  1617.   stop_bpstat = bpstat_copy (stop_bpstat);
  1618.   inf_status->breakpoint_proceeded = breakpoint_proceeded;
  1619.   inf_status->restore_stack_info = restore_stack_info;
  1620.   inf_status->proceed_to_finish = proceed_to_finish;
  1621.   
  1622.   memcpy (inf_status->stop_registers, stop_registers, REGISTER_BYTES);
  1623.   
  1624.   record_selected_frame (&(inf_status->selected_frame_address),
  1625.              &(inf_status->selected_level));
  1626.   return;
  1627. }
  1628.  
  1629. void
  1630. restore_inferior_status (inf_status)
  1631.      struct inferior_status *inf_status;
  1632. {
  1633.   FRAME fid;
  1634.   int level = inf_status->selected_level;
  1635.  
  1636.   pc_changed = inf_status->pc_changed;
  1637.   stop_signal = inf_status->stop_signal;
  1638.   stop_pc = inf_status->stop_pc;
  1639.   stop_frame_address = inf_status->stop_frame_address;
  1640.   stop_step = inf_status->stop_step;
  1641.   stop_stack_dummy = inf_status->stop_stack_dummy;
  1642.   stopped_by_random_signal = inf_status->stopped_by_random_signal;
  1643.   trap_expected = inf_status->trap_expected;
  1644.   step_range_start = inf_status->step_range_start;
  1645.   step_range_end = inf_status->step_range_end;
  1646.   step_frame_address = inf_status->step_frame_address;
  1647.   step_over_calls = inf_status->step_over_calls;
  1648.   step_resume_break_address = inf_status->step_resume_break_address;
  1649.   stop_after_trap = inf_status->stop_after_trap;
  1650.   stop_soon_quietly = inf_status->stop_soon_quietly;
  1651.   bpstat_clear (&stop_bpstat);
  1652.   stop_bpstat = inf_status->stop_bpstat;
  1653.   breakpoint_proceeded = inf_status->breakpoint_proceeded;
  1654.   proceed_to_finish = inf_status->proceed_to_finish;
  1655.  
  1656.   memcpy (stop_registers, inf_status->stop_registers, REGISTER_BYTES);
  1657.  
  1658.   /* The inferior can be gone if the user types "print exit(0)"
  1659.      (and perhaps other times).  */
  1660.   if (target_has_stack && inf_status->restore_stack_info)
  1661.     {
  1662.       fid = find_relative_frame (get_current_frame (),
  1663.                  &level);
  1664.  
  1665.       /* If inf_status->selected_frame_address is NULL, there was no
  1666.      previously selected frame.  */
  1667.       if (fid == 0 ||
  1668.       FRAME_FP (fid) != inf_status->selected_frame_address ||
  1669.       level != 0)
  1670.     {
  1671. #if 1
  1672.       /* I'm not sure this error message is a good idea.  I have
  1673.          only seen it occur after "Can't continue previously
  1674.          requested operation" (we get called from do_cleanups), in
  1675.          which case it just adds insult to injury (one confusing
  1676.          error message after another.  Besides which, does the
  1677.          user really care if we can't restore the previously
  1678.          selected frame?  */
  1679.       fprintf (stderr, "Unable to restore previously selected frame.\n");
  1680. #endif
  1681.       select_frame (get_current_frame (), 0);
  1682.       return;
  1683.     }
  1684.       
  1685.       select_frame (fid, inf_status->selected_level);
  1686.     }
  1687. }
  1688.  
  1689.  
  1690. void
  1691. _initialize_infrun ()
  1692. {
  1693.   register int i;
  1694.   register int numsigs;
  1695.  
  1696.   add_info ("signals", signals_info,
  1697.         "What debugger does when program gets various signals.\n\
  1698. Specify a signal number as argument to print info on that signal only.");
  1699.   add_info_alias ("handle", "signals", 0);
  1700.  
  1701.   add_com ("handle", class_run, handle_command,
  1702.        "Specify how to handle a signal.\n\
  1703. Args are signal numbers and actions to apply to those signals.\n\
  1704. Signal numbers may be numeric (ex. 11) or symbolic (ex. SIGSEGV).\n\
  1705. Numeric ranges may be specified with the form LOW-HIGH (ex. 14-21).\n\
  1706. The special arg \"all\" is recognized to mean all signals except those\n\
  1707. used by the debugger, typically SIGTRAP and SIGINT.\n\
  1708. Recognized actions include \"stop\", \"nostop\", \"print\", \"noprint\",\n\
  1709. \"pass\", \"nopass\", \"ignore\", or \"noignore\".\n\
  1710. Stop means reenter debugger if this signal happens (implies print).\n\
  1711. Print means print a message if this signal happens.\n\
  1712. Pass means let program see this signal; otherwise program doesn't know.\n\
  1713. Ignore is a synonym for nopass and noignore is a synonym for pass.\n\
  1714. Pass and Stop may be combined.");
  1715.  
  1716.   stop_command = add_cmd ("stop", class_obscure, not_just_help_class_command,
  1717.        "There is no `stop' command, but you can set a hook on `stop'.\n\
  1718. This allows you to set a list of commands to be run each time execution\n\
  1719. of the inferior program stops.", &cmdlist);
  1720.  
  1721.   numsigs = signo_max () + 1;
  1722.   signal_stop    = (unsigned char *)    
  1723.            xmalloc (sizeof (signal_stop[0]) * numsigs);
  1724.   signal_print   = (unsigned char *)
  1725.            xmalloc (sizeof (signal_print[0]) * numsigs);
  1726.   signal_program = (unsigned char *)
  1727.            xmalloc (sizeof (signal_program[0]) * numsigs);
  1728.   for (i = 0; i < numsigs; i++)
  1729.     {
  1730.       signal_stop[i] = 1;
  1731.       signal_print[i] = 1;
  1732.       signal_program[i] = 1;
  1733.     }
  1734.  
  1735.   /* Signals caused by debugger's own actions
  1736.      should not be given to the program afterwards.  */
  1737.   signal_program[SIGTRAP] = 0;
  1738.   signal_program[SIGINT] = 0;
  1739.  
  1740.   /* Signals that are not errors should not normally enter the debugger.  */
  1741. #ifdef SIGALRM
  1742.   signal_stop[SIGALRM] = 0;
  1743.   signal_print[SIGALRM] = 0;
  1744. #endif /* SIGALRM */
  1745. #ifdef SIGVTALRM
  1746.   signal_stop[SIGVTALRM] = 0;
  1747.   signal_print[SIGVTALRM] = 0;
  1748. #endif /* SIGVTALRM */
  1749. #ifdef SIGPROF
  1750.   signal_stop[SIGPROF] = 0;
  1751.   signal_print[SIGPROF] = 0;
  1752. #endif /* SIGPROF */
  1753. #ifdef SIGCHLD
  1754.   signal_stop[SIGCHLD] = 0;
  1755.   signal_print[SIGCHLD] = 0;
  1756. #endif /* SIGCHLD */
  1757. #ifdef SIGCLD
  1758.   signal_stop[SIGCLD] = 0;
  1759.   signal_print[SIGCLD] = 0;
  1760. #endif /* SIGCLD */
  1761. #ifdef SIGIO
  1762.   signal_stop[SIGIO] = 0;
  1763.   signal_print[SIGIO] = 0;
  1764. #endif /* SIGIO */
  1765. #ifdef SIGURG
  1766.   signal_stop[SIGURG] = 0;
  1767.   signal_print[SIGURG] = 0;
  1768. #endif /* SIGURG */
  1769. }
  1770.