diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index c63ea3fa8573b8a7fd739931869c8f53259d8a77..52be0c805bbd2a68481a6cdbfef0de42b1a946f5 100644 --- a/paddle/fluid/platform/enforce.h +++ b/paddle/fluid/platform/enforce.h @@ -270,12 +270,14 @@ inline std::string SimplifyDemangleStr(std::string str) { return str; } -inline std::string GetCurrentTraceBackString() { +inline std::string GetCurrentTraceBackString(bool for_signal = false) { std::ostringstream sout; - sout << "\n\n--------------------------------------\n"; - sout << "C++ Traceback (most recent call last):"; - sout << "\n--------------------------------------\n"; + if (!for_signal) { + sout << "\n\n--------------------------------------\n"; + sout << "C++ Traceback (most recent call last):"; + sout << "\n--------------------------------------\n"; + } #if !defined(_WIN32) && !defined(PADDLE_WITH_MUSL) static constexpr int TRACE_STACK_LIMIT = 100; @@ -284,7 +286,12 @@ inline std::string GetCurrentTraceBackString() { auto symbols = backtrace_symbols(call_stack, size); Dl_info info; int idx = 0; - for (int i = size - 1; i >= 0; --i) { + // `for_signal` used to remove the stack trace introduced by + // obtaining the error stack trace when the signal error occurred, + // that is not related to the signal error self, remove it to + // avoid misleading users and developers + int end_idx = for_signal ? 2 : 0; + for (int i = size - 1; i >= end_idx; --i) { if (dladdr(call_stack[i], &info) && info.dli_sname) { auto demangled = demangle(info.dli_sname); std::string path(info.dli_fname); diff --git a/paddle/fluid/platform/init.cc b/paddle/fluid/platform/init.cc index 2e0ba9d241c72b2ec0e99f54b2241ba11fcc8590..3ee5a57860104508f041e4ed823ded3befe35ca1 100644 --- a/paddle/fluid/platform/init.cc +++ b/paddle/fluid/platform/init.cc @@ -294,7 +294,17 @@ void SignalHandle(const char *data, int size) { // Here does not throw an exception, // otherwise it will casue "terminate called recursively" std::ostringstream sout; - sout << platform::GetCurrentTraceBackString(); + sout << "\n\n--------------------------------------\n"; + sout << "C++ Traceback (most recent call last):"; + sout << "\n--------------------------------------\n"; + auto traceback = platform::GetCurrentTraceBackString(/*for_signal=*/true); + if (traceback.empty()) { + sout + << "No stack trace in paddle, may be caused by external reasons.\n"; + } else { + sout << traceback; + } + sout << "\n----------------------\nError Message " "Summary:\n----------------------\n"; sout << platform::errors::Fatal(