diff --git a/paddle/fluid/framework/op_call_stack.cc b/paddle/fluid/framework/op_call_stack.cc index 3a9b113ceac573c831ce39993d7e2f6df37ee5fe..80db35e0c391747cd5058cee3352fc496efa07f3 100644 --- a/paddle/fluid/framework/op_call_stack.cc +++ b/paddle/fluid/framework/op_call_stack.cc @@ -35,26 +35,14 @@ void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs, } std::ostringstream sout; - std::ostringstream sout_py_trace; // Step 1. Construct python call stack string if (callstack) { - sout_py_trace << "\n------------------------------------------\n"; - sout_py_trace << "Python Call Stacks (More useful to users):"; - sout_py_trace << "\n------------------------------------------\n"; + sout << "\n\n Compile Traceback (most recent call last):"; for (auto &line : *callstack) { - sout_py_trace << line; + sout << "\n " << line; } } - // Step 2. Insert python traceback into err_str_ - std::size_t found = exception->err_str_.rfind( - "\n----------------------\nError Message " - "Summary:\n----------------------\n"); - if (found != std::string::npos) { - exception->err_str_.insert(found, sout_py_trace.str()); - } else { - exception->err_str_.append(sout_py_trace.str()); - } - // Step 3. Construct final call stack & append error op name + // Step 2. Construct final call stack & append error op name sout << exception->err_str_; sout << " [operator < " << type << " > error]"; exception->err_str_ = sout.str(); diff --git a/paddle/fluid/framework/op_desc.cc b/paddle/fluid/framework/op_desc.cc index e490d571a699e38d4762cb1d1771fb15639e8e13..66fe71a80a7b0165a0d4afb38c89fc1fdb339190 100644 --- a/paddle/fluid/framework/op_desc.cc +++ b/paddle/fluid/framework/op_desc.cc @@ -700,7 +700,7 @@ void OpDesc::InferShape(const BlockDesc &block) const { } infer_shape(&ctx); } catch (platform::EnforceNotMet &exception) { - framework::InsertCallStackInfo(Type(), attrs_, &exception); + framework::AppendErrorOpHint(Type(), &exception); throw std::move(exception); } catch (...) { std::rethrow_exception(std::current_exception()); diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index 475256826f36086dae2eed1773829fae0e6db537..9a3a639579bd9d44f257c3f0f1aa63e0ae27e8e2 100644 --- a/paddle/fluid/platform/enforce.h +++ b/paddle/fluid/platform/enforce.h @@ -232,16 +232,16 @@ inline std::string GetTraceBackString(StrType&& what, const char* file, static constexpr int TRACE_STACK_LIMIT = 100; std::ostringstream sout; - sout << "\n\n--------------------------------------------\n"; - sout << "C++ Call Stacks (More useful to developers):"; - sout << "\n--------------------------------------------\n"; + sout << "\n\n--------------------------------------\n"; + sout << "C++ Traceback (most recent call last):"; + sout << "\n--------------------------------------\n"; #if !defined(_WIN32) void* call_stack[TRACE_STACK_LIMIT]; auto size = backtrace(call_stack, TRACE_STACK_LIMIT); auto symbols = backtrace_symbols(call_stack, size); Dl_info info; int idx = 0; - for (int i = 0; i < size; ++i) { + for (int i = size - 1; i >= 0; --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/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 1c28ecd3a8f5e477f722badbbb6fe69de2036723..9ba1b33c739b6910a8e67133e0d38e39448aaecd 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -1913,8 +1913,13 @@ class Operator(object): "`type` to initialized an Operator can not be None.") else: callstack_var_name = op_maker.kOpCreationCallstackAttrName() - op_attrs[callstack_var_name] = list( - reversed(traceback.format_stack()))[1:] + op_attrs[callstack_var_name] = [] + for frame in traceback.extract_stack(): + op_attrs[callstack_var_name].append( + ' File "{}", line {}, in {}'.format(frame[0], frame[1], + frame[2])) + op_attrs[callstack_var_name].append(' {}'.format(frame[ + 3])) self.desc.set_type(type) proto = OpProtoHolder.instance().get_op_proto(type)