Skip to content

Commit 4e4bf8e

Browse files
committed
allow debug messages with backtrace on linux
1 parent cf22bcf commit 4e4bf8e

1 file changed

Lines changed: 53 additions & 43 deletions

File tree

‎src/app/main.cpp‎

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,37 @@ void qgisCrash( int signal )
199199
{
200200
qFatal( "QGIS died on signal %d", signal );
201201
}
202+
203+
void dumpBacktrace()
204+
{
205+
if ( access( "/usr/bin/c++filt", X_OK ) )
206+
{
207+
( void ) write( STDERR_FILENO, "Stacktrace (c++filt NOT FOUND):\n", 32 );
208+
}
209+
else
210+
{
211+
int fd[2];
212+
213+
if ( pipe( fd ) == 0 && fork() == 0 )
214+
{
215+
close( STDIN_FILENO );
216+
close( fd[1] );
217+
dup( fd[0] );
218+
execl( "/usr/bin/c++filt", "c++filt", ( char * ) 0 );
219+
exit( 1 );
220+
}
221+
222+
( void ) write( STDERR_FILENO, "Stacktrace (piped through c++filt):\n", 36 );
223+
224+
close( STDERR_FILENO );
225+
close( fd[0] );
226+
dup( fd[1] );
227+
}
228+
229+
void *buffer[256];
230+
int nptrs = backtrace( buffer, sizeof( buffer ) / sizeof( *buffer ) );
231+
backtrace_symbols_fd( buffer, nptrs, STDERR_FILENO );
232+
}
202233
#endif
203234

204235
/*
@@ -218,6 +249,10 @@ void myMessageOutput( QtMsgType type, const char *msg )
218249
{
219250
case QtDebugMsg:
220251
fprintf( stderr, "Debug: %s\n", msg );
252+
#if (defined(linux) && !defined(ANDROID)) || defined(__FreeBSD__)
253+
if ( strncmp( msg, "Backtrace", 9 ) == 0 )
254+
dumpBacktrace();
255+
#endif
221256
break;
222257
case QtCriticalMsg:
223258
fprintf( stderr, "Critical: %s\n", msg );
@@ -228,14 +263,12 @@ void myMessageOutput( QtMsgType type, const char *msg )
228263
#ifdef QGISDEBUG
229264
if ( 0 == strncmp( msg, "Object::", 8 )
230265
|| 0 == strncmp( msg, "QWidget::", 9 )
231-
|| 0 == strncmp( msg, "QPainter::", 10 ) )
266+
|| 0 == strncmp( msg, "QPainter::", 10 )
267+
)
232268
{
233269
#if 0
234270
#if (defined(linux) && !defined(ANDROID)) || defined(__FreeBSD__)
235-
fprintf( stderr, "Stacktrace (run through c++filt):\n" );
236-
void *buffer[256];
237-
int nptrs = backtrace( buffer, sizeof( buffer ) / sizeof( *buffer ) );
238-
backtrace_symbols_fd( buffer, nptrs, STDERR_FILENO );
271+
dumpBacktrace();
239272
#endif
240273
#endif
241274
QgsMessageLog::logMessage( msg, "Qt" );
@@ -254,33 +287,7 @@ void myMessageOutput( QtMsgType type, const char *msg )
254287
{
255288
fprintf( stderr, "Fatal: %s\n", msg );
256289
#if (defined(linux) && !defined(ANDROID)) || defined(__FreeBSD__)
257-
if ( access( "/usr/bin/c++filt", X_OK ) )
258-
{
259-
( void ) write( STDERR_FILENO, "Stacktrace (c++filt NOT FOUND):\n", 32 );
260-
}
261-
else
262-
{
263-
int fd[2];
264-
265-
if ( pipe( fd ) == 0 && fork() == 0 )
266-
{
267-
close( STDIN_FILENO );
268-
close( fd[1] );
269-
dup( fd[0] );
270-
execl( "/usr/bin/c++filt", "c++filt", ( char * ) 0 );
271-
exit( 1 );
272-
}
273-
274-
( void ) write( STDERR_FILENO, "Stacktrace (piped through c++filt):\n", 36 );
275-
276-
close( STDERR_FILENO );
277-
close( fd[0] );
278-
dup( fd[1] );
279-
}
280-
281-
void *buffer[256];
282-
int nptrs = backtrace( buffer, sizeof( buffer ) / sizeof( *buffer ) );
283-
backtrace_symbols_fd( buffer, nptrs, STDERR_FILENO );
290+
dumpBacktrace();
284291
#endif
285292
abort(); // deliberately core dump
286293
}
@@ -852,17 +859,20 @@ int main( int argc, char *argv[] )
852859
QCoreApplication::addLibraryPath( QTPLUGINSDIR );
853860
}
854861
//next two lines should not be needed, testing only
855-
//QCoreApplication::addLibraryPath( myPath + "/imageformats" );
856-
//QCoreApplication::addLibraryPath( myPath + "/sqldrivers" );
857-
//foreach (myPath, myApp.libraryPaths())
858-
//{
859-
//qDebug("Path:" + myPath.toLocal8Bit());
860-
//}
861-
//qDebug( "Added %s to plugin search path", qPrintable( myPath ) );
862-
//QList<QByteArray> myFormats = QImageReader::supportedImageFormats();
863-
//for ( int x = 0; x < myFormats.count(); ++x ) {
864-
// qDebug("Format: " + myFormats[x]);
865-
//}
862+
#if 0
863+
QCoreApplication::addLibraryPath( myPath + "/imageformats" );
864+
QCoreApplication::addLibraryPath( myPath + "/sqldrivers" );
865+
foreach ( myPath, myApp.libraryPaths() )
866+
{
867+
qDebug( "Path:" + myPath.toLocal8Bit() );
868+
}
869+
qDebug( "Added %s to plugin search path", qPrintable( myPath ) );
870+
QList<QByteArray> myFormats = QImageReader::supportedImageFormats();
871+
for ( int x = 0; x < myFormats.count(); ++x )
872+
{
873+
qDebug( "Format: " + myFormats[x] );
874+
}
875+
#endif
866876
}
867877
#endif
868878

0 commit comments

Comments
 (0)