1 /* Native-dependent code for GNU/Linux x86-64.
3 Copyright 2001, 2002 Free Software Foundation, Inc.
5 Contributed by Jiri Smid, SuSE Labs.
7 This file is part of GDB.
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.
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.
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. */
28 #include "i387-tdep.h"
29 #include "gdb_assert.h"
30 #include "x86-64-tdep.h"
32 #include <sys/ptrace.h>
33 #include <sys/debugreg.h>
34 #include <sys/syscall.h>
35 #include <sys/procfs.h>
38 /* Mapping between the general-purpose registers in `struct user'
39 format and GDB's register array layout. */
41 static int x86_64_regmap
[] = {
51 x86_64_linux_dr_get (int regnum
)
56 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
57 multi-threaded processes here. For now, pretend there is just
59 tid
= PIDGET (inferior_ptid
);
61 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
62 ptrace call fails breaks debugging remote targets. The correct
63 way to fix this is to add the hardware breakpoint and watchpoint
64 stuff to the target vectore. For now, just return zero if the
67 value
= ptrace (PT_READ_U
, tid
,
68 offsetof (struct user
, u_debugreg
[regnum
]), 0);
71 perror_with_name ("Couldn't read debug register");
80 x86_64_linux_dr_set (int regnum
, unsigned long value
)
84 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
85 multi-threaded processes here. For now, pretend there is just
87 tid
= PIDGET (inferior_ptid
);
90 ptrace (PT_WRITE_U
, tid
, offsetof (struct user
, u_debugreg
[regnum
]), value
);
92 perror_with_name ("Couldn't write debug register");
96 x86_64_linux_dr_set_control (unsigned long control
)
98 x86_64_linux_dr_set (DR_CONTROL
, control
);
102 x86_64_linux_dr_set_addr (int regnum
, CORE_ADDR addr
)
104 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
106 x86_64_linux_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
110 x86_64_linux_dr_reset_addr (int regnum
)
112 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
114 x86_64_linux_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
118 x86_64_linux_dr_get_status (void)
120 return x86_64_linux_dr_get (DR_STATUS
);
124 /* The register sets used in GNU/Linux ELF core-dumps are identical to
125 the register sets used by `ptrace'. */
127 #define GETREGS_SUPPLIES(regno) \
128 (0 <= (regno) && (regno) < x86_64_num_gregs)
129 #define GETFPREGS_SUPPLIES(regno) \
130 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
133 /* Transfering the general-purpose registers between GDB, inferiors
136 /* Fill GDB's register array with the general-purpose register values
140 supply_gregset (elf_gregset_t
* gregsetp
)
142 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
145 for (i
= 0; i
< x86_64_num_gregs
; i
++)
146 supply_register (i
, (char *) (regp
+ x86_64_regmap
[i
]));
149 /* Fill register REGNO (if it is a general-purpose register) in
150 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
151 do this for all registers. */
154 fill_gregset (elf_gregset_t
* gregsetp
, int regno
)
156 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
159 for (i
= 0; i
< x86_64_num_gregs
; i
++)
160 if ((regno
== -1 || regno
== i
))
161 read_register_gen (i
, (char *) (regp
+ x86_64_regmap
[i
]));
164 /* Fetch all general-purpose registers from process/thread TID and
165 store their values in GDB's register array. */
172 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
173 perror_with_name ("Couldn't get registers");
175 supply_gregset (®s
);
178 /* Store all valid general-purpose registers in GDB's register array
179 into the process/thread specified by TID. */
182 store_regs (int tid
, int regno
)
186 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
187 perror_with_name ("Couldn't get registers");
189 fill_gregset (®s
, regno
);
191 if (ptrace (PTRACE_SETREGS
, tid
, 0, (long) ®s
) < 0)
192 perror_with_name ("Couldn't write registers");
196 /* Transfering floating-point registers between GDB, inferiors and cores. */
198 /* Fill GDB's register array with the floating-point register values in
202 supply_fpregset (elf_fpregset_t
* fpregsetp
)
204 i387_supply_fxsave ((char *) fpregsetp
);
207 /* Fill register REGNO (if it is a floating-point register) in
208 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
209 do this for all registers. */
212 fill_fpregset (elf_fpregset_t
* fpregsetp
, int regno
)
214 i387_fill_fxsave ((char *) fpregsetp
, regno
);
217 /* Fetch all floating-point registers from process/thread TID and store
218 thier values in GDB's register array. */
221 fetch_fpregs (int tid
)
223 elf_fpregset_t fpregs
;
225 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (long) &fpregs
) < 0)
226 perror_with_name ("Couldn't get floating point status");
228 supply_fpregset (&fpregs
);
231 /* Store all valid floating-point registers in GDB's register array
232 into the process/thread specified by TID. */
235 store_fpregs (int tid
, int regno
)
237 elf_fpregset_t fpregs
;
239 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (long) &fpregs
) < 0)
240 perror_with_name ("Couldn't get floating point status");
242 fill_fpregset (&fpregs
, regno
);
244 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (long) &fpregs
) < 0)
245 perror_with_name ("Couldn't write floating point status");
249 /* Transferring arbitrary registers between GDB and inferior. */
251 /* Fetch register REGNO from the child process. If REGNO is -1, do
252 this for all registers (including the floating point and SSE
256 fetch_inferior_registers (int regno
)
260 /* GNU/Linux LWP ID's are process ID's. */
261 if ((tid
= TIDGET (inferior_ptid
)) == 0)
262 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
271 if (GETREGS_SUPPLIES (regno
))
277 if (GETFPREGS_SUPPLIES (regno
))
283 internal_error (__FILE__
, __LINE__
,
284 "Got request for bad register number %d.", regno
);
287 /* Store register REGNO back into the child process. If REGNO is -1,
288 do this for all registers (including the floating point and SSE
291 store_inferior_registers (int regno
)
295 /* GNU/Linux LWP ID's are process ID's. */
296 if ((tid
= TIDGET (inferior_ptid
)) == 0)
297 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
301 store_regs (tid
, regno
);
302 store_fpregs (tid
, regno
);
306 if (GETREGS_SUPPLIES (regno
))
308 store_regs (tid
, regno
);
312 if (GETFPREGS_SUPPLIES (regno
))
314 store_fpregs (tid
, regno
);
318 internal_error (__FILE__
, __LINE__
,
319 "Got request to store bad register number %d.", regno
);
323 static const unsigned char linux_syscall
[] = { 0x0f, 0x05 };
325 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
327 /* The system call number is stored in the %rax register. */
328 #define LINUX_SYSCALL_REGNUM 0 /* %rax */
330 /* We are specifically interested in the sigreturn and rt_sigreturn
333 #ifndef SYS_sigreturn
334 #define SYS_sigreturn __NR_sigreturn
336 #ifndef SYS_rt_sigreturn
337 #define SYS_rt_sigreturn __NR_rt_sigreturn
340 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
341 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (152)
342 /* Offset to saved processor registers from <asm/ucontext.h> */
343 #define LINUX_UCONTEXT_SIGCONTEXT_OFFSET (36)
345 /* Interpreting register set info found in core files. */
346 /* Provide registers to GDB from a core file.
348 CORE_REG_SECT points to an array of bytes, which are the contents
349 of a `note' from a core file which BFD thinks might contain
350 register contents. CORE_REG_SIZE is its size.
352 WHICH says which register set corelow suspects this is:
353 0 --- the general-purpose register set, in elf_gregset_t format
354 2 --- the floating-point register set, in elf_fpregset_t format
356 REG_ADDR isn't used on GNU/Linux. */
359 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
360 int which
, CORE_ADDR reg_addr
)
362 elf_gregset_t gregset
;
363 elf_fpregset_t fpregset
;
367 if (core_reg_size
!= sizeof (gregset
))
368 warning ("Wrong size gregset in core file.");
371 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
372 supply_gregset (&gregset
);
377 if (core_reg_size
!= sizeof (fpregset
))
378 warning ("Wrong size fpregset in core file.");
381 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
382 supply_fpregset (&fpregset
);
387 /* We've covered all the kinds of registers we know about here,
388 so this must be something we wouldn't know what to do with
389 anyway. Just ignore it. */
394 /* Register that we are able to handle GNU/Linux ELF core file formats. */
396 static struct core_fns linux_elf_core_fns
= {
397 bfd_target_elf_flavour
, /* core_flavour */
398 default_check_format
, /* check_format */
399 default_core_sniffer
, /* core_sniffer */
400 fetch_core_registers
, /* core_read_registers */
405 #if !defined (offsetof)
406 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
409 /* Return the address of register REGNUM. BLOCKEND is the value of
410 u.u_ar0, which should point to the registers. */
412 x86_64_register_u_addr (CORE_ADDR blockend
, int regnum
)
418 if (IS_FP_REGNUM (regnum
))
420 fpstate
= ubase
+ ((char *) &u
.i387
.st_space
- (char *) &u
);
421 return (fpstate
+ 16 * (regnum
- FP0_REGNUM
));
423 else if (IS_SSE_REGNUM (regnum
))
425 fpstate
= ubase
+ ((char *) &u
.i387
.xmm_space
- (char *) &u
);
426 return (fpstate
+ 16 * (regnum
- XMM0_REGNUM
));
429 return (ubase
+ 8 * x86_64_regmap
[regnum
]);
433 _initialize_x86_64_linux_nat (void)
435 add_core_fns (&linux_elf_core_fns
);
441 return (sizeof (struct user
));