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" | |
c4f35dd8 | 28 | |
53e95fcf | 29 | #include "gdb_assert.h" |
30d52491 | 30 | #include "gdb_string.h" |
53e95fcf JS |
31 | #include <sys/ptrace.h> |
32 | #include <sys/debugreg.h> | |
33 | #include <sys/syscall.h> | |
34 | #include <sys/procfs.h> | |
33a0a2ac ML |
35 | #include <sys/reg.h> |
36 | ||
c4f35dd8 MK |
37 | /* Prototypes for supply_gregset etc. */ |
38 | #include "gregset.h" | |
39 | ||
40 | #include "x86-64-tdep.h" | |
41 | ||
42 | /* The register sets used in GNU/Linux ELF core-dumps are identical to | |
43 | the register sets used by `ptrace'. The corresponding types are | |
44 | `elf_gregset_t' for the general-purpose registers (with | |
45 | `elf_greg_t' the type of a single GP register) and `elf_fpregset_t' | |
6a2751d2 | 46 | for the floating-point registers. */ |
c4f35dd8 | 47 | |
33a0a2ac ML |
48 | /* Mapping between the general-purpose registers in `struct user' |
49 | format and GDB's register array layout. */ | |
c4f35dd8 MK |
50 | static int regmap[] = |
51 | { | |
de220d0f | 52 | RAX, RBX, RCX, RDX, |
33a0a2ac ML |
53 | RSI, RDI, RBP, RSP, |
54 | R8, R9, R10, R11, | |
55 | R12, R13, R14, R15, | |
cb7e422f | 56 | RIP, EFLAGS, CS, SS, |
de220d0f | 57 | DS, ES, FS, GS |
33a0a2ac | 58 | }; |
53e95fcf | 59 | |
c4f35dd8 MK |
60 | /* Which ptrace request retrieves which registers? |
61 | These apply to the corresponding SET requests as well. */ | |
53e95fcf JS |
62 | |
63 | #define GETREGS_SUPPLIES(regno) \ | |
c4f35dd8 MK |
64 | (0 <= (regno) && (regno) < X86_64_NUM_GREGS) |
65 | ||
53e95fcf JS |
66 | #define GETFPREGS_SUPPLIES(regno) \ |
67 | (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM) | |
53e95fcf JS |
68 | \f |
69 | ||
70 | /* Transfering the general-purpose registers between GDB, inferiors | |
71 | and core files. */ | |
72 | ||
73 | /* Fill GDB's register array with the general-purpose register values | |
74 | in *GREGSETP. */ | |
75 | ||
76 | void | |
c4f35dd8 | 77 | supply_gregset (elf_gregset_t *gregsetp) |
53e95fcf JS |
78 | { |
79 | elf_greg_t *regp = (elf_greg_t *) gregsetp; | |
80 | int i; | |
81 | ||
c4f35dd8 MK |
82 | for (i = 0; i < X86_64_NUM_GREGS; i++) |
83 | supply_register (i, regp + regmap[i]); | |
53e95fcf JS |
84 | } |
85 | ||
86 | /* Fill register REGNO (if it is a general-purpose register) in | |
87 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, | |
88 | do this for all registers. */ | |
89 | ||
90 | void | |
c4f35dd8 | 91 | fill_gregset (elf_gregset_t *gregsetp, int regno) |
53e95fcf JS |
92 | { |
93 | elf_greg_t *regp = (elf_greg_t *) gregsetp; | |
94 | int i; | |
95 | ||
c4f35dd8 MK |
96 | for (i = 0; i < X86_64_NUM_GREGS; i++) |
97 | if (regno == -1 || regno == i) | |
98 | regcache_collect (i, regp + regmap[i]); | |
53e95fcf JS |
99 | } |
100 | ||
101 | /* Fetch all general-purpose registers from process/thread TID and | |
102 | store their values in GDB's register array. */ | |
103 | ||
104 | static void | |
105 | fetch_regs (int tid) | |
106 | { | |
107 | elf_gregset_t regs; | |
108 | ||
109 | if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0) | |
7b3fabf0 | 110 | perror_with_name ("Couldn't get registers"); |
53e95fcf JS |
111 | |
112 | supply_gregset (®s); | |
113 | } | |
114 | ||
115 | /* Store all valid general-purpose registers in GDB's register array | |
116 | into the process/thread specified by TID. */ | |
117 | ||
118 | static void | |
119 | store_regs (int tid, int regno) | |
120 | { | |
121 | elf_gregset_t regs; | |
122 | ||
123 | if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0) | |
124 | perror_with_name ("Couldn't get registers"); | |
125 | ||
126 | fill_gregset (®s, regno); | |
127 | ||
128 | if (ptrace (PTRACE_SETREGS, tid, 0, (long) ®s) < 0) | |
129 | perror_with_name ("Couldn't write registers"); | |
130 | } | |
131 | \f | |
132 | ||
133 | /* Transfering floating-point registers between GDB, inferiors and cores. */ | |
134 | ||
8dda9770 | 135 | /* Fill GDB's register array with the floating-point and SSE register |
c4f35dd8 | 136 | values in *FPREGSETP. */ |
53e95fcf JS |
137 | |
138 | void | |
c4f35dd8 | 139 | supply_fpregset (elf_fpregset_t *fpregsetp) |
53e95fcf | 140 | { |
c4f35dd8 | 141 | x86_64_supply_fxsave ((char *) fpregsetp); |
53e95fcf JS |
142 | } |
143 | ||
8dda9770 | 144 | /* Fill register REGNUM (if it is a floating-point or SSE register) in |
c4f35dd8 MK |
145 | *FPREGSETP with the value in GDB's register array. If REGNUM is |
146 | -1, do this for all registers. */ | |
53e95fcf JS |
147 | |
148 | void | |
c4f35dd8 | 149 | fill_fpregset (elf_fpregset_t *fpregsetp, int regnum) |
53e95fcf | 150 | { |
c4f35dd8 | 151 | x86_64_fill_fxsave ((char *) fpregsetp, regnum); |
53e95fcf JS |
152 | } |
153 | ||
154 | /* Fetch all floating-point registers from process/thread TID and store | |
155 | thier values in GDB's register array. */ | |
156 | ||
157 | static void | |
158 | fetch_fpregs (int tid) | |
159 | { | |
160 | elf_fpregset_t fpregs; | |
161 | ||
162 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0) | |
163 | perror_with_name ("Couldn't get floating point status"); | |
164 | ||
165 | supply_fpregset (&fpregs); | |
166 | } | |
167 | ||
168 | /* Store all valid floating-point registers in GDB's register array | |
169 | into the process/thread specified by TID. */ | |
170 | ||
171 | static void | |
172 | store_fpregs (int tid, int regno) | |
173 | { | |
174 | elf_fpregset_t fpregs; | |
175 | ||
176 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0) | |
177 | perror_with_name ("Couldn't get floating point status"); | |
178 | ||
179 | fill_fpregset (&fpregs, regno); | |
180 | ||
181 | if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0) | |
182 | perror_with_name ("Couldn't write floating point status"); | |
183 | } | |
184 | \f | |
185 | ||
186 | /* Transferring arbitrary registers between GDB and inferior. */ | |
187 | ||
188 | /* Fetch register REGNO from the child process. If REGNO is -1, do | |
189 | this for all registers (including the floating point and SSE | |
190 | registers). */ | |
191 | ||
192 | void | |
193 | fetch_inferior_registers (int regno) | |
194 | { | |
195 | int tid; | |
196 | ||
a4b6fc86 | 197 | /* GNU/Linux LWP ID's are process ID's. */ |
c4f35dd8 MK |
198 | tid = TIDGET (inferior_ptid); |
199 | if (tid == 0) | |
200 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ | |
53e95fcf JS |
201 | |
202 | if (regno == -1) | |
203 | { | |
204 | fetch_regs (tid); | |
205 | fetch_fpregs (tid); | |
206 | return; | |
207 | } | |
208 | ||
209 | if (GETREGS_SUPPLIES (regno)) | |
210 | { | |
211 | fetch_regs (tid); | |
212 | return; | |
213 | } | |
214 | ||
215 | if (GETFPREGS_SUPPLIES (regno)) | |
216 | { | |
217 | fetch_fpregs (tid); | |
218 | return; | |
219 | } | |
220 | ||
221 | internal_error (__FILE__, __LINE__, | |
222 | "Got request for bad register number %d.", regno); | |
223 | } | |
224 | ||
225 | /* Store register REGNO back into the child process. If REGNO is -1, | |
c4f35dd8 | 226 | do this for all registers (including the floating-point and SSE |
53e95fcf | 227 | registers). */ |
c4f35dd8 | 228 | |
53e95fcf JS |
229 | void |
230 | store_inferior_registers (int regno) | |
231 | { | |
232 | int tid; | |
233 | ||
a4b6fc86 | 234 | /* GNU/Linux LWP ID's are process ID's. */ |
c4f35dd8 MK |
235 | tid = TIDGET (inferior_ptid); |
236 | if (tid == 0) | |
237 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ | |
53e95fcf JS |
238 | |
239 | if (regno == -1) | |
240 | { | |
241 | store_regs (tid, regno); | |
242 | store_fpregs (tid, regno); | |
243 | return; | |
244 | } | |
245 | ||
246 | if (GETREGS_SUPPLIES (regno)) | |
247 | { | |
248 | store_regs (tid, regno); | |
249 | return; | |
250 | } | |
251 | ||
252 | if (GETFPREGS_SUPPLIES (regno)) | |
253 | { | |
254 | store_fpregs (tid, regno); | |
255 | return; | |
256 | } | |
257 | ||
258 | internal_error (__FILE__, __LINE__, | |
259 | "Got request to store bad register number %d.", regno); | |
260 | } | |
261 | \f | |
262 | ||
c4f35dd8 MK |
263 | static unsigned long |
264 | x86_64_linux_dr_get (int regnum) | |
265 | { | |
266 | int tid; | |
267 | unsigned long value; | |
53e95fcf | 268 | |
c4f35dd8 MK |
269 | /* FIXME: kettenis/2001-01-29: It's not clear what we should do with |
270 | multi-threaded processes here. For now, pretend there is just | |
271 | one thread. */ | |
272 | tid = PIDGET (inferior_ptid); | |
53e95fcf | 273 | |
c4f35dd8 MK |
274 | /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the |
275 | ptrace call fails breaks debugging remote targets. The correct | |
276 | way to fix this is to add the hardware breakpoint and watchpoint | |
277 | stuff to the target vectore. For now, just return zero if the | |
278 | ptrace call fails. */ | |
279 | errno = 0; | |
280 | value = ptrace (PT_READ_U, tid, | |
281 | offsetof (struct user, u_debugreg[regnum]), 0); | |
282 | if (errno != 0) | |
283 | #if 0 | |
284 | perror_with_name ("Couldn't read debug register"); | |
285 | #else | |
286 | return 0; | |
53e95fcf JS |
287 | #endif |
288 | ||
c4f35dd8 MK |
289 | return value; |
290 | } | |
53e95fcf JS |
291 | |
292 | static void | |
c4f35dd8 | 293 | x86_64_linux_dr_set (int regnum, unsigned long value) |
53e95fcf | 294 | { |
c4f35dd8 | 295 | int tid; |
53e95fcf | 296 | |
c4f35dd8 MK |
297 | /* FIXME: kettenis/2001-01-29: It's not clear what we should do with |
298 | multi-threaded processes here. For now, pretend there is just | |
299 | one thread. */ | |
300 | tid = PIDGET (inferior_ptid); | |
53e95fcf | 301 | |
c4f35dd8 MK |
302 | errno = 0; |
303 | ptrace (PT_WRITE_U, tid, offsetof (struct user, u_debugreg[regnum]), value); | |
304 | if (errno != 0) | |
305 | perror_with_name ("Couldn't write debug register"); | |
306 | } | |
53e95fcf | 307 | |
c4f35dd8 MK |
308 | void |
309 | x86_64_linux_dr_set_control (unsigned long control) | |
310 | { | |
311 | x86_64_linux_dr_set (DR_CONTROL, control); | |
312 | } | |
53e95fcf | 313 | |
c4f35dd8 MK |
314 | void |
315 | x86_64_linux_dr_set_addr (int regnum, CORE_ADDR addr) | |
b7c4cbf8 | 316 | { |
c4f35dd8 MK |
317 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); |
318 | ||
319 | x86_64_linux_dr_set (DR_FIRSTADDR + regnum, addr); | |
b7c4cbf8 AJ |
320 | } |
321 | ||
53e95fcf | 322 | void |
c4f35dd8 | 323 | x86_64_linux_dr_reset_addr (int regnum) |
53e95fcf | 324 | { |
c4f35dd8 MK |
325 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); |
326 | ||
327 | x86_64_linux_dr_set (DR_FIRSTADDR + regnum, 0L); | |
53e95fcf | 328 | } |
8cfda98c | 329 | |
c4f35dd8 MK |
330 | unsigned long |
331 | x86_64_linux_dr_get_status (void) | |
8cfda98c | 332 | { |
c4f35dd8 | 333 | return x86_64_linux_dr_get (DR_STATUS); |
8cfda98c | 334 | } |