mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-21 04:42:53 +08:00
0747795c08
This changes all includes to use the form "common/filename.h" rather than just "filename.h". This was written by a script. gdb/ChangeLog 2019-01-25 Tom Tromey <tom@tromey.com> * xtensa-linux-nat.c: Fix common/ includes. * xml-support.h: Fix common/ includes. * xml-support.c: Fix common/ includes. * x86-linux-nat.c: Fix common/ includes. * windows-nat.c: Fix common/ includes. * varobj.h: Fix common/ includes. * varobj.c: Fix common/ includes. * value.c: Fix common/ includes. * valops.c: Fix common/ includes. * utils.c: Fix common/ includes. * unittests/xml-utils-selftests.c: Fix common/ includes. * unittests/utils-selftests.c: Fix common/ includes. * unittests/unpack-selftests.c: Fix common/ includes. * unittests/tracepoint-selftests.c: Fix common/ includes. * unittests/style-selftests.c: Fix common/ includes. * unittests/string_view-selftests.c: Fix common/ includes. * unittests/scoped_restore-selftests.c: Fix common/ includes. * unittests/scoped_mmap-selftests.c: Fix common/ includes. * unittests/scoped_fd-selftests.c: Fix common/ includes. * unittests/rsp-low-selftests.c: Fix common/ includes. * unittests/parse-connection-spec-selftests.c: Fix common/ includes. * unittests/optional-selftests.c: Fix common/ includes. * unittests/offset-type-selftests.c: Fix common/ includes. * unittests/observable-selftests.c: Fix common/ includes. * unittests/mkdir-recursive-selftests.c: Fix common/ includes. * unittests/memrange-selftests.c: Fix common/ includes. * unittests/memory-map-selftests.c: Fix common/ includes. * unittests/lookup_name_info-selftests.c: Fix common/ includes. * unittests/function-view-selftests.c: Fix common/ includes. * unittests/environ-selftests.c: Fix common/ includes. * unittests/copy_bitwise-selftests.c: Fix common/ includes. * unittests/common-utils-selftests.c: Fix common/ includes. * unittests/cli-utils-selftests.c: Fix common/ includes. * unittests/array-view-selftests.c: Fix common/ includes. * ui-file.c: Fix common/ includes. * tui/tui-io.c: Fix common/ includes. * tracepoint.h: Fix common/ includes. * tracepoint.c: Fix common/ includes. * tracefile-tfile.c: Fix common/ includes. * top.h: Fix common/ includes. * top.c: Fix common/ includes. * thread.c: Fix common/ includes. * target/waitstatus.h: Fix common/ includes. * target/waitstatus.c: Fix common/ includes. * target.h: Fix common/ includes. * target.c: Fix common/ includes. * target-memory.c: Fix common/ includes. * target-descriptions.c: Fix common/ includes. * symtab.h: Fix common/ includes. * symfile.c: Fix common/ includes. * stap-probe.c: Fix common/ includes. * spu-linux-nat.c: Fix common/ includes. * sparc-nat.c: Fix common/ includes. * source.c: Fix common/ includes. * solib.c: Fix common/ includes. * solib-target.c: Fix common/ includes. * ser-unix.c: Fix common/ includes. * ser-tcp.c: Fix common/ includes. * ser-pipe.c: Fix common/ includes. * ser-base.c: Fix common/ includes. * selftest-arch.c: Fix common/ includes. * s12z-tdep.c: Fix common/ includes. * rust-exp.y: Fix common/ includes. * rs6000-aix-tdep.c: Fix common/ includes. * riscv-tdep.c: Fix common/ includes. * remote.c: Fix common/ includes. * remote-notif.h: Fix common/ includes. * remote-fileio.h: Fix common/ includes. * remote-fileio.c: Fix common/ includes. * regcache.h: Fix common/ includes. * regcache.c: Fix common/ includes. * record-btrace.c: Fix common/ includes. * python/python.c: Fix common/ includes. * python/py-type.c: Fix common/ includes. * python/py-inferior.c: Fix common/ includes. * progspace.h: Fix common/ includes. * producer.c: Fix common/ includes. * procfs.c: Fix common/ includes. * proc-api.c: Fix common/ includes. * printcmd.c: Fix common/ includes. * ppc-linux-nat.c: Fix common/ includes. * parser-defs.h: Fix common/ includes. * osdata.c: Fix common/ includes. * obsd-nat.c: Fix common/ includes. * nat/x86-linux.c: Fix common/ includes. * nat/x86-linux-dregs.c: Fix common/ includes. * nat/x86-dregs.h: Fix common/ includes. * nat/x86-dregs.c: Fix common/ includes. * nat/ppc-linux.c: Fix common/ includes. * nat/mips-linux-watch.h: Fix common/ includes. * nat/mips-linux-watch.c: Fix common/ includes. * nat/linux-waitpid.c: Fix common/ includes. * nat/linux-ptrace.h: Fix common/ includes. * nat/linux-ptrace.c: Fix common/ includes. * nat/linux-procfs.c: Fix common/ includes. * nat/linux-personality.c: Fix common/ includes. * nat/linux-osdata.c: Fix common/ includes. * nat/linux-namespaces.c: Fix common/ includes. * nat/linux-btrace.h: Fix common/ includes. * nat/linux-btrace.c: Fix common/ includes. * nat/fork-inferior.c: Fix common/ includes. * nat/amd64-linux-siginfo.c: Fix common/ includes. * nat/aarch64-sve-linux-ptrace.c: Fix common/ includes. * nat/aarch64-linux.c: Fix common/ includes. * nat/aarch64-linux-hw-point.h: Fix common/ includes. * nat/aarch64-linux-hw-point.c: Fix common/ includes. * namespace.h: Fix common/ includes. * mips-linux-tdep.c: Fix common/ includes. * minsyms.c: Fix common/ includes. * mi/mi-parse.h: Fix common/ includes. * mi/mi-main.c: Fix common/ includes. * mi/mi-cmd-env.c: Fix common/ includes. * memrange.h: Fix common/ includes. * memattr.c: Fix common/ includes. * maint.h: Fix common/ includes. * maint.c: Fix common/ includes. * main.c: Fix common/ includes. * machoread.c: Fix common/ includes. * location.c: Fix common/ includes. * linux-thread-db.c: Fix common/ includes. * linux-nat.c: Fix common/ includes. * linux-fork.c: Fix common/ includes. * inline-frame.c: Fix common/ includes. * infrun.c: Fix common/ includes. * inflow.c: Fix common/ includes. * inferior.h: Fix common/ includes. * inferior.c: Fix common/ includes. * infcmd.c: Fix common/ includes. * inf-ptrace.c: Fix common/ includes. * inf-child.c: Fix common/ includes. * ia64-linux-nat.c: Fix common/ includes. * i387-tdep.c: Fix common/ includes. * i386-tdep.c: Fix common/ includes. * i386-linux-tdep.c: Fix common/ includes. * i386-linux-nat.c: Fix common/ includes. * i386-go32-tdep.c: Fix common/ includes. * i386-fbsd-tdep.c: Fix common/ includes. * i386-fbsd-nat.c: Fix common/ includes. * guile/scm-type.c: Fix common/ includes. * guile/guile.c: Fix common/ includes. * go32-nat.c: Fix common/ includes. * gnu-nat.c: Fix common/ includes. * gdbthread.h: Fix common/ includes. * gdbarch-selftests.c: Fix common/ includes. * gdb_usleep.c: Fix common/ includes. * gdb_select.h: Fix common/ includes. * gdb_bfd.c: Fix common/ includes. * gcore.c: Fix common/ includes. * fork-child.c: Fix common/ includes. * findvar.c: Fix common/ includes. * fbsd-nat.c: Fix common/ includes. * event-top.c: Fix common/ includes. * event-loop.c: Fix common/ includes. * dwarf2read.c: Fix common/ includes. * dwarf2loc.c: Fix common/ includes. * dwarf2-frame.c: Fix common/ includes. * dwarf-index-cache.c: Fix common/ includes. * dtrace-probe.c: Fix common/ includes. * disasm-selftests.c: Fix common/ includes. * defs.h: Fix common/ includes. * csky-tdep.c: Fix common/ includes. * cp-valprint.c: Fix common/ includes. * cp-support.h: Fix common/ includes. * cp-support.c: Fix common/ includes. * corelow.c: Fix common/ includes. * completer.h: Fix common/ includes. * completer.c: Fix common/ includes. * compile/compile.c: Fix common/ includes. * compile/compile-loc2c.c: Fix common/ includes. * compile/compile-cplus-types.c: Fix common/ includes. * compile/compile-cplus-symbols.c: Fix common/ includes. * command.h: Fix common/ includes. * cli/cli-dump.c: Fix common/ includes. * cli/cli-cmds.c: Fix common/ includes. * charset.c: Fix common/ includes. * build-id.c: Fix common/ includes. * btrace.h: Fix common/ includes. * btrace.c: Fix common/ includes. * breakpoint.h: Fix common/ includes. * breakpoint.c: Fix common/ includes. * ax.h: (enum agent_op): Fix common/ includes. * ax-general.c (struct aop_map): Fix common/ includes. * ax-gdb.c: Fix common/ includes. * auxv.c: Fix common/ includes. * auto-load.c: Fix common/ includes. * arm-tdep.c: Fix common/ includes. * arch/riscv.c: Fix common/ includes. * arch/ppc-linux-common.c: Fix common/ includes. * arch/i386.c: Fix common/ includes. * arch/arm.c: Fix common/ includes. * arch/arm-linux.c: Fix common/ includes. * arch/arm-get-next-pcs.c: Fix common/ includes. * arch/amd64.c: Fix common/ includes. * arch/aarch64.c: Fix common/ includes. * arch/aarch64-insn.c: Fix common/ includes. * arch-utils.c: Fix common/ includes. * amd64-windows-tdep.c: Fix common/ includes. * amd64-tdep.c: Fix common/ includes. * amd64-sol2-tdep.c: Fix common/ includes. * amd64-obsd-tdep.c: Fix common/ includes. * amd64-nbsd-tdep.c: Fix common/ includes. * amd64-linux-tdep.c: Fix common/ includes. * amd64-linux-nat.c: Fix common/ includes. * amd64-fbsd-tdep.c: Fix common/ includes. * amd64-fbsd-nat.c: Fix common/ includes. * amd64-dicos-tdep.c: Fix common/ includes. * amd64-darwin-tdep.c: Fix common/ includes. * agent.c: Fix common/ includes. * ada-lang.h: Fix common/ includes. * ada-lang.c: Fix common/ includes. * aarch64-tdep.c: Fix common/ includes. gdb/gdbserver/ChangeLog 2019-01-25 Tom Tromey <tom@tromey.com> * win32-low.c: Fix common/ includes. * win32-i386-low.c: Fix common/ includes. * tracepoint.c: Fix common/ includes. * thread-db.c: Fix common/ includes. * target.h: Fix common/ includes. * symbol.c: Fix common/ includes. * spu-low.c: Fix common/ includes. * server.h: Fix common/ includes. * server.c: Fix common/ includes. * remote-utils.c: Fix common/ includes. * regcache.h: Fix common/ includes. * regcache.c: Fix common/ includes. * nto-x86-low.c: Fix common/ includes. * notif.h: Fix common/ includes. * mem-break.h: Fix common/ includes. * lynx-low.c: Fix common/ includes. * lynx-i386-low.c: Fix common/ includes. * linux-x86-tdesc-selftest.c: Fix common/ includes. * linux-x86-low.c: Fix common/ includes. * linux-low.c: Fix common/ includes. * inferiors.h: Fix common/ includes. * i387-fp.c: Fix common/ includes. * hostio.c: Fix common/ includes. * hostio-errno.c: Fix common/ includes. * gdbthread.h: Fix common/ includes. * gdbreplay.c: Fix common/ includes. * fork-child.c: Fix common/ includes. * event-loop.c: Fix common/ includes. * ax.c: (enum gdb_agent_op): Fix common/ includes.
617 lines
17 KiB
C
617 lines
17 KiB
C
/* Linux-specific ptrace manipulation routines.
|
|
Copyright (C) 2012-2019 Free Software Foundation, Inc.
|
|
|
|
This file is part of GDB.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
#include "common/common-defs.h"
|
|
#include "linux-ptrace.h"
|
|
#include "linux-procfs.h"
|
|
#include "linux-waitpid.h"
|
|
#include "common/buffer.h"
|
|
#ifdef HAVE_SYS_PROCFS_H
|
|
#include <sys/procfs.h>
|
|
#endif
|
|
|
|
/* Stores the ptrace options supported by the running kernel.
|
|
A value of -1 means we did not check for features yet. A value
|
|
of 0 means there are no supported features. */
|
|
static int supported_ptrace_options = -1;
|
|
|
|
/* Find all possible reasons we could fail to attach PID and return these
|
|
as a string. An empty string is returned if we didn't find any reason. */
|
|
|
|
std::string
|
|
linux_ptrace_attach_fail_reason (pid_t pid)
|
|
{
|
|
pid_t tracerpid = linux_proc_get_tracerpid_nowarn (pid);
|
|
std::string result;
|
|
|
|
if (tracerpid > 0)
|
|
string_appendf (result,
|
|
_("process %d is already traced by process %d"),
|
|
(int) pid, (int) tracerpid);
|
|
|
|
if (linux_proc_pid_is_zombie_nowarn (pid))
|
|
string_appendf (result,
|
|
_("process %d is a zombie - the process has already "
|
|
"terminated"),
|
|
(int) pid);
|
|
|
|
return result;
|
|
}
|
|
|
|
/* See linux-ptrace.h. */
|
|
|
|
std::string
|
|
linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err)
|
|
{
|
|
long lwpid = ptid.lwp ();
|
|
std::string reason = linux_ptrace_attach_fail_reason (lwpid);
|
|
|
|
if (!reason.empty ())
|
|
return string_printf ("%s (%d), %s", safe_strerror (err), err,
|
|
reason.c_str ());
|
|
else
|
|
return string_printf ("%s (%d)", safe_strerror (err), err);
|
|
}
|
|
|
|
#if defined __i386__ || defined __x86_64__
|
|
|
|
/* Address of the 'ret' instruction in asm code block below. */
|
|
EXTERN_C void linux_ptrace_test_ret_to_nx_instr (void);
|
|
|
|
#include <sys/reg.h>
|
|
#include <sys/mman.h>
|
|
#include <signal.h>
|
|
|
|
#endif /* defined __i386__ || defined __x86_64__ */
|
|
|
|
/* Kill CHILD. WHO is used to report warnings. */
|
|
|
|
static void
|
|
kill_child (pid_t child, const char *who)
|
|
{
|
|
pid_t got_pid;
|
|
int kill_status;
|
|
|
|
if (kill (child, SIGKILL) != 0)
|
|
{
|
|
warning (_("%s: failed to kill child pid %ld %s"),
|
|
who, (long) child, safe_strerror (errno));
|
|
return;
|
|
}
|
|
|
|
errno = 0;
|
|
got_pid = my_waitpid (child, &kill_status, 0);
|
|
if (got_pid != child)
|
|
{
|
|
warning (_("%s: "
|
|
"kill waitpid returned %ld: %s"),
|
|
who, (long) got_pid, safe_strerror (errno));
|
|
return;
|
|
}
|
|
if (!WIFSIGNALED (kill_status))
|
|
{
|
|
warning (_("%s: "
|
|
"kill status %d is not WIFSIGNALED!"),
|
|
who, kill_status);
|
|
return;
|
|
}
|
|
}
|
|
|
|
/* Test broken off-trunk Linux kernel patchset for NX support on i386. It was
|
|
removed in Fedora kernel 88fa1f0332d188795ed73d7ac2b1564e11a0b4cd.
|
|
|
|
Test also x86_64 arch for PaX support. */
|
|
|
|
static void
|
|
linux_ptrace_test_ret_to_nx (void)
|
|
{
|
|
#if defined __i386__ || defined __x86_64__
|
|
pid_t child, got_pid;
|
|
gdb_byte *return_address, *pc;
|
|
long l;
|
|
int status;
|
|
elf_gregset_t regs;
|
|
|
|
return_address
|
|
= (gdb_byte *) mmap (NULL, 2, PROT_READ | PROT_WRITE,
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
if (return_address == MAP_FAILED)
|
|
{
|
|
warning (_("linux_ptrace_test_ret_to_nx: Cannot mmap: %s"),
|
|
safe_strerror (errno));
|
|
return;
|
|
}
|
|
|
|
/* Put there 'int3'. */
|
|
*return_address = 0xcc;
|
|
|
|
child = fork ();
|
|
switch (child)
|
|
{
|
|
case -1:
|
|
warning (_("linux_ptrace_test_ret_to_nx: Cannot fork: %s"),
|
|
safe_strerror (errno));
|
|
return;
|
|
|
|
case 0:
|
|
l = ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) NULL,
|
|
(PTRACE_TYPE_ARG4) NULL);
|
|
if (l != 0)
|
|
warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_TRACEME: %s"),
|
|
safe_strerror (errno));
|
|
else
|
|
{
|
|
#if defined __i386__
|
|
asm volatile ("pushl %0;"
|
|
".globl linux_ptrace_test_ret_to_nx_instr;"
|
|
"linux_ptrace_test_ret_to_nx_instr:"
|
|
"ret"
|
|
: : "r" (return_address) : "memory");
|
|
#elif defined __x86_64__
|
|
asm volatile ("pushq %0;"
|
|
".globl linux_ptrace_test_ret_to_nx_instr;"
|
|
"linux_ptrace_test_ret_to_nx_instr:"
|
|
"ret"
|
|
: : "r" ((uint64_t) (uintptr_t) return_address)
|
|
: "memory");
|
|
#else
|
|
# error "!__i386__ && !__x86_64__"
|
|
#endif
|
|
gdb_assert_not_reached ("asm block did not terminate");
|
|
}
|
|
|
|
_exit (1);
|
|
}
|
|
|
|
errno = 0;
|
|
got_pid = waitpid (child, &status, 0);
|
|
if (got_pid != child)
|
|
{
|
|
warning (_("linux_ptrace_test_ret_to_nx: waitpid returned %ld: %s"),
|
|
(long) got_pid, safe_strerror (errno));
|
|
return;
|
|
}
|
|
|
|
if (WIFSIGNALED (status))
|
|
{
|
|
if (WTERMSIG (status) != SIGKILL)
|
|
warning (_("linux_ptrace_test_ret_to_nx: WTERMSIG %d is not SIGKILL!"),
|
|
(int) WTERMSIG (status));
|
|
else
|
|
warning (_("Cannot call inferior functions, Linux kernel PaX "
|
|
"protection forbids return to non-executable pages!"));
|
|
return;
|
|
}
|
|
|
|
if (!WIFSTOPPED (status))
|
|
{
|
|
warning (_("linux_ptrace_test_ret_to_nx: status %d is not WIFSTOPPED!"),
|
|
status);
|
|
kill_child (child, "linux_ptrace_test_ret_to_nx");
|
|
return;
|
|
}
|
|
|
|
/* We may get SIGSEGV due to missing PROT_EXEC of the return_address. */
|
|
if (WSTOPSIG (status) != SIGTRAP && WSTOPSIG (status) != SIGSEGV)
|
|
{
|
|
warning (_("linux_ptrace_test_ret_to_nx: "
|
|
"WSTOPSIG %d is neither SIGTRAP nor SIGSEGV!"),
|
|
(int) WSTOPSIG (status));
|
|
kill_child (child, "linux_ptrace_test_ret_to_nx");
|
|
return;
|
|
}
|
|
|
|
if (ptrace (PTRACE_GETREGS, child, (PTRACE_TYPE_ARG3) 0,
|
|
(PTRACE_TYPE_ARG4) ®s) < 0)
|
|
{
|
|
warning (_("linux_ptrace_test_ret_to_nx: Cannot PTRACE_GETREGS: %s"),
|
|
safe_strerror (errno));
|
|
}
|
|
#if defined __i386__
|
|
pc = (gdb_byte *) (uintptr_t) regs[EIP];
|
|
#elif defined __x86_64__
|
|
pc = (gdb_byte *) (uintptr_t) regs[RIP];
|
|
#else
|
|
# error "!__i386__ && !__x86_64__"
|
|
#endif
|
|
|
|
kill_child (child, "linux_ptrace_test_ret_to_nx");
|
|
|
|
/* + 1 is there as x86* stops after the 'int3' instruction. */
|
|
if (WSTOPSIG (status) == SIGTRAP && pc == return_address + 1)
|
|
{
|
|
/* PASS */
|
|
return;
|
|
}
|
|
|
|
/* We may get SIGSEGV due to missing PROT_EXEC of the RETURN_ADDRESS page. */
|
|
if (WSTOPSIG (status) == SIGSEGV && pc == return_address)
|
|
{
|
|
/* PASS */
|
|
return;
|
|
}
|
|
|
|
if ((void (*) (void)) pc != &linux_ptrace_test_ret_to_nx_instr)
|
|
warning (_("linux_ptrace_test_ret_to_nx: PC %p is neither near return "
|
|
"address %p nor is the return instruction %p!"),
|
|
pc, return_address, &linux_ptrace_test_ret_to_nx_instr);
|
|
else
|
|
warning (_("Cannot call inferior functions on this system - "
|
|
"Linux kernel with broken i386 NX (non-executable pages) "
|
|
"support detected!"));
|
|
#endif /* defined __i386__ || defined __x86_64__ */
|
|
}
|
|
|
|
/* Helper function to fork a process and make the child process call
|
|
the function FUNCTION, passing CHILD_STACK as parameter.
|
|
|
|
For MMU-less targets, clone is used instead of fork, and
|
|
CHILD_STACK is used as stack space for the cloned child. If NULL,
|
|
stack space is allocated via malloc (and subsequently passed to
|
|
FUNCTION). For MMU targets, CHILD_STACK is ignored. */
|
|
|
|
static int
|
|
linux_fork_to_function (gdb_byte *child_stack, int (*function) (void *))
|
|
{
|
|
int child_pid;
|
|
|
|
/* Sanity check the function pointer. */
|
|
gdb_assert (function != NULL);
|
|
|
|
#if defined(__UCLIBC__) && defined(HAS_NOMMU)
|
|
#define STACK_SIZE 4096
|
|
|
|
if (child_stack == NULL)
|
|
child_stack = (gdb_byte *) xmalloc (STACK_SIZE * 4);
|
|
|
|
/* Use CLONE_VM instead of fork, to support uClinux (no MMU). */
|
|
#ifdef __ia64__
|
|
child_pid = __clone2 (function, child_stack, STACK_SIZE,
|
|
CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2);
|
|
#else /* !__ia64__ */
|
|
child_pid = clone (function, child_stack + STACK_SIZE,
|
|
CLONE_VM | SIGCHLD, child_stack + STACK_SIZE * 2);
|
|
#endif /* !__ia64__ */
|
|
#else /* !defined(__UCLIBC) && defined(HAS_NOMMU) */
|
|
child_pid = fork ();
|
|
|
|
if (child_pid == 0)
|
|
function (NULL);
|
|
#endif /* defined(__UCLIBC) && defined(HAS_NOMMU) */
|
|
|
|
if (child_pid == -1)
|
|
perror_with_name (("fork"));
|
|
|
|
return child_pid;
|
|
}
|
|
|
|
/* A helper function for linux_check_ptrace_features, called after
|
|
the child forks a grandchild. */
|
|
|
|
static int
|
|
linux_grandchild_function (void *child_stack)
|
|
{
|
|
/* Free any allocated stack. */
|
|
xfree (child_stack);
|
|
|
|
/* This code is only reacheable by the grandchild (child's child)
|
|
process. */
|
|
_exit (0);
|
|
}
|
|
|
|
/* A helper function for linux_check_ptrace_features, called after
|
|
the parent process forks a child. The child allows itself to
|
|
be traced by its parent. */
|
|
|
|
static int
|
|
linux_child_function (void *child_stack)
|
|
{
|
|
ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
|
|
kill (getpid (), SIGSTOP);
|
|
|
|
/* Fork a grandchild. */
|
|
linux_fork_to_function ((gdb_byte *) child_stack, linux_grandchild_function);
|
|
|
|
/* This code is only reacheable by the child (grandchild's parent)
|
|
process. */
|
|
_exit (0);
|
|
}
|
|
|
|
static void linux_test_for_tracesysgood (int child_pid);
|
|
static void linux_test_for_tracefork (int child_pid);
|
|
static void linux_test_for_exitkill (int child_pid);
|
|
|
|
/* Determine ptrace features available on this target. */
|
|
|
|
void
|
|
linux_check_ptrace_features (void)
|
|
{
|
|
int child_pid, ret, status;
|
|
|
|
/* Initialize the options. */
|
|
supported_ptrace_options = 0;
|
|
|
|
/* Fork a child so we can do some testing. The child will call
|
|
linux_child_function and will get traced. The child will
|
|
eventually fork a grandchild so we can test fork event
|
|
reporting. */
|
|
child_pid = linux_fork_to_function (NULL, linux_child_function);
|
|
|
|
ret = my_waitpid (child_pid, &status, 0);
|
|
if (ret == -1)
|
|
perror_with_name (("waitpid"));
|
|
else if (ret != child_pid)
|
|
error (_("linux_check_ptrace_features: waitpid: unexpected result %d."),
|
|
ret);
|
|
if (! WIFSTOPPED (status))
|
|
error (_("linux_check_ptrace_features: waitpid: unexpected status %d."),
|
|
status);
|
|
|
|
linux_test_for_tracesysgood (child_pid);
|
|
|
|
linux_test_for_tracefork (child_pid);
|
|
|
|
linux_test_for_exitkill (child_pid);
|
|
|
|
/* Kill child_pid. */
|
|
kill_child (child_pid, "linux_check_ptrace_features");
|
|
}
|
|
|
|
/* Determine if PTRACE_O_TRACESYSGOOD can be used to catch
|
|
syscalls. */
|
|
|
|
static void
|
|
linux_test_for_tracesysgood (int child_pid)
|
|
{
|
|
int ret;
|
|
|
|
ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
|
|
(PTRACE_TYPE_ARG4) PTRACE_O_TRACESYSGOOD);
|
|
|
|
if (ret == 0)
|
|
supported_ptrace_options |= PTRACE_O_TRACESYSGOOD;
|
|
}
|
|
|
|
/* Determine if PTRACE_O_TRACEFORK can be used to follow fork
|
|
events. */
|
|
|
|
static void
|
|
linux_test_for_tracefork (int child_pid)
|
|
{
|
|
int ret, status;
|
|
long second_pid;
|
|
|
|
/* First, set the PTRACE_O_TRACEFORK option. If this fails, we
|
|
know for sure that it is not supported. */
|
|
ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
|
|
(PTRACE_TYPE_ARG4) PTRACE_O_TRACEFORK);
|
|
|
|
if (ret != 0)
|
|
return;
|
|
|
|
/* Check if the target supports PTRACE_O_TRACEVFORKDONE. */
|
|
ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
|
|
(PTRACE_TYPE_ARG4) (PTRACE_O_TRACEFORK
|
|
| PTRACE_O_TRACEVFORKDONE));
|
|
if (ret == 0)
|
|
supported_ptrace_options |= PTRACE_O_TRACEVFORKDONE;
|
|
|
|
/* Setting PTRACE_O_TRACEFORK did not cause an error, however we
|
|
don't know for sure that the feature is available; old
|
|
versions of PTRACE_SETOPTIONS ignored unknown options.
|
|
Therefore, we attach to the child process, use PTRACE_SETOPTIONS
|
|
to enable fork tracing, and let it fork. If the process exits,
|
|
we assume that we can't use PTRACE_O_TRACEFORK; if we get the
|
|
fork notification, and we can extract the new child's PID, then
|
|
we assume that we can.
|
|
|
|
We do not explicitly check for vfork tracing here. It is
|
|
assumed that vfork tracing is available whenever fork tracing
|
|
is available. */
|
|
ret = ptrace (PTRACE_CONT, child_pid, (PTRACE_TYPE_ARG3) 0,
|
|
(PTRACE_TYPE_ARG4) 0);
|
|
if (ret != 0)
|
|
warning (_("linux_test_for_tracefork: failed to resume child"));
|
|
|
|
ret = my_waitpid (child_pid, &status, 0);
|
|
|
|
/* Check if we received a fork event notification. */
|
|
if (ret == child_pid && WIFSTOPPED (status)
|
|
&& linux_ptrace_get_extended_event (status) == PTRACE_EVENT_FORK)
|
|
{
|
|
/* We did receive a fork event notification. Make sure its PID
|
|
is reported. */
|
|
second_pid = 0;
|
|
ret = ptrace (PTRACE_GETEVENTMSG, child_pid, (PTRACE_TYPE_ARG3) 0,
|
|
(PTRACE_TYPE_ARG4) &second_pid);
|
|
if (ret == 0 && second_pid != 0)
|
|
{
|
|
int second_status;
|
|
|
|
/* We got the PID from the grandchild, which means fork
|
|
tracing is supported. */
|
|
supported_ptrace_options |= PTRACE_O_TRACECLONE;
|
|
supported_ptrace_options |= (PTRACE_O_TRACEFORK
|
|
| PTRACE_O_TRACEVFORK
|
|
| PTRACE_O_TRACEEXEC);
|
|
|
|
/* Do some cleanup and kill the grandchild. */
|
|
my_waitpid (second_pid, &second_status, 0);
|
|
kill_child (second_pid, "linux_test_for_tracefork");
|
|
}
|
|
}
|
|
else
|
|
warning (_("linux_test_for_tracefork: unexpected result from waitpid "
|
|
"(%d, status 0x%x)"), ret, status);
|
|
}
|
|
|
|
/* Determine if PTRACE_O_EXITKILL can be used. */
|
|
|
|
static void
|
|
linux_test_for_exitkill (int child_pid)
|
|
{
|
|
int ret;
|
|
|
|
ret = ptrace (PTRACE_SETOPTIONS, child_pid, (PTRACE_TYPE_ARG3) 0,
|
|
(PTRACE_TYPE_ARG4) PTRACE_O_EXITKILL);
|
|
|
|
if (ret == 0)
|
|
supported_ptrace_options |= PTRACE_O_EXITKILL;
|
|
}
|
|
|
|
/* Enable reporting of all currently supported ptrace events.
|
|
OPTIONS is a bit mask of extended features we want enabled,
|
|
if supported by the kernel. PTRACE_O_TRACECLONE is always
|
|
enabled, if supported. */
|
|
|
|
void
|
|
linux_enable_event_reporting (pid_t pid, int options)
|
|
{
|
|
/* Check if we have initialized the ptrace features for this
|
|
target. If not, do it now. */
|
|
if (supported_ptrace_options == -1)
|
|
linux_check_ptrace_features ();
|
|
|
|
/* We always want clone events. */
|
|
options |= PTRACE_O_TRACECLONE;
|
|
|
|
/* Filter out unsupported options. */
|
|
options &= supported_ptrace_options;
|
|
|
|
/* Set the options. */
|
|
ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0,
|
|
(PTRACE_TYPE_ARG4) (uintptr_t) options);
|
|
}
|
|
|
|
/* Disable reporting of all currently supported ptrace events. */
|
|
|
|
void
|
|
linux_disable_event_reporting (pid_t pid)
|
|
{
|
|
/* Set the options. */
|
|
ptrace (PTRACE_SETOPTIONS, pid, (PTRACE_TYPE_ARG3) 0, 0);
|
|
}
|
|
|
|
/* Returns non-zero if PTRACE_OPTIONS is contained within
|
|
SUPPORTED_PTRACE_OPTIONS, therefore supported. Returns 0
|
|
otherwise. */
|
|
|
|
static int
|
|
ptrace_supports_feature (int ptrace_options)
|
|
{
|
|
if (supported_ptrace_options == -1)
|
|
linux_check_ptrace_features ();
|
|
|
|
return ((supported_ptrace_options & ptrace_options) == ptrace_options);
|
|
}
|
|
|
|
/* Returns non-zero if PTRACE_EVENT_FORK is supported by ptrace,
|
|
0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is
|
|
PTRACE_EVENT_CLONE, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
|
|
since they were all added to the kernel at the same time. */
|
|
|
|
int
|
|
linux_supports_tracefork (void)
|
|
{
|
|
return ptrace_supports_feature (PTRACE_O_TRACEFORK);
|
|
}
|
|
|
|
/* Returns non-zero if PTRACE_EVENT_EXEC is supported by ptrace,
|
|
0 otherwise. Note that if PTRACE_EVENT_FORK is supported so is
|
|
PTRACE_EVENT_CLONE, PTRACE_EVENT_FORK and PTRACE_EVENT_VFORK,
|
|
since they were all added to the kernel at the same time. */
|
|
|
|
int
|
|
linux_supports_traceexec (void)
|
|
{
|
|
return ptrace_supports_feature (PTRACE_O_TRACEEXEC);
|
|
}
|
|
|
|
/* Returns non-zero if PTRACE_EVENT_CLONE is supported by ptrace,
|
|
0 otherwise. Note that if PTRACE_EVENT_CLONE is supported so is
|
|
PTRACE_EVENT_FORK, PTRACE_EVENT_EXEC and PTRACE_EVENT_VFORK,
|
|
since they were all added to the kernel at the same time. */
|
|
|
|
int
|
|
linux_supports_traceclone (void)
|
|
{
|
|
return ptrace_supports_feature (PTRACE_O_TRACECLONE);
|
|
}
|
|
|
|
/* Returns non-zero if PTRACE_O_TRACEVFORKDONE is supported by
|
|
ptrace, 0 otherwise. */
|
|
|
|
int
|
|
linux_supports_tracevforkdone (void)
|
|
{
|
|
return ptrace_supports_feature (PTRACE_O_TRACEVFORKDONE);
|
|
}
|
|
|
|
/* Returns non-zero if PTRACE_O_TRACESYSGOOD is supported by ptrace,
|
|
0 otherwise. */
|
|
|
|
int
|
|
linux_supports_tracesysgood (void)
|
|
{
|
|
return ptrace_supports_feature (PTRACE_O_TRACESYSGOOD);
|
|
}
|
|
|
|
/* Display possible problems on this system. Display them only once per GDB
|
|
execution. */
|
|
|
|
void
|
|
linux_ptrace_init_warnings (void)
|
|
{
|
|
static int warned = 0;
|
|
|
|
if (warned)
|
|
return;
|
|
warned = 1;
|
|
|
|
linux_ptrace_test_ret_to_nx ();
|
|
}
|
|
|
|
/* Extract extended ptrace event from wait status. */
|
|
|
|
int
|
|
linux_ptrace_get_extended_event (int wstat)
|
|
{
|
|
return (wstat >> 16);
|
|
}
|
|
|
|
/* Determine whether wait status denotes an extended event. */
|
|
|
|
int
|
|
linux_is_extended_waitstatus (int wstat)
|
|
{
|
|
return (linux_ptrace_get_extended_event (wstat) != 0);
|
|
}
|
|
|
|
/* Return true if the event in LP may be caused by breakpoint. */
|
|
|
|
int
|
|
linux_wstatus_maybe_breakpoint (int wstat)
|
|
{
|
|
return (WIFSTOPPED (wstat)
|
|
&& (WSTOPSIG (wstat) == SIGTRAP
|
|
/* SIGILL and SIGSEGV are also treated as traps in case a
|
|
breakpoint is inserted at the current PC. */
|
|
|| WSTOPSIG (wstat) == SIGILL
|
|
|| WSTOPSIG (wstat) == SIGSEGV));
|
|
}
|