* aarch64-linux-nat.c: Replace PIDGET with ptid_get_pid.
[deliverable/binutils-gdb.git] / gdb / amd64-linux-nat.c
CommitLineData
a4b6fc86 1/* Native-dependent code for GNU/Linux x86-64.
0a65a603 2
28e7fd62 3 Copyright (C) 2001-2013 Free Software Foundation, Inc.
53e95fcf
JS
4 Contributed by Jiri Smid, SuSE Labs.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
53e95fcf
JS
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
53e95fcf
JS
20
21#include "defs.h"
22#include "inferior.h"
23#include "gdbcore.h"
24#include "regcache.h"
a055a187 25#include "regset.h"
4056d258 26#include "linux-nat.h"
8695c747 27#include "amd64-linux-tdep.h"
3e3aea48
MM
28#include "linux-btrace.h"
29#include "btrace.h"
c4f35dd8 30
53e95fcf 31#include "gdb_assert.h"
30d52491 32#include "gdb_string.h"
a055a187
L
33#include "elf/common.h"
34#include <sys/uio.h>
53e95fcf
JS
35#include <sys/ptrace.h>
36#include <sys/debugreg.h>
37#include <sys/syscall.h>
38#include <sys/procfs.h>
6fea9e18 39#include <sys/user.h>
c43af07c
EZ
40#include <asm/prctl.h>
41/* FIXME ezannoni-2003-07-09: we need <sys/reg.h> to be included after
42 <asm/ptrace.h> because the latter redefines FS and GS for no apparent
43 reason, and those definitions don't match the ones that libpthread_db
44 uses, which come from <sys/reg.h>. */
0963b4bd 45/* ezannoni-2003-07-09: I think this is fixed. The extraneous defs have
c43af07c
EZ
46 been removed from ptrace.h in the kernel. However, better safe than
47 sorry. */
48#include <asm/ptrace.h>
33a0a2ac 49#include <sys/reg.h>
c43af07c 50#include "gdb_proc_service.h"
33a0a2ac 51
c4f35dd8
MK
52/* Prototypes for supply_gregset etc. */
53#include "gregset.h"
54
9c1488cb 55#include "amd64-tdep.h"
60fac5b8
MK
56#include "i386-linux-tdep.h"
57#include "amd64-nat.h"
9bb9e8ad 58#include "i386-nat.h"
a055a187
L
59#include "i386-xstate.h"
60
61#ifndef PTRACE_GETREGSET
62#define PTRACE_GETREGSET 0x4204
63#endif
64
65#ifndef PTRACE_SETREGSET
66#define PTRACE_SETREGSET 0x4205
67#endif
68
7b50312a
PA
69/* Per-thread arch-specific data we want to keep. */
70
71struct arch_lwp_info
72{
73 /* Non-zero if our copy differs from what's recorded in the thread. */
74 int debug_registers_changed;
75};
76
a055a187
L
77/* Does the current host support PTRACE_GETREGSET? */
78static int have_ptrace_getregset = -1;
60fac5b8 79
60fac5b8
MK
80/* Mapping between the general-purpose registers in GNU/Linux x86-64
81 `struct user' format and GDB's register cache layout for GNU/Linux
82 i386.
83
84 Note that most GNU/Linux x86-64 registers are 64-bit, while the
85 GNU/Linux i386 registers are all 32-bit, but since we're
86 little-endian we get away with that. */
87
88/* From <sys/reg.h> on GNU/Linux i386. */
430eaf2e 89static int amd64_linux_gregset32_reg_offset[] =
60fac5b8 90{
f5859b4d
MK
91 RAX * 8, RCX * 8, /* %eax, %ecx */
92 RDX * 8, RBX * 8, /* %edx, %ebx */
93 RSP * 8, RBP * 8, /* %esp, %ebp */
94 RSI * 8, RDI * 8, /* %esi, %edi */
95 RIP * 8, EFLAGS * 8, /* %eip, %eflags */
96 CS * 8, SS * 8, /* %cs, %ss */
97 DS * 8, ES * 8, /* %ds, %es */
98 FS * 8, GS * 8, /* %fs, %gs */
60fac5b8
MK
99 -1, -1, -1, -1, -1, -1, -1, -1,
100 -1, -1, -1, -1, -1, -1, -1, -1,
101 -1, -1, -1, -1, -1, -1, -1, -1, -1,
a055a187 102 -1, -1, -1, -1, -1, -1, -1, -1,
f5859b4d 103 ORIG_RAX * 8 /* "orig_eax" */
60fac5b8 104};
53e95fcf
JS
105\f
106
107/* Transfering the general-purpose registers between GDB, inferiors
108 and core files. */
109
60fac5b8 110/* Fill GDB's register cache with the general-purpose register values
53e95fcf
JS
111 in *GREGSETP. */
112
113void
7f7fe91e 114supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
53e95fcf 115{
7f7fe91e 116 amd64_supply_native_gregset (regcache, gregsetp, -1);
53e95fcf
JS
117}
118
60fac5b8
MK
119/* Fill register REGNUM (if it is a general-purpose register) in
120 *GREGSETP with the value in GDB's register cache. If REGNUM is -1,
53e95fcf
JS
121 do this for all registers. */
122
123void
7f7fe91e
UW
124fill_gregset (const struct regcache *regcache,
125 elf_gregset_t *gregsetp, int regnum)
53e95fcf 126{
7f7fe91e 127 amd64_collect_native_gregset (regcache, gregsetp, regnum);
53e95fcf
JS
128}
129
53e95fcf
JS
130/* Transfering floating-point registers between GDB, inferiors and cores. */
131
60fac5b8 132/* Fill GDB's register cache with the floating-point and SSE register
c4f35dd8 133 values in *FPREGSETP. */
53e95fcf
JS
134
135void
7f7fe91e 136supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
53e95fcf 137{
7f7fe91e 138 amd64_supply_fxsave (regcache, -1, fpregsetp);
53e95fcf
JS
139}
140
8dda9770 141/* Fill register REGNUM (if it is a floating-point or SSE register) in
60fac5b8 142 *FPREGSETP with the value in GDB's register cache. If REGNUM is
c4f35dd8 143 -1, do this for all registers. */
53e95fcf
JS
144
145void
7f7fe91e
UW
146fill_fpregset (const struct regcache *regcache,
147 elf_fpregset_t *fpregsetp, int regnum)
53e95fcf 148{
7f7fe91e 149 amd64_collect_fxsave (regcache, regnum, fpregsetp);
53e95fcf 150}
53e95fcf
JS
151\f
152
153/* Transferring arbitrary registers between GDB and inferior. */
154
60fac5b8 155/* Fetch register REGNUM from the child process. If REGNUM is -1, do
53e95fcf
JS
156 this for all registers (including the floating point and SSE
157 registers). */
158
10d6c8cd 159static void
28439f5e
PA
160amd64_linux_fetch_inferior_registers (struct target_ops *ops,
161 struct regcache *regcache, int regnum)
53e95fcf 162{
f8028488 163 struct gdbarch *gdbarch = get_regcache_arch (regcache);
53e95fcf
JS
164 int tid;
165
a4b6fc86 166 /* GNU/Linux LWP ID's are process ID's. */
dfd4cc63 167 tid = ptid_get_lwp (inferior_ptid);
c4f35dd8 168 if (tid == 0)
dfd4cc63 169 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
53e95fcf 170
f8028488 171 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
53e95fcf 172 {
99679982
MK
173 elf_gregset_t regs;
174
175 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
edefbb7c 176 perror_with_name (_("Couldn't get registers"));
99679982 177
56be3814 178 amd64_supply_native_gregset (regcache, &regs, -1);
60fac5b8
MK
179 if (regnum != -1)
180 return;
53e95fcf
JS
181 }
182
f8028488 183 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
53e95fcf 184 {
99679982 185 elf_fpregset_t fpregs;
53e95fcf 186
a055a187
L
187 if (have_ptrace_getregset)
188 {
189 char xstateregs[I386_XSTATE_MAX_SIZE];
190 struct iovec iov;
191
192 iov.iov_base = xstateregs;
193 iov.iov_len = sizeof (xstateregs);
194 if (ptrace (PTRACE_GETREGSET, tid,
195 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
196 perror_with_name (_("Couldn't get extended state status"));
99679982 197
a055a187
L
198 amd64_supply_xsave (regcache, -1, xstateregs);
199 }
200 else
201 {
202 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
203 perror_with_name (_("Couldn't get floating point status"));
204
205 amd64_supply_fxsave (regcache, -1, &fpregs);
206 }
99679982 207 }
53e95fcf
JS
208}
209
60fac5b8
MK
210/* Store register REGNUM back into the child process. If REGNUM is
211 -1, do this for all registers (including the floating-point and SSE
53e95fcf 212 registers). */
c4f35dd8 213
10d6c8cd 214static void
28439f5e
PA
215amd64_linux_store_inferior_registers (struct target_ops *ops,
216 struct regcache *regcache, int regnum)
53e95fcf 217{
f8028488 218 struct gdbarch *gdbarch = get_regcache_arch (regcache);
53e95fcf
JS
219 int tid;
220
a4b6fc86 221 /* GNU/Linux LWP ID's are process ID's. */
dfd4cc63 222 tid = ptid_get_lwp (inferior_ptid);
c4f35dd8 223 if (tid == 0)
dfd4cc63 224 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
53e95fcf 225
f8028488 226 if (regnum == -1 || amd64_native_gregset_supplies_p (gdbarch, regnum))
53e95fcf 227 {
99679982
MK
228 elf_gregset_t regs;
229
230 if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
edefbb7c 231 perror_with_name (_("Couldn't get registers"));
99679982 232
56be3814 233 amd64_collect_native_gregset (regcache, &regs, regnum);
99679982
MK
234
235 if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
edefbb7c 236 perror_with_name (_("Couldn't write registers"));
99679982 237
60fac5b8
MK
238 if (regnum != -1)
239 return;
53e95fcf
JS
240 }
241
f8028488 242 if (regnum == -1 || !amd64_native_gregset_supplies_p (gdbarch, regnum))
53e95fcf 243 {
99679982
MK
244 elf_fpregset_t fpregs;
245
a055a187
L
246 if (have_ptrace_getregset)
247 {
248 char xstateregs[I386_XSTATE_MAX_SIZE];
249 struct iovec iov;
250
251 iov.iov_base = xstateregs;
252 iov.iov_len = sizeof (xstateregs);
253 if (ptrace (PTRACE_GETREGSET, tid,
254 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
255 perror_with_name (_("Couldn't get extended state status"));
99679982 256
a055a187
L
257 amd64_collect_xsave (regcache, regnum, xstateregs, 0);
258
259 if (ptrace (PTRACE_SETREGSET, tid,
260 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
261 perror_with_name (_("Couldn't write extended state status"));
262 }
263 else
264 {
265 if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0)
266 perror_with_name (_("Couldn't get floating point status"));
99679982 267
a055a187 268 amd64_collect_fxsave (regcache, regnum, &fpregs);
99679982 269
a055a187
L
270 if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0)
271 perror_with_name (_("Couldn't write floating point status"));
272 }
53e95fcf 273 }
53e95fcf
JS
274}
275\f
9f0bdab8
DJ
276/* Support for debug registers. */
277
c4f35dd8 278static unsigned long
9f0bdab8 279amd64_linux_dr_get (ptid_t ptid, int regnum)
c4f35dd8
MK
280{
281 int tid;
282 unsigned long value;
53e95fcf 283
dfd4cc63 284 tid = ptid_get_lwp (ptid);
9f0bdab8 285 if (tid == 0)
dfd4cc63 286 tid = ptid_get_pid (ptid);
53e95fcf 287
c4f35dd8 288 errno = 0;
9f0bdab8 289 value = ptrace (PTRACE_PEEKUSER, tid,
c4f35dd8
MK
290 offsetof (struct user, u_debugreg[regnum]), 0);
291 if (errno != 0)
edefbb7c 292 perror_with_name (_("Couldn't read debug register"));
53e95fcf 293
c4f35dd8
MK
294 return value;
295}
53e95fcf 296
a79d3c27
JK
297/* Set debug register REGNUM to VALUE in only the one LWP of PTID. */
298
53e95fcf 299static void
9f0bdab8 300amd64_linux_dr_set (ptid_t ptid, int regnum, unsigned long value)
53e95fcf 301{
c4f35dd8 302 int tid;
53e95fcf 303
dfd4cc63 304 tid = ptid_get_lwp (ptid);
9f0bdab8 305 if (tid == 0)
dfd4cc63 306 tid = ptid_get_pid (ptid);
53e95fcf 307
c4f35dd8 308 errno = 0;
9f0bdab8
DJ
309 ptrace (PTRACE_POKEUSER, tid,
310 offsetof (struct user, u_debugreg[regnum]), value);
c4f35dd8 311 if (errno != 0)
edefbb7c 312 perror_with_name (_("Couldn't write debug register"));
c4f35dd8 313}
53e95fcf 314
7b50312a 315/* Return the inferior's debug register REGNUM. */
a79d3c27 316
7b50312a
PA
317static CORE_ADDR
318amd64_linux_dr_get_addr (int regnum)
c4f35dd8 319{
7b50312a
PA
320 /* DR6 and DR7 are retrieved with some other way. */
321 gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR);
9f0bdab8 322
7b50312a 323 return amd64_linux_dr_get (inferior_ptid, regnum);
c4f35dd8 324}
53e95fcf 325
7b50312a 326/* Return the inferior's DR7 debug control register. */
a79d3c27 327
7b50312a
PA
328static unsigned long
329amd64_linux_dr_get_control (void)
b7c4cbf8 330{
7b50312a
PA
331 return amd64_linux_dr_get (inferior_ptid, DR_CONTROL);
332}
9f0bdab8 333
7b50312a
PA
334/* Get DR_STATUS from only the one LWP of INFERIOR_PTID. */
335
336static unsigned long
337amd64_linux_dr_get_status (void)
338{
339 return amd64_linux_dr_get (inferior_ptid, DR_STATUS);
340}
341
26cb8b7c
PA
342/* Callback for iterate_over_lwps. Update the debug registers of
343 LWP. */
7b50312a
PA
344
345static int
346update_debug_registers_callback (struct lwp_info *lwp, void *arg)
347{
6e012a6c
PA
348 if (lwp->arch_private == NULL)
349 lwp->arch_private = XCNEW (struct arch_lwp_info);
350
7b50312a
PA
351 /* The actual update is done later just before resuming the lwp, we
352 just mark that the registers need updating. */
353 lwp->arch_private->debug_registers_changed = 1;
354
355 /* If the lwp isn't stopped, force it to momentarily pause, so we
356 can update its debug registers. */
357 if (!lwp->stopped)
358 linux_stop_lwp (lwp);
c4f35dd8 359
8da828f7 360 /* Continue the iteration. */
7b50312a 361 return 0;
b7c4cbf8
AJ
362}
363
7b50312a 364/* Set DR_CONTROL to CONTROL in all LWPs of the current inferior. */
a79d3c27 365
9bb9e8ad 366static void
7b50312a 367amd64_linux_dr_set_control (unsigned long control)
53e95fcf 368{
26cb8b7c
PA
369 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
370
371 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
53e95fcf 372}
8cfda98c 373
7b50312a
PA
374/* Set address REGNUM (zero based) to ADDR in all LWPs of the current
375 inferior. */
a79d3c27 376
7b50312a
PA
377static void
378amd64_linux_dr_set_addr (int regnum, CORE_ADDR addr)
8cfda98c 379{
26cb8b7c
PA
380 ptid_t pid_ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
381
7b50312a
PA
382 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
383
26cb8b7c 384 iterate_over_lwps (pid_ptid, update_debug_registers_callback, NULL);
9f0bdab8
DJ
385}
386
7b50312a
PA
387/* Called when resuming a thread.
388 If the debug regs have changed, update the thread's copies. */
a79d3c27
JK
389
390static void
7b50312a 391amd64_linux_prepare_to_resume (struct lwp_info *lwp)
a79d3c27 392{
7b50312a 393 int clear_status = 0;
a79d3c27 394
6e012a6c
PA
395 /* NULL means this is the main thread still going through the shell,
396 or, no watchpoint has been set yet. In that case, there's
397 nothing to do. */
398 if (lwp->arch_private == NULL)
399 return;
400
7b50312a 401 if (lwp->arch_private->debug_registers_changed)
a79d3c27 402 {
26cb8b7c
PA
403 struct i386_debug_reg_state *state
404 = i386_debug_reg_state (ptid_get_pid (lwp->ptid));
7b50312a
PA
405 int i;
406
4403d8e9
JK
407 /* On Linux kernel before 2.6.33 commit
408 72f674d203cd230426437cdcf7dd6f681dad8b0d
409 if you enable a breakpoint by the DR_CONTROL bits you need to have
410 already written the corresponding DR_FIRSTADDR...DR_LASTADDR registers.
411
412 Ensure DR_CONTROL gets written as the very last register here. */
413
7b50312a
PA
414 for (i = DR_FIRSTADDR; i <= DR_LASTADDR; i++)
415 if (state->dr_ref_count[i] > 0)
416 {
417 amd64_linux_dr_set (lwp->ptid, i, state->dr_mirror[i]);
418
419 /* If we're setting a watchpoint, any change the inferior
420 had done itself to the debug registers needs to be
421 discarded, otherwise, i386_stopped_data_address can get
422 confused. */
423 clear_status = 1;
424 }
425
426 amd64_linux_dr_set (lwp->ptid, DR_CONTROL, state->dr_control_mirror);
427
428 lwp->arch_private->debug_registers_changed = 0;
a79d3c27 429 }
a79d3c27 430
7b50312a
PA
431 if (clear_status || lwp->stopped_by_watchpoint)
432 amd64_linux_dr_set (lwp->ptid, DR_STATUS, 0);
433}
a79d3c27 434
9f0bdab8 435static void
7b50312a 436amd64_linux_new_thread (struct lwp_info *lp)
9f0bdab8 437{
7b50312a 438 struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
9f0bdab8 439
7b50312a 440 info->debug_registers_changed = 1;
9f0bdab8 441
7b50312a 442 lp->arch_private = info;
8cfda98c 443}
26cb8b7c
PA
444
445/* linux_nat_new_fork hook. */
446
447static void
448amd64_linux_new_fork (struct lwp_info *parent, pid_t child_pid)
449{
450 pid_t parent_pid;
451 struct i386_debug_reg_state *parent_state;
452 struct i386_debug_reg_state *child_state;
453
454 /* NULL means no watchpoint has ever been set in the parent. In
455 that case, there's nothing to do. */
456 if (parent->arch_private == NULL)
457 return;
458
459 /* Linux kernel before 2.6.33 commit
460 72f674d203cd230426437cdcf7dd6f681dad8b0d
461 will inherit hardware debug registers from parent
462 on fork/vfork/clone. Newer Linux kernels create such tasks with
463 zeroed debug registers.
464
465 GDB core assumes the child inherits the watchpoints/hw
466 breakpoints of the parent, and will remove them all from the
467 forked off process. Copy the debug registers mirrors into the
468 new process so that all breakpoints and watchpoints can be
469 removed together. The debug registers mirror will become zeroed
470 in the end before detaching the forked off process, thus making
471 this compatible with older Linux kernels too. */
472
473 parent_pid = ptid_get_pid (parent->ptid);
474 parent_state = i386_debug_reg_state (parent_pid);
475 child_state = i386_debug_reg_state (child_pid);
476 *child_state = *parent_state;
477}
478
5bca7895 479\f
c43af07c 480
50d71875
AC
481/* This function is called by libthread_db as part of its handling of
482 a request for a thread's local storage address. */
483
5bca7895 484ps_err_e
c43af07c
EZ
485ps_get_thread_area (const struct ps_prochandle *ph,
486 lwpid_t lwpid, int idx, void **base)
487{
f5656ead 488 if (gdbarch_bfd_arch_info (target_gdbarch ())->bits_per_word == 32)
50d71875
AC
489 {
490 /* The full structure is found in <asm-i386/ldt.h>. The second
491 integer is the LDT's base_address and that is used to locate
492 the thread's local storage. See i386-linux-nat.c more
493 info. */
494 unsigned int desc[4];
495
496 /* This code assumes that "int" is 32 bits and that
497 GET_THREAD_AREA returns no more than 4 int values. */
498 gdb_assert (sizeof (int) == 4);
499#ifndef PTRACE_GET_THREAD_AREA
500#define PTRACE_GET_THREAD_AREA 25
501#endif
502 if (ptrace (PTRACE_GET_THREAD_AREA,
503 lwpid, (void *) (long) idx, (unsigned long) &desc) < 0)
504 return PS_ERR;
505
506 /* Extend the value to 64 bits. Here it's assumed that a "long"
507 and a "void *" are the same. */
508 (*base) = (void *) (long) desc[1];
509 return PS_OK;
510 }
511 else
512 {
513 /* This definition comes from prctl.h, but some kernels may not
514 have it. */
c43af07c
EZ
515#ifndef PTRACE_ARCH_PRCTL
516#define PTRACE_ARCH_PRCTL 30
517#endif
50d71875
AC
518 /* FIXME: ezannoni-2003-07-09 see comment above about include
519 file order. We could be getting bogus values for these two. */
520 gdb_assert (FS < ELF_NGREG);
521 gdb_assert (GS < ELF_NGREG);
522 switch (idx)
523 {
524 case FS:
6fea9e18
L
525#ifdef HAVE_STRUCT_USER_REGS_STRUCT_FS_BASE
526 {
527 /* PTRACE_ARCH_PRCTL is obsolete since 2.6.25, where the
528 fs_base and gs_base fields of user_regs_struct can be
529 used directly. */
530 unsigned long fs;
531 errno = 0;
532 fs = ptrace (PTRACE_PEEKUSER, lwpid,
533 offsetof (struct user_regs_struct, fs_base), 0);
534 if (errno == 0)
535 {
536 *base = (void *) fs;
537 return PS_OK;
538 }
539 }
540#endif
50d71875
AC
541 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0)
542 return PS_OK;
543 break;
544 case GS:
6fea9e18
L
545#ifdef HAVE_STRUCT_USER_REGS_STRUCT_GS_BASE
546 {
547 unsigned long gs;
548 errno = 0;
549 gs = ptrace (PTRACE_PEEKUSER, lwpid,
550 offsetof (struct user_regs_struct, gs_base), 0);
551 if (errno == 0)
552 {
553 *base = (void *) gs;
554 return PS_OK;
555 }
556 }
557#endif
50d71875
AC
558 if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0)
559 return PS_OK;
560 break;
561 default: /* Should not happen. */
562 return PS_BADADDR;
563 }
c43af07c 564 }
b6d42148 565 return PS_ERR; /* ptrace failed. */
c43af07c 566}
5bca7895 567\f
c43af07c 568
10d6c8cd
DJ
569static void (*super_post_startup_inferior) (ptid_t ptid);
570
571static void
572amd64_linux_child_post_startup_inferior (ptid_t ptid)
4056d258
ML
573{
574 i386_cleanup_dregs ();
10d6c8cd 575 super_post_startup_inferior (ptid);
4056d258 576}
60fac5b8
MK
577\f
578
5b009018
PA
579/* When GDB is built as a 64-bit application on linux, the
580 PTRACE_GETSIGINFO data is always presented in 64-bit layout. Since
581 debugging a 32-bit inferior with a 64-bit GDB should look the same
582 as debugging it with a 32-bit GDB, we do the 32-bit <-> 64-bit
583 conversion in-place ourselves. */
584
585/* These types below (compat_*) define a siginfo type that is layout
586 compatible with the siginfo type exported by the 32-bit userspace
587 support. */
588
589typedef int compat_int_t;
590typedef unsigned int compat_uptr_t;
591
592typedef int compat_time_t;
593typedef int compat_timer_t;
594typedef int compat_clock_t;
595
596struct compat_timeval
597{
598 compat_time_t tv_sec;
599 int tv_usec;
600};
601
602typedef union compat_sigval
603{
604 compat_int_t sival_int;
605 compat_uptr_t sival_ptr;
606} compat_sigval_t;
607
608typedef struct compat_siginfo
609{
610 int si_signo;
611 int si_errno;
612 int si_code;
613
614 union
615 {
616 int _pad[((128 / sizeof (int)) - 3)];
617
618 /* kill() */
619 struct
620 {
621 unsigned int _pid;
622 unsigned int _uid;
623 } _kill;
624
625 /* POSIX.1b timers */
626 struct
627 {
628 compat_timer_t _tid;
629 int _overrun;
630 compat_sigval_t _sigval;
631 } _timer;
632
633 /* POSIX.1b signals */
634 struct
635 {
636 unsigned int _pid;
637 unsigned int _uid;
638 compat_sigval_t _sigval;
639 } _rt;
640
641 /* SIGCHLD */
642 struct
643 {
644 unsigned int _pid;
645 unsigned int _uid;
646 int _status;
647 compat_clock_t _utime;
648 compat_clock_t _stime;
649 } _sigchld;
650
651 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
652 struct
653 {
654 unsigned int _addr;
655 } _sigfault;
656
657 /* SIGPOLL */
658 struct
659 {
660 int _band;
661 int _fd;
662 } _sigpoll;
663 } _sifields;
664} compat_siginfo_t;
665
ba224c70
L
666/* For x32, clock_t in _sigchld is 64bit aligned at 4 bytes. */
667typedef struct compat_x32_clock
668{
669 int lower;
670 int upper;
671} compat_x32_clock_t;
672
673typedef struct compat_x32_siginfo
674{
675 int si_signo;
676 int si_errno;
677 int si_code;
678
679 union
680 {
681 int _pad[((128 / sizeof (int)) - 3)];
682
683 /* kill() */
684 struct
685 {
686 unsigned int _pid;
687 unsigned int _uid;
688 } _kill;
689
690 /* POSIX.1b timers */
691 struct
692 {
693 compat_timer_t _tid;
694 int _overrun;
695 compat_sigval_t _sigval;
696 } _timer;
697
698 /* POSIX.1b signals */
699 struct
700 {
701 unsigned int _pid;
702 unsigned int _uid;
703 compat_sigval_t _sigval;
704 } _rt;
705
706 /* SIGCHLD */
707 struct
708 {
709 unsigned int _pid;
710 unsigned int _uid;
711 int _status;
712 compat_x32_clock_t _utime;
713 compat_x32_clock_t _stime;
714 } _sigchld;
715
716 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
717 struct
718 {
719 unsigned int _addr;
720 } _sigfault;
721
722 /* SIGPOLL */
723 struct
724 {
725 int _band;
726 int _fd;
727 } _sigpoll;
728 } _sifields;
729} compat_x32_siginfo_t;
730
5b009018
PA
731#define cpt_si_pid _sifields._kill._pid
732#define cpt_si_uid _sifields._kill._uid
733#define cpt_si_timerid _sifields._timer._tid
734#define cpt_si_overrun _sifields._timer._overrun
735#define cpt_si_status _sifields._sigchld._status
736#define cpt_si_utime _sifields._sigchld._utime
737#define cpt_si_stime _sifields._sigchld._stime
738#define cpt_si_ptr _sifields._rt._sigval.sival_ptr
739#define cpt_si_addr _sifields._sigfault._addr
740#define cpt_si_band _sifields._sigpoll._band
741#define cpt_si_fd _sifields._sigpoll._fd
742
14064aa2
DE
743/* glibc at least up to 2.3.2 doesn't have si_timerid, si_overrun.
744 In their place is si_timer1,si_timer2. */
745#ifndef si_timerid
746#define si_timerid si_timer1
747#endif
748#ifndef si_overrun
749#define si_overrun si_timer2
750#endif
751
5b009018
PA
752static void
753compat_siginfo_from_siginfo (compat_siginfo_t *to, siginfo_t *from)
754{
755 memset (to, 0, sizeof (*to));
756
757 to->si_signo = from->si_signo;
758 to->si_errno = from->si_errno;
759 to->si_code = from->si_code;
760
b53a1623 761 if (to->si_code == SI_TIMER)
5b009018 762 {
b53a1623
PA
763 to->cpt_si_timerid = from->si_timerid;
764 to->cpt_si_overrun = from->si_overrun;
5b009018
PA
765 to->cpt_si_ptr = (intptr_t) from->si_ptr;
766 }
767 else if (to->si_code == SI_USER)
768 {
769 to->cpt_si_pid = from->si_pid;
770 to->cpt_si_uid = from->si_uid;
771 }
b53a1623 772 else if (to->si_code < 0)
5b009018 773 {
b53a1623
PA
774 to->cpt_si_pid = from->si_pid;
775 to->cpt_si_uid = from->si_uid;
5b009018
PA
776 to->cpt_si_ptr = (intptr_t) from->si_ptr;
777 }
778 else
779 {
780 switch (to->si_signo)
781 {
782 case SIGCHLD:
783 to->cpt_si_pid = from->si_pid;
784 to->cpt_si_uid = from->si_uid;
785 to->cpt_si_status = from->si_status;
786 to->cpt_si_utime = from->si_utime;
787 to->cpt_si_stime = from->si_stime;
788 break;
789 case SIGILL:
790 case SIGFPE:
791 case SIGSEGV:
792 case SIGBUS:
793 to->cpt_si_addr = (intptr_t) from->si_addr;
794 break;
795 case SIGPOLL:
796 to->cpt_si_band = from->si_band;
797 to->cpt_si_fd = from->si_fd;
798 break;
799 default:
800 to->cpt_si_pid = from->si_pid;
801 to->cpt_si_uid = from->si_uid;
802 to->cpt_si_ptr = (intptr_t) from->si_ptr;
803 break;
804 }
805 }
806}
807
808static void
809siginfo_from_compat_siginfo (siginfo_t *to, compat_siginfo_t *from)
810{
811 memset (to, 0, sizeof (*to));
812
813 to->si_signo = from->si_signo;
814 to->si_errno = from->si_errno;
815 to->si_code = from->si_code;
816
b53a1623 817 if (to->si_code == SI_TIMER)
5b009018 818 {
b53a1623
PA
819 to->si_timerid = from->cpt_si_timerid;
820 to->si_overrun = from->cpt_si_overrun;
5b009018
PA
821 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
822 }
823 else if (to->si_code == SI_USER)
824 {
825 to->si_pid = from->cpt_si_pid;
826 to->si_uid = from->cpt_si_uid;
827 }
b53a1623 828 if (to->si_code < 0)
5b009018 829 {
b53a1623
PA
830 to->si_pid = from->cpt_si_pid;
831 to->si_uid = from->cpt_si_uid;
5b009018
PA
832 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
833 }
834 else
835 {
836 switch (to->si_signo)
837 {
838 case SIGCHLD:
839 to->si_pid = from->cpt_si_pid;
840 to->si_uid = from->cpt_si_uid;
841 to->si_status = from->cpt_si_status;
842 to->si_utime = from->cpt_si_utime;
843 to->si_stime = from->cpt_si_stime;
844 break;
845 case SIGILL:
846 case SIGFPE:
847 case SIGSEGV:
848 case SIGBUS:
849 to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
850 break;
851 case SIGPOLL:
852 to->si_band = from->cpt_si_band;
853 to->si_fd = from->cpt_si_fd;
854 break;
855 default:
856 to->si_pid = from->cpt_si_pid;
857 to->si_uid = from->cpt_si_uid;
858 to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
859 break;
860 }
861 }
862}
863
ba224c70
L
864static void
865compat_x32_siginfo_from_siginfo (compat_x32_siginfo_t *to,
866 siginfo_t *from)
867{
868 memset (to, 0, sizeof (*to));
869
870 to->si_signo = from->si_signo;
871 to->si_errno = from->si_errno;
872 to->si_code = from->si_code;
873
874 if (to->si_code == SI_TIMER)
875 {
876 to->cpt_si_timerid = from->si_timerid;
877 to->cpt_si_overrun = from->si_overrun;
878 to->cpt_si_ptr = (intptr_t) from->si_ptr;
879 }
880 else if (to->si_code == SI_USER)
881 {
882 to->cpt_si_pid = from->si_pid;
883 to->cpt_si_uid = from->si_uid;
884 }
885 else if (to->si_code < 0)
886 {
887 to->cpt_si_pid = from->si_pid;
888 to->cpt_si_uid = from->si_uid;
889 to->cpt_si_ptr = (intptr_t) from->si_ptr;
890 }
891 else
892 {
893 switch (to->si_signo)
894 {
895 case SIGCHLD:
896 to->cpt_si_pid = from->si_pid;
897 to->cpt_si_uid = from->si_uid;
898 to->cpt_si_status = from->si_status;
899 memcpy (&to->cpt_si_utime, &from->si_utime,
900 sizeof (to->cpt_si_utime));
901 memcpy (&to->cpt_si_stime, &from->si_stime,
902 sizeof (to->cpt_si_stime));
903 break;
904 case SIGILL:
905 case SIGFPE:
906 case SIGSEGV:
907 case SIGBUS:
908 to->cpt_si_addr = (intptr_t) from->si_addr;
909 break;
910 case SIGPOLL:
911 to->cpt_si_band = from->si_band;
912 to->cpt_si_fd = from->si_fd;
913 break;
914 default:
915 to->cpt_si_pid = from->si_pid;
916 to->cpt_si_uid = from->si_uid;
917 to->cpt_si_ptr = (intptr_t) from->si_ptr;
918 break;
919 }
920 }
921}
922
923static void
924siginfo_from_compat_x32_siginfo (siginfo_t *to,
925 compat_x32_siginfo_t *from)
926{
927 memset (to, 0, sizeof (*to));
928
929 to->si_signo = from->si_signo;
930 to->si_errno = from->si_errno;
931 to->si_code = from->si_code;
932
933 if (to->si_code == SI_TIMER)
934 {
935 to->si_timerid = from->cpt_si_timerid;
936 to->si_overrun = from->cpt_si_overrun;
937 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
938 }
939 else if (to->si_code == SI_USER)
940 {
941 to->si_pid = from->cpt_si_pid;
942 to->si_uid = from->cpt_si_uid;
943 }
944 if (to->si_code < 0)
945 {
946 to->si_pid = from->cpt_si_pid;
947 to->si_uid = from->cpt_si_uid;
948 to->si_ptr = (void *) (intptr_t) from->cpt_si_ptr;
949 }
950 else
951 {
952 switch (to->si_signo)
953 {
954 case SIGCHLD:
955 to->si_pid = from->cpt_si_pid;
956 to->si_uid = from->cpt_si_uid;
957 to->si_status = from->cpt_si_status;
958 memcpy (&to->si_utime, &from->cpt_si_utime,
959 sizeof (to->si_utime));
960 memcpy (&to->si_stime, &from->cpt_si_stime,
961 sizeof (to->si_stime));
962 break;
963 case SIGILL:
964 case SIGFPE:
965 case SIGSEGV:
966 case SIGBUS:
967 to->si_addr = (void *) (intptr_t) from->cpt_si_addr;
968 break;
969 case SIGPOLL:
970 to->si_band = from->cpt_si_band;
971 to->si_fd = from->cpt_si_fd;
972 break;
973 default:
974 to->si_pid = from->cpt_si_pid;
975 to->si_uid = from->cpt_si_uid;
976 to->si_ptr = (void* ) (intptr_t) from->cpt_si_ptr;
977 break;
978 }
979 }
980}
981
5b009018
PA
982/* Convert a native/host siginfo object, into/from the siginfo in the
983 layout of the inferiors' architecture. Returns true if any
984 conversion was done; false otherwise. If DIRECTION is 1, then copy
985 from INF to NATIVE. If DIRECTION is 0, copy from NATIVE to
986 INF. */
987
988static int
a5362b9a 989amd64_linux_siginfo_fixup (siginfo_t *native, gdb_byte *inf, int direction)
5b009018 990{
ba224c70
L
991 struct gdbarch *gdbarch = get_frame_arch (get_current_frame ());
992
5b009018
PA
993 /* Is the inferior 32-bit? If so, then do fixup the siginfo
994 object. */
ba224c70 995 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 32)
5b009018 996 {
a5362b9a 997 gdb_assert (sizeof (siginfo_t) == sizeof (compat_siginfo_t));
5b009018
PA
998
999 if (direction == 0)
1000 compat_siginfo_from_siginfo ((struct compat_siginfo *) inf, native);
1001 else
1002 siginfo_from_compat_siginfo (native, (struct compat_siginfo *) inf);
1003
ba224c70
L
1004 return 1;
1005 }
1006 /* No fixup for native x32 GDB. */
1007 else if (gdbarch_addr_bit (gdbarch) == 32 && sizeof (void *) == 8)
1008 {
1009 gdb_assert (sizeof (siginfo_t) == sizeof (compat_x32_siginfo_t));
1010
1011 if (direction == 0)
1012 compat_x32_siginfo_from_siginfo ((struct compat_x32_siginfo *) inf,
1013 native);
1014 else
1015 siginfo_from_compat_x32_siginfo (native,
1016 (struct compat_x32_siginfo *) inf);
1017
5b009018
PA
1018 return 1;
1019 }
1020 else
1021 return 0;
1022}
1023
90884b2b
L
1024/* Get Linux/x86 target description from running target.
1025
1026 Value of CS segment register:
1027 1. 64bit process: 0x33.
1028 2. 32bit process: 0x23.
6da38f42
L
1029
1030 Value of DS segment register:
1031 1. LP64 process: 0x0.
1032 2. X32 process: 0x2b.
90884b2b
L
1033 */
1034
1035#define AMD64_LINUX_USER64_CS 0x33
6da38f42 1036#define AMD64_LINUX_X32_DS 0x2b
90884b2b
L
1037
1038static const struct target_desc *
1039amd64_linux_read_description (struct target_ops *ops)
1040{
1041 unsigned long cs;
6da38f42 1042 unsigned long ds;
90884b2b 1043 int tid;
a055a187 1044 int is_64bit;
6da38f42 1045 int is_x32;
a055a187 1046 static uint64_t xcr0;
90884b2b
L
1047
1048 /* GNU/Linux LWP ID's are process ID's. */
dfd4cc63 1049 tid = ptid_get_lwp (inferior_ptid);
90884b2b 1050 if (tid == 0)
dfd4cc63 1051 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
90884b2b
L
1052
1053 /* Get CS register. */
1054 errno = 0;
1055 cs = ptrace (PTRACE_PEEKUSER, tid,
1056 offsetof (struct user_regs_struct, cs), 0);
1057 if (errno != 0)
1058 perror_with_name (_("Couldn't get CS register"));
1059
a055a187
L
1060 is_64bit = cs == AMD64_LINUX_USER64_CS;
1061
6da38f42
L
1062 /* Get DS register. */
1063 errno = 0;
1064 ds = ptrace (PTRACE_PEEKUSER, tid,
1065 offsetof (struct user_regs_struct, ds), 0);
1066 if (errno != 0)
1067 perror_with_name (_("Couldn't get DS register"));
1068
1069 is_x32 = ds == AMD64_LINUX_X32_DS;
1070
1071 if (sizeof (void *) == 4 && is_64bit && !is_x32)
1072 error (_("Can't debug 64-bit process with 32-bit GDB"));
1073
a055a187
L
1074 if (have_ptrace_getregset == -1)
1075 {
1076 uint64_t xstateregs[(I386_XSTATE_SSE_SIZE / sizeof (uint64_t))];
1077 struct iovec iov;
1078
1079 iov.iov_base = xstateregs;
1080 iov.iov_len = sizeof (xstateregs);
1081
1082 /* Check if PTRACE_GETREGSET works. */
1083 if (ptrace (PTRACE_GETREGSET, tid,
1084 (unsigned int) NT_X86_XSTATE, (long) &iov) < 0)
1085 have_ptrace_getregset = 0;
1086 else
1087 {
1088 have_ptrace_getregset = 1;
1089
1090 /* Get XCR0 from XSAVE extended state. */
1091 xcr0 = xstateregs[(I386_LINUX_XSAVE_XCR0_OFFSET
1092 / sizeof (uint64_t))];
1093 }
1094 }
1095
1096 /* Check the native XCR0 only if PTRACE_GETREGSET is available. */
1097 if (have_ptrace_getregset
1098 && (xcr0 & I386_XSTATE_AVX_MASK) == I386_XSTATE_AVX_MASK)
1099 {
1100 if (is_64bit)
6da38f42
L
1101 {
1102 if (is_x32)
1103 return tdesc_x32_avx_linux;
1104 else
1105 return tdesc_amd64_avx_linux;
1106 }
a055a187
L
1107 else
1108 return tdesc_i386_avx_linux;
1109 }
90884b2b 1110 else
a055a187
L
1111 {
1112 if (is_64bit)
6da38f42
L
1113 {
1114 if (is_x32)
1115 return tdesc_x32_linux;
1116 else
1117 return tdesc_amd64_linux;
1118 }
a055a187
L
1119 else
1120 return tdesc_i386_linux;
1121 }
90884b2b
L
1122}
1123
3e3aea48
MM
1124/* Enable branch tracing. */
1125
1126static struct btrace_target_info *
1127amd64_linux_enable_btrace (ptid_t ptid)
1128{
1129 struct btrace_target_info *tinfo;
1130 struct gdbarch *gdbarch;
1131
1132 errno = 0;
1133 tinfo = linux_enable_btrace (ptid);
1134
1135 if (tinfo == NULL)
1136 error (_("Could not enable branch tracing for %s: %s."),
1137 target_pid_to_str (ptid), safe_strerror (errno));
1138
1139 /* Fill in the size of a pointer in bits. */
1140 gdbarch = target_thread_architecture (ptid);
1141 tinfo->ptr_bits = gdbarch_ptr_bit (gdbarch);
1142
1143 return tinfo;
1144}
1145
1146/* Disable branch tracing. */
1147
1148static void
1149amd64_linux_disable_btrace (struct btrace_target_info *tinfo)
1150{
1151 int errcode = linux_disable_btrace (tinfo);
1152
1153 if (errcode != 0)
1154 error (_("Could not disable branch tracing: %s."), safe_strerror (errcode));
1155}
1156
1157/* Teardown branch tracing. */
1158
1159static void
1160amd64_linux_teardown_btrace (struct btrace_target_info *tinfo)
1161{
1162 /* Ignore errors. */
1163 linux_disable_btrace (tinfo);
1164}
1165
60fac5b8 1166/* Provide a prototype to silence -Wmissing-prototypes. */
430eaf2e 1167void _initialize_amd64_linux_nat (void);
60fac5b8
MK
1168
1169void
430eaf2e 1170_initialize_amd64_linux_nat (void)
60fac5b8 1171{
10d6c8cd
DJ
1172 struct target_ops *t;
1173
430eaf2e 1174 amd64_native_gregset32_reg_offset = amd64_linux_gregset32_reg_offset;
60fac5b8 1175 amd64_native_gregset32_num_regs = I386_LINUX_NUM_REGS;
6cd6a2ae 1176 amd64_native_gregset64_reg_offset = amd64_linux_gregset_reg_offset;
8695c747 1177 amd64_native_gregset64_num_regs = AMD64_LINUX_NUM_REGS;
60fac5b8 1178
430eaf2e 1179 gdb_assert (ARRAY_SIZE (amd64_linux_gregset32_reg_offset)
60fac5b8 1180 == amd64_native_gregset32_num_regs);
10d6c8cd
DJ
1181
1182 /* Fill in the generic GNU/Linux methods. */
1183 t = linux_target ();
1184
c03374d5
DJ
1185 i386_use_watchpoints (t);
1186
9bb9e8ad
PM
1187 i386_dr_low.set_control = amd64_linux_dr_set_control;
1188 i386_dr_low.set_addr = amd64_linux_dr_set_addr;
7b50312a 1189 i386_dr_low.get_addr = amd64_linux_dr_get_addr;
9bb9e8ad 1190 i386_dr_low.get_status = amd64_linux_dr_get_status;
7b50312a 1191 i386_dr_low.get_control = amd64_linux_dr_get_control;
9bb9e8ad
PM
1192 i386_set_debug_register_length (8);
1193
10d6c8cd
DJ
1194 /* Override the GNU/Linux inferior startup hook. */
1195 super_post_startup_inferior = t->to_post_startup_inferior;
1196 t->to_post_startup_inferior = amd64_linux_child_post_startup_inferior;
1197
1198 /* Add our register access methods. */
1199 t->to_fetch_registers = amd64_linux_fetch_inferior_registers;
1200 t->to_store_registers = amd64_linux_store_inferior_registers;
1201
90884b2b
L
1202 t->to_read_description = amd64_linux_read_description;
1203
3e3aea48
MM
1204 /* Add btrace methods. */
1205 t->to_supports_btrace = linux_supports_btrace;
1206 t->to_enable_btrace = amd64_linux_enable_btrace;
1207 t->to_disable_btrace = amd64_linux_disable_btrace;
1208 t->to_teardown_btrace = amd64_linux_teardown_btrace;
1209 t->to_read_btrace = linux_read_btrace;
1210
10d6c8cd 1211 /* Register the target. */
f973ed9c 1212 linux_nat_add_target (t);
9f0bdab8 1213 linux_nat_set_new_thread (t, amd64_linux_new_thread);
26cb8b7c
PA
1214 linux_nat_set_new_fork (t, amd64_linux_new_fork);
1215 linux_nat_set_forget_process (t, i386_forget_process);
5b009018 1216 linux_nat_set_siginfo_fixup (t, amd64_linux_siginfo_fixup);
7b50312a 1217 linux_nat_set_prepare_to_resume (t, amd64_linux_prepare_to_resume);
60fac5b8 1218}
This page took 1.001562 seconds and 4 git commands to generate.