Commit 612dddaf authored by Philip Quinn's avatar Philip Quinn

Support for exception backtraces on OS X.

parent c8598892
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "exception.h" #include "exception.h"
#include "string.h" #include "string.h"
#include "string-tree.h"
#include "debug.h" #include "debug.h"
#include "threadlocal.h" #include "threadlocal.h"
#include <unistd.h> #include <unistd.h>
...@@ -32,7 +33,7 @@ ...@@ -32,7 +33,7 @@
#include <execinfo.h> #include <execinfo.h>
#endif #endif
#if __linux__ && defined(KJ_DEBUG) #if (__linux__ || __APPLE__) && defined(KJ_DEBUG)
#include <stdio.h> #include <stdio.h>
#include <pthread.h> #include <pthread.h>
#endif #endif
...@@ -108,6 +109,23 @@ String getStackSymbols(ArrayPtr<void* const> trace) { ...@@ -108,6 +109,23 @@ String getStackSymbols(ArrayPtr<void* const> trace) {
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
return strArray(arrayPtr(lines, i), ""); return strArray(arrayPtr(lines, i), "");
#elif __APPLE__ && defined(KJ_DEBUG)
// The Mac OS X equivalent of addr2line is atos(1).
// (Internally, it uses the private CoreSymbolication.framework library.)
FILE* p = popen(str("atos -d -p ", getpid(), ' ', strArray(trace, " ")).cStr(), "r");
if (p == nullptr) {
return nullptr;
}
StringTree stackSymbols;
char line[512];
while (fgets(line, sizeof(line), p) != nullptr) {
stackSymbols = strTree(mv(stackSymbols), str(line));
}
pclose(p);
return stackSymbols.flatten();
#else #else
return nullptr; return nullptr;
#endif #endif
...@@ -171,6 +189,7 @@ String KJ_STRINGIFY(const Exception& e) { ...@@ -171,6 +189,7 @@ String KJ_STRINGIFY(const Exception& e) {
e.getDurability() == Exception::Durability::TEMPORARY ? " (temporary)" : "", e.getDurability() == Exception::Durability::TEMPORARY ? " (temporary)" : "",
e.getDescription() == nullptr ? "" : ": ", e.getDescription(), e.getDescription() == nullptr ? "" : ": ", e.getDescription(),
e.getStackTrace().size() > 0 ? "\nstack: " : "", strArray(e.getStackTrace(), " "), e.getStackTrace().size() > 0 ? "\nstack: " : "", strArray(e.getStackTrace(), " "),
"\n",
getStackSymbols(e.getStackTrace())); getStackSymbols(e.getStackTrace()));
} }
...@@ -330,6 +349,7 @@ private: ...@@ -330,6 +349,7 @@ private:
e.getNature(), e.getDurability() == Exception::Durability::TEMPORARY ? " (temporary)" : "", e.getNature(), e.getDurability() == Exception::Durability::TEMPORARY ? " (temporary)" : "",
e.getDescription() == nullptr ? "" : ": ", e.getDescription(), e.getDescription() == nullptr ? "" : ": ", e.getDescription(),
e.getStackTrace().size() > 0 ? "\nstack: " : "", strArray(e.getStackTrace(), " "), e.getStackTrace().size() > 0 ? "\nstack: " : "", strArray(e.getStackTrace(), " "),
"\n",
getStackSymbols(e.getStackTrace()), "\n")); getStackSymbols(e.getStackTrace()), "\n"));
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment