aboutsummaryrefslogtreecommitdiff
path: root/gdb/nat
diff options
context:
space:
mode:
authorSergio Durigan Junior <sergiodj@redhat.com>2019-09-26 14:24:30 -0400
committerSergio Durigan Junior <sergiodj@redhat.com>2019-09-26 14:24:30 -0400
commit50fa3001ce25e221ca2e54564b5589d29c4bed19 (patch)
tree4f99db1e48244cf2e3d86b867a90314dcf6d7777 /gdb/nat
parent381beca6146ac68b57edf47d28cdb335fbd11635 (diff)
downloadgdb-50fa3001ce25e221ca2e54564b5589d29c4bed19.zip
gdb-50fa3001ce25e221ca2e54564b5589d29c4bed19.tar.gz
gdb-50fa3001ce25e221ca2e54564b5589d29c4bed19.tar.bz2
Revert "Improve ptrace-error detection on Linux targets"
This reverts commit 381beca6146ac68b57edf47d28cdb335fbd11635. The patch hasn't been fully reviewed yet, and Pedro would like to see more fixes.
Diffstat (limited to 'gdb/nat')
-rw-r--r--gdb/nat/fork-inferior.c4
-rw-r--r--gdb/nat/fork-inferior.h7
-rw-r--r--gdb/nat/linux-ptrace.c192
-rw-r--r--gdb/nat/linux-ptrace.h27
4 files changed, 18 insertions, 212 deletions
diff --git a/gdb/nat/fork-inferior.c b/gdb/nat/fork-inferior.c
index 2ead4a4..355e9be 100644
--- a/gdb/nat/fork-inferior.c
+++ b/gdb/nat/fork-inferior.c
@@ -591,7 +591,7 @@ trace_start_error (const char *fmt, ...)
/* See nat/fork-inferior.h. */
void
-trace_start_error_with_name (const char *string, const char *append)
+trace_start_error_with_name (const char *string)
{
- trace_start_error ("%s: %s%s", string, safe_strerror (errno), append);
+ trace_start_error ("%s: %s", string, safe_strerror (errno));
}
diff --git a/gdb/nat/fork-inferior.h b/gdb/nat/fork-inferior.h
index 405f2cf..065496c 100644
--- a/gdb/nat/fork-inferior.h
+++ b/gdb/nat/fork-inferior.h
@@ -98,10 +98,9 @@ extern void trace_start_error (const char *fmt, ...)
ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);
/* Like "trace_start_error", but the error message is constructed by
- combining STRING with the system error message for errno, and
- (optionally) with APPEND. This function does not return. */
-extern void trace_start_error_with_name (const char *string,
- const char *append = "")
+ combining STRING with the system error message for errno. This
+ function does not return. */
+extern void trace_start_error_with_name (const char *string)
ATTRIBUTE_NORETURN;
#endif /* NAT_FORK_INFERIOR_H */
diff --git a/gdb/nat/linux-ptrace.c b/gdb/nat/linux-ptrace.c
index 8a048d2..c1ebc0a 100644
--- a/gdb/nat/linux-ptrace.c
+++ b/gdb/nat/linux-ptrace.c
@@ -21,9 +21,6 @@
#include "linux-procfs.h"
#include "linux-waitpid.h"
#include "gdbsupport/buffer.h"
-#include "gdbsupport/gdb-dlfcn.h"
-#include "nat/fork-inferior.h"
-#include "gdbsupport/filestuff.h"
#ifdef HAVE_SYS_PROCFS_H
#include <sys/procfs.h>
#endif
@@ -33,94 +30,11 @@
of 0 means there are no supported features. */
static int supported_ptrace_options = -1;
-typedef int (*selinux_ftype) (const char *);
+/* 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. */
-/* Helper function which checks if ptrace is probably restricted
- (i.e., if ERR is either EACCES or EPERM), and returns a string with
- possible workarounds. */
-
-static std::string
-linux_ptrace_restricted_fail_reason (int err)
-{
- if (err != EACCES && err != EPERM)
- {
- /* It just makes sense to perform the checks below if errno was
- either EACCES or EPERM. */
- return {};
- }
-
- std::string ret;
- gdb_dlhandle_up handle;
-
- try
- {
- handle = gdb_dlopen ("libselinux.so.1");
- }
- catch (const gdb_exception_error &e)
- {
- handle.reset (nullptr);
- }
-
- if (handle != nullptr)
- {
- selinux_ftype selinux_get_bool
- = (selinux_ftype) gdb_dlsym (handle, "security_get_boolean_active");
-
- if (selinux_get_bool != NULL
- && (*selinux_get_bool) ("deny_ptrace") == 1)
- string_appendf (ret,
- _("\n\
-The SELinux 'deny_ptrace' option is enabled and preventing GDB\n\
-from using 'ptrace'. You can disable it by executing (as root):\n\
-\n\
- setsebool deny_ptrace off\n"));
- }
-
- gdb_file_up yama_ptrace_scope
- = gdb_fopen_cloexec ("/proc/sys/kernel/yama/ptrace_scope", "r");
-
- if (yama_ptrace_scope != nullptr)
- {
- char yama_scope = fgetc (yama_ptrace_scope.get ());
-
- if (yama_scope != '0')
- string_appendf (ret,
- _("\n\
-The Linux kernel's Yama ptrace scope is in effect, which can prevent\n\
-GDB from using 'ptrace'. You can disable it by executing (as root):\n\
-\n\
- echo 0 > /proc/sys/kernel/yama/ptrace_scope\n"));
- }
-
- if (ret.empty ())
- {
- /* It wasn't possible to determine the exact reason for the
- ptrace error. Let's just emit a generic error message
- pointing the user to our documentation, where she can find
- instructions on how to try to diagnose the problem. */
- ret = _("\n\
-There might be restrictions preventing ptrace from working. Please see\n\
-the appendix \"Linux kernel ptrace restrictions\" in the GDB documentation\n\
-for more details.");
- }
-
- /* The user may be debugging remotely, so we have to warn that
- the instructions above should be performed in the target. */
- string_appendf (ret,
- _("\n\
-If you are debugging the inferior remotely, the ptrace restriction(s) must\n\
-be disabled in the target system (e.g., where GDBserver is running)."));
-
- return ret;
-}
-
-/* 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. Helper for linux_ptrace_attach_fail_reason and
- linux_ptrace_attach_fail_reason_lwp. */
-
-static std::string
-linux_ptrace_attach_fail_reason_1 (pid_t pid)
+std::string
+linux_ptrace_attach_fail_reason (pid_t pid)
{
pid_t tracerpid = linux_proc_get_tracerpid_nowarn (pid);
std::string result;
@@ -142,24 +56,10 @@ linux_ptrace_attach_fail_reason_1 (pid_t pid)
/* See linux-ptrace.h. */
std::string
-linux_ptrace_attach_fail_reason (pid_t pid, int err)
-{
- std::string result = linux_ptrace_attach_fail_reason_1 (pid);
- std::string ptrace_restrict = linux_ptrace_restricted_fail_reason (err);
-
- if (!ptrace_restrict.empty ())
- result += "\n" + ptrace_restrict;
-
- return result;
-}
-
-/* See linux-ptrace.h. */
-
-std::string
-linux_ptrace_attach_fail_reason_lwp (ptid_t ptid, int err)
+linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err)
{
long lwpid = ptid.lwp ();
- std::string reason = linux_ptrace_attach_fail_reason_1 (lwpid);
+ std::string reason = linux_ptrace_attach_fail_reason (lwpid);
if (!reason.empty ())
return string_printf ("%s (%d), %s", safe_strerror (err), err,
@@ -168,14 +68,6 @@ linux_ptrace_attach_fail_reason_lwp (ptid_t ptid, int err)
return string_printf ("%s (%d)", safe_strerror (err), err);
}
-/* See linux-ptrace.h. */
-
-std::string
-linux_ptrace_me_fail_reason (int err)
-{
- return linux_ptrace_restricted_fail_reason (err);
-}
-
#if defined __i386__ || defined __x86_64__
/* Address of the 'ret' instruction in asm code block below. */
@@ -365,12 +257,6 @@ linux_ptrace_test_ret_to_nx (void)
#endif /* defined __i386__ || defined __x86_64__ */
}
-/* If the PTRACE_TRACEME call on linux_child_function errors, we need
- to be able to send ERRNO back to the parent so that it can check
- whether there are restrictions in place preventing ptrace from
- working. We do that with a pipe. */
-static int errno_pipe[2];
-
/* Helper function to fork a process and make the child process call
the function FUNCTION, passing CHILD_STACK as parameter.
@@ -387,11 +273,6 @@ linux_fork_to_function (gdb_byte *child_stack, int (*function) (void *))
/* Sanity check the function pointer. */
gdb_assert (function != NULL);
- /* Create the pipe that will be used by the child to pass ERRNO
- after the PTRACE_TRACEME call. */
- if (pipe (errno_pipe) != 0)
- trace_start_error_with_name ("pipe");
-
#if defined(__UCLIBC__) && defined(HAS_NOMMU)
#define STACK_SIZE 4096
@@ -440,21 +321,7 @@ linux_grandchild_function (void *child_stack)
static int
linux_child_function (void *child_stack)
{
- /* Close read end. */
- close (errno_pipe[0]);
-
- int ret = ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0,
- (PTRACE_TYPE_ARG4) 0);
- int ptrace_errno = errno;
-
- /* Write ERRNO to the pipe, even if it's zero, and close the writing
- end of the pipe. */
- write (errno_pipe[1], &ptrace_errno, sizeof (ptrace_errno));
- close (errno_pipe[1]);
-
- if (ret != 0)
- _exit (0);
-
+ ptrace (PTRACE_TRACEME, 0, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
kill (getpid (), SIGSTOP);
/* Fork a grandchild. */
@@ -469,48 +336,6 @@ 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);
-/* Helper function to wait for the child to send us the ptrace ERRNO,
- and check if it's OK. */
-
-static void
-linux_check_child_ptrace_errno ()
-{
- int child_errno;
- fd_set rset;
- struct timeval timeout;
-
- /* Close the writing end of the pipe. */
- close (errno_pipe[1]);
-
- FD_ZERO (&rset);
- FD_SET (errno_pipe[0], &rset);
-
- /* One second should be plenty of time to wait for the child's
- reply. */
- timeout.tv_sec = 1;
- timeout.tv_usec = 0;
-
- int ret = select (errno_pipe[0] + 1, &rset, NULL, NULL, &timeout);
-
- if (ret < 0)
- trace_start_error_with_name ("select");
- else if (ret == 0)
- error (_("Timeout while waiting for child's ptrace errno"));
- else
- read (errno_pipe[0], &child_errno, sizeof (child_errno));
-
- if (child_errno != 0)
- {
- /* The child can't use PTRACE_TRACEME. We just bail out. */
- std::string reason = linux_ptrace_restricted_fail_reason (child_errno);
-
- errno = child_errno;
- trace_start_error_with_name ("ptrace", reason.c_str ());
- }
-
- close (errno_pipe[0]);
-}
-
/* Determine ptrace features available on this target. */
void
@@ -527,9 +352,6 @@ linux_check_ptrace_features (void)
reporting. */
child_pid = linux_fork_to_function (NULL, linux_child_function);
- /* Check if the child can successfully use ptrace. */
- linux_check_child_ptrace_errno ();
-
ret = my_waitpid (child_pid, &status, 0);
if (ret == -1)
perror_with_name (("waitpid"));
diff --git a/gdb/nat/linux-ptrace.h b/gdb/nat/linux-ptrace.h
index 90afb60..fd2f12a 100644
--- a/gdb/nat/linux-ptrace.h
+++ b/gdb/nat/linux-ptrace.h
@@ -176,27 +176,12 @@ struct buffer;
# define TRAP_HWBKPT 4
#endif
-/* 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. If ERR is EACCES or EPERM, we also add a warning about
- possible restrictions to use ptrace. */
-extern std::string linux_ptrace_attach_fail_reason (pid_t pid, int err);
-
-/* Find all possible reasons we could have failed to attach to PID's
- LWPID and return them as a string. ERR is the error PTRACE_ATTACH
- failed with (an errno). Unlike linux_ptrace_attach_fail_reason,
- this function should be used when attaching to an LWP other than
- the leader; it does not warn about ptrace restrictions. */
-extern std::string linux_ptrace_attach_fail_reason_lwp (ptid_t pid, int err);
-
-/* When the call to 'ptrace (PTRACE_TRACEME...' fails, and we have
- already forked, this function can be called in order to try to
- obtain the reason why ptrace failed. ERR should be the ERRNO value
- returned by ptrace.
-
- This function will return a 'std::string' containing the fail
- reason, or an empty string otherwise. */
-extern std::string linux_ptrace_me_fail_reason (int err);
+extern std::string linux_ptrace_attach_fail_reason (pid_t pid);
+
+/* Find all possible reasons we could have failed to attach to PTID
+ and return them as a string. ERR is the error PTRACE_ATTACH failed
+ with (an errno). */
+extern std::string linux_ptrace_attach_fail_reason_string (ptid_t ptid, int err);
extern void linux_ptrace_init_warnings (void);
extern void linux_check_ptrace_features (void);