GDB (xrefs)
/tmp/gdb-7.10/gdb/x86-linux-nat.c
Go to the documentation of this file.
1 /* Native-dependent code for GNU/Linux x86 (i386 and x86-64).
2 
3  Copyright (C) 1999-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 "elf/common.h"
23 #include "gdb_proc_service.h"
24 #include <sys/ptrace.h>
25 #include <sys/user.h>
26 #include <sys/procfs.h>
27 #include <sys/uio.h>
28 
29 #include "x86-nat.h"
30 #include "linux-nat.h"
31 #ifndef __x86_64__
32 #include "i386-linux-nat.h"
33 #endif
34 #include "x86-linux-nat.h"
35 #include "i386-linux-tdep.h"
36 #ifdef __x86_64__
37 #include "amd64-linux-tdep.h"
38 #endif
39 #include "x86-xstate.h"
40 #include "nat/linux-btrace.h"
41 #include "nat/linux-nat.h"
42 #include "nat/x86-linux.h"
43 #include "nat/x86-linux-dregs.h"
44 #include "nat/linux-ptrace.h"
45 
46 /* Per-thread arch-specific data we want to keep. */
47 
48 struct arch_lwp_info
49 {
50  /* Non-zero if our copy differs from what's recorded in the thread. */
52 };
53 
54 
55 
56 /* linux_nat_new_fork hook. */
57 
58 static void
59 x86_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
60 {
61  pid_t parent_pid;
62  struct x86_debug_reg_state *parent_state;
63  struct x86_debug_reg_state *child_state;
64 
65  /* NULL means no watchpoint has ever been set in the parent. In
66  that case, there's nothing to do. */
67  if (parent->arch_private == NULL)
68  return;
69 
70  /* Linux kernel before 2.6.33 commit
71  72f674d203cd230426437cdcf7dd6f681dad8b0d
72  will inherit hardware debug registers from parent
73  on fork/vfork/clone. Newer Linux kernels create such tasks with
74  zeroed debug registers.
75 
76  GDB core assumes the child inherits the watchpoints/hw
77  breakpoints of the parent, and will remove them all from the
78  forked off process. Copy the debug registers mirrors into the
79  new process so that all breakpoints and watchpoints can be
80  removed together. The debug registers mirror will become zeroed
81  in the end before detaching the forked off process, thus making
82  this compatible with older Linux kernels too. */
83 
84  parent_pid = ptid_get_pid (parent->ptid);
85  parent_state = x86_debug_reg_state (parent_pid);
86  child_state = x86_debug_reg_state (child_pid);
87  *child_state = *parent_state;
88 }
89 
90 
91 static void (*super_post_startup_inferior) (struct target_ops *self,
92  ptid_t ptid);
93 
94 static void
96 {
98  super_post_startup_inferior (self, ptid);
99 }
100 
101 #ifdef __x86_64__
102 /* Value of CS segment register:
103  64bit process: 0x33
104  32bit process: 0x23 */
105 #define AMD64_LINUX_USER64_CS 0x33
106 
107 /* Value of DS segment register:
108  LP64 process: 0x0
109  X32 process: 0x2b */
110 #define AMD64_LINUX_X32_DS 0x2b
111 #endif
112 
113 /* Get Linux/x86 target description from running target. */
114 
115 static const struct target_desc *
117 {
118  int tid;
119  int is_64bit = 0;
120 #ifdef __x86_64__
121  int is_x32;
122 #endif
123  static uint64_t xcr0;
124  uint64_t xcr0_features_bits;
125 
126  /* GNU/Linux LWP ID's are process ID's. */
127  tid = ptid_get_lwp (inferior_ptid);
128  if (tid == 0)
129  tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
130 
131 #ifdef __x86_64__
132  {
133  unsigned long cs;
134  unsigned long ds;
135 
136  /* Get CS register. */
137  errno = 0;
138  cs = ptrace (PTRACE_PEEKUSER, tid,
139  offsetof (struct user_regs_struct, cs), 0);
140  if (errno != 0)
141  perror_with_name (_("Couldn't get CS register"));
142 
143  is_64bit = cs == AMD64_LINUX_USER64_CS;
144 
145  /* Get DS register. */
146  errno = 0;
147  ds = ptrace (PTRACE_PEEKUSER, tid,
148  offsetof (struct user_regs_struct, ds), 0);
149  if (errno != 0)
150  perror_with_name (_("Couldn't get DS register"));
151 
152  is_x32 = ds == AMD64_LINUX_X32_DS;
153 
154  if (sizeof (void *) == 4 && is_64bit && !is_x32)
155  error (_("Can't debug 64-bit process with 32-bit GDB"));
156  }
157 #elif HAVE_PTRACE_GETFPXREGS
158  if (have_ptrace_getfpxregs == -1)
159  {
160  elf_fpxregset_t fpxregs;
161 
162  if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
163  {
166  return tdesc_i386_mmx_linux;
167  }
168  }
169 #endif
170 
172  {
173  uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))];
174  struct iovec iov;
175 
176  iov.iov_base = xstateregs;
177  iov.iov_len = sizeof (xstateregs);
178 
179  /* Check if PTRACE_GETREGSET works. */
180  if (ptrace (PTRACE_GETREGSET, tid,
181  (unsigned int) NT_X86_XSTATE, &iov) < 0)
183  else
184  {
186 
187  /* Get XCR0 from XSAVE extended state. */
188  xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
189  / sizeof (uint64_t))];
190  }
191  }
192 
193  /* Check the native XCR0 only if PTRACE_GETREGSET is available. If
194  PTRACE_GETREGSET is not available then set xcr0_features_bits to
195  zero so that the "no-features" descriptions are returned by the
196  switches below. */
198  xcr0_features_bits = xcr0 & X86_XSTATE_ALL_MASK;
199  else
200  xcr0_features_bits = 0;
201 
202  if (is_64bit)
203  {
204 #ifdef __x86_64__
205  switch (xcr0_features_bits)
206  {
209  if (is_x32)
210  return tdesc_x32_avx512_linux;
211  else
213  case X86_XSTATE_MPX_MASK:
214  if (is_x32)
215  return tdesc_x32_avx_linux; /* No MPX on x32 using AVX. */
216  else
217  return tdesc_amd64_mpx_linux;
218  case X86_XSTATE_AVX_MASK:
219  if (is_x32)
220  return tdesc_x32_avx_linux;
221  else
222  return tdesc_amd64_avx_linux;
223  default:
224  if (is_x32)
225  return tdesc_x32_linux;
226  else
227  return tdesc_amd64_linux;
228  }
229 #endif
230  }
231  else
232  {
233  switch (xcr0_features_bits)
234  {
238  case X86_XSTATE_MPX_MASK:
239  return tdesc_i386_mpx_linux;
240  case X86_XSTATE_AVX_MASK:
241  return tdesc_i386_avx_linux;
242  default:
243  return tdesc_i386_linux;
244  }
245  }
246 
247  gdb_assert_not_reached ("failed to return tdesc");
248 }
249 
250 
251 /* Enable branch tracing. */
252 
253 static struct btrace_target_info *
255  const struct btrace_config *conf)
256 {
257  struct btrace_target_info *tinfo;
258  struct gdbarch *gdbarch;
259 
260  errno = 0;
261  tinfo = linux_enable_btrace (ptid, conf);
262 
263  if (tinfo == NULL)
264  error (_("Could not enable branch tracing for %s: %s."),
265  target_pid_to_str (ptid), safe_strerror (errno));
266 
267  /* Fill in the size of a pointer in bits. */
268  if (tinfo->ptr_bits == 0)
269  {
270  gdbarch = target_thread_architecture (ptid);
271  tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);
272  }
273  return tinfo;
274 }
275 
276 /* Disable branch tracing. */
277 
278 static void
280  struct btrace_target_info *tinfo)
281 {
282  enum btrace_error errcode = linux_disable_btrace (tinfo);
283 
284  if (errcode != BTRACE_ERR_NONE)
285  error (_("Could not disable branch tracing."));
286 }
287 
288 /* Teardown branch tracing. */
289 
290 static void
292  struct btrace_target_info *tinfo)
293 {
294  /* Ignore errors. */
295  linux_disable_btrace (tinfo);
296 }
297 
298 static enum btrace_error
300  struct btrace_data *data,
301  struct btrace_target_info *btinfo,
302  enum btrace_read_type type)
303 {
304  return linux_read_btrace (data, btinfo, type);
305 }
306 
307 /* See to_btrace_conf in target.h. */
308 
309 static const struct btrace_config *
311  const struct btrace_target_info *btinfo)
312 {
313  return linux_btrace_conf (btinfo);
314 }
315 
316 
317 
318 /* Helper for ps_get_thread_area. Sets BASE_ADDR to a pointer to
319  the thread local storage (or its descriptor) and returns PS_OK
320  on success. Returns PS_ERR on failure. */
321 
322 ps_err_e
323 x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
324 {
325  /* NOTE: cagney/2003-08-26: The definition of this buffer is found
326  in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
327  4 byte integers in size: `entry_number', `base_addr', `limit',
328  and a bunch of status bits.
329 
330  The values returned by this ptrace call should be part of the
331  regcache buffer, and ps_get_thread_area should channel its
332  request through the regcache. That way remote targets could
333  provide the value using the remote protocol and not this direct
334  call.
335 
336  Is this function needed? I'm guessing that the `base' is the
337  address of a descriptor that libthread_db uses to find the
338  thread local address base that GDB needs. Perhaps that
339  descriptor is defined by the ABI. Anyway, given that
340  libthread_db calls this function without prompting (gdb
341  requesting tls base) I guess it needs info in there anyway. */
342  unsigned int desc[4];
343 
344  /* This code assumes that "int" is 32 bits and that
345  GET_THREAD_AREA returns no more than 4 int values. */
346  gdb_assert (sizeof (int) == 4);
347 
348 #ifndef PTRACE_GET_THREAD_AREA
349 #define PTRACE_GET_THREAD_AREA 25
350 #endif
351 
352  if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
353  return PS_ERR;
354 
355  *base_addr = desc[1];
356  return PS_OK;
357 }
358 
359 
360 /* Create an x86 GNU/Linux target. */
361 
362 struct target_ops *
364 {
365  /* Fill in the generic GNU/Linux methods. */
366  struct target_ops *t = linux_target ();
367 
368  /* Initialize the debug register function vectors. */
375  x86_set_debug_register_length (sizeof (void *));
376 
377  /* Override the GNU/Linux inferior startup hook. */
380 
381  /* Add the description reader. */
383 
384  /* Add btrace methods. */
391 
392  return t;
393 }
394 
395 /* Add an x86 GNU/Linux target. */
396 
397 void
399 {
405 }
struct x86_debug_reg_state * x86_debug_reg_state(pid_t pid)
Definition: x86-nat.c:107
#define X86_XSTATE_AVX_MASK
Definition: x86-xstate.h:41
struct arch_lwp_info * arch_private
Definition: linux-nat.h:107
unsigned long x86_linux_dr_get_status(void)
const struct target_desc *(* to_read_description)(struct target_ops *ops) TARGET_DEFAULT_RETURN(NULL)
Definition: target.h:769
static const struct btrace_config * x86_linux_btrace_conf(struct target_ops *self, const struct btrace_target_info *btinfo)
#define X86_XSTATE_MPX_MASK
Definition: x86-xstate.h:42
void linux_nat_set_new_thread(struct target_ops *t, void(*new_thread)(struct lwp_info *))
Definition: linux-nat.c:5040
int gdbarch_ptr_bit(struct gdbarch *gdbarch)
Definition: gdbarch.c:1690
ptid_t ptid
Definition: linux-nat.h:34
static void x86_linux_disable_btrace(struct target_ops *self, struct btrace_target_info *tinfo)
static void(* super_post_startup_inferior)(struct target_ops *self, ptid_t ptid)
Definition: x86-linux-nat.c:91
struct target_ops * x86_linux_create_target(void)
ps_err_e
ps_err_e x86_linux_get_thread_area(pid_t pid, void *addr, unsigned int *base_addr)
enum btrace_error linux_read_btrace(struct btrace_data *btrace, struct btrace_target_info *tinfo, enum btrace_read_type type)
char * target_pid_to_str(ptid_t ptid)
Definition: target.c:2233
#define _(String)
Definition: gdb_locale.h:40
void(* set_control)(unsigned long)
Definition: x86-dregs.h:44
struct target_desc * tdesc_amd64_mpx_linux
struct target_desc * tdesc_x32_linux
Definition: x32-linux.c:8
struct target_desc * tdesc_amd64_linux
Definition: amd64-linux.c:8
int have_ptrace_getfpxregs
#define PTRACE_GET_THREAD_AREA
void x86_linux_dr_set_control(unsigned long control)
struct x86_dr_low_type x86_dr_low
Definition: x86-nat.c:37
void linux_nat_set_new_fork(struct target_ops *t, linux_nat_new_fork_ftype *new_fork)
Definition: linux-nat.c:5052
CORE_ADDR(* get_addr)(int)
Definition: x86-dregs.h:52
PTRACE_TYPE_RET ptrace()
Definition: ptid.h:35
int debug_registers_changed
Definition: x86-linux.c:30
void x86_linux_new_thread(struct lwp_info *lwp)
Definition: x86-linux.c:63
struct target_desc * tdesc_x32_avx512_linux
struct target_desc * tdesc_i386_avx512_linux
int linux_supports_btrace(struct target_ops *ops, enum btrace_format format)
void(* set_addr)(int, CORE_ADDR)
Definition: x86-dregs.h:48
struct target_desc * tdesc_x32_avx_linux
Definition: x32-avx-linux.c:8
enum tribool have_ptrace_getregset
Definition: linux-nat.c:169
unsigned long(* get_status)(void)
Definition: x86-dregs.h:56
#define X86_XSTATE_SSE_SIZE
Definition: x86-xstate.h:48
#define gdb_assert_not_reached(message)
Definition: gdb_assert.h:56
const struct btrace_config * linux_btrace_conf(const struct btrace_target_info *tinfo)
btrace_read_type
void linux_nat_set_prepare_to_resume(struct target_ops *t, void(*prepare_to_resume)(struct lwp_info *))
Definition: linux-nat.c:5094
struct btrace_target_info *(* to_enable_btrace)(struct target_ops *, ptid_t ptid, const struct btrace_config *conf) TARGET_DEFAULT_NORETURN(tcomplain())
Definition: target.h:1097
Definition: gdbtypes.h:749
static const struct target_desc * x86_linux_read_description(struct target_ops *ops)
void x86_linux_add_target(struct target_ops *t)
#define gdb_assert(expr)
Definition: gdb_assert.h:33
#define X86_XSTATE_MPX_AVX512_MASK
Definition: x86-xstate.h:44
void linux_nat_add_target(struct target_ops *t)
Definition: linux-nat.c:4972
static enum btrace_error x86_linux_read_btrace(struct target_ops *self, struct btrace_data *data, struct btrace_target_info *btinfo, enum btrace_read_type type)
const struct btrace_config *(* to_btrace_conf)(struct target_ops *self, const struct btrace_target_info *) TARGET_DEFAULT_RETURN(NULL)
Definition: target.h:1124
btrace_error
void x86_linux_dr_set_addr(int regnum, CORE_ADDR addr)
struct btrace_config conf
Definition: linux-btrace.h:90
struct target_ops * linux_target(void)
Definition: linux-nat.c:4544
struct target_desc * tdesc_amd64_avx_linux
unsigned long(* get_control)(void)
Definition: x86-dregs.h:60
#define PTRACE_GETREGSET
Definition: linux-ptrace.h:47
int ptid_get_pid(ptid_t ptid)
Definition: ptid.c:52
CORE_ADDR x86_linux_dr_get_addr(int regnum)
enum btrace_error(* to_read_btrace)(struct target_ops *self, struct btrace_data *data, struct btrace_target_info *btinfo, enum btrace_read_type type) TARGET_DEFAULT_NORETURN(tcomplain())
Definition: target.h:1117
#define X86_XSTATE_AVX512_MASK
Definition: x86-xstate.h:43
void x86_forget_process(pid_t pid)
Definition: x86-nat.c:115
void void void void void void void void void perror_with_name(const char *string) ATTRIBUTE_NORETURN
Definition: utils.c:979
struct target_desc * tdesc_i386_mpx_linux
Definition: i386-mpx-linux.c:8
struct target_desc * tdesc_amd64_avx512_linux
void(* to_teardown_btrace)(struct target_ops *, struct btrace_target_info *tinfo) TARGET_DEFAULT_NORETURN(tcomplain())
Definition: target.h:1111
enum btrace_error linux_disable_btrace(struct btrace_target_info *tinfo)
ptid_t inferior_ptid
Definition: infcmd.c:124
char * safe_strerror(int)
unsigned long x86_linux_dr_get_control(void)
void ** data
Definition: gdbarch.c:139
static void x86_linux_teardown_btrace(struct target_ops *self, struct btrace_target_info *tinfo)
static void x86_linux_new_fork(struct lwp_info *parent, pid_t child_pid)
Definition: x86-linux-nat.c:59
struct target_desc * tdesc_i386_mmx_linux
Definition: i386-mmx-linux.c:8
void x86_set_debug_register_length(int len)
Definition: x86-nat.c:307
long ptid_get_lwp(ptid_t ptid)
Definition: ptid.c:60
void x86_use_watchpoints(struct target_ops *t)
Definition: x86-nat.c:289
struct btrace_target_info * linux_enable_btrace(ptid_t ptid, const struct btrace_config *conf)
struct target_desc * tdesc_i386_linux
Definition: i386-linux.c:8
#define I386_LINUX_XSAVE_XCR0_OFFSET
void linux_nat_set_forget_process(struct target_ops *t, linux_nat_forget_process_ftype *fn)
Definition: linux-nat.c:5062
int(* to_supports_btrace)(struct target_ops *, enum btrace_format) TARGET_DEFAULT_RETURN(0)
Definition: target.h:1091
void x86_cleanup_dregs(void)
Definition: x86-nat.c:141
#define target_thread_architecture(ptid)
Definition: target.h:1797
struct target_desc * tdesc_i386_avx_linux
Definition: i386-avx-linux.c:8
void(* to_disable_btrace)(struct target_ops *, struct btrace_target_info *tinfo) TARGET_DEFAULT_NORETURN(tcomplain())
Definition: target.h:1103
void error(const char *fmt,...)
Definition: errors.c:38
void x86_linux_prepare_to_resume(struct lwp_info *lwp)
Definition: x86-linux.c:71
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
static struct btrace_target_info * x86_linux_enable_btrace(struct target_ops *self, ptid_t ptid, const struct btrace_config *conf)
static void x86_linux_child_post_startup_inferior(struct target_ops *self, ptid_t ptid)
Definition: x86-linux-nat.c:95
void(* to_post_startup_inferior)(struct target_ops *, ptid_t) TARGET_DEFAULT_IGNORE()
Definition: target.h:586
#define X86_XSTATE_ALL_MASK
Definition: x86-xstate.h:46