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. */
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 x86_64_linux_dr_get (int regnum
)
43 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
44 multi-threaded processes here. For now, pretend there is just
46 tid
= PIDGET (inferior_ptid
);
48 /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the
49 ptrace call fails breaks debugging remote targets. The correct
50 way to fix this is to add the hardware breakpoint and watchpoint
51 stuff to the target vectore. For now, just return zero if the
54 value
= ptrace (PT_READ_U
, tid
,
55 offsetof (struct user
, u_debugreg
[regnum
]), 0);
58 perror_with_name ("Couldn't read debug register");
67 x86_64_linux_dr_set (int regnum
, unsigned long value
)
71 /* FIXME: kettenis/2001-01-29: It's not clear what we should do with
72 multi-threaded processes here. For now, pretend there is just
74 tid
= PIDGET (inferior_ptid
);
77 ptrace (PT_WRITE_U
, tid
,
78 offsetof (struct user
, u_debugreg
[regnum
]), value
);
80 perror_with_name ("Couldn't write debug register");
84 x86_64_linux_dr_set_control (unsigned long control
)
86 x86_64_linux_dr_set (DR_CONTROL
, control
);
90 x86_64_linux_dr_set_addr (int regnum
, CORE_ADDR addr
)
92 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
94 x86_64_linux_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
98 x86_64_linux_dr_reset_addr (int regnum
)
100 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
102 x86_64_linux_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
106 x86_64_linux_dr_get_status (void)
108 return x86_64_linux_dr_get (DR_STATUS
);
112 /* The register sets used in GNU/Linux ELF core-dumps are identical to
113 the register sets used by `ptrace'. */
115 #define GETREGS_SUPPLIES(regno) \
116 (0 <= (regno) && (regno) <= 17)
117 #define GETFPREGS_SUPPLIES(regno) \
118 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
120 #define PTRACE_XFER_TYPE unsigned long
123 /* Transfering the general-purpose registers between GDB, inferiors
126 /* Fill GDB's register array with the general-purpose register values
130 supply_gregset (elf_gregset_t
* gregsetp
)
132 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
135 for (i
= 0; i
< X86_64_NUM_GREGS
; i
++)
136 supply_register (i
, (char *) (regp
+ x86_64_regmap
[i
]));
139 /* Fill register REGNO (if it is a general-purpose register) in
140 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
141 do this for all registers. */
144 fill_gregset (elf_gregset_t
* gregsetp
, int regno
)
146 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
149 for (i
= 0; i
< X86_64_NUM_GREGS
; i
++)
150 if ((regno
== -1 || regno
== i
))
151 read_register_gen (i
, regp
+ x86_64_regmap
[i
]);
154 /* Fetch all general-purpose registers from process/thread TID and
155 store their values in GDB's register array. */
162 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
163 perror_with_name ("Couldn't get registers");
165 supply_gregset (®s
);
168 /* Store all valid general-purpose registers in GDB's register array
169 into the process/thread specified by TID. */
172 store_regs (int tid
, int regno
)
176 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
177 perror_with_name ("Couldn't get registers");
179 fill_gregset (®s
, regno
);
181 if (ptrace (PTRACE_SETREGS
, tid
, 0, (long) ®s
) < 0)
182 perror_with_name ("Couldn't write registers");
186 /* Transfering floating-point registers between GDB, inferiors and cores. */
188 /* Fill GDB's register array with the floating-point register values in
192 supply_fpregset (elf_fpregset_t
* fpregsetp
)
194 i387_supply_fxsave ((char *) fpregsetp
);
197 /* Fill register REGNO (if it is a floating-point register) in
198 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
199 do this for all registers. */
202 fill_fpregset (elf_fpregset_t
* fpregsetp
, int regno
)
204 i387_fill_fxsave ((char *) fpregsetp
, regno
);
207 /* Fetch all floating-point registers from process/thread TID and store
208 thier values in GDB's register array. */
211 fetch_fpregs (int tid
)
213 elf_fpregset_t fpregs
;
215 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (long) &fpregs
) < 0)
216 perror_with_name ("Couldn't get floating point status");
218 supply_fpregset (&fpregs
);
221 /* Store all valid floating-point registers in GDB's register array
222 into the process/thread specified by TID. */
225 store_fpregs (int tid
, int regno
)
227 elf_fpregset_t fpregs
;
229 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (long) &fpregs
) < 0)
230 perror_with_name ("Couldn't get floating point status");
232 fill_fpregset (&fpregs
, regno
);
234 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (long) &fpregs
) < 0)
235 perror_with_name ("Couldn't write floating point status");
239 /* Transferring arbitrary registers between GDB and inferior. */
241 /* Fetch register REGNO from the child process. If REGNO is -1, do
242 this for all registers (including the floating point and SSE
246 fetch_inferior_registers (int regno
)
250 /* GNU/Linux LWP ID's are process ID's. */
251 if ((tid
= TIDGET (inferior_ptid
)) == 0)
252 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
261 if (GETREGS_SUPPLIES (regno
))
267 if (GETFPREGS_SUPPLIES (regno
))
273 internal_error (__FILE__
, __LINE__
,
274 "Got request for bad register number %d.", regno
);
277 /* Store register REGNO back into the child process. If REGNO is -1,
278 do this for all registers (including the floating point and SSE
281 store_inferior_registers (int regno
)
285 /* GNU/Linux LWP ID's are process ID's. */
286 if ((tid
= TIDGET (inferior_ptid
)) == 0)
287 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
291 store_regs (tid
, regno
);
292 store_fpregs (tid
, regno
);
296 if (GETREGS_SUPPLIES (regno
))
298 store_regs (tid
, regno
);
302 if (GETFPREGS_SUPPLIES (regno
))
304 store_fpregs (tid
, regno
);
308 internal_error (__FILE__
, __LINE__
,
309 "Got request to store bad register number %d.", regno
);
313 static const unsigned char linux_syscall
[] = { 0x0f, 0x05 };
315 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
317 /* The system call number is stored in the %rax register. */
318 #define LINUX_SYSCALL_REGNUM 0 /* %rax */
320 /* We are specifically interested in the sigreturn and rt_sigreturn
323 #ifndef SYS_sigreturn
324 #define SYS_sigreturn __NR_sigreturn
326 #ifndef SYS_rt_sigreturn
327 #define SYS_rt_sigreturn __NR_rt_sigreturn
330 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
331 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (152)
332 /* Offset to saved processor registers from <asm/ucontext.h> */
333 #define LINUX_UCONTEXT_SIGCONTEXT_OFFSET (36)
335 /* Resume execution of the inferior process.
336 If STEP is nonzero, single-step it.
337 If SIGNAL is nonzero, give it that signal. */
340 child_resume (ptid_t ptid
, int step
, enum target_signal signal
)
342 int pid
= PIDGET (ptid
);
343 int request
= PTRACE_CONT
;
346 /* Resume all threads. */
347 /* I think this only gets used in the non-threaded case, where "resume
348 all threads" and "resume inferior_ptid" are the same. */
349 pid
= PIDGET (inferior_ptid
);
353 CORE_ADDR pc
= read_pc_pid (pid_to_ptid (pid
));
354 unsigned char buf
[LINUX_SYSCALL_LEN
];
356 request
= PTRACE_SINGLESTEP
;
358 /* Returning from a signal trampoline is done by calling a
359 special system call (sigreturn or rt_sigreturn, see
360 i386-linux-tdep.c for more information). This system call
361 restores the registers that were saved when the signal was
362 raised, including %eflags. That means that single-stepping
363 won't work. Instead, we'll have to modify the signal context
364 that's about to be restored, and set the trace flag there. */
366 /* First check if PC is at a system call. */
367 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SYSCALL_LEN
) == 0
368 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
371 read_register_pid (LINUX_SYSCALL_REGNUM
, pid_to_ptid (pid
));
373 /* Then check the system call number. */
374 if (syscall
== SYS_rt_sigreturn
)
376 CORE_ADDR sp
= read_register (SP_REGNUM
);
378 unsigned long int eflags
;
381 sizeof (struct siginfo
) + LINUX_UCONTEXT_SIGCONTEXT_OFFSET
;
383 /* Set the trace flag in the context that's about to be
385 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
386 read_memory (addr
, (char *) &eflags
, 8);
388 write_memory (addr
, (char *) &eflags
, 8);
393 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
394 perror_with_name ("ptrace");
398 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
399 to debugger memory starting at MYADDR. Copy to inferior if
400 WRITE is nonzero. TARGET is ignored.
402 Returns the length copied, which is either the LEN argument or zero.
403 This xfer function does not do partial moves, since child_ops
404 doesn't allow memory operations to cross below us in the target stack
408 child_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
409 struct mem_attrib
*attrib
, struct target_ops
*target
)
412 /* Round starting address down to longword boundary. */
413 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
414 /* Round ending address up; get number of longwords that makes. */
416 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
417 / sizeof (PTRACE_XFER_TYPE
);
418 /* Allocate buffer of that many longwords. */
419 /* FIXME (alloca): This code, cloned from infptrace.c, is unsafe
420 because it uses alloca to allocate a buffer of arbitrary size.
421 For very large xfers, this could crash GDB's stack. */
422 register PTRACE_XFER_TYPE
*buffer
423 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
427 /* Fill start and end extra bytes of buffer with existing memory data. */
428 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_XFER_TYPE
))
430 /* Need part of initial word -- fetch it. */
431 ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
432 (PTRACE_ARG3_TYPE
) addr
, buffer
);
435 if (count
> 1) /* FIXME, avoid if even boundary */
437 ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
439 (addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
))),
443 /* Copy data to be written over corresponding part of buffer */
445 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
448 /* Write the entire buffer. */
450 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
453 ptrace (PT_WRITE_D
, PIDGET (inferior_ptid
),
454 (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
457 /* Using the appropriate one (I or D) is necessary for
458 Gould NP1, at least. */
460 ptrace (PT_WRITE_I
, PIDGET (inferior_ptid
),
461 (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
466 #ifdef CLEAR_INSN_CACHE
472 /* Read all the longwords */
473 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
476 ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
477 (PTRACE_ARG3_TYPE
) addr
, buffer
+ i
);
482 /* Copy appropriate bytes out of the buffer. */
484 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
490 /* Interpreting register set info found in core files. */
492 /* Provide registers to GDB from a core file.
494 CORE_REG_SECT points to an array of bytes, which are the contents
495 of a `note' from a core file which BFD thinks might contain
496 register contents. CORE_REG_SIZE is its size.
498 WHICH says which register set corelow suspects this is:
499 0 --- the general-purpose register set, in elf_gregset_t format
500 2 --- the floating-point register set, in elf_fpregset_t format
502 REG_ADDR isn't used on GNU/Linux. */
505 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
506 int which
, CORE_ADDR reg_addr
)
508 elf_gregset_t gregset
;
509 elf_fpregset_t fpregset
;
513 if (core_reg_size
!= sizeof (gregset
))
514 warning ("Wrong size gregset in core file.");
517 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
518 supply_gregset (&gregset
);
523 if (core_reg_size
!= sizeof (fpregset
))
524 warning ("Wrong size fpregset in core file.");
527 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
528 supply_fpregset (&fpregset
);
533 /* We've covered all the kinds of registers we know about here,
534 so this must be something we wouldn't know what to do with
535 anyway. Just ignore it. */
540 /* Register that we are able to handle GNU/Linux ELF core file formats. */
542 static struct core_fns linux_elf_core_fns
= {
543 bfd_target_elf_flavour
, /* core_flavour */
544 default_check_format
, /* check_format */
545 default_core_sniffer
, /* core_sniffer */
546 fetch_core_registers
, /* core_read_registers */
551 #if !defined (offsetof)
552 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
555 /* Record the value of the debug control register. */
556 static long debug_control_mirror
;
558 /* Record which address associates with which register. */
559 static CORE_ADDR address_lookup
[DR_LASTADDR
- DR_FIRSTADDR
+ 1];
561 /* Return the address of register REGNUM. BLOCKEND is the value of
562 u.u_ar0, which should point to the registers. */
564 x86_64_register_u_addr (CORE_ADDR blockend
, int regnum
)
570 if (IS_FP_REGNUM(regnum
))
572 fpstate
= ubase
+ ((char *) &u
.i387
.st_space
- (char *) &u
);
573 return (fpstate
+ 16 * (regnum
- FP0_REGNUM
));
575 else if (IS_SSE_REGNUM(regnum
))
577 fpstate
= ubase
+ ((char *) &u
.i387
.xmm_space
- (char *) &u
);
578 return (fpstate
+ 16 * (regnum
- XMM0_REGNUM
));
581 return (ubase
+ 8 * x86_64_regmap
[regnum
]);
585 _initialize_x86_64_linux_nat (void)
587 add_core_fns (&linux_elf_core_fns
);