1 /* Native-dependent code for GNU/Linux i386.
3 Copyright (C) 1999-2014 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/>. */
27 #include "linux-nat.h"
28 #include "nat/linux-btrace.h"
31 #include "gdb_assert.h"
33 #include "elf/common.h"
35 #include <sys/ptrace.h>
37 #include <sys/procfs.h>
47 #ifdef HAVE_SYS_DEBUGREG_H
48 #include <sys/debugreg.h>
51 /* Prototypes for supply_gregset etc. */
54 #include "i387-tdep.h"
55 #include "i386-tdep.h"
56 #include "i386-linux-tdep.h"
58 /* Defines ps_err_e, struct ps_prochandle. */
59 #include "gdb_proc_service.h"
61 #include "i386-xstate.h"
63 #include "x86-linux-nat.h"
65 /* The register sets used in GNU/Linux ELF core-dumps are identical to
66 the register sets in `struct user' that is used for a.out
67 core-dumps, and is also used by `ptrace'. The corresponding types
68 are `elf_gregset_t' for the general-purpose registers (with
69 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
70 for the floating-point registers.
72 Those types used to be available under the names `gregset_t' and
73 `fpregset_t' too, and this file used those names in the past. But
74 those names are now used for the register sets used in the
75 `mcontext_t' type, and have a different size and layout. */
77 /* Which ptrace request retrieves which registers?
78 These apply to the corresponding SET requests as well. */
80 #define GETREGS_SUPPLIES(regno) \
81 ((0 <= (regno) && (regno) <= 15) || (regno) == I386_LINUX_ORIG_EAX_REGNUM)
83 #define GETFPXREGS_SUPPLIES(regno) \
84 (I386_ST0_REGNUM <= (regno) && (regno) < I386_SSE_NUM_REGS)
86 #define GETXSTATEREGS_SUPPLIES(regno) \
87 (I386_ST0_REGNUM <= (regno) && (regno) < I386_AVX512_NUM_REGS)
89 /* Does the current host support the GETREGS request? */
90 int have_ptrace_getregs
=
91 #ifdef HAVE_PTRACE_GETREGS
98 /* Does the current host support the GETFPXREGS request? The header
99 file may or may not define it, and even if it is defined, the
100 kernel will return EIO if it's running on a pre-SSE processor.
102 My instinct is to attach this to some architecture- or
103 target-specific data structure, but really, a particular GDB
104 process can only run on top of one kernel at a time. So it's okay
105 for this to be a simple variable. */
106 int have_ptrace_getfpxregs
=
107 #ifdef HAVE_PTRACE_GETFPXREGS
115 /* Accessing registers through the U area, one at a time. */
117 /* Fetch one register. */
120 fetch_register (struct regcache
*regcache
, int regno
)
125 gdb_assert (!have_ptrace_getregs
);
126 if (i386_linux_gregset_reg_offset
[regno
] == -1)
128 regcache_raw_supply (regcache
, regno
, NULL
);
132 /* GNU/Linux LWP ID's are process ID's. */
133 tid
= ptid_get_lwp (inferior_ptid
);
135 tid
= ptid_get_pid (inferior_ptid
); /* Not a threaded program. */
138 val
= ptrace (PTRACE_PEEKUSER
, tid
,
139 i386_linux_gregset_reg_offset
[regno
], 0);
141 error (_("Couldn't read register %s (#%d): %s."),
142 gdbarch_register_name (get_regcache_arch (regcache
), regno
),
143 regno
, safe_strerror (errno
));
145 regcache_raw_supply (regcache
, regno
, &val
);
148 /* Store one register. */
151 store_register (const struct regcache
*regcache
, int regno
)
156 gdb_assert (!have_ptrace_getregs
);
157 if (i386_linux_gregset_reg_offset
[regno
] == -1)
160 /* GNU/Linux LWP ID's are process ID's. */
161 tid
= ptid_get_lwp (inferior_ptid
);
163 tid
= ptid_get_pid (inferior_ptid
); /* Not a threaded program. */
166 regcache_raw_collect (regcache
, regno
, &val
);
167 ptrace (PTRACE_POKEUSER
, tid
,
168 i386_linux_gregset_reg_offset
[regno
], val
);
170 error (_("Couldn't write register %s (#%d): %s."),
171 gdbarch_register_name (get_regcache_arch (regcache
), regno
),
172 regno
, safe_strerror (errno
));
176 /* Transfering the general-purpose registers between GDB, inferiors
179 /* Fill GDB's register array with the general-purpose register values
183 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
*gregsetp
)
185 const gdb_byte
*regp
= (const gdb_byte
*) gregsetp
;
188 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
189 regcache_raw_supply (regcache
, i
,
190 regp
+ i386_linux_gregset_reg_offset
[i
]);
192 if (I386_LINUX_ORIG_EAX_REGNUM
193 < gdbarch_num_regs (get_regcache_arch (regcache
)))
194 regcache_raw_supply (regcache
, I386_LINUX_ORIG_EAX_REGNUM
, regp
195 + i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
198 /* Fill register REGNO (if it is a general-purpose register) in
199 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
200 do this for all registers. */
203 fill_gregset (const struct regcache
*regcache
,
204 elf_gregset_t
*gregsetp
, int regno
)
206 gdb_byte
*regp
= (gdb_byte
*) gregsetp
;
209 for (i
= 0; i
< I386_NUM_GREGS
; i
++)
210 if (regno
== -1 || regno
== i
)
211 regcache_raw_collect (regcache
, i
,
212 regp
+ i386_linux_gregset_reg_offset
[i
]);
214 if ((regno
== -1 || regno
== I386_LINUX_ORIG_EAX_REGNUM
)
215 && I386_LINUX_ORIG_EAX_REGNUM
216 < gdbarch_num_regs (get_regcache_arch (regcache
)))
217 regcache_raw_collect (regcache
, I386_LINUX_ORIG_EAX_REGNUM
, regp
218 + i386_linux_gregset_reg_offset
[I386_LINUX_ORIG_EAX_REGNUM
]);
221 #ifdef HAVE_PTRACE_GETREGS
223 /* Fetch all general-purpose registers from process/thread TID and
224 store their values in GDB's register array. */
227 fetch_regs (struct regcache
*regcache
, int tid
)
230 elf_gregset_t
*regs_p
= ®s
;
232 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
236 /* The kernel we're running on doesn't support the GETREGS
237 request. Reset `have_ptrace_getregs'. */
238 have_ptrace_getregs
= 0;
242 perror_with_name (_("Couldn't get registers"));
245 supply_gregset (regcache
, (const elf_gregset_t
*) regs_p
);
248 /* Store all valid general-purpose registers in GDB's register array
249 into the process/thread specified by TID. */
252 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
256 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
257 perror_with_name (_("Couldn't get registers"));
259 fill_gregset (regcache
, ®s
, regno
);
261 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
262 perror_with_name (_("Couldn't write registers"));
267 static void fetch_regs (struct regcache
*regcache
, int tid
) {}
268 static void store_regs (const struct regcache
*regcache
, int tid
, int regno
) {}
273 /* Transfering floating-point registers between GDB, inferiors and cores. */
275 /* Fill GDB's register array with the floating-point register values in
279 supply_fpregset (struct regcache
*regcache
, const elf_fpregset_t
*fpregsetp
)
281 i387_supply_fsave (regcache
, -1, fpregsetp
);
284 /* Fill register REGNO (if it is a floating-point register) in
285 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
286 do this for all registers. */
289 fill_fpregset (const struct regcache
*regcache
,
290 elf_fpregset_t
*fpregsetp
, int regno
)
292 i387_collect_fsave (regcache
, regno
, fpregsetp
);
295 #ifdef HAVE_PTRACE_GETREGS
297 /* Fetch all floating-point registers from process/thread TID and store
298 thier values in GDB's register array. */
301 fetch_fpregs (struct regcache
*regcache
, int tid
)
303 elf_fpregset_t fpregs
;
305 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
306 perror_with_name (_("Couldn't get floating point status"));
308 supply_fpregset (regcache
, (const elf_fpregset_t
*) &fpregs
);
311 /* Store all valid floating-point registers in GDB's register array
312 into the process/thread specified by TID. */
315 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
317 elf_fpregset_t fpregs
;
319 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
320 perror_with_name (_("Couldn't get floating point status"));
322 fill_fpregset (regcache
, &fpregs
, regno
);
324 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
325 perror_with_name (_("Couldn't write floating point status"));
331 fetch_fpregs (struct regcache
*regcache
, int tid
)
336 store_fpregs (const struct regcache
*regcache
, int tid
, int regno
)
343 /* Transfering floating-point and SSE registers to and from GDB. */
345 /* Fetch all registers covered by the PTRACE_GETREGSET request from
346 process/thread TID and store their values in GDB's register array.
347 Return non-zero if successful, zero otherwise. */
350 fetch_xstateregs (struct regcache
*regcache
, int tid
)
352 char xstateregs
[I386_XSTATE_MAX_SIZE
];
355 if (!have_ptrace_getregset
)
358 iov
.iov_base
= xstateregs
;
359 iov
.iov_len
= sizeof(xstateregs
);
360 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
362 perror_with_name (_("Couldn't read extended state status"));
364 i387_supply_xsave (regcache
, -1, xstateregs
);
368 /* Store all valid registers in GDB's register array covered by the
369 PTRACE_SETREGSET request into the process/thread specified by TID.
370 Return non-zero if successful, zero otherwise. */
373 store_xstateregs (const struct regcache
*regcache
, int tid
, int regno
)
375 char xstateregs
[I386_XSTATE_MAX_SIZE
];
378 if (!have_ptrace_getregset
)
381 iov
.iov_base
= xstateregs
;
382 iov
.iov_len
= sizeof(xstateregs
);
383 if (ptrace (PTRACE_GETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
385 perror_with_name (_("Couldn't read extended state status"));
387 i387_collect_xsave (regcache
, regno
, xstateregs
, 0);
389 if (ptrace (PTRACE_SETREGSET
, tid
, (unsigned int) NT_X86_XSTATE
,
391 perror_with_name (_("Couldn't write extended state status"));
396 #ifdef HAVE_PTRACE_GETFPXREGS
398 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
399 process/thread TID and store their values in GDB's register array.
400 Return non-zero if successful, zero otherwise. */
403 fetch_fpxregs (struct regcache
*regcache
, int tid
)
405 elf_fpxregset_t fpxregs
;
407 if (! have_ptrace_getfpxregs
)
410 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, (int) &fpxregs
) < 0)
414 have_ptrace_getfpxregs
= 0;
418 perror_with_name (_("Couldn't read floating-point and SSE registers"));
421 i387_supply_fxsave (regcache
, -1, (const elf_fpxregset_t
*) &fpxregs
);
425 /* Store all valid registers in GDB's register array covered by the
426 PTRACE_SETFPXREGS request into the process/thread specified by TID.
427 Return non-zero if successful, zero otherwise. */
430 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
432 elf_fpxregset_t fpxregs
;
434 if (! have_ptrace_getfpxregs
)
437 if (ptrace (PTRACE_GETFPXREGS
, tid
, 0, &fpxregs
) == -1)
441 have_ptrace_getfpxregs
= 0;
445 perror_with_name (_("Couldn't read floating-point and SSE registers"));
448 i387_collect_fxsave (regcache
, regno
, &fpxregs
);
450 if (ptrace (PTRACE_SETFPXREGS
, tid
, 0, &fpxregs
) == -1)
451 perror_with_name (_("Couldn't write floating-point and SSE registers"));
459 fetch_fpxregs (struct regcache
*regcache
, int tid
)
465 store_fpxregs (const struct regcache
*regcache
, int tid
, int regno
)
470 #endif /* HAVE_PTRACE_GETFPXREGS */
473 /* Transferring arbitrary registers between GDB and inferior. */
475 /* Fetch register REGNO from the child process. If REGNO is -1, do
476 this for all registers (including the floating point and SSE
480 i386_linux_fetch_inferior_registers (struct target_ops
*ops
,
481 struct regcache
*regcache
, int regno
)
485 /* Use the old method of peeking around in `struct user' if the
486 GETREGS request isn't available. */
487 if (!have_ptrace_getregs
)
491 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
492 if (regno
== -1 || regno
== i
)
493 fetch_register (regcache
, i
);
498 /* GNU/Linux LWP ID's are process ID's. */
499 tid
= ptid_get_lwp (inferior_ptid
);
501 tid
= ptid_get_pid (inferior_ptid
); /* Not a threaded program. */
503 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
504 transfers more registers in one system call, and we'll cache the
505 results. But remember that fetch_fpxregs can fail, and return
509 fetch_regs (regcache
, tid
);
511 /* The call above might reset `have_ptrace_getregs'. */
512 if (!have_ptrace_getregs
)
514 i386_linux_fetch_inferior_registers (ops
, regcache
, regno
);
518 if (fetch_xstateregs (regcache
, tid
))
520 if (fetch_fpxregs (regcache
, tid
))
522 fetch_fpregs (regcache
, tid
);
526 if (GETREGS_SUPPLIES (regno
))
528 fetch_regs (regcache
, tid
);
532 if (GETXSTATEREGS_SUPPLIES (regno
))
534 if (fetch_xstateregs (regcache
, tid
))
538 if (GETFPXREGS_SUPPLIES (regno
))
540 if (fetch_fpxregs (regcache
, tid
))
543 /* Either our processor or our kernel doesn't support the SSE
544 registers, so read the FP registers in the traditional way,
545 and fill the SSE registers with dummy values. It would be
546 more graceful to handle differences in the register set using
547 gdbarch. Until then, this will at least make things work
549 fetch_fpregs (regcache
, tid
);
553 internal_error (__FILE__
, __LINE__
,
554 _("Got request for bad register number %d."), regno
);
557 /* Store register REGNO back into the child process. If REGNO is -1,
558 do this for all registers (including the floating point and SSE
561 i386_linux_store_inferior_registers (struct target_ops
*ops
,
562 struct regcache
*regcache
, int regno
)
566 /* Use the old method of poking around in `struct user' if the
567 SETREGS request isn't available. */
568 if (!have_ptrace_getregs
)
572 for (i
= 0; i
< gdbarch_num_regs (get_regcache_arch (regcache
)); i
++)
573 if (regno
== -1 || regno
== i
)
574 store_register (regcache
, i
);
579 /* GNU/Linux LWP ID's are process ID's. */
580 tid
= ptid_get_lwp (inferior_ptid
);
582 tid
= ptid_get_pid (inferior_ptid
); /* Not a threaded program. */
584 /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
585 transfers more registers in one system call. But remember that
586 store_fpxregs can fail, and return zero. */
589 store_regs (regcache
, tid
, regno
);
590 if (store_xstateregs (regcache
, tid
, regno
))
592 if (store_fpxregs (regcache
, tid
, regno
))
594 store_fpregs (regcache
, tid
, regno
);
598 if (GETREGS_SUPPLIES (regno
))
600 store_regs (regcache
, tid
, regno
);
604 if (GETXSTATEREGS_SUPPLIES (regno
))
606 if (store_xstateregs (regcache
, tid
, regno
))
610 if (GETFPXREGS_SUPPLIES (regno
))
612 if (store_fpxregs (regcache
, tid
, regno
))
615 /* Either our processor or our kernel doesn't support the SSE
616 registers, so just write the FP registers in the traditional
618 store_fpregs (regcache
, tid
, regno
);
622 internal_error (__FILE__
, __LINE__
,
623 _("Got request to store bad register number %d."), regno
);
627 /* Called by libthread_db. Returns a pointer to the thread local
628 storage (or its descriptor). */
631 ps_get_thread_area (const struct ps_prochandle
*ph
,
632 lwpid_t lwpid
, int idx
, void **base
)
634 unsigned int base_addr
;
637 result
= x86_linux_get_thread_area (lwpid
, (void *) idx
, &base_addr
);
640 *(int *) base
= base_addr
;
646 /* The instruction for a GNU/Linux system call is:
650 static const unsigned char linux_syscall
[] = { 0xcd, 0x80 };
652 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
654 /* The system call number is stored in the %eax register. */
655 #define LINUX_SYSCALL_REGNUM I386_EAX_REGNUM
657 /* We are specifically interested in the sigreturn and rt_sigreturn
660 #ifndef SYS_sigreturn
661 #define SYS_sigreturn 0x77
663 #ifndef SYS_rt_sigreturn
664 #define SYS_rt_sigreturn 0xad
667 /* Offset to saved processor flags, from <asm/sigcontext.h>. */
668 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
670 /* Resume execution of the inferior process.
671 If STEP is nonzero, single-step it.
672 If SIGNAL is nonzero, give it that signal. */
675 i386_linux_resume (struct target_ops
*ops
,
676 ptid_t ptid
, int step
, enum gdb_signal signal
)
678 int pid
= ptid_get_pid (ptid
);
682 if (catch_syscall_enabled () > 0)
683 request
= PTRACE_SYSCALL
;
685 request
= PTRACE_CONT
;
689 struct regcache
*regcache
= get_thread_regcache (pid_to_ptid (pid
));
690 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
691 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
693 gdb_byte buf
[LINUX_SYSCALL_LEN
];
695 request
= PTRACE_SINGLESTEP
;
697 regcache_cooked_read_unsigned (regcache
,
698 gdbarch_pc_regnum (gdbarch
), &pc
);
700 /* Returning from a signal trampoline is done by calling a
701 special system call (sigreturn or rt_sigreturn, see
702 i386-linux-tdep.c for more information). This system call
703 restores the registers that were saved when the signal was
704 raised, including %eflags. That means that single-stepping
705 won't work. Instead, we'll have to modify the signal context
706 that's about to be restored, and set the trace flag there. */
708 /* First check if PC is at a system call. */
709 if (target_read_memory (pc
, buf
, LINUX_SYSCALL_LEN
) == 0
710 && memcmp (buf
, linux_syscall
, LINUX_SYSCALL_LEN
) == 0)
713 regcache_cooked_read_unsigned (regcache
,
714 LINUX_SYSCALL_REGNUM
, &syscall
);
716 /* Then check the system call number. */
717 if (syscall
== SYS_sigreturn
|| syscall
== SYS_rt_sigreturn
)
720 unsigned long int eflags
;
722 regcache_cooked_read_unsigned (regcache
, I386_ESP_REGNUM
, &sp
);
723 if (syscall
== SYS_rt_sigreturn
)
724 addr
= read_memory_unsigned_integer (sp
+ 8, 4, byte_order
)
729 /* Set the trace flag in the context that's about to be
731 addr
+= LINUX_SIGCONTEXT_EFLAGS_OFFSET
;
732 read_memory (addr
, (gdb_byte
*) &eflags
, 4);
734 write_memory (addr
, (gdb_byte
*) &eflags
, 4);
739 if (ptrace (request
, pid
, 0, gdb_signal_to_host (signal
)) == -1)
740 perror_with_name (("ptrace"));
744 /* -Wmissing-prototypes */
745 extern initialize_file_ftype _initialize_i386_linux_nat
;
748 _initialize_i386_linux_nat (void)
750 /* Create a generic x86 GNU/Linux target. */
751 struct target_ops
*t
= x86_linux_create_target ();
753 /* Override the default ptrace resume method. */
754 t
->to_resume
= i386_linux_resume
;
756 /* Add our register access methods. */
757 t
->to_fetch_registers
= i386_linux_fetch_inferior_registers
;
758 t
->to_store_registers
= i386_linux_store_inferior_registers
;
760 /* Add the target. */
761 x86_linux_add_target (t
);