-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Boost::Stacktrace to Bitshares #38
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// stacktrace.h (c) 2008, Timo Bingmann from http://idlebox.net/ | ||
// published under the WTFPL v2.0 | ||
|
||
// Downloaded from http://panthema.net/2008/0901-stacktrace-demangled/ | ||
// and modified for C++ and FC by Steemit, Inc. | ||
|
||
#pragma once | ||
|
||
#include <iostream> | ||
|
||
namespace fc { | ||
|
||
void print_stacktrace(std::ostream& out); | ||
void print_stacktrace_on_segfault(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// A stacktrace handler for bitshares | ||
// | ||
|
||
#include <signal.h> | ||
#include <fc/log/logger.hpp> | ||
|
||
// only include stacktrace stuff if boost >= 1.65 | ||
#if BOOST_VERSION / 100000 >= 1 && ((BOOST_VERSION / 100) % 1000) >= 65 | ||
#include <boost/stacktrace.hpp> | ||
|
||
namespace fc | ||
{ | ||
|
||
static void segfault_signal_handler(int signum) | ||
{ | ||
::signal(signum, SIG_DFL); | ||
std::stringstream ss; | ||
ss << boost::stacktrace::stacktrace(); | ||
elog(ss.str()); | ||
::raise(SIGABRT); | ||
} | ||
|
||
void print_stacktrace_on_segfault() | ||
{ | ||
::signal(SIGSEGV, &segfault_signal_handler); | ||
} | ||
|
||
void print_stacktrace(std::ostream& out) | ||
{ | ||
out << boost::stacktrace::stacktrace(); | ||
} | ||
|
||
} | ||
#else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment that this branch is for boost pre 1.65 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pmconrad Good idea. Done. Please take a look. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
// Stacktrace output requires Boost 1.65 or above. | ||
// Therefore calls to these methods do nothing. | ||
namespace fc | ||
{ | ||
void print_stacktrace_on_segfault() {} | ||
void print_stacktrace(std::ostream& out) {} | ||
} | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <boost/test/unit_test.hpp> | ||
|
||
#include <signal.h> | ||
#include <fc/stacktrace.hpp> | ||
#include <fc/thread/thread.hpp> | ||
|
||
BOOST_AUTO_TEST_SUITE(fc_stacktrace) | ||
|
||
BOOST_AUTO_TEST_CASE(stacktrace_test) | ||
{ | ||
// print the stack trace | ||
std::stringstream ss; | ||
fc::print_stacktrace(ss); | ||
std::string results = ss.str(); | ||
#if BOOST_VERSION / 100000 >= 1 && ((BOOST_VERSION / 100) % 1000) >= 65 | ||
BOOST_CHECK(!results.empty()); | ||
BOOST_CHECK(results.find("fc::print_stacktrace") != std::string::npos); | ||
#else | ||
BOOST_CHECK(results.empty()); | ||
#endif | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(threaded_stacktrace_test) | ||
{ | ||
fc::thread test_thread("a_thread"); | ||
std::string results = test_thread.async( | ||
[] ()->std::string { | ||
// cause a pause | ||
for(int i = 0; i < 10000; i++); | ||
std::stringstream ss; | ||
fc::print_stacktrace(ss); | ||
return ss.str(); | ||
} | ||
).wait(); | ||
#if BOOST_VERSION / 100000 >= 1 && ((BOOST_VERSION / 100) % 1000) >= 65 | ||
BOOST_CHECK(!results.empty()); | ||
BOOST_CHECK(results.find("fc::print_stacktrace") != std::string::npos); | ||
#else | ||
BOOST_CHECK(results.empty()); | ||
#endif | ||
} | ||
|
||
/* this test causes a segfault on purpose to test the event handler | ||
BOOST_AUTO_TEST_CASE(cause_segfault) | ||
{ | ||
fc::print_stacktrace_on_segfault(); | ||
::raise(SIGSEGV); | ||
} | ||
*/ | ||
BOOST_AUTO_TEST_SUITE_END() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move these includes into
#if
block.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.