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