summaryrefslogtreecommitdiff
path: root/src/mongo/platform
diff options
context:
space:
mode:
authorTad Marshall <tad@10gen.com>2013-06-21 13:34:53 -0400
committerTad Marshall <tad@10gen.com>2013-06-21 13:34:53 -0400
commita5eb3244eb0846bba96ff56289460f38fe26fffb (patch)
treefe1ce38a33f34727ffaac8f3cc6233fa793f6702 /src/mongo/platform
parentfc9491ee7be6a7dc8a92a8422468284359073545 (diff)
downloadmongo-a5eb3244eb0846bba96ff56289460f38fe26fffb.tar.gz
SERVER-7080 Add code to emulate backtrace()
backtrace() fills a buffer provided by the caller with addresses constituting a stack trace. This emulation allows the display of addresses in Solaris 10, which can be used with addr2line to see code locations.
Diffstat (limited to 'src/mongo/platform')
-rw-r--r--src/mongo/platform/backtrace.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/mongo/platform/backtrace.cpp b/src/mongo/platform/backtrace.cpp
index 9bb3fd0283c..64d593d0157 100644
--- a/src/mongo/platform/backtrace.cpp
+++ b/src/mongo/platform/backtrace.cpp
@@ -19,6 +19,7 @@
#include "mongo/platform/backtrace.h"
#include <dlfcn.h>
+#include <ucontext.h>
#include "mongo/base/init.h"
#include "mongo/base/status.h"
@@ -26,7 +27,48 @@
namespace mongo {
namespace pal {
+namespace {
+ class WalkcontextCallback {
+ public:
+ WalkcontextCallback(uintptr_t* array, int size)
+ : _position(0),
+ _count(size),
+ _addresses(array) {}
+
+ // This callback function is called from C code, and so must not throw exceptions
+ //
+ static int callbackFunction(uintptr_t address,
+ int signalNumber,
+ WalkcontextCallback* thisContext) {
+ if (thisContext->_position < thisContext->_count) {
+ thisContext->_addresses[thisContext->_position++] = address;
+ return 0;
+ }
+ return 1;
+ }
+ int getCount() const { return static_cast<int>(_position); }
+ private:
+ size_t _position;
+ size_t _count;
+ uintptr_t* _addresses;
+ };
+} // namespace
+
+ typedef int (*WalkcontextCallbackFunc)(uintptr_t address, int signalNumber, void* thisContext);
+
int backtrace_emulation(void** array, int size) {
+ WalkcontextCallback walkcontextCallback(reinterpret_cast<uintptr_t*>(array), size);
+ ucontext_t context;
+ if (getcontext(&context) != 0) {
+ return 0;
+ }
+ int wcReturn = walkcontext(
+ &context,
+ reinterpret_cast<WalkcontextCallbackFunc>(WalkcontextCallback::callbackFunction),
+ static_cast<void*>(&walkcontextCallback));
+ if (wcReturn == 0) {
+ return walkcontextCallback.getCount();
+ }
return 0;
}