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