a80d0a0af8935b7c82df645ac3e7f244c1447045
[platform/upstream/binutils.git] / gdb / i386fbsd-nat.c
1 /* Native-dependent code for FreeBSD/i386.
2    Copyright 2001, 2002 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "inferior.h"
23 #include "regcache.h"
24
25 #include <sys/types.h>
26 #include <sys/ptrace.h>
27 #include <sys/sysctl.h>
28
29 /* Prevent warning from -Wmissing-prototypes.  */
30 void _initialize_i386fbsd_nat (void);
31
32 /* Resume execution of the inferior process.
33    If STEP is nonzero, single-step it.
34    If SIGNAL is nonzero, give it that signal.  */
35
36 void
37 child_resume (ptid_t ptid, int step, enum target_signal signal)
38 {
39   pid_t pid = ptid_get_pid (ptid);
40   int request = PT_STEP;
41
42   if (pid == -1)
43     /* Resume all threads.  This only gets used in the non-threaded
44        case, where "resume all threads" and "resume inferior_ptid" are
45        the same.  */
46     pid = ptid_get_pid (inferior_ptid);
47
48   if (!step)
49     {
50       unsigned int eflags;
51
52       /* Workaround for a bug in FreeBSD.  Make sure that the trace
53          flag is off when doing a continue.  There is a code path
54          through the kernel which leaves the flag set when it should
55          have been cleared.  If a process has a signal pending (such
56          as SIGALRM) and we do a PT_STEP, the process never really has
57          a chance to run because the kernel needs to notify the
58          debugger that a signal is being sent.  Therefore, the process
59          never goes through the kernel's trap() function which would
60          normally clear it.  */
61
62       eflags = read_register (PS_REGNUM);
63       if (eflags & 0x0100)
64         write_register (PS_REGNUM, eflags & ~0x0100);
65
66       request = PT_CONTINUE;
67     }
68
69   /* An addres of (caddr_t) 1 tells ptrace to continue from where it
70      was.  (If GDB wanted it to start some other way, we have already
71      written a new PC value to the child.)  */
72   if (ptrace (request, pid, (caddr_t) 1,
73               target_signal_to_host (signal)) == -1)
74     perror_with_name ("ptrace");
75 }
76 \f
77 void
78 _initialize_i386fbsd_nat (void)
79 {
80   /* FreeBSD provides a kern.ps_strings sysctl that we can use to
81      locate the sigtramp.  That way we can still recognize a sigtramp
82      if its location is changed in a new kernel.  Of course this is
83      still based on the assumption that the sigtramp is placed
84      directly under the location where the program arguments and
85      environment can be found.  */
86 #ifdef KERN_PS_STRINGS
87   {
88     int mib[2];
89     int ps_strings;
90     size_t len;
91
92     extern CORE_ADDR i386fbsd_sigtramp_start;
93     extern CORE_ADDR i386fbsd_sigtramp_end;
94
95     mib[0] = CTL_KERN;
96     mib[1] = KERN_PS_STRINGS;
97     len = sizeof (ps_strings);
98     if (sysctl (mib, 2, &ps_strings, &len, NULL, 0) == 0)
99       {
100         i386fbsd_sigtramp_start = ps_strings - 128;
101         i386fbsd_sigtramp_end = ps_strings;
102       }
103   }
104 #endif
105 }