GDB (xrefs)
/tmp/gdb-7.10/gdb/i386fbsd-nat.c
Go to the documentation of this file.
1 /* Native-dependent code for FreeBSD/i386.
2 
3  Copyright (C) 2001-2015 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 3 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, see <http://www.gnu.org/licenses/>. */
19 
20 #include "defs.h"
21 #include "inferior.h"
22 #include "regcache.h"
23 #include "target.h"
24 
25 #include <sys/types.h>
26 #include <sys/ptrace.h>
27 #include <sys/sysctl.h>
28 #include <sys/user.h>
29 
30 #include "fbsd-nat.h"
31 #include "i386-tdep.h"
32 #include "x86-nat.h"
33 #include "i386bsd-nat.h"
34 
35 /* Resume execution of the inferior process. If STEP is nonzero,
36  single-step it. If SIGNAL is nonzero, give it that signal. */
37 
38 static void
40  ptid_t ptid, int step, enum gdb_signal signal)
41 {
42  pid_t pid = ptid_get_pid (ptid);
43  int request = PT_STEP;
44 
45  if (pid == -1)
46  /* Resume all threads. This only gets used in the non-threaded
47  case, where "resume all threads" and "resume inferior_ptid" are
48  the same. */
50 
51  if (!step)
52  {
54  ULONGEST eflags;
55 
56  /* Workaround for a bug in FreeBSD. Make sure that the trace
57  flag is off when doing a continue. There is a code path
58  through the kernel which leaves the flag set when it should
59  have been cleared. If a process has a signal pending (such
60  as SIGALRM) and we do a PT_STEP, the process never really has
61  a chance to run because the kernel needs to notify the
62  debugger that a signal is being sent. Therefore, the process
63  never goes through the kernel's trap() function which would
64  normally clear it. */
65 
67  &eflags);
68  if (eflags & 0x0100)
70  eflags & ~0x0100);
71 
72  request = PT_CONTINUE;
73  }
74 
75  /* An addres of (caddr_t) 1 tells ptrace to continue from where it
76  was. (If GDB wanted it to start some other way, we have already
77  written a new PC value to the child.) */
78  if (ptrace (request, pid, (caddr_t) 1,
79  gdb_signal_to_host (signal)) == -1)
80  perror_with_name (("ptrace"));
81 }
82 
83 
84 /* Support for debugging kernel virtual memory images. */
85 
86 #include <machine/pcb.h>
87 
88 #include "bsd-kvm.h"
89 
90 static int
91 i386fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
92 {
93  /* The following is true for FreeBSD 4.7:
94 
95  The pcb contains %eip, %ebx, %esp, %ebp, %esi, %edi and %gs.
96  This accounts for all callee-saved registers specified by the
97  psABI and then some. Here %esp contains the stack pointer at the
98  point just after the call to cpu_switch(). From this information
99  we reconstruct the register state as it would look when we just
100  returned from cpu_switch(). */
101 
102  /* The stack pointer shouldn't be zero. */
103  if (pcb->pcb_esp == 0)
104  return 0;
105 
106  pcb->pcb_esp += 4;
107  regcache_raw_supply (regcache, I386_EDI_REGNUM, &pcb->pcb_edi);
108  regcache_raw_supply (regcache, I386_ESI_REGNUM, &pcb->pcb_esi);
109  regcache_raw_supply (regcache, I386_EBP_REGNUM, &pcb->pcb_ebp);
110  regcache_raw_supply (regcache, I386_ESP_REGNUM, &pcb->pcb_esp);
111  regcache_raw_supply (regcache, I386_EBX_REGNUM, &pcb->pcb_ebx);
112  regcache_raw_supply (regcache, I386_EIP_REGNUM, &pcb->pcb_eip);
113  regcache_raw_supply (regcache, I386_GS_REGNUM, &pcb->pcb_gs);
114 
115  return 1;
116 }
117 
118 
119 #ifdef PT_GETXSTATE_INFO
120 /* Implement the to_read_description method. */
121 
122 static const struct target_desc *
123 i386fbsd_read_description (struct target_ops *ops)
124 {
125  static int xsave_probed;
126  static uint64_t xcr0;
127 
128  if (!xsave_probed)
129  {
130  struct ptrace_xstate_info info;
131 
132  if (ptrace (PT_GETXSTATE_INFO, ptid_get_pid (inferior_ptid),
133  (PTRACE_TYPE_ARG3) &info, sizeof (info)) == 0)
134  {
135  i386bsd_xsave_len = info.xsave_len;
136  xcr0 = info.xsave_mask;
137  }
138  xsave_probed = 1;
139  }
140 
141  if (i386bsd_xsave_len != 0)
142  {
143  return i386_target_description (xcr0);
144  }
145  else
146  return tdesc_i386;
147 }
148 #endif
149 
150 /* Prevent warning from -Wmissing-prototypes. */
151 void _initialize_i386fbsd_nat (void);
152 
153 void
155 {
156  struct target_ops *t;
157 
158  /* Add some extra features to the common *BSD/i386 target. */
159  t = i386bsd_target ();
160 
161 #ifdef HAVE_PT_GETDBREGS
162 
164 
171 
172 #endif /* HAVE_PT_GETDBREGS */
173 
174 #ifdef PT_GETXSTATE_INFO
175  t->to_read_description = i386fbsd_read_description;
176 #endif
177 
180 
181  /* Support debugging kernel virtual memory images. */
183 
184 #ifdef KERN_PROC_SIGTRAMP
185  /* Normally signal frames are detected via i386fbsd_sigtramp_p.
186  However, FreeBSD 9.2 through 10.1 do not include the page holding
187  the signal code in core dumps. These releases do provide a
188  kern.proc.sigtramp.<pid> sysctl that returns the location of the
189  signal trampoline for a running process. We fetch the location
190  of the current (gdb) process and use this to identify signal
191  frames in core dumps from these releases. */
192  {
193  int mib[4];
194  struct kinfo_sigtramp kst;
195  size_t len;
196 
197  mib[0] = CTL_KERN;
198  mib[1] = KERN_PROC;
199  mib[2] = KERN_PROC_SIGTRAMP;
200  mib[3] = getpid ();
201  len = sizeof (kst);
202  if (sysctl (mib, 4, &kst, &len, NULL, 0) == 0)
203  {
204  i386fbsd_sigtramp_start_addr = (uintptr_t) kst.ksigtramp_start;
205  i386fbsd_sigtramp_end_addr = (uintptr_t) kst.ksigtramp_end;
206  }
207  }
208 #endif
209 }
struct target_ops * i386bsd_target(void)
Definition: i386bsd-nat.c:282
const struct target_desc *(* to_read_description)(struct target_ops *ops) TARGET_DEFAULT_RETURN(NULL)
Definition: target.h:769
void fbsd_nat_add_target(struct target_ops *t)
Definition: fbsd-nat.c:491
#define PT_STEP
Definition: gdb_ptrace.h:91
void(* set_control)(unsigned long)
Definition: x86-dregs.h:44
static int i386fbsd_supply_pcb(struct regcache *regcache, struct pcb *pcb)
Definition: i386fbsd-nat.c:91
struct x86_dr_low_type x86_dr_low
Definition: x86-nat.c:37
CORE_ADDR(* get_addr)(int)
Definition: x86-dregs.h:52
CORE_ADDR i386fbsd_sigtramp_start_addr
struct regcache * get_current_regcache(void)
Definition: regcache.c:541
PTRACE_TYPE_RET ptrace()
Definition: ptid.h:35
void(* set_addr)(int, CORE_ADDR)
Definition: x86-dregs.h:48
static void i386fbsd_resume(struct target_ops *ops, ptid_t ptid, int step, enum gdb_signal signal)
Definition: i386fbsd-nat.c:39
unsigned long(* get_status)(void)
Definition: x86-dregs.h:56
enum register_status regcache_cooked_read_unsigned(struct regcache *regcache, int regnum, ULONGEST *val)
Definition: regcache.c:837
void(* to_resume)(struct target_ops *, ptid_t, int TARGET_DEBUG_PRINTER(target_debug_print_step), enum gdb_signal) TARGET_DEFAULT_NORETURN(noprocess())
Definition: target.h:464
struct target_desc * tdesc_i386
Definition: i386.c:8
CORE_ADDR i386fbsd_sigtramp_end_addr
int gdb_signal_to_host(enum gdb_signal)
Definition: signals.c:631
CORE_ADDR i386bsd_dr_get_addr(int regnum)
#define PT_CONTINUE
Definition: gdb_ptrace.h:79
unsigned long(* get_control)(void)
Definition: x86-dregs.h:60
int ptid_get_pid(ptid_t ptid)
Definition: ptid.c:52
void i386bsd_dr_set_control(unsigned long control)
void i386bsd_dr_set_addr(int regnum, CORE_ADDR addr)
void void void void void void void void void perror_with_name(const char *string) ATTRIBUTE_NORETURN
Definition: utils.c:979
unsigned long i386bsd_dr_get_status(void)
void regcache_cooked_write_unsigned(struct regcache *regcache, int regnum, ULONGEST val)
Definition: regcache.c:871
ptid_t inferior_ptid
Definition: infcmd.c:124
size_t i386bsd_xsave_len
void regcache_raw_supply(struct regcache *regcache, int regnum, const void *buf)
Definition: regcache.c:1041
unsigned long long ULONGEST
Definition: common-types.h:53
void x86_set_debug_register_length(int len)
Definition: x86-nat.c:307
#define PTRACE_TYPE_ARG3
Definition: config.h:658
void x86_use_watchpoints(struct target_ops *t)
Definition: x86-nat.c:289
void bsd_kvm_add_target(int(*supply_pcb)(struct regcache *, struct pcb *))
Definition: bsd-kvm.c:351
unsigned long i386bsd_dr_get_control(void)
mach_port_t mach_port_t name mach_port_t mach_port_t name error_t int int rusage_t pid_t pid
Definition: gnu-nat.c:1818
const struct target_desc * i386_target_description(uint64_t xcr0)
Definition: i386-tdep.c:8604
void _initialize_i386fbsd_nat(void)
Definition: i386fbsd-nat.c:154
const ULONGEST const LONGEST len
Definition: target.h:309