MFC: An off-by-one malloc size was corrupting the installer's memory,
[dragonfly.git] / contrib / gdb-6.2.1 / gdb / i386fbsd-nat.c
blob7d488b6b6dc98773c74ebf68791847763a62b929
1 /* Native-dependent code for FreeBSD/i386.
3 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "defs.h"
23 #include "inferior.h"
24 #include "regcache.h"
26 #include <sys/types.h>
27 #include <sys/ptrace.h>
28 #include <sys/sysctl.h>
30 #include "i386-tdep.h"
32 /* Resume execution of the inferior process.
33 If STEP is nonzero, single-step it.
34 If SIGNAL is nonzero, give it that signal. */
36 void
37 child_resume (ptid_t ptid, int step, enum target_signal signal)
39 pid_t pid = ptid_get_pid (ptid);
40 int request = PT_STEP;
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);
48 if (!step)
50 ULONGEST eflags;
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. */
62 regcache_cooked_read_unsigned (current_regcache, I386_EFLAGS_REGNUM,
63 &eflags);
64 if (eflags & 0x0100)
65 regcache_cooked_write_unsigned (current_regcache, I386_EFLAGS_REGNUM,
66 eflags & ~0x0100);
68 request = PT_CONTINUE;
71 /* An addres of (caddr_t) 1 tells ptrace to continue from where it
72 was. (If GDB wanted it to start some other way, we have already
73 written a new PC value to the child.) */
74 if (ptrace (request, pid, (caddr_t) 1,
75 target_signal_to_host (signal)) == -1)
76 perror_with_name ("ptrace");
80 /* Support for debugging kernel virtual memory images. */
82 #include <sys/types.h>
83 #include <machine/pcb.h>
85 #include "bsd-kvm.h"
87 static int
88 i386fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
90 /* The following is true for FreeBSD 4.7:
92 The pcb contains %eip, %ebx, %esp, %ebp, %esi, %edi and %gs.
93 This accounts for all callee-saved registers specified by the
94 psABI and then some. Here %esp contains the stack pointer at the
95 point just after the call to cpu_switch(). From this information
96 we reconstruct the register state as it would look when we just
97 returned from cpu_switch(). */
99 /* The stack pointer shouldn't be zero. */
100 if (pcb->pcb_esp == 0)
101 return 0;
103 pcb->pcb_esp += 4;
104 regcache_raw_supply (regcache, I386_EDI_REGNUM, &pcb->pcb_edi);
105 regcache_raw_supply (regcache, I386_ESI_REGNUM, &pcb->pcb_esi);
106 regcache_raw_supply (regcache, I386_EBP_REGNUM, &pcb->pcb_ebp);
107 regcache_raw_supply (regcache, I386_ESP_REGNUM, &pcb->pcb_esp);
108 regcache_raw_supply (regcache, I386_EBX_REGNUM, &pcb->pcb_ebx);
109 regcache_raw_supply (regcache, I386_EIP_REGNUM, &pcb->pcb_eip);
111 return 1;
115 /* Prevent warning from -Wmissing-prototypes. */
116 void _initialize_i386fbsd_nat (void);
118 void
119 _initialize_i386fbsd_nat (void)
121 /* FreeBSD provides a kern.ps_strings sysctl that we can use to
122 locate the sigtramp. That way we can still recognize a sigtramp
123 if its location is changed in a new kernel. Of course this is
124 still based on the assumption that the sigtramp is placed
125 directly under the location where the program arguments and
126 environment can be found. */
127 #ifdef KERN_PS_STRINGS
129 int mib[2];
130 int ps_strings;
131 size_t len;
133 mib[0] = CTL_KERN;
134 mib[1] = KERN_PS_STRINGS;
135 len = sizeof (ps_strings);
136 if (sysctl (mib, 2, &ps_strings, &len, NULL, 0) == 0)
138 i386fbsd_sigtramp_start_addr = ps_strings - 128;
139 i386fbsd_sigtramp_end_addr = ps_strings;
142 #endif