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

Implement C++ symbol demangling on macOS #47486

Merged
merged 1 commit into from
Feb 14, 2021
Merged
Changes from all 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
32 changes: 31 additions & 1 deletion src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <map>
#include <memory>
#include <new>
#include <regex>
#include <set>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -61,6 +62,7 @@
# include <backtrace.h>
# endif
# else
# include <cxxabi.h>
# include <execinfo.h>
# include <unistd.h>
# endif
Expand Down Expand Up @@ -791,6 +793,34 @@ static constexpr int bt_cnt = 20;
static void *bt[bt_cnt];
#endif

#if !defined(_WIN32)
static void write_demangled_frame( std::ostream &out, const char *frame )
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to hide this function for Android:

20:11:27 [arm64-v8a] Compile++      : main <= debug.cpp

20:11:27 /var/lib/jenkins/workspace/Cataclysm-Android/android/app/jni/src/../../../../src/debug.cpp:831:13: warning: unused function 'write_demangled_frame' [-Wunused-function]
20:11:27 static void write_demangled_frame( std::ostream &out, const char *frame )
20:11:27             ^
20:11:27 1 warning generated.

{
#if defined(MACOSX)
//1 cataclysm-tiles 0x0000000102ba2244 _ZL9log_crashPKcS0_ + 608
static const std::regex symbol_regex( R"(^(.*)(0x[a-f0-9]{16})\s(.*)\s\+\s([0-9]+)$)" );
std::cmatch match_result;
if( std::regex_search( frame, match_result, symbol_regex ) && match_result.size() == 5 ) {
std::csub_match prefix = match_result[1];
std::csub_match address = match_result[2];
std::csub_match raw_symbol_name = match_result[3];
std::csub_match offset = match_result[4];
int status = -1;
char *demangled = abi::__cxa_demangle( raw_symbol_name.str().c_str(), nullptr, nullptr, &status );
if( status == 0 ) {
out << "\n " << prefix.str() << address.str() << ' ' << demangled << " + " << offset.str();
} else {
out << "\n " << frame;
}
} else {
out << "\n " << frame;
}
#else
out << "\n " << frame;
#endif
}
#endif // !defined(_WIN32)

void debug_write_backtrace( std::ostream &out )
{
#if defined(_WIN32)
Expand Down Expand Up @@ -899,7 +929,7 @@ void debug_write_backtrace( std::ostream &out )
int count = backtrace( bt, bt_cnt );
char **funcNames = backtrace_symbols( bt, count );
for( int i = 0; i < count; ++i ) {
out << "\n " << funcNames[i];
write_demangled_frame( out, funcNames[i] );
}
out << "\n\n Attempting to repeat stack trace using debug symbols…\n";
// Try to print the backtrace again, but this time using addr2line
Expand Down