Use XCNEW rather than xcalloc (1, ...) in linux-x86-low.c
[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
3 Copyright (C) 1999-2014 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
28#include "i386-nat.h"
29#include "linux-nat.h"
30#ifndef __x86_64__
31#include "i386-linux-nat.h"
32#endif
33#include "x86-linux-nat.h"
34#include "i386-linux-tdep.h"
35#ifdef __x86_64__
36#include "amd64-linux-tdep.h"
37#endif
38#include "i386-xstate.h"
39#include "nat/linux-btrace.h"
40
41/* Per-thread arch-specific data we want to keep. */
42
43struct arch_lwp_info
44{
45 /* Non-zero if our copy differs from what's recorded in the thread. */
46 int debug_registers_changed;
47};
48
49/* Does the current host support PTRACE_GETREGSET? */
50int have_ptrace_getregset = -1;
51\f
52
53/* Support for debug registers. */
54
55/* Get debug register REGNUM value from only the one LWP of PTID. */
56
57static unsigned long
58x86_linux_dr_get (ptid_t ptid, int regnum)
59{
60 int tid;
61 unsigned long value;
62
63 tid = ptid_get_lwp (ptid);
64 if (tid == 0)
65 tid = ptid_get_pid (ptid);
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
83 tid = ptid_get_lwp (ptid);
84 if (tid == 0)
85 tid = ptid_get_pid (ptid);
86
87 errno = 0;
88 ptrace (PTRACE_POKEUSER, tid,
89 offsetof (struct user, u_debugreg[regnum]), value);
90 if (errno != 0)
91 perror_with_name (_("Couldn't write debug register"));
92}
93
94/* Return the inferior's debug register REGNUM. */
95
96static CORE_ADDR
97x86_linux_dr_get_addr (int regnum)
98{
99 /* DR6 and DR7 are retrieved with some other way. */
100 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
101
102 return x86_linux_dr_get (inferior_ptid, regnum);
103}
104
105/* Return the inferior's DR7 debug control register. */
106
107static unsigned long
108x86_linux_dr_get_control (void)
109{
110 return x86_linux_dr_get (inferior_ptid, DR_CONTROL);
111}
112
113/* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
114
115static unsigned long
116x86_linux_dr_get_status (void)
117{
118 return x86_linux_dr_get (inferior_ptid, DR_STATUS);
119}
120
121/* Callback for iterate_over_lwps. Update the debug registers of
122 LWP. */
123
124static int
125update_debug_registers_callback (struct lwp_info *lwp, void *arg)
126{
127 if (lwp->arch_private == NULL)
128 lwp->arch_private = XCNEW (struct arch_lwp_info);
129
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;
133
134 /* If the lwp isn't stopped, force it to momentarily pause, so we
135 can update its debug registers. */
136 if (!lwp->stopped)
137 linux_stop_lwp (lwp);
138
139 /* Continue the iteration. */
140 return 0;
141}
142
143/* Set DR_CONTROL to CONTROL in all LWPs of the current inferior. */
144
145static void
146x86_linux_dr_set_control (unsigned long control)
147{
148 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
149
150 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
151}
152
153/* Set address REGNUM (zero based) to ADDR in all LWPs of the current
154 inferior. */
155
156static void
157x86_linux_dr_set_addr (int regnum, CORE_ADDR addr)
158{
159 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
160
161 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
162
163 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
164}
165
166/* Called when resuming a thread.
167 If the debug regs have changed, update the thread's copies. */
168
169static void
170x86_linux_prepare_to_resume (struct lwp_info *lwp)
171{
172 int clear_status = 0;
173
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
176 nothing to do. */
177 if (lwp->arch_private == NULL)
178 return;
179
180 if (lwp->arch_private->debug_registers_changed)
181 {
182 struct i386_debug_reg_state *state
183 = i386_debug_reg_state (ptid_get_pid (lwp->ptid));
184 int i;
185
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.
190
191 Ensure DR_CONTROL gets written as the very last register here. */
192
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);
197
198 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
199 if (state->dr_ref_count[i] > 0)
200 {
201 x86_linux_dr_set (lwp->ptid, i, state->dr_mirror[i]);
202
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
206 confused. */
207 clear_status = 1;
208 }
209
210 /* If DR_CONTROL is supposed to be zero, we've already set it
211 above. */
212 if (state->dr_control_mirror != 0)
213 x86_linux_dr_set (lwp->ptid, DR_CONTROL, state->dr_control_mirror);
214
215 lwp->arch_private->debug_registers_changed = 0;
216 }
217
218 if (clear_status || lwp->stopped_by_watchpoint)
219 x86_linux_dr_set (lwp->ptid, DR_STATUS, 0);
220}
221
222static void
223x86_linux_new_thread (struct lwp_info *lp)
224{
225 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
226
227 info->debug_registers_changed = 1;
228
229 lp->arch_private = info;
230}
231\f
232
233/* linux_nat_new_fork hook. */
234
235static void
236x86_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
237{
238 pid_t parent_pid;
239 struct i386_debug_reg_state *parent_state;
240 struct i386_debug_reg_state *child_state;
241
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)
245 return;
246
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.
252
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. */
260
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;
265}
266\f
267
268static void (*super_post_startup_inferior) (struct target_ops *self,
269 ptid_t ptid);
270
271static void
272x86_linux_child_post_startup_inferior (struct target_ops *self, ptid_t ptid)
273{
274 i386_cleanup_dregs ();
275 super_post_startup_inferior (self, ptid);
276}
277
278#ifdef __x86_64__
279/* Value of CS segment register:
280 64bit process: 0x33
281 32bit process: 0x23 */
282#define AMD64_LINUX_USER64_CS 0x33
283
284/* Value of DS segment register:
285 LP64 process: 0x0
286 X32 process: 0x2b */
287#define AMD64_LINUX_X32_DS 0x2b
288#endif
289
290/* Get Linux/x86 target description from running target. */
291
292static const struct target_desc *
293x86_linux_read_description (struct target_ops *ops)
294{
295 int tid;
296 int is_64bit = 0;
297#ifdef __x86_64__
298 int is_x32;
299#endif
300 static uint64_t xcr0;
301 uint64_t xcr0_features_bits;
302
303 /* GNU/Linux LWP ID's are process ID's. */
304 tid = ptid_get_lwp (inferior_ptid);
305 if (tid == 0)
306 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
307
308#ifdef __x86_64__
309 {
310 unsigned long cs;
311 unsigned long ds;
312
313 /* Get CS register. */
314 errno = 0;
315 cs = ptrace (PTRACE_PEEKUSER, tid,
316 offsetof (struct user_regs_struct, cs), 0);
317 if (errno != 0)
318 perror_with_name (_("Couldn't get CS register"));
319
320 is_64bit = cs == AMD64_LINUX_USER64_CS;
321
322 /* Get DS register. */
323 errno = 0;
324 ds = ptrace (PTRACE_PEEKUSER, tid,
325 offsetof (struct user_regs_struct, ds), 0);
326 if (errno != 0)
327 perror_with_name (_("Couldn't get DS register"));
328
329 is_x32 = ds == AMD64_LINUX_X32_DS;
330
331 if (sizeof (void *) == 4 && is_64bit && !is_x32)
332 error (_("Can't debug 64-bit process with 32-bit GDB"));
333 }
334#elif HAVE_PTRACE_GETFPXREGS
335 if (have_ptrace_getfpxregs == -1)
336 {
337 elf_fpxregset_t fpxregs;
338
339 if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
340 {
341 have_ptrace_getfpxregs = 0;
342 have_ptrace_getregset = 0;
343 return tdesc_i386_mmx_linux;
344 }
345 }
346#endif
347
348 if (have_ptrace_getregset == -1)
349 {
350 uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
351 struct iovec iov;
352
353 iov.iov_base = xstateregs;
354 iov.iov_len = sizeof (xstateregs);
355
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;
360 else
361 {
362 have_ptrace_getregset = 1;
363
364 /* Get XCR0 from XSAVE extended state. */
365 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
366 / sizeof (uint64_t))];
367 }
368 }
369
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
373 switches below. */
374 if (have_ptrace_getregset)
375 xcr0_features_bits = xcr0 & I386_XSTATE_ALL_MASK;
376 else
377 xcr0_features_bits = 0;
378
379 if (is_64bit)
380 {
381#ifdef __x86_64__
382 switch (xcr0_features_bits)
383 {
384 case I386_XSTATE_MPX_AVX512_MASK:
385 case I386_XSTATE_AVX512_MASK:
386 if (is_x32)
387 return tdesc_x32_avx512_linux;
388 else
389 return tdesc_amd64_avx512_linux;
390 case I386_XSTATE_MPX_MASK:
391 if (is_x32)
392 return tdesc_x32_avx_linux; /* No MPX on x32 using AVX. */
393 else
394 return tdesc_amd64_mpx_linux;
395 case I386_XSTATE_AVX_MASK:
396 if (is_x32)
397 return tdesc_x32_avx_linux;
398 else
399 return tdesc_amd64_avx_linux;
400 default:
401 if (is_x32)
402 return tdesc_x32_linux;
403 else
404 return tdesc_amd64_linux;
405 }
406#endif
407 }
408 else
409 {
410 switch (xcr0_features_bits)
411 {
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;
419 default:
420 return tdesc_i386_linux;
421 }
422 }
423
424 gdb_assert_not_reached ("failed to return tdesc");
425}
426\f
427
428/* Enable branch tracing. */
429
430static struct btrace_target_info *
431x86_linux_enable_btrace (struct target_ops *self, ptid_t ptid)
432{
433 struct btrace_target_info *tinfo;
434 struct gdbarch *gdbarch;
435
436 errno = 0;
437 tinfo = linux_enable_btrace (ptid);
438
439 if (tinfo == NULL)
440 error (_("Could not enable branch tracing for %s: %s."),
441 target_pid_to_str (ptid), safe_strerror (errno));
442
443 /* Fill in the size of a pointer in bits. */
444 gdbarch = target_thread_architecture (ptid);
445 tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);
446
447 return tinfo;
448}
449
450/* Disable branch tracing. */
451
452static void
453x86_linux_disable_btrace (struct target_ops *self,
454 struct btrace_target_info *tinfo)
455{
456 enum btrace_error errcode = linux_disable_btrace (tinfo);
457
458 if (errcode != BTRACE_ERR_NONE)
459 error (_("Could not disable branch tracing."));
460}
461
462/* Teardown branch tracing. */
463
464static void
465x86_linux_teardown_btrace (struct target_ops *self,
466 struct btrace_target_info *tinfo)
467{
468 /* Ignore errors. */
469 linux_disable_btrace (tinfo);
470}
471
472static enum btrace_error
473x86_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)
477{
478 return linux_read_btrace (data, btinfo, type);
479}
480\f
481
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. */
485
486ps_err_e
487x86_linux_get_thread_area (pid_t pid, void *addr, unsigned int *base_addr)
488{
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.
493
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
498 call.
499
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];
507
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);
511
512#ifndef PTRACE_GET_THREAD_AREA
513#define PTRACE_GET_THREAD_AREA 25
514#endif
515
516 if (ptrace (PTRACE_GET_THREAD_AREA, pid, addr, &desc) < 0)
517 return PS_ERR;
518
519 *base_addr = desc[1];
520 return PS_OK;
521}
522\f
523
524/* Create an x86 GNU/Linux target. */
525
526struct target_ops *
527x86_linux_create_target (void)
528{
529 /* Fill in the generic GNU/Linux methods. */
530 struct target_ops *t = linux_target ();
531
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 *));
540
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;
544
545 /* Add the description reader. */
546 t->to_read_description = x86_linux_read_description;
547
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;
554
555 return t;
556}
557
558/* Add an x86 GNU/Linux target. */
559
560void
561x86_linux_add_target (struct target_ops *t)
562{
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);
568}
This page took 0.058012 seconds and 4 git commands to generate.