Browse Source

better stacktraces

master
churchianity 1 year ago
parent
commit
96a7636db7
  1. 42
      print.cpp

42
print.cpp

@ -119,11 +119,30 @@ void trace(String* string) {
// the names as provided by 'backtrace_symbols' are mangled for some reason. // the names as provided by 'backtrace_symbols' are mangled for some reason.
// we have to demangle them, using this weird api // we have to demangle them, using this weird api
// example mangled name (wrapped in double quotes):
// example mangled names (wrapped in double quotes):
// "2 shard_tracy 0x00000001032d5618 _ZL17drawSettingsPanelv + 904" // "2 shard_tracy 0x00000001032d5618 _ZL17drawSettingsPanelv + 904"
// "2 shard 0x000000010ed8be34 _ZN6ShaderC2EPKcS1_P5ArrayIS1_ES1_ + 1108"
// "12 shard 0x000000010ed5edeb main + 43"
//
// the rule for finding the 'first' character is annoying, because it's usually but not always starting with an underscore,
// and when it is an underscore it's usually but not always the first underscore in the string.
char buffer[1024]; char buffer[1024];
const char* mangledNameBegin = String::lastCharOccurence(traces[i], '_');
const char* mangledNameEnd = String::lastCharOccurence(traces[i], '+'); // it actually ends one char before. const char* mangledNameEnd = String::lastCharOccurence(traces[i], '+'); // it actually ends one char before.
const char* mangledNameBegin = null;
if (mangledNameEnd != null && ((mangledNameEnd - traces[i]) > 2)) {
const char* cursor = mangledNameEnd - 2;
while (cursor != traces[i]) {
if (*cursor == '_') {
mangledNameBegin = cursor;
} else if (*cursor == ' ') {
mangledNameBegin = cursor + 1;
break;
}
cursor--;
}
}
if (mangledNameBegin == null || mangledNameEnd == null) { if (mangledNameBegin == null || mangledNameEnd == null) {
// we can't demangle this name for some reason, just copy the mangled name to the buffer // we can't demangle this name for some reason, just copy the mangled name to the buffer
@ -132,7 +151,7 @@ void trace(String* string) {
buffer[length] = '\0'; buffer[length] = '\0';
} else { } else {
size_t length = mangledNameEnd - 1 - mangledNameBegin;
size_t length = mangledNameEnd - mangledNameBegin - 1;
String::memcpy(buffer, (void*)mangledNameBegin, length); String::memcpy(buffer, (void*)mangledNameBegin, length);
buffer[length] = '\0'; buffer[length] = '\0';
} }
@ -140,9 +159,20 @@ void trace(String* string) {
s32 status = -1; s32 status = -1;
char* trace = abi::__cxa_demangle(buffer, null, null, &status); char* trace = abi::__cxa_demangle(buffer, null, null, &status);
if (trace == null) { if (trace == null) {
println("warning: failed to demangle name: %s, exit status of attempt: %d", traces[i], status);
// just write back the original trace, un-demangled.
trace = traces[i];
// @HACK, both 'main' and 'start' symbols will fail to be demangled, and we don't really care about printing them
// in most cases. your application (certainly true for us) will have its own endpoint which itself is of questionable
// usefulness to print, but 'main' and 'start' are even less useful.
if (String::memeq((const unsigned char*)(mangledNameBegin), (const unsigned char*)"main", sizeof("main") - 1)) {
continue;
} else if (String::memeq((const unsigned char*)(mangledNameBegin), (const unsigned char*)"start", sizeof("start") - 1)) {
continue;
} else {
println("warning: failed to demangle name: %s, exit status of attempt: %d", traces[i], status);
// just write back the original trace, un-demangled.
trace = traces[i];
}
} }
if (string == null) { if (string == null) {

Loading…
Cancel
Save