From b4755c5aa7ede517bf9bc559e9247c050c6711f2 Mon Sep 17 00:00:00 2001 From: liaogang Date: Mon, 14 Aug 2017 15:50:28 +0800 Subject: [PATCH 1/3] Demangle exception call stack for PADDLE_ENFORCE --- paddle/platform/enforce.h | 47 ++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/paddle/platform/enforce.h b/paddle/platform/enforce.h index 337a059fb1494d..aa0660df88a057 100644 --- a/paddle/platform/enforce.h +++ b/paddle/platform/enforce.h @@ -14,14 +14,20 @@ limitations under the License. */ #pragma once -#include +#include // for dladdr +#include // for backtrace #include #include #include #include + #include "paddle/string/printf.h" #include "paddle/string/to_string.h" +#ifdef __GNUC__ +#include // for __cxa_demangle +#endif + #ifndef PADDLE_ONLY_CPU #include "paddle/platform/dynload/cublas.h" @@ -39,6 +45,19 @@ limitations under the License. */ namespace paddle { namespace platform { +namespace { +#ifdef __GNUC__ +inline std::string demangle(std::string name) { + int status = -4; // some arbitrary value to eliminate the compiler warning + std::unique_ptr res{ + abi::__cxa_demangle(name.c_str(), NULL, NULL, &status), std::free}; + return (status == 0) ? res.get() : name; +} +#else +inline std::string demangle(std::string name) { return name; } +#endif +} + struct EnforceNotMet : public std::exception { std::exception_ptr exp_; std::string err_str_; @@ -48,15 +67,27 @@ struct EnforceNotMet : public std::exception { std::rethrow_exception(exp_); } catch (const std::exception& exp) { std::ostringstream sout; + sout << string::Sprintf("%s at [%s:%d]", exp.what(), f, l) << std::endl; - sout << "Call Stacks: " << std::endl; + sout << "PaddlePaddle Call Stacks: " << std::endl; + void* call_stack[TRACE_STACK_LIMIT]; - int sz = backtrace(call_stack, TRACE_STACK_LIMIT); - auto line = backtrace_symbols(call_stack, sz); - for (int i = 0; i < sz; ++i) { - sout << line[i] << std::endl; + auto size = backtrace(call_stack, TRACE_STACK_LIMIT); + auto symbols = backtrace_symbols(call_stack, size); + + Dl_info info; + for (int i = 0; i < size; ++i) { + if (dladdr(call_stack[i], &info)) { + auto demangled = demangle(info.dli_sname); + sout << string::Sprintf( + "%-3d %*0p %s + %zd\n", i, 2 + sizeof(void*) * 2, call_stack[i], + demangled, (char*)call_stack[i] - (char*)info.dli_saddr); + } else { + sout << string::Sprintf("%-3d %*0p %s\n", i, 2 + sizeof(void*) * 2, + call_stack[i]); + } } - free(line); + free(symbols); err_str_ = sout.str(); } } @@ -170,7 +201,7 @@ inline void throw_on_error(T e) { * PADDLE_ENFORCE_EQ(a, b); * * will raise an expression described as follows: - * "enforce a == b failed, 1 != 2" with detailed stack infomation. + * "enforce a == b failed, 1 != 2" with detailed stack information. * * extra messages is also supported, for example: * PADDLE_ENFORCE(a, b, "some simple enforce failed between %d numbers", 2) From 8210bcea6116be008aa027c6e868acbf5ebfeda6 Mon Sep 17 00:00:00 2001 From: liaogang Date: Mon, 14 Aug 2017 15:59:02 +0800 Subject: [PATCH 2/3] Using static_cast to replace (char*) --- paddle/platform/enforce.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/paddle/platform/enforce.h b/paddle/platform/enforce.h index aa0660df88a057..cc38dc4ffede09 100644 --- a/paddle/platform/enforce.h +++ b/paddle/platform/enforce.h @@ -79,9 +79,11 @@ struct EnforceNotMet : public std::exception { for (int i = 0; i < size; ++i) { if (dladdr(call_stack[i], &info)) { auto demangled = demangle(info.dli_sname); - sout << string::Sprintf( - "%-3d %*0p %s + %zd\n", i, 2 + sizeof(void*) * 2, call_stack[i], - demangled, (char*)call_stack[i] - (char*)info.dli_saddr); + auto addr_offset = static_cast(call_stack[i]) - + static_cast(info.dli_saddr); + sout << string::Sprintf("%-3d %*0p %s + %zd\n", i, + 2 + sizeof(void*) * 2, call_stack[i], + demangled, addr_offset); } else { sout << string::Sprintf("%-3d %*0p %s\n", i, 2 + sizeof(void*) * 2, call_stack[i]); From ffbb4c19634778d5380954dd55f4f56ec17b3859 Mon Sep 17 00:00:00 2001 From: liaogang Date: Mon, 14 Aug 2017 16:27:25 +0800 Subject: [PATCH 3/3] memory.h for unique_ptr --- paddle/platform/enforce.h | 1 + 1 file changed, 1 insertion(+) diff --git a/paddle/platform/enforce.h b/paddle/platform/enforce.h index cc38dc4ffede09..15fdf7a94f462a 100644 --- a/paddle/platform/enforce.h +++ b/paddle/platform/enforce.h @@ -17,6 +17,7 @@ limitations under the License. */ #include // for dladdr #include // for backtrace #include +#include #include #include #include