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 /* 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
,
91 offsetof (struct user
, u_debugreg
[regnum
]), value
);
93 perror_with_name ("Couldn't write debug register");
97 x86_64_linux_dr_set_control (unsigned long control
)
99 x86_64_linux_dr_set (DR_CONTROL
, control
);
103 x86_64_linux_dr_set_addr (int regnum
, CORE_ADDR addr
)
105 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
107 x86_64_linux_dr_set (DR_FIRSTADDR
+ regnum
, addr
);
111 x86_64_linux_dr_reset_addr (int regnum
)
113 gdb_assert (regnum
>= 0 && regnum
<= DR_LASTADDR
- DR_FIRSTADDR
);
115 x86_64_linux_dr_set (DR_FIRSTADDR
+ regnum
, 0L);
119 x86_64_linux_dr_get_status (void)
121 return x86_64_linux_dr_get (DR_STATUS
);
125 /* The register sets used in GNU/Linux ELF core-dumps are identical to
126 the register sets used by `ptrace'. */
128 #define GETREGS_SUPPLIES(regno) \
129 (0 <= (regno) && (regno) < x86_64_num_gregs)
130 #define GETFPREGS_SUPPLIES(regno) \
131 (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
133 #define PTRACE_XFER_TYPE unsigned long
136 /* Transfering the general-purpose registers between GDB, inferiors
139 /* Fill GDB's register array with the general-purpose register values
143 supply_gregset (elf_gregset_t
* gregsetp
)
145 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
148 for (i
= 0; i
< x86_64_num_gregs
; i
++)
149 supply_register (i
, (char *) (regp
+ x86_64_regmap
[i
]));
152 /* Fill register REGNO (if it is a general-purpose register) in
153 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
154 do this for all registers. */
157 fill_gregset (elf_gregset_t
* gregsetp
, int regno
)
159 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
162 for (i
= 0; i
< x86_64_num_gregs
; i
++)
163 if ((regno
== -1 || regno
== i
))
164 read_register_gen (i
, regp
+ x86_64_regmap
[i
]);
167 /* Fetch all general-purpose registers from process/thread TID and
168 store their values in GDB's register array. */
175 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
176 perror_with_name ("Couldn't get registers");
178 supply_gregset (®s
);
181 /* Store all valid general-purpose registers in GDB's register array
182 into the process/thread specified by TID. */
185 store_regs (int tid
, int regno
)
189 if (ptrace (PTRACE_GETREGS
, tid
, 0, (long) ®s
) < 0)
190 perror_with_name ("Couldn't get registers");
192 fill_gregset (®s
, regno
);
194 if (ptrace (PTRACE_SETREGS
, tid
, 0, (long) ®s
) < 0)
195 perror_with_name ("Couldn't write registers");
199 /* Transfering floating-point registers between GDB, inferiors and cores. */
201 /* Fill GDB's register array with the floating-point register values in
205 supply_fpregset (elf_fpregset_t
* fpregsetp
)
207 i387_supply_fxsave ((char *) fpregsetp
);
210 /* Fill register REGNO (if it is a floating-point register) in
211 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
212 do this for all registers. */
215 fill_fpregset (elf_fpregset_t
* fpregsetp
, int regno
)
217 i387_fill_fxsave ((char *) fpregsetp
, regno
);
220 /* Fetch all floating-point registers from process/thread TID and store
221 thier values in GDB's register array. */
224 fetch_fpregs (int tid
)
226 elf_fpregset_t fpregs
;
228 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (long) &fpregs
) < 0)
229 perror_with_name ("Couldn't get floating point status");
231 supply_fpregset (&fpregs
);
234 /* Store all valid floating-point registers in GDB's register array
235 into the process/thread specified by TID. */
238 store_fpregs (int tid
, int regno
)
240 elf_fpregset_t fpregs
;
242 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (long) &fpregs
) < 0)
243 perror_with_name ("Couldn't get floating point status");
245 fill_fpregset (&fpregs
, regno
);
247 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (long) &fpregs
) < 0)
248 perror_with_name ("Couldn't write floating point status");
252 /* Transferring arbitrary registers between GDB and inferior. */
254 /* Fetch register REGNO from the child process. If REGNO is -1, do
255 this for all registers (including the floating point and SSE
259 fetch_inferior_registers (int regno
)
263 /* GNU/Linux LWP ID's are process ID's. */
264 if ((tid
= TIDGET (inferior_ptid
)) == 0)
265 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
274 if (GETREGS_SUPPLIES (regno
))
280 if (GETFPREGS_SUPPLIES (regno
))
286 internal_error (__FILE__
, __LINE__
,
287 "Got request for bad register number %d.", regno
);
290 /* Store register REGNO back into the child process. If REGNO is -1,
291 do this for all registers (including the floating point and SSE
294 store_inferior_registers (int regno
)
298 /* GNU/Linux LWP ID's are process ID's. */
299 if ((tid
= TIDGET (inferior_ptid
)) == 0)
300 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
304 store_regs (tid
, regno
);
305 store_fpregs (tid
, regno
);
309 if (GETREGS_SUPPLIES (regno
))
311 store_regs (tid
, regno
);
315 if (GETFPREGS_SUPPLIES (regno
))
317 store_fpregs (tid
, regno
);
321 internal_error (__FILE__
, __LINE__
,
322 "Got request to store bad register number %d.", regno
);
326 static const unsigned char linux_syscall
[] = { 0x0f, 0x05 };
328 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
330 /* The system call number is stored in the %rax register. */
331 #define LINUX_SYSCALL_REGNUM 0 /* %rax */
333 /* We are specifically interested in the sigreturn and rt_sigreturn
336 #ifndef SYS_sigreturn
337 #define SYS_sigreturn __NR_sigreturn
339 #ifndef SYS_rt_sigreturn
340 #define SYS_rt_sigreturn __NR_rt_sigreturn
343 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
344 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (152)
345 /* Offset to saved processor registers from <asm/ucontext.h> */
346 #define LINUX_UCONTEXT_SIGCONTEXT_OFFSET (36)
348 /* Resume execution of the inferior process.
349 If STEP is nonzero, single-step it.
350 If SIGNAL is nonzero, give it that signal. */
353 child_resume (ptid_t ptid
, int step
, enum target_signal signal
)
355 int pid
= PIDGET (ptid
);
356 int request
= PTRACE_CONT
;
359 /* Resume all threads. */
360 /* I think this only gets used in the non-threaded case, where "resume
361 all threads" and "resume inferior_ptid" are the same. */
362 pid
= PIDGET (inferior_ptid
);
366 CORE_ADDR pc
= read_pc_pid (pid_to_ptid (pid
));
367 unsigned char buf
[LINUX_SYSCALL_LEN
];
369 request
= PTRACE_SINGLESTEP
;
371 /* Returning from a signal trampoline is done by calling a
372 special system call (sigreturn or rt_sigreturn, see
373 i386-linux-tdep.c for more information). This system call
374 restores the registers that were saved when the signal was
375 raised, including %eflags. That means that single-stepping
376 won't work. Instead, we'll have to modify the signal context
377 that's about to be restored, and set the trace flag there. */
379 /* First check if PC is at a system call. */
380 if (read_memory_nobpt (pc
, (char *) buf
, LINUX_SYSCALL_LEN
) == 0
381 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
384 read_register_pid (LINUX_SYSCALL_REGNUM
, pid_to_ptid (pid
));
386 /* Then check the system call number. */
387 if (syscall
== SYS_rt_sigreturn
)
389 CORE_ADDR sp
= read_register (SP_REGNUM
);
391 unsigned long int eflags
;
394 sizeof (struct siginfo
) + LINUX_UCONTEXT_SIGCONTEXT_OFFSET
;
396 /* Set the trace flag in the context that's about to be
398 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
399 read_memory (addr
, (char *) &eflags
, 8);
401 write_memory (addr
, (char *) &eflags
, 8);
406 if (ptrace (request
, pid
, 0, target_signal_to_host (signal
)) == -1)
407 perror_with_name ("ptrace");
411 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
412 to debugger memory starting at MYADDR. Copy to inferior if
413 WRITE is nonzero. TARGET is ignored.
415 Returns the length copied, which is either the LEN argument or zero.
416 This xfer function does not do partial moves, since child_ops
417 doesn't allow memory operations to cross below us in the target stack
421 child_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
, int write
,
422 struct mem_attrib
*attrib
, struct target_ops
*target
)
425 /* Round starting address down to longword boundary. */
426 register CORE_ADDR addr
= memaddr
& -sizeof (PTRACE_XFER_TYPE
);
427 /* Round ending address up; get number of longwords that makes. */
429 = (((memaddr
+ len
) - addr
) + sizeof (PTRACE_XFER_TYPE
) - 1)
430 / sizeof (PTRACE_XFER_TYPE
);
431 /* Allocate buffer of that many longwords. */
432 /* FIXME (alloca): This code, cloned from infptrace.c, is unsafe
433 because it uses alloca to allocate a buffer of arbitrary size.
434 For very large xfers, this could crash GDB's stack. */
435 register PTRACE_XFER_TYPE
*buffer
436 = (PTRACE_XFER_TYPE
*) alloca (count
* sizeof (PTRACE_XFER_TYPE
));
440 /* Fill start and end extra bytes of buffer with existing memory data. */
441 if (addr
!= memaddr
|| len
< (int) sizeof (PTRACE_XFER_TYPE
))
443 /* Need part of initial word -- fetch it. */
444 ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
445 (PTRACE_ARG3_TYPE
) addr
, buffer
);
448 if (count
> 1) /* FIXME, avoid if even boundary */
450 ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
452 (addr
+ (count
- 1) * sizeof (PTRACE_XFER_TYPE
))),
456 /* Copy data to be written over corresponding part of buffer */
458 memcpy ((char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
461 /* Write the entire buffer. */
463 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
466 ptrace (PT_WRITE_D
, PIDGET (inferior_ptid
),
467 (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
470 /* Using the appropriate one (I or D) is necessary for
471 Gould NP1, at least. */
473 ptrace (PT_WRITE_I
, PIDGET (inferior_ptid
),
474 (PTRACE_ARG3_TYPE
) addr
, buffer
[i
]);
479 #ifdef CLEAR_INSN_CACHE
485 /* Read all the longwords */
486 for (i
= 0; i
< count
; i
++, addr
+= sizeof (PTRACE_XFER_TYPE
))
489 ptrace (PT_READ_I
, PIDGET (inferior_ptid
),
490 (PTRACE_ARG3_TYPE
) addr
, buffer
+ i
);
495 /* Copy appropriate bytes out of the buffer. */
497 (char *) buffer
+ (memaddr
& (sizeof (PTRACE_XFER_TYPE
) - 1)),
503 /* Interpreting register set info found in core files. */
505 /* Provide registers to GDB from a core file.
507 CORE_REG_SECT points to an array of bytes, which are the contents
508 of a `note' from a core file which BFD thinks might contain
509 register contents. CORE_REG_SIZE is its size.
511 WHICH says which register set corelow suspects this is:
512 0 --- the general-purpose register set, in elf_gregset_t format
513 2 --- the floating-point register set, in elf_fpregset_t format
515 REG_ADDR isn't used on GNU/Linux. */
518 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
519 int which
, CORE_ADDR reg_addr
)
521 elf_gregset_t gregset
;
522 elf_fpregset_t fpregset
;
526 if (core_reg_size
!= sizeof (gregset
))
527 warning ("Wrong size gregset in core file.");
530 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
531 supply_gregset (&gregset
);
536 if (core_reg_size
!= sizeof (fpregset
))
537 warning ("Wrong size fpregset in core file.");
540 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
541 supply_fpregset (&fpregset
);
546 /* We've covered all the kinds of registers we know about here,
547 so this must be something we wouldn't know what to do with
548 anyway. Just ignore it. */
553 /* Register that we are able to handle GNU/Linux ELF core file formats. */
555 static struct core_fns linux_elf_core_fns
= {
556 bfd_target_elf_flavour
, /* core_flavour */
557 default_check_format
, /* check_format */
558 default_core_sniffer
, /* core_sniffer */
559 fetch_core_registers
, /* core_read_registers */
564 #if !defined (offsetof)
565 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
568 /* Record the value of the debug control register. */
569 static long debug_control_mirror
;
571 /* Record which address associates with which register. */
572 static CORE_ADDR address_lookup
[DR_LASTADDR
- DR_FIRSTADDR
+ 1];
574 /* Return the address of register REGNUM. BLOCKEND is the value of
575 u.u_ar0, which should point to the registers. */
577 x86_64_register_u_addr (CORE_ADDR blockend
, int regnum
)
583 if (IS_FP_REGNUM(regnum
))
585 fpstate
= ubase
+ ((char *) &u
.i387
.st_space
- (char *) &u
);
586 return (fpstate
+ 16 * (regnum
- FP0_REGNUM
));
588 else if (IS_SSE_REGNUM(regnum
))
590 fpstate
= ubase
+ ((char *) &u
.i387
.xmm_space
- (char *) &u
);
591 return (fpstate
+ 16 * (regnum
- XMM0_REGNUM
));
594 return (ubase
+ 8 * x86_64_regmap
[regnum
]);
598 _initialize_x86_64_linux_nat (void)
600 add_core_fns (&linux_elf_core_fns
);