From a2c8981a9e01aa87d2ee38a113ad6bbffd218bd3 Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Fri, 3 Jan 2020 13:43:57 +1000 Subject: [PATCH] Add basic stacktrace output for posix debug builds. (#2749) --- onnxruntime/core/platform/posix/stacktrace.cc | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/onnxruntime/core/platform/posix/stacktrace.cc b/onnxruntime/core/platform/posix/stacktrace.cc index f41ef0e1d4..73a4e2b274 100644 --- a/onnxruntime/core/platform/posix/stacktrace.cc +++ b/onnxruntime/core/platform/posix/stacktrace.cc @@ -3,10 +3,44 @@ #include "core/common/common.h" +#include +#include + namespace onnxruntime { std::vector GetStackTrace() { - return {""}; -} + std::vector stack; +#ifndef NDEBUG + constexpr int kCallstackLimit = 64; // Maximum depth of callstack + + void* array[kCallstackLimit]; + char** strings = nullptr; + + size_t size = backtrace(array, kCallstackLimit); + stack.reserve(size); + strings = backtrace_symbols(array, size); + + // NOTE: To get meaningful info from the output, addr2line (or atos on osx) would need to be used. + // See https://gist.github.com/jvranish/4441299 for an example. + // + // To manually translate the output, use the value in the '()' after the executable name with addr2line + // e.g. + // Stacktrace: + // /home/me/src/github/onnxruntime/build/Linux/Debug/onnxruntime_test_all(+0x3f46cc) [0x559543faf6cc] + // + // >addr2line -f -C -e /home/me/src/github/onnxruntime/build/Linux/Debug/onnxruntime_test_all +0x3f46cc + + // hide GetStackTrace so the output starts with the 'real' location + constexpr size_t start_frame = 1; + for (size_t i = start_frame; i < size; i++) { + stack.push_back(strings[i]); + } + + free(strings); + +#endif + + return stack; +} } // namespace onnxruntime