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

Conversation

BrettDong
Copy link
Member

@BrettDong BrettDong commented Feb 14, 2021

Summary

Infrastructure "Demangle C++ symbols in crash report on macOS"

Purpose of change

Try to implement C++ symbol demangling on macOS so that crash reports are easier to read.

Describe the solution

Extract symbol names from the output of backtrace_symbols() system function by regular expression and then use abi::__cxa_demangle in <cxxabi.h> to demangle C++ symbols. The pattern of the output from backtrace_symbols() differ across different systems, so in this PR I am only trying to implement the functionality on macOS.

Describe the alternative you've considered

  • Instead of using abi::__cxa_demangle in <cxxabi.h>, establish a pipe and invoke c++filt command in the host system.
  • Leave the code here as is and continue to generate raw stack trace crash logs. Let developers demangle symbol names themselves by this online tool http://demangler.com. However it does not seem to support clang symbols.

Testing

Before:
螢幕截圖 2021-02-13 下午8 46 04

After:
螢幕截圖 2021-02-13 下午8 47 25

Additional context

The idea was raised in #46758.

Edit: This should work perfectly if the binary is built natively on the same system. I don't know whether this will work in Jenkins cross-compiled version.

Edit 2: Jenkins also uses clang with libc++ when cross-compiling to macOS so theoretically I guess it will work too.

Edit 3: <cxxabi.h> does not exist on Windows, so I moved it to !_WIN32 region.

@BrettDong BrettDong added OS: macOS Issues related to macOS / OSX operating system [C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style labels Feb 14, 2021
@BrettDong BrettDong force-pushed the demangle-mac branch 2 times, most recently from e43fe6b to e1b702d Compare February 14, 2021 06:20
@ZhilkinSerg
Copy link
Contributor

I think this is a gcc header, so it could be available when building using Windows cross-compile/MSYS2/Cygwin (needs testing).

Demangling when using MSVC should be different.

Match function symbol names by a regular expression designed
specifically for the stack trace pattern returned from
backtrace_symbols() on macOS, and then try to demangle the symbol by
abi::__cxa_demangle(). If demangling succeeds, then output the demangled
symbol name, otherwise output the original raw symbol name.

References:
[1] https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/backtrace_symbols.3.html
[2] https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html
@BrettDong
Copy link
Member Author

BrettDong commented Feb 14, 2021

I'll try to implement symbol demangling on macOS, Linux and BSD first (in this and subsequent PRs), and then see if MinGW will also work. I don't have a x86 arch computer right now so I'm unable to code and test MSVC demangling.

@BrettDong
Copy link
Member Author

I tried to install Visual Studio 2019 on Windows 10 ARM VM but the debugger doesn't work.

@ZhilkinSerg ZhilkinSerg merged commit 57f291c into CleverRaven:master Feb 14, 2021
@BrettDong BrettDong deleted the demangle-mac branch February 14, 2021 22:53
@@ -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.

scarf005 added a commit to scarf005/Cataclysm-BN that referenced this pull request Aug 30, 2023
scarf005 added a commit to scarf005/Cataclysm-BN that referenced this pull request Aug 31, 2023
scarf005 added a commit to scarf005/Cataclysm-BN that referenced this pull request Sep 1, 2023
Firestorm01X2 pushed a commit to cataclysmbnteam/Cataclysm-BN that referenced this pull request Sep 3, 2023
* chore: unindent common messages

was very confusing with `CMAKE_BUILD_TYPE`

* fix(port): BSD macro in src/debug.cpp

from: CleverRaven/Cataclysm-DDA#47440

Co-authored-by: Brett Dong <[email protected]>

* fix(port): Implement C++ symbol demangling on macOS

from: CleverRaven/Cataclysm-DDA#47486

Co-authored-by: Brett Dong <[email protected]>

* refactor(port):  Extract symbol demangling to a separate function

from: CleverRaven/Cataclysm-DDA#47517

Co-authored-by: Brett Dong <[email protected]>

* feat(port):  Demangle symbol names on MinGW

from: CleverRaven/Cataclysm-DDA#47517

Co-authored-by: Brett Dong <[email protected]>

* feat(port):  Implement C++ symbol demangling on Linux

from: CleverRaven/Cataclysm-DDA#47621

Co-authored-by: Brett Dong <[email protected]>

* feat(port):  Implement C++ symbol demangling on FreeBSD and OpenBSD

from: CleverRaven/Cataclysm-DDA#48152

Co-authored-by: Brett Dong <[email protected]>

* fix(port):  C++ symbol demangling on cross-compile MinGW

from: CleverRaven/Cataclysm-DDA#48220

Co-authored-by: Brett Dong <[email protected]>

* fix(port): memory leak in demangle()

from: CleverRaven/Cataclysm-DDA#48230

Co-authored-by: Brett Dong <[email protected]>

* feat(port):
Backtrace on Android

from: CleverRaven/Cataclysm-DDA#48371

Co-authored-by: Brett Dong <[email protected]>

* chore(port): hide write_demangled_frame() for Android build

from: CleverRaven/Cataclysm-DDA#48414

Co-authored-by: Brett Dong <[email protected]>

* feat(port):
demangle RTTI type names in debug messages

from: CleverRaven/Cataclysm-DDA#52633

Co-authored-by: Brett Dong <[email protected]>

* perf(port): optimize null stream in DebugLog

from: CleverRaven/Cataclysm-DDA#55156

Co-authored-by: Brett Dong <[email protected]>

* fix(port): reported version of newer Windows 10 releases

from: CleverRaven/Cataclysm-DDA#55506

Co-authored-by: xuv <[email protected]>

* feat(port): include build number in windows version report

CleverRaven/Cataclysm-DDA#55646

Co-authored-by: xuv <[email protected]>

* feat(port): get the actual static image base of modules

from: CleverRaven/Cataclysm-DDA#55782

Co-authored-by: Qrox <[email protected]>

* ci: fix version typo

* feat(port): support libbacktrace on linux

from: CleverRaven/Cataclysm-DDA#59020

Co-authored-by: John Bytheway <[email protected]>

* build(cmake): support custom linker

context:
https://stackoverflow.com//questions/1867745/cmake-use-a-custom-linker#answer-66730019

* docs(cmake): linker, backtrace and libbacktrace

---------

Co-authored-by: Brett Dong <[email protected]>
Co-authored-by: xuv <[email protected]>
Co-authored-by: Qrox <[email protected]>
Co-authored-by: John Bytheway <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C++] Changes (can be) made in C++. Previously named `Code` Code: Infrastructure / Style / Static Analysis Code internal infrastructure and style OS: macOS Issues related to macOS / OSX operating system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants