Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QgsCrashHandler for single place for all platforms (#4395)
Add QgsCrashHandler for single place for all platforms
- Loading branch information
Showing
8 changed files
with
227 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/*************************************************************************** | ||
qgscrashhandler.cpp - QgsCrashHandler | ||
--------------------- | ||
begin : 23.4.2017 | ||
copyright : (C) 2017 by Nathan Woodrow | ||
email : woodrow.nathan@gmail.com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
#include "qgscrashhandler.h" | ||
|
||
#include <QProcess> | ||
#include <QDir> | ||
|
||
#include "qgsproject.h" | ||
#include "qgscrashdialog.h" | ||
#include "qgscrashreport.h" | ||
|
||
#ifdef Q_OS_WIN | ||
LONG WINAPI QgsCrashHandler::handle( struct _EXCEPTION_POINTERS *ExceptionInfo ) | ||
{ | ||
HANDLE process = GetCurrentProcess(); | ||
// TOOD Pull symbols from symbol server. | ||
// TOOD Move this logic to generic stack trace class to handle each | ||
// platform. | ||
SymInitialize( process, NULL, TRUE ); | ||
|
||
// StackWalk64() may modify context record passed to it, so we will | ||
// use a copy. | ||
CONTEXT context_record = *ExceptionInfo->ContextRecord; | ||
// Initialize stack walking. | ||
STACKFRAME64 stack_frame; | ||
memset( &stack_frame, 0, sizeof( stack_frame ) ); | ||
#if defined(_WIN64) | ||
int machine_type = IMAGE_FILE_MACHINE_AMD64; | ||
stack_frame.AddrPC.Offset = context_record.Rip; | ||
stack_frame.AddrFrame.Offset = context_record.Rbp; | ||
stack_frame.AddrStack.Offset = context_record.Rsp; | ||
#else | ||
int machine_type = IMAGE_FILE_MACHINE_I386; | ||
stack_frame.AddrPC.Offset = context_record.Eip; | ||
stack_frame.AddrFrame.Offset = context_record.Ebp; | ||
stack_frame.AddrStack.Offset = context_record.Esp; | ||
#endif | ||
stack_frame.AddrPC.Mode = AddrModeFlat; | ||
stack_frame.AddrFrame.Mode = AddrModeFlat; | ||
stack_frame.AddrStack.Mode = AddrModeFlat; | ||
|
||
SYMBOL_INFO *symbol = ( SYMBOL_INFO * ) qgsMalloc( sizeof( SYMBOL_INFO ) + MAX_SYM_NAME ); | ||
symbol->SizeOfStruct = sizeof( SYMBOL_INFO ); | ||
symbol->MaxNameLen = MAX_SYM_NAME; | ||
|
||
IMAGEHLP_LINE *line = ( IMAGEHLP_LINE * ) qgsMalloc( sizeof( IMAGEHLP_LINE ) ); | ||
line->SizeOfStruct = sizeof( IMAGEHLP_LINE ); | ||
|
||
IMAGEHLP_MODULE *module = ( IMAGEHLP_MODULE * ) qgsMalloc( sizeof( IMAGEHLP_MODULE ) ); | ||
module->SizeOfStruct = sizeof( IMAGEHLP_MODULE ); | ||
|
||
QList<QgsCrashReport::StackLine> stack; | ||
while ( StackWalk64( machine_type, | ||
GetCurrentProcess(), | ||
GetCurrentThread(), | ||
&stack_frame, | ||
&context_record, | ||
NULL, | ||
&SymFunctionTableAccess64, | ||
&SymGetModuleBase64, | ||
NULL ) ) | ||
{ | ||
|
||
DWORD64 displacement = 0; | ||
|
||
if ( SymFromAddr( process, ( DWORD64 )stack_frame.AddrPC.Offset, &displacement, symbol ) ) | ||
{ | ||
DWORD dwDisplacement; | ||
QString fileName; | ||
QString lineNumber; | ||
QString moduleName; | ||
if ( SymGetLineFromAddr( process, ( DWORD )( stack_frame.AddrPC.Offset ), &dwDisplacement, line ) ) | ||
{ | ||
fileName = QString( line->FileName ); | ||
lineNumber = QString::number( line->LineNumber ); | ||
} | ||
else | ||
{ | ||
fileName = "(unknown file)"; | ||
lineNumber = "(unknown line)"; | ||
} | ||
if ( SymGetModuleInfo( process, ( DWORD )( stack_frame.AddrPC.Offset ), module ) ) | ||
{ | ||
moduleName = QString( module->ModuleName ); | ||
} | ||
else | ||
{ | ||
moduleName = "(unknown module)"; | ||
} | ||
QgsCrashReport::StackLine stackline; | ||
stackline.moduleName = moduleName; | ||
stackline.fileName = fileName; | ||
stackline.lineNumber = lineNumber; | ||
stackline.symbolName = QString( symbol->Name ); | ||
stack.append( stackline ); | ||
} | ||
} | ||
|
||
qgsFree( symbol ); | ||
qgsFree( line ); | ||
qgsFree( module ); | ||
|
||
showCrashDialog( stack ); | ||
|
||
return EXCEPTION_EXECUTE_HANDLER; | ||
} | ||
|
||
void QgsCrashHandler::showCrashDialog( const QList<QgsCrashReport::StackLine> &stack ) | ||
{ | ||
|
||
QgsCrashDialog dlg( QApplication::activeWindow() ); | ||
QgsCrashReport report; | ||
report.setStackTrace( stack ); | ||
dlg.setBugReport( report.toString() ); | ||
if ( dlg.exec() ) | ||
{ | ||
restartApplication(); | ||
} | ||
} | ||
|
||
void QgsCrashHandler::restartApplication() | ||
{ | ||
QStringList arguments; | ||
arguments = QCoreApplication::arguments(); | ||
QString path = arguments.at( 0 ); | ||
arguments.removeFirst(); | ||
arguments << QgsProject::instance()->fileName(); | ||
QProcess::startDetached( path, arguments, QDir::toNativeSeparators( QCoreApplication::applicationDirPath() ) ); | ||
|
||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/*************************************************************************** | ||
qgscrashhandler.h - QgsCrashHandler | ||
--------------------- | ||
begin : 23.4.2017 | ||
copyright : (C) 2017 by Nathan Woodrow | ||
email : woodrow.nathan@gmail.com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
#ifndef QGSCRASHHANDLER_H | ||
#define QGSCRASHHANDLER_H | ||
|
||
#include "qgis.h" | ||
#include "qgis_app.h" | ||
#include "qgscrashreport.h" | ||
|
||
#ifdef WIN32 | ||
#include <windows.h> | ||
#include <dbghelp.h> | ||
#endif | ||
|
||
/** | ||
* Utility object to handle crashes in QGIS. | ||
*/ | ||
class APP_EXPORT QgsCrashHandler | ||
{ | ||
|
||
public: | ||
#ifdef Q_OS_WIN | ||
static LONG WINAPI handle( struct _EXCEPTION_POINTERS *ExceptionInfo ); | ||
#endif | ||
|
||
/** | ||
* Show the crash dialog. | ||
* @param stack The current stack of the crash point. | ||
*/ | ||
static void showCrashDialog( const QList<QgsCrashReport::StackLine> &stack ); | ||
|
||
/** | ||
* Restart the application. | ||
* Restores project and arguments used when application was loaded. | ||
*/ | ||
static void restartApplication(); | ||
|
||
private: | ||
|
||
/** | ||
* This class doesn't need to be created by anyone as is only used to handle | ||
* crashes in the application. | ||
*/ | ||
QgsCrashHandler() {} | ||
}; | ||
|
||
|
||
#endif // QGSCRASHHANDLER_H |
Oops, something went wrong.