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