-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Backtrace support for flang #118179
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
Backtrace support for flang #118179
Changes from 17 commits
2d5d310
6b57193
0862afd
d28136f
bf60ffe
594c2b3
ca2ceb5
156e434
6c22384
c2e4cca
0270969
d3df426
96a861c
30e5858
d7af048
f2fdf26
6e6c1a5
1e59e61
441eede
624a42a
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -29,6 +29,7 @@ NORETURN void RTNAME(ProgramEndStatement)(NO_ARGUMENTS); | |||||
| // Extensions | ||||||
| NORETURN void RTNAME(Exit)(int status DEFAULT_VALUE(EXIT_SUCCESS)); | ||||||
| NORETURN void RTNAME(Abort)(NO_ARGUMENTS); | ||||||
| void RTNAME(Backtrace)(NO_ARGUMENTS); | ||||||
|
||||||
| void RTNAME(Backtrace)(NO_ARGUMENTS); | |
| void FORTRAN_PROCEDURE_NAME(Backtrace)(NO_ARGUMENTS); |
With this you shouldn't need any of the frontend changes (flang/lib, and associated header changes).
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "flang/Runtime/stop.h" | ||
| #include "config.h" | ||
| #include "environment.h" | ||
| #include "file.h" | ||
| #include "io-error.h" | ||
|
|
@@ -16,6 +17,10 @@ | |
| #include <cstdio> | ||
| #include <cstdlib> | ||
|
|
||
| #ifdef HAVE_BACKTRACE | ||
| #include BACKTRACE_HEADER | ||
| #endif | ||
|
|
||
| extern "C" { | ||
|
|
||
| static void DescribeIEEESignaledExceptions() { | ||
|
|
@@ -152,11 +157,36 @@ void RTNAME(PauseStatementText)(const char *code, std::size_t length) { | |
| std::exit(status); | ||
| } | ||
|
|
||
| static void PrintBacktrace() { | ||
| #ifdef HAVE_BACKTRACE | ||
| // TODO: Need to parse DWARF information to print function line numbers | ||
| constexpr int MAX_CALL_STACK{999}; | ||
| void *buffer[MAX_CALL_STACK]; | ||
| int nptrs{backtrace(buffer, MAX_CALL_STACK)}; | ||
|
|
||
| if (char **symbols{backtrace_symbols(buffer, nptrs)}) { | ||
| for (int i = 0; i < nptrs; i++) { | ||
| Fortran::runtime::Terminator{}.PrintCrashArgs("#%d %s\n", i, symbols[i]); | ||
| } | ||
| free(symbols); | ||
| } | ||
|
|
||
| #else | ||
|
|
||
| // TODO: Need to implement the version for other platforms. | ||
| Fortran::runtime::Terminator{}.PrintCrashArgs( | ||
| "Handle the case when a backtrace is not available\n"); | ||
|
|
||
| #endif | ||
| } | ||
|
|
||
| [[noreturn]] void RTNAME(Abort)() { | ||
| // TODO: Add backtrace call, unless with `-fno-backtrace`. | ||
| PrintBacktrace(); | ||
|
Contributor
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. Could you make that one also conditional on It could lead people to believe that the abort is related to BACKTRACE while it is not.
Contributor
Author
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. You are right, thanks for pointing this out. |
||
| std::abort(); | ||
| } | ||
|
|
||
| void RTNAME(Backtrace)() { PrintBacktrace(); } | ||
|
|
||
| [[noreturn]] void RTNAME(ReportFatalUserError)( | ||
| const char *message, const char *source, int line) { | ||
| Fortran::runtime::Terminator{source, line}.Crash(message); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| ! RUN: bbc -emit-fir %s -o - | FileCheck %s | ||
|
|
||
| ! CHECK-LABEL: func.func @_QPbacktrace_test() { | ||
| ! CHECK: %[[VAL_0:.*]] = fir.call @_FortranABacktrace() {{.*}}: () -> none | ||
| ! CHECK: return | ||
| ! CHECK: } | ||
|
|
||
| subroutine backtrace_test() | ||
| call backtrace | ||
| end subroutine |
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 could you try putting this in
flang/runtime/CMakeLists.txt