Commit | Line | Data |
---|---|---|
a4b6fc86 | 1 | /* Native-dependent code for GNU/Linux x86-64. |
0a65a603 | 2 | |
1bac305b | 3 | Copyright 2001, 2002, 2003 Free Software Foundation, Inc. |
0a65a603 | 4 | |
53e95fcf JS |
5 | Contributed by Jiri Smid, SuSE Labs. |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
23 | ||
24 | #include "defs.h" | |
25 | #include "inferior.h" | |
26 | #include "gdbcore.h" | |
27 | #include "regcache.h" | |
4056d258 | 28 | #include "linux-nat.h" |
c4f35dd8 | 29 | |
53e95fcf | 30 | #include "gdb_assert.h" |
30d52491 | 31 | #include "gdb_string.h" |
53e95fcf JS |
32 | #include <sys/ptrace.h> |
33 | #include <sys/debugreg.h> | |
34 | #include <sys/syscall.h> | |
35 | #include <sys/procfs.h> | |
c43af07c EZ |
36 | #include <asm/prctl.h> |
37 | /* FIXME ezannoni-2003-07-09: we need <sys/reg.h> to be included after | |
38 | <asm/ptrace.h> because the latter redefines FS and GS for no apparent | |
39 | reason, and those definitions don't match the ones that libpthread_db | |
40 | uses, which come from <sys/reg.h>. */ | |
41 | /* ezannoni-2003-07-09: I think this is fixed. The extraneous defs have | |
42 | been removed from ptrace.h in the kernel. However, better safe than | |
43 | sorry. */ | |
44 | #include <asm/ptrace.h> | |
33a0a2ac | 45 | #include <sys/reg.h> |
c43af07c | 46 | #include "gdb_proc_service.h" |
33a0a2ac | 47 | |
c4f35dd8 MK |
48 | /* Prototypes for supply_gregset etc. */ |
49 | #include "gregset.h" | |
50 | ||
51 | #include "x86-64-tdep.h" | |
eba29c8c | 52 | #include "x86-64-linux-tdep.h" |
53e95fcf | 53 | |
c4f35dd8 MK |
54 | /* Which ptrace request retrieves which registers? |
55 | These apply to the corresponding SET requests as well. */ | |
53e95fcf JS |
56 | |
57 | #define GETREGS_SUPPLIES(regno) \ | |
c4f35dd8 MK |
58 | (0 <= (regno) && (regno) < X86_64_NUM_GREGS) |
59 | ||
53e95fcf JS |
60 | #define GETFPREGS_SUPPLIES(regno) \ |
61 | (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM) | |
53e95fcf JS |
62 | \f |
63 | ||
64 | /* Transfering the general-purpose registers between GDB, inferiors | |
65 | and core files. */ | |
66 | ||
67 | /* Fill GDB's register array with the general-purpose register values | |
68 | in *GREGSETP. */ | |
69 | ||
70 | void | |
c4f35dd8 | 71 | supply_gregset (elf_gregset_t *gregsetp) |
53e95fcf | 72 | { |
eba29c8c | 73 | x86_64_linux_supply_gregset ((char *) gregsetp); |
53e95fcf JS |
74 | } |
75 | ||
76 | /* Fill register REGNO (if it is a general-purpose register) in | |
77 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, | |
78 | do this for all registers. */ | |
79 | ||
80 | void | |
c4f35dd8 | 81 | fill_gregset (elf_gregset_t *gregsetp, int regno) |
53e95fcf | 82 | { |
eba29c8c | 83 | x86_64_linux_fill_gregset ((char *) gregsetp, regno); |
53e95fcf JS |
84 | } |
85 | ||
86 | /* Fetch all general-purpose registers from process/thread TID and | |
87 | store their values in GDB's register array. */ | |
88 | ||
89 | static void | |
90 | fetch_regs (int tid) | |
91 | { | |
92 | elf_gregset_t regs; | |
93 | ||
94 | if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0) | |
7b3fabf0 | 95 | perror_with_name ("Couldn't get registers"); |
53e95fcf JS |
96 | |
97 | supply_gregset (®s); | |
98 | } | |
99 | ||
100 | /* Store all valid general-purpose registers in GDB's register array | |
101 | into the process/thread specified by TID. */ | |
102 | ||
103 | static void | |
104 | store_regs (int tid, int regno) | |
105 | { | |
106 | elf_gregset_t regs; | |
107 | ||
108 | if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0) | |
109 | perror_with_name ("Couldn't get registers"); | |
110 | ||
111 | fill_gregset (®s, regno); | |
112 | ||
113 | if (ptrace (PTRACE_SETREGS, tid, 0, (long) ®s) < 0) | |
114 | perror_with_name ("Couldn't write registers"); | |
115 | } | |
116 | \f | |
117 | ||
118 | /* Transfering floating-point registers between GDB, inferiors and cores. */ | |
119 | ||
8dda9770 | 120 | /* Fill GDB's register array with the floating-point and SSE register |
c4f35dd8 | 121 | values in *FPREGSETP. */ |
53e95fcf JS |
122 | |
123 | void | |
c4f35dd8 | 124 | supply_fpregset (elf_fpregset_t *fpregsetp) |
53e95fcf | 125 | { |
0485f6ad | 126 | x86_64_supply_fxsave ((const char *) fpregsetp, -1); |
53e95fcf JS |
127 | } |
128 | ||
8dda9770 | 129 | /* Fill register REGNUM (if it is a floating-point or SSE register) in |
c4f35dd8 MK |
130 | *FPREGSETP with the value in GDB's register array. If REGNUM is |
131 | -1, do this for all registers. */ | |
53e95fcf JS |
132 | |
133 | void | |
c4f35dd8 | 134 | fill_fpregset (elf_fpregset_t *fpregsetp, int regnum) |
53e95fcf | 135 | { |
c4f35dd8 | 136 | x86_64_fill_fxsave ((char *) fpregsetp, regnum); |
53e95fcf JS |
137 | } |
138 | ||
139 | /* Fetch all floating-point registers from process/thread TID and store | |
140 | thier values in GDB's register array. */ | |
141 | ||
142 | static void | |
143 | fetch_fpregs (int tid) | |
144 | { | |
145 | elf_fpregset_t fpregs; | |
146 | ||
147 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0) | |
148 | perror_with_name ("Couldn't get floating point status"); | |
149 | ||
150 | supply_fpregset (&fpregs); | |
151 | } | |
152 | ||
153 | /* Store all valid floating-point registers in GDB's register array | |
154 | into the process/thread specified by TID. */ | |
155 | ||
156 | static void | |
157 | store_fpregs (int tid, int regno) | |
158 | { | |
159 | elf_fpregset_t fpregs; | |
160 | ||
161 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0) | |
162 | perror_with_name ("Couldn't get floating point status"); | |
163 | ||
164 | fill_fpregset (&fpregs, regno); | |
165 | ||
166 | if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0) | |
167 | perror_with_name ("Couldn't write floating point status"); | |
168 | } | |
169 | \f | |
170 | ||
171 | /* Transferring arbitrary registers between GDB and inferior. */ | |
172 | ||
173 | /* Fetch register REGNO from the child process. If REGNO is -1, do | |
174 | this for all registers (including the floating point and SSE | |
175 | registers). */ | |
176 | ||
177 | void | |
178 | fetch_inferior_registers (int regno) | |
179 | { | |
180 | int tid; | |
181 | ||
a4b6fc86 | 182 | /* GNU/Linux LWP ID's are process ID's. */ |
c4f35dd8 MK |
183 | tid = TIDGET (inferior_ptid); |
184 | if (tid == 0) | |
185 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ | |
53e95fcf JS |
186 | |
187 | if (regno == -1) | |
188 | { | |
189 | fetch_regs (tid); | |
190 | fetch_fpregs (tid); | |
191 | return; | |
192 | } | |
193 | ||
194 | if (GETREGS_SUPPLIES (regno)) | |
195 | { | |
196 | fetch_regs (tid); | |
197 | return; | |
198 | } | |
199 | ||
200 | if (GETFPREGS_SUPPLIES (regno)) | |
201 | { | |
202 | fetch_fpregs (tid); | |
203 | return; | |
204 | } | |
205 | ||
206 | internal_error (__FILE__, __LINE__, | |
207 | "Got request for bad register number %d.", regno); | |
208 | } | |
209 | ||
210 | /* Store register REGNO back into the child process. If REGNO is -1, | |
c4f35dd8 | 211 | do this for all registers (including the floating-point and SSE |
53e95fcf | 212 | registers). */ |
c4f35dd8 | 213 | |
53e95fcf JS |
214 | void |
215 | store_inferior_registers (int regno) | |
216 | { | |
217 | int tid; | |
218 | ||
a4b6fc86 | 219 | /* GNU/Linux LWP ID's are process ID's. */ |
c4f35dd8 MK |
220 | tid = TIDGET (inferior_ptid); |
221 | if (tid == 0) | |
222 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ | |
53e95fcf JS |
223 | |
224 | if (regno == -1) | |
225 | { | |
226 | store_regs (tid, regno); | |
227 | store_fpregs (tid, regno); | |
228 | return; | |
229 | } | |
230 | ||
231 | if (GETREGS_SUPPLIES (regno)) | |
232 | { | |
233 | store_regs (tid, regno); | |
234 | return; | |
235 | } | |
236 | ||
237 | if (GETFPREGS_SUPPLIES (regno)) | |
238 | { | |
239 | store_fpregs (tid, regno); | |
240 | return; | |
241 | } | |
242 | ||
243 | internal_error (__FILE__, __LINE__, | |
244 | "Got request to store bad register number %d.", regno); | |
245 | } | |
246 | \f | |
247 | ||
c4f35dd8 MK |
248 | static unsigned long |
249 | x86_64_linux_dr_get (int regnum) | |
250 | { | |
251 | int tid; | |
252 | unsigned long value; | |
53e95fcf | 253 | |
c4f35dd8 MK |
254 | /* FIXME: kettenis/2001-01-29: It's not clear what we should do with |
255 | multi-threaded processes here. For now, pretend there is just | |
256 | one thread. */ | |
257 | tid = PIDGET (inferior_ptid); | |
53e95fcf | 258 | |
c4f35dd8 MK |
259 | /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the |
260 | ptrace call fails breaks debugging remote targets. The correct | |
261 | way to fix this is to add the hardware breakpoint and watchpoint | |
262 | stuff to the target vectore. For now, just return zero if the | |
263 | ptrace call fails. */ | |
264 | errno = 0; | |
265 | value = ptrace (PT_READ_U, tid, | |
266 | offsetof (struct user, u_debugreg[regnum]), 0); | |
267 | if (errno != 0) | |
268 | #if 0 | |
269 | perror_with_name ("Couldn't read debug register"); | |
270 | #else | |
271 | return 0; | |
53e95fcf JS |
272 | #endif |
273 | ||
c4f35dd8 MK |
274 | return value; |
275 | } | |
53e95fcf JS |
276 | |
277 | static void | |
c4f35dd8 | 278 | x86_64_linux_dr_set (int regnum, unsigned long value) |
53e95fcf | 279 | { |
c4f35dd8 | 280 | int tid; |
53e95fcf | 281 | |
c4f35dd8 MK |
282 | /* FIXME: kettenis/2001-01-29: It's not clear what we should do with |
283 | multi-threaded processes here. For now, pretend there is just | |
284 | one thread. */ | |
285 | tid = PIDGET (inferior_ptid); | |
53e95fcf | 286 | |
c4f35dd8 MK |
287 | errno = 0; |
288 | ptrace (PT_WRITE_U, tid, offsetof (struct user, u_debugreg[regnum]), value); | |
289 | if (errno != 0) | |
290 | perror_with_name ("Couldn't write debug register"); | |
291 | } | |
53e95fcf | 292 | |
c4f35dd8 MK |
293 | void |
294 | x86_64_linux_dr_set_control (unsigned long control) | |
295 | { | |
296 | x86_64_linux_dr_set (DR_CONTROL, control); | |
297 | } | |
53e95fcf | 298 | |
c4f35dd8 MK |
299 | void |
300 | x86_64_linux_dr_set_addr (int regnum, CORE_ADDR addr) | |
b7c4cbf8 | 301 | { |
c4f35dd8 MK |
302 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); |
303 | ||
304 | x86_64_linux_dr_set (DR_FIRSTADDR + regnum, addr); | |
b7c4cbf8 AJ |
305 | } |
306 | ||
53e95fcf | 307 | void |
c4f35dd8 | 308 | x86_64_linux_dr_reset_addr (int regnum) |
53e95fcf | 309 | { |
c4f35dd8 MK |
310 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); |
311 | ||
312 | x86_64_linux_dr_set (DR_FIRSTADDR + regnum, 0L); | |
53e95fcf | 313 | } |
8cfda98c | 314 | |
c4f35dd8 MK |
315 | unsigned long |
316 | x86_64_linux_dr_get_status (void) | |
8cfda98c | 317 | { |
c4f35dd8 | 318 | return x86_64_linux_dr_get (DR_STATUS); |
8cfda98c | 319 | } |
c43af07c EZ |
320 | |
321 | extern ps_err_e | |
322 | ps_get_thread_area (const struct ps_prochandle *ph, | |
323 | lwpid_t lwpid, int idx, void **base) | |
324 | { | |
325 | ||
326 | /* This definition comes from prctl.h, but some kernels may not have it. */ | |
327 | #ifndef PTRACE_ARCH_PRCTL | |
328 | #define PTRACE_ARCH_PRCTL 30 | |
329 | #endif | |
330 | ||
331 | /* FIXME: ezannoni-2003-07-09 see comment above about include file order. | |
332 | We could be getting bogus values for these two. */ | |
333 | gdb_assert (FS < ELF_NGREG); | |
334 | gdb_assert (GS < ELF_NGREG); | |
335 | switch (idx) | |
336 | { | |
337 | case FS: | |
338 | if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_FS) == 0) | |
339 | return PS_OK; | |
340 | break; | |
341 | case GS: | |
342 | if (ptrace (PTRACE_ARCH_PRCTL, lwpid, base, ARCH_GET_GS) == 0) | |
343 | return PS_OK; | |
344 | break; | |
345 | default: /* Should not happen. */ | |
346 | return PS_BADADDR; | |
347 | } | |
348 | return PS_ERR; /* ptrace failed. */ | |
349 | } | |
350 | ||
4056d258 ML |
351 | void |
352 | child_post_startup_inferior (ptid_t ptid) | |
353 | { | |
354 | i386_cleanup_dregs (); | |
355 | linux_child_post_startup_inferior (ptid); | |
356 | } |