Skip to content
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

Merged
merged 3 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ set( fc_sources
src/asio.cpp
src/string.cpp
src/shared_ptr.cpp
src/stacktrace.cpp
src/time.cpp
src/utf8.cpp
src/io/iostream.cpp
Expand Down
16 changes: 16 additions & 0 deletions include/fc/stacktrace.hpp
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();

}
44 changes: 44 additions & 0 deletions src/stacktrace.cpp
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>
Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


// 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
Copy link

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pmconrad Good idea. Done. Please take a look.

Copy link

Choose a reason for hiding this comment

The 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
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ add_executable( all_tests all_tests.cpp
bloom_test.cpp
real128_test.cpp
serialization_test.cpp
stacktrace_test.cpp
time_test.cpp
utf8_test.cpp
variant_test.cpp
Expand Down
50 changes: 50 additions & 0 deletions tests/stacktrace_test.cpp
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()