Combine backtrace print into one string to avoid interleaving. (#56961)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/56961

As described in https://github.com/pytorch/pytorch/issues/56583, the
backtrace amongst several processes was garbled.
https://github.com/pytorch/pytorch/pull/56198 would've alleviated this to some
extent, but this PR combines all the logging into just one string to reduce
interleaving further.
ghstack-source-id: 128730047

Test Plan: waitforbuildbot

Reviewed By: cbalioglu

Differential Revision: D28013191

fbshipit-source-id: 8bd8978a92ee2fbcd18472e1293d5809455b411b
This commit is contained in:
Pritam Damania
2021-05-12 15:50:18 -07:00
committed by Facebook GitHub Bot
parent d09abf004c
commit d230045fde
3 changed files with 11 additions and 4 deletions

View File

@ -83,6 +83,7 @@ cc_library(
copts = ["-DCAFFE2_BUILD_MAIN_LIB"],
deps = [
":c10_headers",
"@fmt",
] + if_cuda(
["@cuda"],
[],

View File

@ -59,6 +59,7 @@ endif()
if(${USE_GLOG})
target_link_libraries(c10 PUBLIC glog::glog)
endif()
target_link_libraries(c10 PRIVATE fmt::fmt-header-only)
find_package(Backtrace)
if(Backtrace_FOUND)

View File

@ -1,5 +1,6 @@
#include <c10/util/Backtrace.h>
#include <c10/util/signal_handler.h>
#include <fmt/format.h>
#if defined(C10_SUPPORTS_SIGNAL_HANDLER)
@ -162,10 +163,14 @@ void FatalSignalHandler::stacktraceSignalHandler(bool needsLock) {
pthread_mutex_lock(&writingMutex);
}
pid_t tid = syscall(SYS_gettid);
std::cerr << fatalSignalName << "(" << fatalSignum << "), PID: " << ::getpid()
<< ", Thread " << tid << ": " << std::endl;
std::cerr << c10::get_backtrace();
std::cerr << std::endl;
std::string backtrace = fmt::format(
"{}({}), PID: {}, Thread {}: \n {}",
fatalSignalName,
fatalSignum,
::getpid(),
tid,
c10::get_backtrace());
std::cerr << backtrace << std::endl;
if (needsLock) {
pthread_mutex_unlock(&writingMutex);
pthread_cond_signal(&writingCond);