Skip to content

Commit

Permalink
Crashhandler allthreads (#6013)
Browse files Browse the repository at this point in the history
* [CrashHandler] Suspend all threads on crash
  • Loading branch information
NathanW2 authored Jan 8, 2018
1 parent 25770c4 commit 39d4d9a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/crashhandler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ int main( int argc, char *argv[] )
dlg.show();
app.exec();

ResumeThread( stackTrace->thread );
CloseHandle( stackTrace->thread );

for ( HANDLE threadHandle : stackTrace->threads )
{
ResumeThread( threadHandle );
CloseHandle( threadHandle );
}
CloseHandle( stackTrace->process );

return 0;
Expand Down
28 changes: 27 additions & 1 deletion src/crashhandler/qgsstacktrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <tlhelp32.h>

#include <Windows.h>
#include <DbgHelp.h>
Expand Down Expand Up @@ -780,10 +781,35 @@ QgsStackTrace *QgsStackTrace::trace( DWORD processId, DWORD threadId, LPEXCEPTIO
StackTrace *stackTrace = ( StackTrace * )calloc( sizeof( *stackTrace ), 1 );
stackTrace->process = OpenProcess( PROCESS_ALL_ACCESS, FALSE, processId );
stackTrace->thread = OpenThread( THREAD_ALL_ACCESS, FALSE, threadId );
SuspendThread( stackTrace->thread );
trace->process = stackTrace->process;
trace->thread = stackTrace->thread;

HANDLE h = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
if ( h != INVALID_HANDLE_VALUE )
{
THREADENTRY32 te;
te.dwSize = sizeof( te );
if ( Thread32First( h, &te ) )
{
do
{
if ( te.dwSize >= FIELD_OFFSET( THREADENTRY32, th32OwnerProcessID ) +
sizeof( te.th32OwnerProcessID ) )
{
if ( te.th32OwnerProcessID == processId )
{
HANDLE threadHandle = OpenThread( THREAD_ALL_ACCESS, FALSE, te.th32ThreadID );
trace->threads.push_back( threadHandle );
SuspendThread( threadHandle );
}
}
te.dwSize = sizeof( te );
}
while ( Thread32Next( h, &te ) );
}
CloseHandle( h );
}

ReadProcessMemory( stackTrace->process, exception, &remoteException, sizeof( remoteException ), NULL );
ReadProcessMemory( stackTrace->process, remoteException.ContextRecord, &remoteContextRecord, sizeof( remoteContextRecord ), NULL );

Expand Down
1 change: 1 addition & 0 deletions src/crashhandler/qgsstacktrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class QgsStackTrace
#ifdef _MSC_VER
HANDLE process;
HANDLE thread;
std::vector<HANDLE> threads;

/**
* Return a demangled stack backtrace of the caller function.
Expand Down

0 comments on commit 39d4d9a

Please sign in to comment.