btrace, linux: add perf event buffer abstraction
[deliverable/binutils-gdb.git] / gdb / x86-linux-nat.c
CommitLineData
040baaf6
GB
1/* Native-dependent code for GNU/Linux x86 (i386 and x86-64).
2
32d0add0 3 Copyright (C) 1999-2015 Free Software Foundation, Inc.
040baaf6
GB
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>
72fde3df 27#include <sys/uio.h>
040baaf6 28
df7e5265 29#include "x86-nat.h"
040baaf6
GB
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
df7e5265 39#include "x86-xstate.h"
040baaf6
GB
40#include "nat/linux-btrace.h"
41
42/* Per-thread arch-specific data we want to keep. */
43
44struct arch_lwp_info
45{
46 /* Non-zero if our copy differs from what's recorded in the thread. */
47 int debug_registers_changed;
48};
49
50/* Does the current host support PTRACE_GETREGSET? */
51int have_ptrace_getregset = -1;
52\f
53
54/* Support for debug registers. */
55
56/* Get debug register REGNUM value from only the one LWP of PTID. */
57
58static unsigned long
59x86_linux_dr_get (ptid_t ptid, int regnum)
60{
61 int tid;
62 unsigned long value;
63
5ee44bfa 64 gdb_assert (ptid_lwp_p (ptid));
040baaf6 65 tid = ptid_get_lwp (ptid);
040baaf6
GB
66
67 errno = 0;
68 value = ptrace (PTRACE_PEEKUSER, tid,
69 offsetof (struct user, u_debugreg[regnum]), 0);
70 if (errno != 0)
71 perror_with_name (_("Couldn't read debug register"));
72
73 return value;
74}
75
76/* Set debug register REGNUM to VALUE in only the one LWP of PTID. */
77
78static void
79x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
80{
81 int tid;
82
5ee44bfa 83 gdb_assert (ptid_lwp_p (ptid));
040baaf6 84 tid = ptid_get_lwp (ptid);
040baaf6
GB
85
86 errno = 0;
87 ptrace (PTRACE_POKEUSER, tid,
88 offsetof (struct user, u_debugreg[regnum]), value);
89 if (errno != 0)
90 perror_with_name (_("Couldn't write debug register"));
91}
92
93/* Return the inferior's debug register REGNUM. */
94
95static CORE_ADDR
96x86_linux_dr_get_addr (int regnum)
97{
98 /* DR6 and DR7 are retrieved with some other way. */
99 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
100
101 return x86_linux_dr_get (inferior_ptid, regnum);
102}
103
104/* Return the inferior's DR7 debug control register. */
105
106static unsigned long
107x86_linux_dr_get_control (void)
108{
109 return x86_linux_dr_get (inferior_ptid, DR_CONTROL);
110}
111
112/* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
113
114static unsigned long
115x86_linux_dr_get_status (void)
116{
117 return x86_linux_dr_get (inferior_ptid, DR_STATUS);
118}
119
120/* Callback for iterate_over_lwps. Update the debug registers of
121 LWP. */
122
123static int
124update_debug_registers_callback (struct lwp_info *lwp, void *arg)
125{
126 if (lwp->arch_private == NULL)
127 lwp->arch_private = XCNEW (struct arch_lwp_info);
128
129 /* The actual update is done later just before resuming the lwp, we
130 just mark that the registers need updating. */
131 lwp->arch_private->debug_registers_changed = 1;
132
133 /* If the lwp isn't stopped, force it to momentarily pause, so we
134 can update its debug registers. */
135 if (!lwp->stopped)
136 linux_stop_lwp (lwp);
137
138 /* Continue the iteration. */
139 return 0;
140}
141
142/* Set DR_CONTROL to CONTROL in all LWPs of the current inferior. */
143
144static void
145x86_linux_dr_set_control (unsigned long control)
146{
147 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
148
149 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
150}
151
152/* Set address REGNUM (zero based) to ADDR in all LWPs of the current
153 inferior. */
154
155static void
156x86_linux_dr_set_addr (int regnum, CORE_ADDR addr)
157{
158 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
159
160 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
161
162 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
163}
164
165/* Called when resuming a thread.
166 If the debug regs have changed, update the thread's copies. */
167
168static void
169x86_linux_prepare_to_resume (struct lwp_info *lwp)
170{
171 int clear_status = 0;
172
173 /* NULL means this is the main thread still going through the shell,
174 or, no watchpoint has been set yet. In that case, there's
175 nothing to do. */
176 if (lwp->arch_private == NULL)
177 return;
178
179 if (lwp->arch_private->debug_registers_changed)
180 {
df7e5265
GB
181 struct x86_debug_reg_state *state
182 = x86_debug_reg_state (ptid_get_pid (lwp->ptid));
040baaf6
GB
183 int i;
184
185 /* On Linux kernel before 2.6.33 commit
186 72f674d203cd230426437cdcf7dd6f681dad8b0d
187 if you enable a breakpoint by the DR_CONTROL bits you need to have
188 already written the corresponding DR_FIRSTADDR...DR_LASTADDR registers.
189
190 Ensure DR_CONTROL gets written as the very last register here. */
191
192 /* Clear DR_CONTROL first. In some cases, setting DR0-3 to a
193 value that doesn't match what is enabled in DR_CONTROL
194 results in EINVAL. */
195 x86_linux_dr_set (lwp->ptid, DR_CONTROL, 0);
196
97ea6506 197 ALL_DEBUG_ADDRESS_REGISTERS (i)
040baaf6
GB
198 if (state->dr_ref_count[i] > 0)
199 {
200 x86_linux_dr_set (lwp->ptid, i, state->dr_mirror[i]);
201
202 /* If we're setting a watchpoint, any change the inferior
203 had done itself to the debug registers needs to be
df7e5265 204 discarded, otherwise, x86_stopped_data_address can get
040baaf6
GB
205 confused. */
206 clear_status = 1;
207 }
208
209 /* If DR_CONTROL is supposed to be zero, we've already set it
210 above. */
211 if (state->dr_control_mirror != 0)
212 x86_linux_dr_set (lwp->ptid, DR_CONTROL, state->dr_control_mirror);
213
214 lwp->arch_private->debug_registers_changed = 0;
215 }
216
9c02b525 217 if (clear_status || lwp->stop_reason == LWP_STOPPED_BY_WATCHPOINT)
040baaf6
GB
218 x86_linux_dr_set (lwp->ptid, DR_STATUS, 0);
219}
220
221static void
222x86_linux_new_thread (struct lwp_info *lp)
223{
224 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
225
226 info->debug_registers_changed = 1;
227
228 lp->arch_private = info;
229}
230\f
231
232/* linux_nat_new_fork hook. */
233
234static void
235x86_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
236{
237 pid_t parent_pid;
df7e5265
GB
238 struct x86_debug_reg_state *parent_state;
239 struct x86_debug_reg_state *child_state;
040baaf6
GB
240
241 /* NULL means no watchpoint has ever been set in the parent. In
242 that case, there's nothing to do. */
243 if (parent->arch_private == NULL)
244 return;
245
246 /* Linux kernel before 2.6.33 commit
247 72f674d203cd230426437cdcf7dd6f681dad8b0d
248 will inherit hardware debug registers from parent
249 on fork/vfork/clone. Newer Linux kernels create such tasks with
250 zeroed debug registers.
251
252 GDB core assumes the child inherits the watchpoints/hw
253 breakpoints of the parent, and will remove them all from the
254 forked off process. Copy the debug registers mirrors into the
255 new process so that all breakpoints and watchpoints can be
256 removed together. The debug registers mirror will become zeroed
257 in the end before detaching the forked off process, thus making
258 this compatible with older Linux kernels too. */
259
260 parent_pid = ptid_get_pid (parent->ptid);
df7e5265
GB
261 parent_state = x86_debug_reg_state (parent_pid);
262 child_state = x86_debug_reg_state (child_pid);
040baaf6
GB
263 *child_state = *parent_state;
264}
265\f
266
267static void (*super_post_startup_inferior) (struct target_ops *self,
268 ptid_t ptid);
269
270static void
271x86_linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
272{
df7e5265 273 x86_cleanup_dregs ();
040baaf6
GB
274 super_post_startup_inferior (self, ptid);
275}
276
277#ifdef __x86_64__
278/* Value of CS segment register:
279 64bit process: 0x33
280 32bit process: 0x23 */
281#define AMD64_LINUX_USER64_CS 0x33
282
283/* Value of DS segment register:
284 LP64 process: 0x0
285 X32 process: 0x2b */
286#define AMD64_LINUX_X32_DS 0x2b
287#endif
288
289/* Get Linux/x86 target description from running target. */
290
291static const struct target_desc *
292x86_linux_read_description (struct target_ops *ops)
293{
294 int tid;
295 int is_64bit = 0;
296#ifdef __x86_64__
297 int is_x32;
298#endif
299 static uint64_t xcr0;
300 uint64_t xcr0_features_bits;
301
302 /* GNU/Linux LWP ID's are process ID's. */
303 tid = ptid_get_lwp (inferior_ptid);
304 if (tid == 0)
305 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
306
307#ifdef __x86_64__
308 {
309 unsigned long cs;
310 unsigned long ds;
311
312 /* Get CS register. */
313 errno = 0;
314 cs = ptrace (PTRACE_PEEKUSER, tid,
315 offsetof (struct user_regs_struct, cs), 0);
316 if (errno != 0)
317 perror_with_name (_("Couldn't get CS register"));
318
319 is_64bit = cs == AMD64_LINUX_USER64_CS;
320
321 /* Get DS register. */
322 errno = 0;
323 ds = ptrace (PTRACE_PEEKUSER, tid,
324 offsetof (struct user_regs_struct, ds), 0);
325 if (errno != 0)
326 perror_with_name (_("Couldn't get DS register"));
327
328 is_x32 = ds == AMD64_LINUX_X32_DS;
329
330 if (sizeof (void *) == 4 && is_64bit && !is_x32)
331 error (_("Can't debug 64-bit process with 32-bit GDB"));
332 }
333#elif HAVE_PTRACE_GETFPXREGS
334 if (have_ptrace_getfpxregs == -1)
335 {
336 elf_fpxregset_t fpxregs;
337
338 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
339 {
340 have_ptrace_getfpxregs = 0;
341 have_ptrace_getregset = 0;
342 return tdesc_i386_mmx_linux;
343 }
344 }
345#endif
346
347 if (have_ptrace_getregset == -1)
348 {
df7e5265 349 uint64_t xstateregs[(X86_XSTATE_SSE_SIZE / sizeof (uint64_t))];
040baaf6
GB
350 struct iovec iov;
351
352 iov.iov_base = xstateregs;
353 iov.iov_len = sizeof (xstateregs);
354
355 /* Check if PTRACE_GETREGSET works. */
356 if (ptrace (PTRACE_GETREGSET, tid,
357 (unsigned int) NT_X86_XSTATE, &iov) < 0)
358 have_ptrace_getregset = 0;
359 else
360 {
361 have_ptrace_getregset = 1;
362
363 /* Get XCR0 from XSAVE extended state. */
364 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
365 / sizeof (uint64_t))];
366 }
367 }
368
369 /* Check the native XCR0 only if PTRACE_GETREGSET is available. If
370 PTRACE_GETREGSET is not available then set xcr0_features_bits to
371 zero so that the "no-features" descriptions are returned by the
372 switches below. */
373 if (have_ptrace_getregset)
df7e5265 374 xcr0_features_bits = xcr0 & X86_XSTATE_ALL_MASK;
040baaf6
GB
375 else
376 xcr0_features_bits = 0;
377
378 if (is_64bit)
379 {
380#ifdef __x86_64__
381 switch (xcr0_features_bits)
382 {
df7e5265
GB
383 case X86_XSTATE_MPX_AVX512_MASK:
384 case X86_XSTATE_AVX512_MASK:
040baaf6
GB
385 if (is_x32)
386 return tdesc_x32_avx512_linux;
387 else
388 return tdesc_amd64_avx512_linux;
df7e5265 389 case X86_XSTATE_MPX_MASK:
040baaf6
GB
390 if (is_x32)
391 return tdesc_x32_avx_linux; /* No MPX on x32 using AVX. */
392 else
393 return tdesc_amd64_mpx_linux;
df7e5265 394 case X86_XSTATE_AVX_MASK:
040baaf6
GB
395 if (is_x32)
396 return tdesc_x32_avx_linux;
397 else
398 return tdesc_amd64_avx_linux;
399 default:
400 if (is_x32)
401 return tdesc_x32_linux;
402 else
403 return tdesc_amd64_linux;
404 }
405#endif
406 }
407 else
408 {
409 switch (xcr0_features_bits)
410 {
df7e5265
GB
411 case X86_XSTATE_MPX_AVX512_MASK:
412 case X86_XSTATE_AVX512_MASK:
040baaf6 413 return tdesc_i386_avx512_linux;
df7e5265 414 case X86_XSTATE_MPX_MASK:
040baaf6 415 return tdesc_i386_mpx_linux;
df7e5265 416 case X86_XSTATE_AVX_MASK:
040baaf6
GB
417 return tdesc_i386_avx_linux;
418 default:
419 return tdesc_i386_linux;
420 }
421 }
422
423 gdb_assert_not_reached ("failed to return tdesc");
424}
425\f
426
427/* Enable branch tracing. */
428
429static struct btrace_target_info *
430x86_linux_enable_btrace (struct target_ops *self, ptid_t ptid)
431{
432 struct btrace_target_info *tinfo;
433 struct gdbarch *gdbarch;
434
435 errno = 0;
436 tinfo = linux_enable_btrace (ptid);
437
438 if (tinfo == NULL)
439 error (_("Could not enable branch tracing for %s: %s."),
440 target_pid_to_str (ptid), safe_strerror (errno));
441
442 /* Fill in the size of a pointer in bits. */
443 gdbarch = target_thread_architecture (ptid);
444 tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);
445
446 return tinfo;
447}
448
449/* Disable branch tracing. */
450
451static void
452x86_linux_disable_btrace (struct target_ops *self,
453 struct btrace_target_info *tinfo)
454{
455 enum btrace_error errcode = linux_disable_btrace (tinfo);
456
457 if (errcode != BTRACE_ERR_NONE)
458 error (_("Could not disable branch tracing."));
459}
460
461/* Teardown branch tracing. */
462
463static void
464x86_linux_teardown_btrace (struct target_ops *self,
465 struct btrace_target_info *tinfo)
466{
467 /* Ignore errors. */
468 linux_disable_btrace (tinfo);
469}
470
471static enum btrace_error
472x86_linux_read_btrace (struct target_ops *self,
734b0e4b 473 struct btrace_data *data,
040baaf6
GB
474 struct btrace_target_info *btinfo,
475 enum btrace_read_type type)
476{
477 return linux_read_btrace (data, btinfo, type);
478}
479\f
480
481/* Helper for ps_get_thread_area. Sets BASE_ADDR to a pointer to
482 the thread local storage (or its descriptor) and returns PS_OK
483 on success. Returns PS_ERR on failure. */
484
485ps_err_e
486x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
487{
488 /* NOTE: cagney/2003-08-26: The definition of this buffer is found
489 in the kernel header <asm-i386/ldt.h>. It, after padding, is 4 x
490 4 byte integers in size: `entry_number', `base_addr', `limit',
491 and a bunch of status bits.
492
493 The values returned by this ptrace call should be part of the
494 regcache buffer, and ps_get_thread_area should channel its
495 request through the regcache. That way remote targets could
496 provide the value using the remote protocol and not this direct
497 call.
498
499 Is this function needed? I'm guessing that the `base' is the
500 address of a descriptor that libthread_db uses to find the
501 thread local address base that GDB needs. Perhaps that
502 descriptor is defined by the ABI. Anyway, given that
503 libthread_db calls this function without prompting (gdb
504 requesting tls base) I guess it needs info in there anyway. */
505 unsigned int desc[4];
506
507 /* This code assumes that "int" is 32 bits and that
508 GET_THREAD_AREA returns no more than 4 int values. */
509 gdb_assert (sizeof (int) == 4);
510
511#ifndef PTRACE_GET_THREAD_AREA
512#define PTRACE_GET_THREAD_AREA 25
513#endif
514
515 if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
516 return PS_ERR;
517
518 *base_addr = desc[1];
519 return PS_OK;
520}
521\f
522
523/* Create an x86 GNU/Linux target. */
524
525struct target_ops *
526x86_linux_create_target (void)
527{
528 /* Fill in the generic GNU/Linux methods. */
529 struct target_ops *t = linux_target ();
530
531 /* Initialize the debug register function vectors. */
df7e5265
GB
532 x86_use_watchpoints (t);
533 x86_dr_low.set_control = x86_linux_dr_set_control;
534 x86_dr_low.set_addr = x86_linux_dr_set_addr;
535 x86_dr_low.get_addr = x86_linux_dr_get_addr;
536 x86_dr_low.get_status = x86_linux_dr_get_status;
537 x86_dr_low.get_control = x86_linux_dr_get_control;
538 x86_set_debug_register_length (sizeof (void *));
040baaf6
GB
539
540 /* Override the GNU/Linux inferior startup hook. */
541 super_post_startup_inferior = t->to_post_startup_inferior;
542 t->to_post_startup_inferior = x86_linux_child_post_startup_inferior;
543
544 /* Add the description reader. */
545 t->to_read_description = x86_linux_read_description;
546
547 /* Add btrace methods. */
548 t->to_supports_btrace = linux_supports_btrace;
549 t->to_enable_btrace = x86_linux_enable_btrace;
550 t->to_disable_btrace = x86_linux_disable_btrace;
551 t->to_teardown_btrace = x86_linux_teardown_btrace;
552 t->to_read_btrace = x86_linux_read_btrace;
553
554 return t;
555}
556
557/* Add an x86 GNU/Linux target. */
558
559void
560x86_linux_add_target (struct target_ops *t)
561{
562 linux_nat_add_target (t);
563 linux_nat_set_new_thread (t, x86_linux_new_thread);
564 linux_nat_set_new_fork (t, x86_linux_new_fork);
df7e5265 565 linux_nat_set_forget_process (t, x86_forget_process);
040baaf6
GB
566 linux_nat_set_prepare_to_resume (t, x86_linux_prepare_to_resume);
567}
This page took 0.103031 seconds and 4 git commands to generate.