Merge pull request #855 from wolfman2000/wolf-linux-backtrace

Backtrace stuff ahoy!
This commit is contained in:
Jason Felds
2015-10-08 20:10:29 -04:00
6 changed files with 84 additions and 168 deletions
+24
View File
@@ -0,0 +1,24 @@
# Stick to traditional approaches here. The following are defined.
# DL_FOUND - The system has the dl library.
# DL_INCLUDE_DIR - The dl include directory.
# DL_LIBRARIES - The library file to link to.
if (DL_INCLUDE_DIR AND DL_LIBRARIES)
# Already in cache, so don't repeat the finding procedures.
set(DL_FIND_QUIETLY TRUE)
endif()
find_path(DL_INCLUDE_DIR dlfcn.h
PATHS /usr/local/include /usr/include
)
find_library(DL_LIBRARIES dl
PATHS /usr/local/lib /usr/lib /lib
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(DL DEFAULT_MSG DL_LIBRARIES DL_INCLUDE_DIR)
mark_as_advanced(DL_INCLUDE_DIR DL_LIBRARIES)
+1 -6
View File
@@ -342,12 +342,7 @@ elseif(LINUX)
message(FATAL_ERROR "jpeg support required.") message(FATAL_ERROR "jpeg support required.")
endif() endif()
find_library(DL_LIBRARY dl) find_package(Dl)
if(${LIBDL_FOUND})
set(HAS_LIBDL TRUE)
else()
set(HAS_LIBDL FALSE)
endif()
find_package(Xrandr) find_package(Xrandr)
if (${XRANDR_FOUND}) if (${XRANDR_FOUND})
+11 -3
View File
@@ -280,6 +280,11 @@ else() # Linux
sm_add_compile_definition("${SM_EXE_NAME}" BACKTRACE_METHOD_X86_LINUX) sm_add_compile_definition("${SM_EXE_NAME}" BACKTRACE_METHOD_X86_LINUX)
sm_add_compile_definition("${SM_EXE_NAME}" BACKTRACE_METHOD_TEXT="x86 custom backtrace") sm_add_compile_definition("${SM_EXE_NAME}" BACKTRACE_METHOD_TEXT="x86 custom backtrace")
sm_add_compile_definition("${SM_EXE_NAME}" BACKTRACE_LOOKUP_METHOD_TEXT="backtrace_symbols") sm_add_compile_definition("${SM_EXE_NAME}" BACKTRACE_LOOKUP_METHOD_TEXT="backtrace_symbols")
if (${DL_FOUND})
sm_add_compile_definition("${SM_EXE_NAME}" BACKTRACE_LOOKUP_METHOD_DLADDR)
else()
sm_add_compile_definition("${SM_EXE_NAME}" BACKTRACE_LOOKUP_METHOD_BACKTRACE_SYMBOLS)
endif()
endif() endif()
endif() endif()
if (${HAS_PTHREAD}) if (${HAS_PTHREAD})
@@ -440,9 +445,9 @@ else() # Unix / Linux
endif() endif()
endif() endif()
list(APPEND SMDATA_LINK_LIB if (${DL_FOUND})
"${DL_LIBRARY}" list(APPEND SMDATA_LINK_LIB ${DL_LIBRARIES})
) endif()
if(HAS_OGG) if(HAS_OGG)
list(APPEND SMDATA_LINK_LIB list(APPEND SMDATA_LINK_LIB
@@ -557,6 +562,9 @@ if(NOT APPLE)
if (PCRE_FOUND) if (PCRE_FOUND)
list(APPEND SM_INCLUDE_DIRS "${PCRE_INCLUDE_DIR}") list(APPEND SM_INCLUDE_DIRS "${PCRE_INCLUDE_DIR}")
endif() endif()
if (DL_FOUND)
list(APPEND SM_INCLUDE_DIRS "${DL_INCLUDE_DIR}")
endif()
endif() endif()
else() else()
if(WITH_OGG) if(WITH_OGG)
+4 -115
View File
@@ -97,7 +97,7 @@ RString BacktraceNames::Format() const
#if defined(BACKTRACE_LOOKUP_METHOD_DLADDR) #if defined(BACKTRACE_LOOKUP_METHOD_DLADDR)
/* This version simply asks libdl, which is more robust. */ /* This version simply asks libdl, which is more robust. */
#include <dlfcn.h> #include <dlfcn.h>
void BacktraceNames::FromAddr( const void *p ) void BacktraceNames::FromAddr( void * const p )
{ {
Address = (intptr_t) p; Address = (intptr_t) p;
@@ -222,7 +222,7 @@ static const char *osx_find_link_edit( const struct mach_header *header )
return NULL; return NULL;
} }
void BacktraceNames::FromAddr( const void *p ) void BacktraceNames::FromAddr( void * const p )
{ {
Address = (intptr_t) p; Address = (intptr_t) p;
@@ -294,7 +294,7 @@ void BacktraceNames::FromAddr( const void *p )
#elif defined(BACKTRACE_LOOKUP_METHOD_BACKTRACE_SYMBOLS) #elif defined(BACKTRACE_LOOKUP_METHOD_BACKTRACE_SYMBOLS)
/* This version parses backtrace_symbols(), an doesn't need libdl. */ /* This version parses backtrace_symbols(), an doesn't need libdl. */
#include <execinfo.h> #include <execinfo.h>
void BacktraceNames::FromAddr( const void *p ) void BacktraceNames::FromAddr( void * const p )
{ {
Address = (intptr_t) p; Address = (intptr_t) p;
@@ -340,120 +340,9 @@ void BacktraceNames::FromString( RString s )
} }
} }
} }
#elif defined(BACKTRACE_LOOKUP_METHOD_ATOS)
void BacktraceNames::FromAddr( const void *p )
{
int fds[2];
pid_t pid;
pid_t ppid = getpid(); /* Do this before fork()ing! */
Offset = 0;
Address = intptr_t(p);
if (pipe(fds) != 0)
{
fprintf(stderr, "FromAddr pipe() failed: %s\n", strerror(errno));
return;
}
pid = fork();
if (pid == -1)
{
fprintf(stderr, "FromAddr fork() failed: %s\n", strerror(errno));
return;
}
if (pid == 0)
{
close(fds[0]);
for (int fd = 3; fd < 1024; ++fd)
if (fd != fds[1])
close(fd);
dup2(fds[1], fileno(stdout));
close(fds[1]);
char *addy;
asprintf(&addy, "0x%x", long(p));
char *p;
asprintf(&p, "%d", ppid);
execl("/usr/bin/atos", "/usr/bin/atos", "-p", p, addy, NULL);
fprintf(stderr, "execl(atos) failed: %s\n", strerror(errno));
free(addy);
free(p);
_exit(1);
}
close(fds[1]);
char f[1024];
bzero(f, 1024);
int len = read(fds[0], f, 1024);
Symbol = "";
File = "";
if (len == -1)
{
fprintf(stderr, "FromAddr read() failed: %s\n", strerror(errno));
return;
}
vector<RString> mangledAndFile;
split(f, " ", mangledAndFile, true);
if (mangledAndFile.size() == 0)
return;
Symbol = mangledAndFile[0];
/* eg
* -[NSApplication run]
* +[SomeClass initialize]
*/
if (Symbol[0] == '-' || Symbol[0] == '+')
{
Symbol = mangledAndFile[0] + " " + mangledAndFile[1];
/* eg
* (crt.c:300)
* (AppKit)
*/
if (mangledAndFile.size() == 3)
{
File = mangledAndFile[2];
size_t pos = File.find('(');
size_t start = (pos == File.npos ? 0 : pos+1);
pos = File.rfind(')') - 1;
File = File.substr(start, pos);
}
return;
}
/* eg
* __start -> _start
* _SDL_main -> SDL_main
*/
if (Symbol[0] == '_')
Symbol = Symbol.substr(1);
/* eg, the full line:
* __Z1Ci (in a.out) (asmtest.cc:33)
* _main (in a.out) (asmtest.cc:52)
*/
if (mangledAndFile.size() > 3)
{
File = mangledAndFile[3];
size_t pos = File.find('(');
size_t start = (pos == File.npos ? 0 : pos+1);
pos = File.rfind(')') - 1;
File = File.substr(start, pos);
}
/* eg, the full line:
* _main (SDLMain.m:308)
* __Z8GameLoopv (crt.c:300)
*/
else if (mangledAndFile.size() == 3)
File = mangledAndFile[2].substr(0, mangledAndFile[2].rfind(')'));
}
#else #else
#warning Undefined BACKTRACE_LOOKUP_METHOD_* #warning Undefined BACKTRACE_LOOKUP_METHOD_*
void BacktraceNames::FromAddr( const void *p ) void BacktraceNames::FromAddr( void * const p )
{ {
Address = intptr_t(p); Address = intptr_t(p);
Offset = 0; Offset = 0;
+1 -1
View File
@@ -6,7 +6,7 @@ struct BacktraceNames
RString Symbol, File; RString Symbol, File;
intptr_t Address; intptr_t Address;
int Offset; int Offset;
void FromAddr( const void *p ); void FromAddr( void * const p );
void FromString( RString str ); void FromString( RString str );
void Demangle(); void Demangle();
RString Format() const; RString Format() const;
+1 -1
View File
@@ -46,7 +46,7 @@ static void output_stack_trace( FILE *out, const void **BacktracePointers )
for( int i = 0; BacktracePointers[i]; ++i) for( int i = 0; BacktracePointers[i]; ++i)
{ {
BacktraceNames bn; BacktraceNames bn;
bn.FromAddr( BacktracePointers[i] ); bn.FromAddr( const_cast<void *>(BacktracePointers[i]) );
bn.Demangle(); bn.Demangle();
/* Don't show the main module name. */ /* Don't show the main module name. */