Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arch_DebuggerIsAttachedPosix failure. #148

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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