1 /* Native-dependent code for GNU/Linux x86 (i386 and x86-64).
3 Copyright (C) 1999-2014 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "elf/common.h"
23 #include "gdb_proc_service.h"
24 #include <sys/ptrace.h>
26 #include <sys/procfs.h>
29 #include "linux-nat.h"
31 #include "i386-linux-nat.h"
33 #include "x86-linux-nat.h"
34 #include "i386-linux-tdep.h"
36 #include "amd64-linux-tdep.h"
38 #include "i386-xstate.h"
39 #include "nat/linux-btrace.h"
41 /* Per-thread arch-specific data we want to keep. */
45 /* Non-zero if our copy differs from what's recorded in the thread. */
46 int debug_registers_changed;
49 /* Does the current host support PTRACE_GETREGSET? */
50 int have_ptrace_getregset = -1;
53 /* Support for debug registers. */
55 /* Get debug register REGNUM value from only the one LWP of PTID. */
58 x86_linux_dr_get (ptid_t ptid, int regnum)
63 tid = ptid_get_lwp (ptid);
65 tid = ptid_get_pid (ptid);
68 value = ptrace (PTRACE_PEEKUSER, tid,
69 offsetof (struct user, u_debugreg[regnum]), 0);
71 perror_with_name (_("Couldn't read debug register"));
76 /* Set debug register REGNUM to VALUE in only the one LWP of PTID. */
79 x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
83 tid = ptid_get_lwp (ptid);
85 tid = ptid_get_pid (ptid);
88 ptrace (PTRACE_POKEUSER, tid,
89 offsetof (struct user, u_debugreg[regnum]), value);
91 perror_with_name (_("Couldn't write debug register"));
94 /* Return the inferior's debug register REGNUM. */
97 x86_linux_dr_get_addr (int regnum)
99 /* DR6 and DR7 are retrieved with some other way. */
100 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
102 return x86_linux_dr_get (inferior_ptid, regnum);
105 /* Return the inferior's DR7 debug control register. */
108 x86_linux_dr_get_control (void)
110 return x86_linux_dr_get (inferior_ptid, DR_CONTROL);
113 /* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
116 x86_linux_dr_get_status (void)
118 return x86_linux_dr_get (inferior_ptid, DR_STATUS);
121 /* Callback for iterate_over_lwps. Update the debug registers of
125 update_debug_registers_callback (struct lwp_info *lwp, void *arg)
127 if (lwp->arch_private == NULL)
128 lwp->arch_private = XCNEW (struct arch_lwp_info);
130 /* The actual update is done later just before resuming the lwp, we
131 just mark that the registers need updating. */
132 lwp->arch_private->debug_registers_changed = 1;
134 /* If the lwp isn't stopped, force it to momentarily pause, so we
135 can update its debug registers. */
137 linux_stop_lwp (lwp);
139 /* Continue the iteration. */
143 /* Set DR_CONTROL to CONTROL in all LWPs of the current inferior. */
146 x86_linux_dr_set_control (unsigned long control)
148 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
150 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
153 /* Set address REGNUM (zero based) to ADDR in all LWPs of the current
157 x86_linux_dr_set_addr (int regnum, CORE_ADDR addr)
159 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
161 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
163 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
166 /* Called when resuming a thread.
167 If the debug regs have changed, update the thread's copies. */
170 x86_linux_prepare_to_resume (struct lwp_info *lwp)
172 int clear_status = 0;
174 /* NULL means this is the main thread still going through the shell,
175 or, no watchpoint has been set yet. In that case, there's
177 if (lwp->arch_private == NULL)
180 if (lwp->arch_private->debug_registers_changed)
182 struct i386_debug_reg_state *state
183 = i386_debug_reg_state (ptid_get_pid (lwp->ptid));
186 /* On Linux kernel before 2.6.33 commit
187 72f674d203cd230426437cdcf7dd6f681dad8b0d
188 if you enable a breakpoint by the DR_CONTROL bits you need to have
189 already written the corresponding DR_FIRSTADDR...DR_LASTADDR registers.
191 Ensure DR_CONTROL gets written as the very last register here. */
193 /* Clear DR_CONTROL first. In some cases, setting DR0-3 to a
194 value that doesn't match what is enabled in DR_CONTROL
195 results in EINVAL. */
196 x86_linux_dr_set (lwp->ptid, DR_CONTROL, 0);
198 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
199 if (state->dr_ref_count[i] > 0)
201 x86_linux_dr_set (lwp->ptid, i, state->dr_mirror[i]);
203 /* If we're setting a watchpoint, any change the inferior
204 had done itself to the debug registers needs to be
205 discarded, otherwise, i386_stopped_data_address can get
210 /* If DR_CONTROL is supposed to be zero, we've already set it
212 if (state->dr_control_mirror != 0)
213 x86_linux_dr_set (lwp->ptid, DR_CONTROL, state->dr_control_mirror);
215 lwp->arch_private->debug_registers_changed = 0;
218 if (clear_status || lwp->stopped_by_watchpoint)
219 x86_linux_dr_set (lwp->ptid, DR_STATUS, 0);
223 x86_linux_new_thread (struct lwp_info *lp)
225 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
227 info->debug_registers_changed = 1;
229 lp->arch_private = info;
233 /* linux_nat_new_fork hook. */
236 x86_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
239 struct i386_debug_reg_state *parent_state;
240 struct i386_debug_reg_state *child_state;
242 /* NULL means no watchpoint has ever been set in the parent. In
243 that case, there's nothing to do. */
244 if (parent->arch_private == NULL)
247 /* Linux kernel before 2.6.33 commit
248 72f674d203cd230426437cdcf7dd6f681dad8b0d
249 will inherit hardware debug registers from parent
250 on fork/vfork/clone. Newer Linux kernels create such tasks with
251 zeroed debug registers.
253 GDB core assumes the child inherits the watchpoints/hw
254 breakpoints of the parent, and will remove them all from the
255 forked off process. Copy the debug registers mirrors into the
256 new process so that all breakpoints and watchpoints can be
257 removed together. The debug registers mirror will become zeroed
258 in the end before detaching the forked off process, thus making
259 this compatible with older Linux kernels too. */
261 parent_pid = ptid_get_pid (parent->ptid);
262 parent_state = i386_debug_reg_state (parent_pid);
263 child_state = i386_debug_reg_state (child_pid);
264 *child_state = *parent_state;
268 static void (*super_post_startup_inferior) (struct target_ops *self,
272 x86_linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
274 i386_cleanup_dregs ();
275 super_post_startup_inferior (self, ptid);
279 /* Value of CS segment register:
281 32bit process: 0x23 */
282 #define AMD64_LINUX_USER64_CS 0x33
284 /* Value of DS segment register:
287 #define AMD64_LINUX_X32_DS 0x2b
290 /* Get Linux/x86 target description from running target. */
292 static const struct target_desc *
293 x86_linux_read_description (struct target_ops *ops)
300 static uint64_t xcr0;
301 uint64_t xcr0_features_bits;
303 /* GNU/Linux LWP ID's are process ID's. */
304 tid = ptid_get_lwp (inferior_ptid);
306 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
313 /* Get CS register. */
315 cs = ptrace (PTRACE_PEEKUSER, tid,
316 offsetof (struct user_regs_struct, cs), 0);
318 perror_with_name (_("Couldn't get CS register"));
320 is_64bit = cs == AMD64_LINUX_USER64_CS;
322 /* Get DS register. */
324 ds = ptrace (PTRACE_PEEKUSER, tid,
325 offsetof (struct user_regs_struct, ds), 0);
327 perror_with_name (_("Couldn't get DS register"));
329 is_x32 = ds == AMD64_LINUX_X32_DS;
331 if (sizeof (void *) == 4 && is_64bit && !is_x32)
332 error (_("Can't debug 64-bit process with 32-bit GDB"));
334 #elif HAVE_PTRACE_GETFPXREGS
335 if (have_ptrace_getfpxregs == -1)
337 elf_fpxregset_t fpxregs;
339 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
341 have_ptrace_getfpxregs = 0;
342 have_ptrace_getregset = 0;
343 return tdesc_i386_mmx_linux;
348 if (have_ptrace_getregset == -1)
350 uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
353 iov.iov_base = xstateregs;
354 iov.iov_len = sizeof (xstateregs);
356 /* Check if PTRACE_GETREGSET works. */
357 if (ptrace (PTRACE_GETREGSET, tid,
358 (unsigned int) NT_X86_XSTATE, &iov) < 0)
359 have_ptrace_getregset = 0;
362 have_ptrace_getregset = 1;
364 /* Get XCR0 from XSAVE extended state. */
365 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
366 / sizeof (uint64_t))];
370 /* Check the native XCR0 only if PTRACE_GETREGSET is available. If
371 PTRACE_GETREGSET is not available then set xcr0_features_bits to
372 zero so that the "no-features" descriptions are returned by the
374 if (have_ptrace_getregset)
375 xcr0_features_bits = xcr0 & I386_XSTATE_ALL_MASK;
377 xcr0_features_bits = 0;
382 switch (xcr0_features_bits)
384 case I386_XSTATE_MPX_AVX512_MASK:
385 case I386_XSTATE_AVX512_MASK:
387 return tdesc_x32_avx512_linux;
389 return tdesc_amd64_avx512_linux;
390 case I386_XSTATE_MPX_MASK:
392 return tdesc_x32_avx_linux; /* No MPX on x32 using AVX. */
394 return tdesc_amd64_mpx_linux;
395 case I386_XSTATE_AVX_MASK:
397 return tdesc_x32_avx_linux;
399 return tdesc_amd64_avx_linux;
402 return tdesc_x32_linux;
404 return tdesc_amd64_linux;
410 switch (xcr0_features_bits)
412 case I386_XSTATE_MPX_AVX512_MASK:
413 case I386_XSTATE_AVX512_MASK:
414 return tdesc_i386_avx512_linux;
415 case I386_XSTATE_MPX_MASK:
416 return tdesc_i386_mpx_linux;
417 case I386_XSTATE_AVX_MASK:
418 return tdesc_i386_avx_linux;
420 return tdesc_i386_linux;
424 gdb_assert_not_reached ("failed to return tdesc");
428 /* Enable branch tracing. */
430 static struct btrace_target_info *
431 x86_linux_enable_btrace (struct target_ops *self, ptid_t ptid)
433 struct btrace_target_info *tinfo;
434 struct gdbarch *gdbarch;
437 tinfo = linux_enable_btrace (ptid);
440 error (_("Could not enable branch tracing for %s: %s."),
441 target_pid_to_str (ptid), safe_strerror (errno));
443 /* Fill in the size of a pointer in bits. */
444 gdbarch = target_thread_architecture (ptid);
445 tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);
450 /* Disable branch tracing. */
453 x86_linux_disable_btrace (struct target_ops *self,
454 struct btrace_target_info *tinfo)
456 enum btrace_error errcode = linux_disable_btrace (tinfo);
458 if (errcode != BTRACE_ERR_NONE)
459 error (_("Could not disable branch tracing."));
462 /* Teardown branch tracing. */
465 x86_linux_teardown_btrace (struct target_ops *self,
466 struct btrace_target_info *tinfo)
469 linux_disable_btrace (tinfo);
472 static enum btrace_error
473 x86_linux_read_btrace (struct target_ops *self,
474 VEC (btrace_block_s) **data,
475 struct btrace_target_info *btinfo,
476 enum btrace_read_type type)
478 return linux_read_btrace (data, btinfo, type);
482 /* Helper for ps_get_thread_area. Sets BASE_ADDR to a pointer to
483 the thread local storage (or its descriptor) and returns PS_OK
484 on success. Returns PS_ERR on failure. */
487 x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
489 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
490 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
491 4 byte integers in size: `entry_number', `base_addr', `limit',
492 and a bunch of status bits.
494 The values returned by this ptrace call should be part of the
495 regcache buffer, and ps_get_thread_area should channel its
496 request through the regcache. That way remote targets could
497 provide the value using the remote protocol and not this direct
500 Is this function needed? I'm guessing that the `base' is the
501 address of a descriptor that libthread_db uses to find the
502 thread local address base that GDB needs. Perhaps that
503 descriptor is defined by the ABI. Anyway, given that
504 libthread_db calls this function without prompting (gdb
505 requesting tls base) I guess it needs info in there anyway. */
506 unsigned int desc[4];
508 /* This code assumes that "int" is 32 bits and that
509 GET_THREAD_AREA returns no more than 4 int values. */
510 gdb_assert (sizeof (int) == 4);
512 #ifndef PTRACE_GET_THREAD_AREA
513 #define PTRACE_GET_THREAD_AREA 25
516 if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
519 *base_addr = desc[1];
524 /* Create an x86 GNU/Linux target. */
527 x86_linux_create_target (void)
529 /* Fill in the generic GNU/Linux methods. */
530 struct target_ops *t = linux_target ();
532 /* Initialize the debug register function vectors. */
533 i386_use_watchpoints (t);
534 i386_dr_low.set_control = x86_linux_dr_set_control;
535 i386_dr_low.set_addr = x86_linux_dr_set_addr;
536 i386_dr_low.get_addr = x86_linux_dr_get_addr;
537 i386_dr_low.get_status = x86_linux_dr_get_status;
538 i386_dr_low.get_control = x86_linux_dr_get_control;
539 i386_set_debug_register_length (sizeof (void *));
541 /* Override the GNU/Linux inferior startup hook. */
542 super_post_startup_inferior = t->to_post_startup_inferior;
543 t->to_post_startup_inferior = x86_linux_child_post_startup_inferior;
545 /* Add the description reader. */
546 t->to_read_description = x86_linux_read_description;
548 /* Add btrace methods. */
549 t->to_supports_btrace = linux_supports_btrace;
550 t->to_enable_btrace = x86_linux_enable_btrace;
551 t->to_disable_btrace = x86_linux_disable_btrace;
552 t->to_teardown_btrace = x86_linux_teardown_btrace;
553 t->to_read_btrace = x86_linux_read_btrace;
558 /* Add an x86 GNU/Linux target. */
561 x86_linux_add_target (struct target_ops *t)
563 linux_nat_add_target (t);
564 linux_nat_set_new_thread (t, x86_linux_new_thread);
565 linux_nat_set_new_fork (t, x86_linux_new_fork);
566 linux_nat_set_forget_process (t, i386_forget_process);
567 linux_nat_set_prepare_to_resume (t, x86_linux_prepare_to_resume);