2003-06-11 David Carlton <carlton@bactrian.org>
[deliverable/binutils-gdb.git] / gdb / x86-64-linux-nat.c
1 /* Native-dependent code for GNU/Linux x86-64.
2
3 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
4
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"
28
29 #include "gdb_assert.h"
30 #include "gdb_string.h"
31 #include <sys/ptrace.h>
32 #include <sys/debugreg.h>
33 #include <sys/syscall.h>
34 #include <sys/procfs.h>
35 #include <sys/reg.h>
36
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'
46 for the floating-point registers. */
47
48 /* Mapping between the general-purpose registers in `struct user'
49 format and GDB's register array layout. */
50 static int regmap[] =
51 {
52 RAX, RBX, RCX, RDX,
53 RSI, RDI, RBP, RSP,
54 R8, R9, R10, R11,
55 R12, R13, R14, R15,
56 RIP, EFLAGS, CS, SS,
57 DS, ES, FS, GS
58 };
59
60 /* Which ptrace request retrieves which registers?
61 These apply to the corresponding SET requests as well. */
62
63 #define GETREGS_SUPPLIES(regno) \
64 (0 <= (regno) && (regno) < X86_64_NUM_GREGS)
65
66 #define GETFPREGS_SUPPLIES(regno) \
67 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
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
77 supply_gregset (elf_gregset_t *gregsetp)
78 {
79 elf_greg_t *regp = (elf_greg_t *) gregsetp;
80 int i;
81
82 for (i = 0; i < X86_64_NUM_GREGS; i++)
83 supply_register (i, regp + regmap[i]);
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
91 fill_gregset (elf_gregset_t *gregsetp, int regno)
92 {
93 elf_greg_t *regp = (elf_greg_t *) gregsetp;
94 int i;
95
96 for (i = 0; i < X86_64_NUM_GREGS; i++)
97 if (regno == -1 || regno == i)
98 regcache_collect (i, regp + regmap[i]);
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) &regs) < 0)
110 perror_with_name ("Couldn't get registers");
111
112 supply_gregset (&regs);
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) &regs) < 0)
124 perror_with_name ("Couldn't get registers");
125
126 fill_gregset (&regs, regno);
127
128 if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
129 perror_with_name ("Couldn't write registers");
130 }
131 \f
132
133 /* Transfering floating-point registers between GDB, inferiors and cores. */
134
135 /* Fill GDB's register array with the floating-point and SSE register
136 values in *FPREGSETP. */
137
138 void
139 supply_fpregset (elf_fpregset_t *fpregsetp)
140 {
141 x86_64_supply_fxsave ((char *) fpregsetp);
142 }
143
144 /* Fill register REGNUM (if it is a floating-point or SSE register) in
145 *FPREGSETP with the value in GDB's register array. If REGNUM is
146 -1, do this for all registers. */
147
148 void
149 fill_fpregset (elf_fpregset_t *fpregsetp, int regnum)
150 {
151 x86_64_fill_fxsave ((char *) fpregsetp, regnum);
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
197 /* GNU/Linux LWP ID's are process ID's. */
198 tid = TIDGET (inferior_ptid);
199 if (tid == 0)
200 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
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,
226 do this for all registers (including the floating-point and SSE
227 registers). */
228
229 void
230 store_inferior_registers (int regno)
231 {
232 int tid;
233
234 /* GNU/Linux LWP ID's are process ID's. */
235 tid = TIDGET (inferior_ptid);
236 if (tid == 0)
237 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
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
263 static unsigned long
264 x86_64_linux_dr_get (int regnum)
265 {
266 int tid;
267 unsigned long value;
268
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);
273
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;
287 #endif
288
289 return value;
290 }
291
292 static void
293 x86_64_linux_dr_set (int regnum, unsigned long value)
294 {
295 int tid;
296
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);
301
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 }
307
308 void
309 x86_64_linux_dr_set_control (unsigned long control)
310 {
311 x86_64_linux_dr_set (DR_CONTROL, control);
312 }
313
314 void
315 x86_64_linux_dr_set_addr (int regnum, CORE_ADDR addr)
316 {
317 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
318
319 x86_64_linux_dr_set (DR_FIRSTADDR + regnum, addr);
320 }
321
322 void
323 x86_64_linux_dr_reset_addr (int regnum)
324 {
325 gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR);
326
327 x86_64_linux_dr_set (DR_FIRSTADDR + regnum, 0L);
328 }
329
330 unsigned long
331 x86_64_linux_dr_get_status (void)
332 {
333 return x86_64_linux_dr_get (DR_STATUS);
334 }
This page took 0.04063 seconds and 4 git commands to generate.