Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use backtrace_symbols() when printing stack traces on FreeBSD
Unfortunately, the symbol resolution of boost::stacktrace is broken on FreeBSD, therefore fall back to using backtrace_symbols() to print the stack trace saved by Boost. Additionally, -D_GNU_SOURCE is required on FreeBSD for the _Unwind_Backtrace function used by boost::stacktrace.
- Loading branch information
1 parent
7d3885d
commit b931194
Showing
7 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* Icinga 2 | (c) 2020 Icinga GmbH | GPLv2+ */ | ||
|
||
#include <base/i2-base.hpp> | ||
#include "base/stacktrace.hpp" | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <vector> | ||
|
||
#ifdef HAVE_BACKTRACE_SYMBOLS | ||
# include <execinfo.h> | ||
#endif /* HAVE_BACKTRACE_SYMBOLS */ | ||
|
||
using namespace icinga; | ||
|
||
std::ostream &icinga::operator<<(std::ostream &os, const StackTraceFormatter &f) | ||
{ | ||
const boost::stacktrace::stacktrace &stack = f.m_Stack; | ||
|
||
#ifdef ICINGA2_STACKTRACE_USE_BACKTRACE_SYMBOLS | ||
std::vector<void *> addrs; | ||
addrs.reserve(stack.size()); | ||
std::transform(stack.begin(), stack.end(), std::back_inserter(addrs), [](const boost::stacktrace::frame &f) { | ||
return const_cast<void *>(f.address()); | ||
}); | ||
|
||
char **symbols = backtrace_symbols(addrs.data(), addrs.size()); | ||
for (size_t i = 0; i < addrs.size(); i++) { | ||
os << std::setw(2) << i << "# " << symbols[i] << std::endl; | ||
} | ||
std::free(symbols); | ||
#else /* ICINGA2_STACKTRACE_USE_BACKTRACE_SYMBOLS */ | ||
os << stack; | ||
#endif /* ICINGA2_STACKTRACE_USE_BACKTRACE_SYMBOLS */ | ||
|
||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* Icinga 2 | (c) 2020 Icinga GmbH | GPLv2+ */ | ||
|
||
#ifndef STACKTRACE_H | ||
#define STACKTRACE_H | ||
|
||
#include <boost/stacktrace.hpp> | ||
|
||
namespace icinga | ||
{ | ||
|
||
class StackTraceFormatter { | ||
public: | ||
StackTraceFormatter(const boost::stacktrace::stacktrace &stack) : m_Stack(stack) {} | ||
|
||
private: | ||
const boost::stacktrace::stacktrace &m_Stack; | ||
|
||
friend std::ostream &operator<<(std::ostream &os, const StackTraceFormatter &f); | ||
}; | ||
|
||
std::ostream& operator<<(std::ostream& os, const StackTraceFormatter &f); | ||
|
||
} | ||
|
||
#endif /* STACKTRACE_H */ |