1 /* Native-dependent code for GNU/Linux i386.
3 Copyright (C) 1999-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "elf/common.h"
25 #include <sys/ptrace.h>
28 #include "gdb_proc_service.h"
30 #include "i386-linux-nat.h"
31 #include "i387-tdep.h"
32 #include "i386-tdep.h"
33 #include "i386-linux-tdep.h"
34 #include "x86-xstate.h"
36 #include "x86-linux-nat.h"
37 #include "nat/linux-ptrace.h"
39 /* The register sets used in GNU/Linux ELF core-dumps are identical to
40 the register sets in `struct user' that is used for a.out
41 core-dumps, and is also used by `ptrace'. The corresponding types
42 are `elf_gregset_t' for the general-purpose registers (with
43 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
44 for the floating-point registers.
46 Those types used to be available under the names `gregset_t' and
47 `fpregset_t' too, and this file used those names in the past. But
48 those names are now used for the register sets used in the
49 `mcontext_t' type, and have a different size and layout. */
51 /* Which ptrace request retrieves which registers?
52 These apply to the corresponding SET requests as well. */
54 #define GETREGS_SUPPLIES(regno) \
55 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
57 #define GETFPXREGS_SUPPLIES(regno) \
58 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
60 #define GETXSTATEREGS_SUPPLIES(regno) \
61 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX512_NUM_REGS)
63 /* Does the current host support the GETREGS request? */
64 int have_ptrace_getregs
=
65 #ifdef HAVE_PTRACE_GETREGS
72 /* Does the current host support the GETFPXREGS request? The header
73 file may or may not define it, and even if it is defined, the
74 kernel will return EIO if it's running on a pre-SSE processor.
76 My instinct is to attach this to some architecture- or
77 target-specific data structure, but really, a particular GDB
78 process can only run on top of one kernel at a time. So it's okay
79 for this to be a simple variable. */
80 int have_ptrace_getfpxregs
=
81 #ifdef HAVE_PTRACE_GETFPXREGS
89 /* Accessing registers through the U area, one at a time. */
91 /* Fetch one register. */
94 fetch_register (struct regcache
*regcache
, int regno
)
99 gdb_assert (!have_ptrace_getregs
);
100 if (i386_linux_gregset_reg_offset
[regno
] == -1)
102 regcache_raw_supply (regcache
, regno
, NULL
);
106 /* GNU/Linux LWP ID's are process ID's. */
107 tid
= ptid_get_lwp (inferior_ptid
);
109 tid
= ptid_get_pid (inferior_ptid
); /* Not a threaded program. */
112 val
= ptrace (PTRACE_PEEKUSER
, tid
,
113 i386_linux_gregset_reg_offset
[regno
], 0);
115 error (_("Couldn't read register %s (#%d): %s."),
116 gdbarch_register_name (get_regcache_arch (regcache
), regno
),
117 regno
, safe_strerror (errno
));
119 regcache_raw_supply (regcache
, regno
, &val
);
122 /* Store one register. */
125 store_register (const struct regcache
*regcache
, int regno
)
130 gdb_assert (!have_ptrace_getregs
);
131 if (i386_linux_gregset_reg_offset
[regno
] == -1)
134 /* GNU/Linux LWP ID's are process ID's. */
135 tid
= ptid_get_lwp (inferior_ptid
);
137 tid
= ptid_get_pid (inferior_ptid
); /* Not a threaded program. */
140 regcache_raw_collect (regcache
, regno
, &val
);
141 ptrace (PTRACE_POKEUSER
, tid
,
142 i386_linux_gregset_reg_offset
[regno
], val
);
144 error (_("Couldn't write register %s (#%d): %s."),
145 gdbarch_register_name (get_regcache_arch (regcache
), regno
),
146 regno
, safe_strerror (errno
));
150 /* Transfering the general-purpose registers between GDB, inferiors
153 /* Fill GDB's register array with the general-purpose register values
157 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
*gregsetp
)
159 const gdb_byte
*regp
= (const gdb_byte
*) gregsetp
;
162 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
163 regcache_raw_supply (regcache
, i
,
164 regp
+ i386_linux_gregset_reg_offset
[i
]);
166 if (I386_LINUX_ORIG_EAX_REGNUM
167 < gdbarch_num_regs (get_regcache_arch (regcache
)))
168 regcache_raw_supply (regcache
, I386_LINUX_ORIG_EAX_REGNUM
, regp
169 + i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
172 /* Fill register REGNO (if it is a general-purpose register) in
173 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
174 do this for all registers. */
177 fill_gregset (const struct regcache
*regcache
,
178 elf_gregset_t
*gregsetp
, int regno
)
180 gdb_byte
*regp
= (gdb_byte
*) gregsetp
;
183 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
184 if (regno
== -1 || regno
== i
)
185 regcache_raw_collect (regcache
, i
,
186 regp
+ i386_linux_gregset_reg_offset
[i
]);
188 if ((regno
== -1 || regno
== I386_LINUX_ORIG_EAX_REGNUM
)
189 && I386_LINUX_ORIG_EAX_REGNUM
190 < gdbarch_num_regs (get_regcache_arch (regcache
)))
191 regcache_raw_collect (regcache
, I386_LINUX_ORIG_EAX_REGNUM
, regp
192 + i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
195 #ifdef HAVE_PTRACE_GETREGS
197 /* Fetch all general-purpose registers from process/thread TID and
198 store their values in GDB's register array. */
201 fetch_regs (struct regcache
*regcache
, int tid
)
204 elf_gregset_t
*regs_p
= ®s
;
206 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
210 /* The kernel we're running on doesn't support the GETREGS
211 request. Reset `have_ptrace_getregs'. */
212 have_ptrace_getregs
= 0;
216 perror_with_name (_("Couldn't get registers"));
219 supply_gregset (regcache
, (const elf_gregset_t
*) regs_p
);
222 /* Store all valid general-purpose registers in GDB's register array
223 into the process/thread specified by TID. */
226 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
230 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
231 perror_with_name (_("Couldn't get registers"));
233 fill_gregset (regcache
, ®s
, regno
);
235 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
236 perror_with_name (_("Couldn't write registers"));
241 static void fetch_regs (struct regcache
*regcache
, int tid
) {}
242 static void store_regs (const struct regcache
*regcache
, int tid
, int regno
) {}
247 /* Transfering floating-point registers between GDB, inferiors and cores. */
249 /* Fill GDB's register array with the floating-point register values in
253 supply_fpregset (struct regcache
*regcache
, const elf_fpregset_t
*fpregsetp
)
255 i387_supply_fsave (regcache
, -1, fpregsetp
);
258 /* Fill register REGNO (if it is a floating-point register) in
259 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
260 do this for all registers. */
263 fill_fpregset (const struct regcache
*regcache
,
264 elf_fpregset_t
*fpregsetp
, int regno
)
266 i387_collect_fsave (regcache
, regno
, fpregsetp
);
269 #ifdef HAVE_PTRACE_GETREGS
271 /* Fetch all floating-point registers from process/thread TID and store
272 thier values in GDB's register array. */
275 fetch_fpregs (struct regcache
*regcache
, int tid
)
277 elf_fpregset_t fpregs
;
279 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
280 perror_with_name (_("Couldn't get floating point status"));
282 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregs
);
285 /* Store all valid floating-point registers in GDB's register array
286 into the process/thread specified by TID. */
289 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
291 elf_fpregset_t fpregs
;
293 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
294 perror_with_name (_("Couldn't get floating point status"));
296 fill_fpregset (regcache
, &fpregs
, regno
);
298 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
299 perror_with_name (_("Couldn't write floating point status"));
305 fetch_fpregs (struct regcache
*regcache
, int tid
)
310 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
317 /* Transfering floating-point and SSE registers to and from GDB. */
319 /* Fetch all registers covered by the PTRACE_GETREGSET request from
320 process/thread TID and store their values in GDB's register array.
321 Return non-zero if successful, zero otherwise. */
324 fetch_xstateregs (struct regcache
*regcache
, int tid
)
326 char xstateregs
[X86_XSTATE_MAX_SIZE
];
329 if (!have_ptrace_getregset
)
332 iov
.iov_base
= xstateregs
;
333 iov
.iov_len
= sizeof(xstateregs
);
334 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
336 perror_with_name (_("Couldn't read extended state status"));
338 i387_supply_xsave (regcache
, -1, xstateregs
);
342 /* Store all valid registers in GDB's register array covered by the
343 PTRACE_SETREGSET request into the process/thread specified by TID.
344 Return non-zero if successful, zero otherwise. */
347 store_xstateregs (const struct regcache
*regcache
, int tid
, int regno
)
349 char xstateregs
[X86_XSTATE_MAX_SIZE
];
352 if (!have_ptrace_getregset
)
355 iov
.iov_base
= xstateregs
;
356 iov
.iov_len
= sizeof(xstateregs
);
357 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
359 perror_with_name (_("Couldn't read extended state status"));
361 i387_collect_xsave (regcache
, regno
, xstateregs
, 0);
363 if (ptrace (PTRACE_SETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
365 perror_with_name (_("Couldn't write extended state status"));
370 #ifdef HAVE_PTRACE_GETFPXREGS
372 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
373 process/thread TID and store their values in GDB's register array.
374 Return non-zero if successful, zero otherwise. */
377 fetch_fpxregs (struct regcache
*regcache
, int tid
)
379 elf_fpxregset_t fpxregs
;
381 if (! have_ptrace_getfpxregs
)
384 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
388 have_ptrace_getfpxregs
= 0;
392 perror_with_name (_("Couldn't read floating-point and SSE registers"));
395 i387_supply_fxsave (regcache
, -1, (const elf_fpxregset_t
*) &fpxregs
);
399 /* Store all valid registers in GDB's register array covered by the
400 PTRACE_SETFPXREGS request into the process/thread specified by TID.
401 Return non-zero if successful, zero otherwise. */
404 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
406 elf_fpxregset_t fpxregs
;
408 if (! have_ptrace_getfpxregs
)
411 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
415 have_ptrace_getfpxregs
= 0;
419 perror_with_name (_("Couldn't read floating-point and SSE registers"));
422 i387_collect_fxsave (regcache
, regno
, &fpxregs
);
424 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
425 perror_with_name (_("Couldn't write floating-point and SSE registers"));
433 fetch_fpxregs (struct regcache
*regcache
, int tid
)
439 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
444 #endif /* HAVE_PTRACE_GETFPXREGS */
447 /* Transferring arbitrary registers between GDB and inferior. */
449 /* Fetch register REGNO from the child process. If REGNO is -1, do
450 this for all registers (including the floating point and SSE
454 i386_linux_fetch_inferior_registers (struct target_ops
*ops
,
455 struct regcache
*regcache
, int regno
)
459 /* Use the old method of peeking around in `struct user' if the
460 GETREGS request isn't available. */
461 if (!have_ptrace_getregs
)
465 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
466 if (regno
== -1 || regno
== i
)
467 fetch_register (regcache
, i
);
472 /* GNU/Linux LWP ID's are process ID's. */
473 tid
= ptid_get_lwp (inferior_ptid
);
475 tid
= ptid_get_pid (inferior_ptid
); /* Not a threaded program. */
477 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
478 transfers more registers in one system call, and we'll cache the
479 results. But remember that fetch_fpxregs can fail, and return
483 fetch_regs (regcache
, tid
);
485 /* The call above might reset `have_ptrace_getregs'. */
486 if (!have_ptrace_getregs
)
488 i386_linux_fetch_inferior_registers (ops
, regcache
, regno
);
492 if (fetch_xstateregs (regcache
, tid
))
494 if (fetch_fpxregs (regcache
, tid
))
496 fetch_fpregs (regcache
, tid
);
500 if (GETREGS_SUPPLIES (regno
))
502 fetch_regs (regcache
, tid
);
506 if (GETXSTATEREGS_SUPPLIES (regno
))
508 if (fetch_xstateregs (regcache
, tid
))
512 if (GETFPXREGS_SUPPLIES (regno
))
514 if (fetch_fpxregs (regcache
, tid
))
517 /* Either our processor or our kernel doesn't support the SSE
518 registers, so read the FP registers in the traditional way,
519 and fill the SSE registers with dummy values. It would be
520 more graceful to handle differences in the register set using
521 gdbarch. Until then, this will at least make things work
523 fetch_fpregs (regcache
, tid
);
527 internal_error (__FILE__
, __LINE__
,
528 _("Got request for bad register number %d."), regno
);
531 /* Store register REGNO back into the child process. If REGNO is -1,
532 do this for all registers (including the floating point and SSE
535 i386_linux_store_inferior_registers (struct target_ops
*ops
,
536 struct regcache
*regcache
, int regno
)
540 /* Use the old method of poking around in `struct user' if the
541 SETREGS request isn't available. */
542 if (!have_ptrace_getregs
)
546 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
547 if (regno
== -1 || regno
== i
)
548 store_register (regcache
, i
);
553 /* GNU/Linux LWP ID's are process ID's. */
554 tid
= ptid_get_lwp (inferior_ptid
);
556 tid
= ptid_get_pid (inferior_ptid
); /* Not a threaded program. */
558 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
559 transfers more registers in one system call. But remember that
560 store_fpxregs can fail, and return zero. */
563 store_regs (regcache
, tid
, regno
);
564 if (store_xstateregs (regcache
, tid
, regno
))
566 if (store_fpxregs (regcache
, tid
, regno
))
568 store_fpregs (regcache
, tid
, regno
);
572 if (GETREGS_SUPPLIES (regno
))
574 store_regs (regcache
, tid
, regno
);
578 if (GETXSTATEREGS_SUPPLIES (regno
))
580 if (store_xstateregs (regcache
, tid
, regno
))
584 if (GETFPXREGS_SUPPLIES (regno
))
586 if (store_fpxregs (regcache
, tid
, regno
))
589 /* Either our processor or our kernel doesn't support the SSE
590 registers, so just write the FP registers in the traditional
592 store_fpregs (regcache
, tid
, regno
);
596 internal_error (__FILE__
, __LINE__
,
597 _("Got request to store bad register number %d."), regno
);
601 /* Called by libthread_db. Returns a pointer to the thread local
602 storage (or its descriptor). */
605 ps_get_thread_area (const struct ps_prochandle
*ph
,
606 lwpid_t lwpid
, int idx
, void **base
)
608 unsigned int base_addr
;
611 result
= x86_linux_get_thread_area (lwpid
, (void *) idx
, &base_addr
);
614 *(int *) base
= base_addr
;
620 /* The instruction for a GNU/Linux system call is:
624 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
626 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
628 /* The system call number is stored in the %eax register. */
629 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
631 /* We are specifically interested in the sigreturn and rt_sigreturn
634 #ifndef SYS_sigreturn
635 #define SYS_sigreturn 0x77
637 #ifndef SYS_rt_sigreturn
638 #define SYS_rt_sigreturn 0xad
641 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
642 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
644 /* Resume execution of the inferior process.
645 If STEP is nonzero, single-step it.
646 If SIGNAL is nonzero, give it that signal. */
649 i386_linux_resume (struct target_ops
*ops
,
650 ptid_t ptid
, int step
, enum gdb_signal signal
)
652 int pid
= ptid_get_lwp (ptid
);
655 if (catch_syscall_enabled () > 0)
656 request
= PTRACE_SYSCALL
;
658 request
= PTRACE_CONT
;
662 struct regcache
*regcache
= get_thread_regcache (ptid
);
663 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
664 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
666 gdb_byte buf
[LINUX_SYSCALL_LEN
];
668 request
= PTRACE_SINGLESTEP
;
670 regcache_cooked_read_unsigned (regcache
,
671 gdbarch_pc_regnum (gdbarch
), &pc
);
673 /* Returning from a signal trampoline is done by calling a
674 special system call (sigreturn or rt_sigreturn, see
675 i386-linux-tdep.c for more information). This system call
676 restores the registers that were saved when the signal was
677 raised, including %eflags. That means that single-stepping
678 won't work. Instead, we'll have to modify the signal context
679 that's about to be restored, and set the trace flag there. */
681 /* First check if PC is at a system call. */
682 if (target_read_memory (pc
, buf
, LINUX_SYSCALL_LEN
) == 0
683 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
686 regcache_cooked_read_unsigned (regcache
,
687 LINUX_SYSCALL_REGNUM
, &syscall
);
689 /* Then check the system call number. */
690 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
693 unsigned long int eflags
;
695 regcache_cooked_read_unsigned (regcache
, I386_ESP_REGNUM
, &sp
);
696 if (syscall
== SYS_rt_sigreturn
)
697 addr
= read_memory_unsigned_integer (sp
+ 8, 4, byte_order
)
702 /* Set the trace flag in the context that's about to be
704 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
705 read_memory (addr
, (gdb_byte
*) &eflags
, 4);
707 write_memory (addr
, (gdb_byte
*) &eflags
, 4);
712 if (ptrace (request
, pid
, 0, gdb_signal_to_host (signal
)) == -1)
713 perror_with_name (("ptrace"));
717 /* -Wmissing-prototypes */
718 extern initialize_file_ftype _initialize_i386_linux_nat
;
721 _initialize_i386_linux_nat (void)
723 /* Create a generic x86 GNU/Linux target. */
724 struct target_ops
*t
= x86_linux_create_target ();
726 /* Override the default ptrace resume method. */
727 t
->to_resume
= i386_linux_resume
;
729 /* Add our register access methods. */
730 t
->to_fetch_registers
= i386_linux_fetch_inferior_registers
;
731 t
->to_store_registers
= i386_linux_store_inferior_registers
;
733 /* Add the target. */
734 x86_linux_add_target (t
);