Skip to content

Commit

Permalink
[Arch] Fix Arch_DebuggerIsAttachedPosix for modern Linux kernels with…
Browse files Browse the repository at this point in the history
… YAMA.
  • Loading branch information
marsupial committed Jun 10, 2017
1 parent b4e6a61 commit 3419a4f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pxr/base/lib/arch/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
#endif
#if defined(ARCH_OS_DARWIN)
#include <sys/sysctl.h>
#endif
#if defined(ARCH_OS_WINDOWS)
#elif defined(ARCH_OS_LINUX)
#include <sys/prctl.h>
#include <semaphore.h>
#elif defined(ARCH_OS_WINDOWS)
#include <Windows.h>
#endif
#include <atomic>
Expand Down Expand Up @@ -347,6 +349,7 @@ bool
Arch_DebuggerIsAttachedPosix()
{
// Check for a ptrace based debugger by trying to ptrace.
sem_t *yama = sem_open("usd_ptrace_enable", O_CREAT|O_EXCL);
pid_t parent = getpid();
pid_t pid = nonLockingFork();
if (pid < 0) {
Expand All @@ -356,6 +359,8 @@ Arch_DebuggerIsAttachedPosix()

// Child process.
if (pid == 0) {
// Wait for the parent to allow attaching
sem_wait(yama);
// Attach to the parent with ptrace() this will fail if the
// parent is already being traced.
if (ptrace(PTRACE_ATTACH, parent, NULL, NULL) == -1) {
Expand All @@ -374,13 +379,22 @@ Arch_DebuggerIsAttachedPosix()

// A debugger was not attached.
_exit(0);
} else {
// Allow forked child to ptrace the parent process on kernels with YAMA
prctl(PR_SET_PTRACER, pid, 0, 0, 0);
// Tell the child to continue.
sem_post(yama);
}

// Parent process
int status;
while (waitpid(pid, &status, 0) == -1 && errno == EINTR) {
// Do nothing
}

// Disable ptracing from child
prctl(PR_SET_PTRACER, 0, 0, 0, 0);

if (WIFEXITED(status)) {
return WEXITSTATUS(status) != 0;
}
Expand Down

0 comments on commit 3419a4f

Please sign in to comment.