diff options
| author | Michael Kruse <llvm-project@meinersbur.de> | 2025-01-03 10:22:51 +0100 |
|---|---|---|
| committer | Michael Kruse <llvm-project@meinersbur.de> | 2025-01-03 10:22:51 +0100 |
| commit | 38500d63e14ce340236840f60d356cdefb56a52c (patch) | |
| tree | 17edbec446ce9b50d2f215a483b83afb293a635d /flang/runtime/stop.cpp | |
| parent | 1a3d5daaef7a6a63448a497da3eff7fc9e23df26 (diff) | |
| parent | 27f30029741ecf023baece7b3dde1ff9011ffefc (diff) | |
Merge branch 'main' into users/meinersbur/flang_runtime_split-headersusers/meinersbur/flang_runtime_split-headers
Diffstat (limited to 'flang/runtime/stop.cpp')
| -rw-r--r-- | flang/runtime/stop.cpp | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/flang/runtime/stop.cpp b/flang/runtime/stop.cpp index cfb36b408402..a7be8a082e02 100644 --- a/flang/runtime/stop.cpp +++ b/flang/runtime/stop.cpp @@ -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,37 @@ 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{(int)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("backtrace is not supported."); + +#endif +} + [[noreturn]] void RTNAME(Abort)() { - // TODO: Add backtrace call, unless with `-fno-backtrace`. +#ifdef HAVE_BACKTRACE + PrintBacktrace(); +#endif std::abort(); } +void FORTRAN_PROCEDURE_NAME(backtrace)() { PrintBacktrace(); } + [[noreturn]] void RTNAME(ReportFatalUserError)( const char *message, const char *source, int line) { Fortran::runtime::Terminator{source, line}.Crash(message); |
