Permalink
Browse files

Move printStackTrace into new stacktrace.h/cpp; improve detection of …

…execinfo.h.
  • Loading branch information...
1 parent 7cb696d commit 890d3b586d95761f041eb7afe0032c7e18737890 @andy10gen andy10gen committed Apr 5, 2012
View
@@ -779,8 +779,11 @@ def doConfigure(myenv):
l + boostCompiler + boostVersion ], language='C++' ):
Exit(1)
- if not conf.CheckCXXHeader( "execinfo.h" ):
- myenv.Append( CPPDEFINES=[ "NOEXECINFO" ] )
+ if (conf.CheckCXXHeader( "execinfo.h" ) and
+ conf.CheckDeclaration('backtrace', includes='#include <execinfo.h>') and
+ conf.CheckDeclaration('backtrace_symbols', includes='#include <execinfo.h>')):
+
+ myenv.Append( CPPDEFINES=[ "MONGO_HAVE_EXECINFO_BACKTRACE" ] )
myenv["_HAVEPCAP"] = conf.CheckLib( ["pcap", "wpcap"], autoadd=False )
@@ -34,6 +34,7 @@ clientSource = [
'mongo/util/concurrency/thread_pool.cpp',
'mongo/util/concurrency/vars.cpp',
'mongo/util/debug_util.cpp',
+ 'mongo/util/stacktrace.cpp',
'mongo/util/file_allocator.cpp',
'mongo/util/histogram.cpp',
'mongo/util/intrusive_counter.cpp',
@@ -72,7 +72,8 @@ commonFiles = [ "pch.cpp",
"s/shardconnection.cpp"]
env.StaticLibrary('mongocommon', commonFiles,
- LIBDEPS=['$BUILD_DIR/third_party/pcrecpp',
+ LIBDEPS=['stacktrace',
+ '$BUILD_DIR/third_party/pcrecpp',
'$BUILD_DIR/third_party/murmurhash3/murmurhash3',
'$BUILD_DIR/third_party/mongo_boost'],)
@@ -262,6 +263,8 @@ serverOnlyFiles = [ "db/curop.cpp",
env.Library( "dbcmdline", "db/cmdline.cpp" )
+env.Library('stacktrace', 'util/stacktrace.cpp')
+
serverOnlyFiles += mmapFiles
serverOnlyFiles += [ "db/stats/snapshots.cpp" ]
@@ -19,8 +19,10 @@
#pragma once
#include <boost/detail/endian.hpp>
-#include "concurrency/mutex.h"
-#include "../bson/util/misc.h"
+
+#include "mongo/bson/util/misc.h"
+#include "mongo/util/concurrency/mutex.h"
+#include "mongo/util/stacktrace.h"
namespace mongo {
@@ -41,40 +43,6 @@ namespace mongo {
return s.str();
}
-#if !defined(_WIN32) && !defined(NOEXECINFO) && !defined(__freebsd__) && !defined(__openbsd__) && !defined(__sun__)
-
-} // namespace mongo
-
-#include <pthread.h>
-#include <execinfo.h>
-
-namespace mongo {
-
- inline pthread_t GetCurrentThreadId() {
- return pthread_self();
- }
-
- /* use "addr2line -CFe <exe>" to parse. */
- inline void printStackTrace(ostream &o = cout) {
- void *b[20];
-
- int size = backtrace(b, 20);
- for (int i = 0; i < size; i++)
- o << hex << b[i] << dec << ' ';
- o << endl;
-
- char **strings;
-
- strings = backtrace_symbols(b, size);
- for (int i = 0; i < size; i++)
- o << ' ' << strings[i] << '\n';
- o.flush();
- free (strings);
- }
-#else
- inline void printStackTrace(ostream &o = cout) { }
-#endif
-
bool isPrime(int n);
int nextPrime(int n);
@@ -26,7 +26,7 @@
#include <unistd.h>
#endif
-#if !defined(_WIN32) && !defined(NOEXECINFO)
+#ifdef MONGO_HAVE_EXECINFO_BACKTRACE
#include <execinfo.h>
#endif
@@ -84,7 +84,7 @@ namespace mongo {
static void formattedBacktrace( int fd ) {
-#if !defined(_WIN32) && !defined(NOEXECINFO)
+#ifdef MONGO_HAVE_EXECINFO_BACKTRACE
int numFrames;
const int MAX_DEPTH = 20;
@@ -0,0 +1,37 @@
+// Copyright 2009. 10gen, Inc.
+
+#include "mongo/util/stacktrace.h"
+
+#include <cstdlib>
+#include <iostream>
+
+#ifdef MONGO_HAVE_EXECINFO_BACKTRACE
+
+#include <execinfo.h>
+
+static const int maxBackTraceFrames = 20;
+
+void ::mongo::printStackTrace( std::ostream &os ) {
+
+ void *b[maxBackTraceFrames];
+
+ int size = ::backtrace( b, maxBackTraceFrames );
+ for ( int i = 0; i < size; i++ )
+ os << std::hex << b[i] << std::dec << ' ';
+ os << std::endl;
+
+ char **strings;
+
+ strings = ::backtrace_symbols( b, size );
+ for ( int i = 0; i < size; i++ )
+ os << ' ' << strings[i] << '\n';
+ os.flush();
+ ::free( strings );
+}
+
+#else
+
+void ::mongo::printStackTrace( std::ostream &os ) {}
+
+#endif // defined(MONGO_HAVE_EXECINFO_BACKTRACE)
+
@@ -0,0 +1,16 @@
+// Copyright 2009. 10gen, Inc.
+
+/**
+ * Tools for working with in-process stack traces.
+ */
+
+#pragma once
+
+#include <iostream>
+
+namespace mongo {
+
+ // Print stack trace information to "os", default to std::cout.
+ void printStackTrace(std::ostream &os=std::cout);
+
+} // namespace mongo

0 comments on commit 890d3b5

Please sign in to comment.