From 5ead4a6530a5812ced36fd1ddb30bf6adc795448 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Fri, 19 Mar 2004 20:39:15 +0000 Subject: [PATCH] GetThreadBacktraceContext leaves the given thread suspended --- stepmania/src/archutils/Unix/Backtrace.h | 2 +- .../src/archutils/Unix/LinuxThreadHelpers.cpp | 34 ++++++------------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/stepmania/src/archutils/Unix/Backtrace.h b/stepmania/src/archutils/Unix/Backtrace.h index 0b70bbd6c5..86c724e357 100644 --- a/stepmania/src/archutils/Unix/Backtrace.h +++ b/stepmania/src/archutils/Unix/Backtrace.h @@ -28,7 +28,7 @@ void InitializeBacktrace(); void GetBacktrace( const void **buf, size_t size, const BacktraceContext *ctx = NULL ); /* Set up a BacktraceContext to get a backtrace for a thread. ThreadID may - * not be the current thread. */ + * not be the current thread. True is returned on success, false on failure. */ bool GetThreadBacktraceContext( int ThreadID, BacktraceContext *ctx ); /* Set up a BacktraceContext to get a backtrace after receiving a signal, given diff --git a/stepmania/src/archutils/Unix/LinuxThreadHelpers.cpp b/stepmania/src/archutils/Unix/LinuxThreadHelpers.cpp index 00a04f8ef6..4a874c044f 100644 --- a/stepmania/src/archutils/Unix/LinuxThreadHelpers.cpp +++ b/stepmania/src/archutils/Unix/LinuxThreadHelpers.cpp @@ -151,46 +151,34 @@ int ResumeThread( int ThreadID ) * * tid() is a PID (from getpid) or a TID (from gettid). Note that this may have kernel compatibility * problems, because NPTL is new and its interactions with ptrace() aren't well-defined. - * If we're on a non-NPTL system, tid is a regular PID. */ + * If we're on a non-NPTL system, tid is a regular PID. + * + * This call leaves the given thread suspended, so the returned context doesn't become invalid. + * ResumeThread() can be used to resume a thread after this call. */ bool GetThreadBacktraceContext( int ThreadID, BacktraceContext *ctx ) { /* Can't GetThreadBacktraceContext the current thread. */ ASSERT( ThreadID != GetCurrentThreadId() ); - /* Attach to the thread. */ - int ret = PtraceAttach( ThreadID ); - - /* This may fail with EPERM. This can happen for at least two common - * reasons: the process might be in a debugger already, or *we* might + /* Attach to the thread. This may fail with EPERM. This can happen for at least + * two common reasons: the process might be in a debugger already, or *we* might * already have attached to it via SuspendThread. * * If it's in a debugger, we won't be able to ptrace(PTRACE_GETREGS). If - * it's us that attached, we will. Be careful: if SuspendThread was - * called to stop the thread (causing this attach to fail), we must not - * restart the thread when we're finished. */ - bool bAttachFailed = (ret == -1); - if( bAttachFailed ) + * it's us that attached, we will. */ + if( PtraceAttach( ThreadID ) == -1 ) { RAGE_ASSERT_M( errno == EPERM, ssprintf( "%s", strerror(errno) ) ); } user_regs_struct regs; - ret = ptrace( PTRACE_GETREGS, ThreadID, NULL, ®s ); - if( ret != 0 ) - ret = -1; - ASSERT( ret == 0 ); - - if( !bAttachFailed ) - { - /* We attached to it, so we need to detach. */ - ret = ptrace( PTRACE_DETACH, ThreadID, NULL, NULL ); - ASSERT( ret == 0 ); - } + if( ptrace( PTRACE_GETREGS, ThreadID, NULL, ®s ) == -1 ) + return false; ctx->pid = ThreadID; ctx->eip = regs.eip; ctx->esp = regs.esp; ctx->ebp = regs.ebp; - return ret; + return true; }