GDB (xrefs)
/tmp/gdb-8.1/gdb/i386-fbsd-nat.c
Go to the documentation of this file.
1 /* Native-dependent code for FreeBSD/i386.
2 
3  Copyright (C) 2001-2018 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 "x86-xstate.h"
34 #include "x86-bsd-nat.h"
35 #include "i386-bsd-nat.h"
36 
37 /* Resume execution of the inferior process. If STEP is nonzero,
38  single-step it. If SIGNAL is nonzero, give it that signal. */
39 
40 static void
42  ptid_t ptid, int step, enum gdb_signal signal)
43 {
44  pid_t pid = ptid_get_pid (ptid);
45  int request = PT_STEP;
46 
47  if (pid == -1)
48  /* Resume all threads. This only gets used in the non-threaded
49  case, where "resume all threads" and "resume inferior_ptid" are
50  the same. */
52 
53  if (!step)
54  {
56  ULONGEST eflags;
57 
58  /* Workaround for a bug in FreeBSD. Make sure that the trace
59  flag is off when doing a continue. There is a code path
60  through the kernel which leaves the flag set when it should
61  have been cleared. If a process has a signal pending (such
62  as SIGALRM) and we do a PT_STEP, the process never really has
63  a chance to run because the kernel needs to notify the
64  debugger that a signal is being sent. Therefore, the process
65  never goes through the kernel's trap() function which would
66  normally clear it. */
67 
69  &eflags);
70  if (eflags & 0x0100)
72  eflags & ~0x0100);
73 
74  request = PT_CONTINUE;
75  }
76 
77  /* An addres of (caddr_t) 1 tells ptrace to continue from where it
78  was. (If GDB wanted it to start some other way, we have already
79  written a new PC value to the child.) */
80  if (ptrace (request, pid, (caddr_t) 1,
81  gdb_signal_to_host (signal)) == -1)
82  perror_with_name (("ptrace"));
83 }
84 
85 
86 /* Support for debugging kernel virtual memory images. */
87 
88 #include <machine/pcb.h>
89 
90 #include "bsd-kvm.h"
91 
92 static int
93 i386fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
94 {
95  /* The following is true for FreeBSD 4.7:
96 
97  The pcb contains %eip, %ebx, %esp, %ebp, %esi, %edi and %gs.
98  This accounts for all callee-saved registers specified by the
99  psABI and then some. Here %esp contains the stack pointer at the
100  point just after the call to cpu_switch(). From this information
101  we reconstruct the register state as it would look when we just
102  returned from cpu_switch(). */
103 
104  /* The stack pointer shouldn't be zero. */
105  if (pcb->pcb_esp == 0)
106  return 0;
107 
108  pcb->pcb_esp += 4;
109  regcache_raw_supply (regcache, I386_EDI_REGNUM, &pcb->pcb_edi);
110  regcache_raw_supply (regcache, I386_ESI_REGNUM, &pcb->pcb_esi);
111  regcache_raw_supply (regcache, I386_EBP_REGNUM, &pcb->pcb_ebp);
112  regcache_raw_supply (regcache, I386_ESP_REGNUM, &pcb->pcb_esp);
113  regcache_raw_supply (regcache, I386_EBX_REGNUM, &pcb->pcb_ebx);
114  regcache_raw_supply (regcache, I386_EIP_REGNUM, &pcb->pcb_eip);
116 
117  return 1;
118 }
119 
120 
121 #ifdef PT_GETXSTATE_INFO
122 /* Implement the to_read_description method. */
123 
124 static const struct target_desc *
125 i386fbsd_read_description (struct target_ops *ops)
126 {
127  static int xsave_probed;
128  static uint64_t xcr0;
129 
130  if (!xsave_probed)
131  {
132  struct ptrace_xstate_info info;
133 
134  if (ptrace (PT_GETXSTATE_INFO, ptid_get_pid (inferior_ptid),
135  (PTRACE_TYPE_ARG3) &info, sizeof (info)) == 0)
136  {
137  x86bsd_xsave_len = info.xsave_len;
138  xcr0 = info.xsave_mask;
139  }
140  xsave_probed = 1;
141  }
142 
143  if (x86bsd_xsave_len == 0)
144  xcr0 = X86_XSTATE_SSE_MASK;
145 
146  return i386_target_description (xcr0);
147 }
148 #endif
149 
150 void
152 {
153  struct target_ops *t;
154 
155  /* Add some extra features to the common *BSD/i386 target. */
156  t = i386bsd_target ();
157 
158 #ifdef PT_GETXSTATE_INFO
159  t->to_read_description = i386fbsd_read_description;
160 #endif
161 
164 
165  /* Support debugging kernel virtual memory images. */
167 
168 #ifdef KERN_PROC_SIGTRAMP
169  /* Normally signal frames are detected via i386fbsd_sigtramp_p.
170  However, FreeBSD 9.2 through 10.1 do not include the page holding
171  the signal code in core dumps. These releases do provide a
172  kern.proc.sigtramp.<pid> sysctl that returns the location of the
173  signal trampoline for a running process. We fetch the location
174  of the current (gdb) process and use this to identify signal
175  frames in core dumps from these releases. */
176  {
177  int mib[4];
178  struct kinfo_sigtramp kst;
179  size_t len;
180 
181  mib[0] = CTL_KERN;
182  mib[1] = KERN_PROC;
183  mib[2] = KERN_PROC_SIGTRAMP;
184  mib[3] = getpid ();
185  len = sizeof (kst);
186  if (sysctl (mib, 4, &kst, &len, NULL, 0) == 0)
187  {
188  i386fbsd_sigtramp_start_addr = (uintptr_t) kst.ksigtramp_start;
189  i386fbsd_sigtramp_end_addr = (uintptr_t) kst.ksigtramp_end;
190  }
191  }
192 #endif
193 }
const struct target_desc *(* to_read_description)(struct target_ops *ops) TARGET_DEFAULT_RETURN(NULL)
Definition: target.h:790
#define X86_XSTATE_SSE_MASK
Definition: x86-xstate.h:42
int ptid_get_pid(const ptid_t &ptid)
Definition: ptid.c:47
void fbsd_nat_add_target(struct target_ops *t)
Definition: fbsd-nat.c:1180
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
Definition: gnu-nat.c:1824
static void i386fbsd_resume(struct target_ops *ops, ptid_t ptid, int step, enum gdb_signal signal)
Definition: i386-fbsd-nat.c:41
CORE_ADDR i386fbsd_sigtramp_start_addr
#define PT_STEP
Definition: gdb_ptrace.h:91
struct regcache * get_current_regcache(void)
Definition: regcache.c:446
PTRACE_TYPE_RET ptrace()
enum register_status regcache_cooked_read_unsigned(struct regcache *regcache, int regnum, ULONGEST *val)
Definition: regcache.c:777
Definition: ptid.h:35
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:447
int gdb_signal_to_host(enum gdb_signal)
Definition: signals.c:639
static int i386fbsd_supply_pcb(struct regcache *regcache, struct pcb *pcb)
Definition: i386-fbsd-nat.c:93
CORE_ADDR i386fbsd_sigtramp_end_addr
#define PT_CONTINUE
Definition: gdb_ptrace.h:79
void void void void void void void void void perror_with_name(const char *string) ATTRIBUTE_NORETURN
Definition: utils.c:692
void regcache_cooked_write_unsigned(struct regcache *regcache, int regnum, ULONGEST val)
Definition: regcache.c:806
ptid_t inferior_ptid
Definition: infcmd.c:94
size_t x86bsd_xsave_len
struct target_ops * i386bsd_target(void)
Definition: i386-bsd-nat.c:274
void regcache_raw_supply(struct regcache *regcache, int regnum, const void *buf)
Definition: regcache.c:1004
unsigned long long ULONGEST
Definition: common-types.h:53
#define PTRACE_TYPE_ARG3
Definition: config.h:642
void _initialize_i386fbsd_nat(void)
void bsd_kvm_add_target(int(*supply_pcb)(struct regcache *, struct pcb *))
Definition: bsd-kvm.c:354
const struct target_desc * i386_target_description(uint64_t xcr0)
Definition: i386-tdep.c:8714