From 572adccdeec5b6531c67e31d95c77e8fe0cdc056 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Thu, 12 Aug 2021 20:11:32 +0800 Subject: [PATCH] Remove incorrect signal error stack trace (#34842) * remove unmatched signal error stack * fix error writing for cond --- paddle/fluid/platform/enforce.h | 17 ++++++++++++----- paddle/fluid/platform/init.cc | 12 +++++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index c63ea3fa857..52be0c805bb 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 2e0ba9d241c..3ee5a578601 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( -- GitLab