Files
pytorch/torch/csrc/utils/crash_handler.cpp
davidriazati@fb.com 638617f9f8 Write mini dump on pybind exceptions (#55652)
Summary:
We register an [error handler](https://pybind11.readthedocs.io/en/stable/advanced/exceptions.html#registering-custom-translators) with pybind so that C++ exceptions are passed to Python and raised as runtime errors that can be `try...except`ed etc. Since these don't terminate the program (until Python does), they never fire the signal handler to write a minidump out with the crash information. This PR adds some logic in the exception translator to write out a minidump if enabled.
](https://our.intern.facebook.com/intern/diff/27830952/)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/55652

Pulled By: driazati

Reviewed By: bertmaher

Differential Revision: D27830952

fbshipit-source-id: 26e8f913e99dff971a4eb09eb87221c66f759763
2021-04-19 14:53:43 -07:00

84 lines
2.0 KiB
C++

#include <iostream>
#ifdef ADD_BREAKPAD_SIGNAL_HANDLER
#include <breakpad/client/linux/handler/exception_handler.h>
#endif
#include <c10/util/Exception.h>
#include <torch/csrc/utils/crash_handler.h>
namespace torch {
namespace crash_handler {
#ifdef ADD_BREAKPAD_SIGNAL_HANDLER
bool dumpCallback(
const google_breakpad::MinidumpDescriptor& descriptor,
void* context,
bool succeeded) {
if (succeeded) {
std::cerr << "Wrote minidump to " << descriptor.path() << std::endl;
}
return succeeded;
}
static std::unique_ptr<google_breakpad::ExceptionHandler> handler; // NOLINT
static std::string minidump_directory; // NOLINT
void _enable_minidump_collection(const std::string& dir) {
minidump_directory = dir;
handler = std::make_unique<google_breakpad::ExceptionHandler>(
google_breakpad::MinidumpDescriptor(minidump_directory),
nullptr,
dumpCallback,
nullptr,
true,
-1);
}
void _disable_minidump_collection() {
handler.reset();
}
const std::string& _get_minidump_directory() {
if (handler == nullptr) {
AT_ERROR(
"Minidump handler is uninintialized, make sure to call _enable_minidump_collection first");
}
return minidump_directory;
}
bool is_enabled() {
return handler != nullptr;
}
void write_minidump() {
TORCH_CHECK(handler != nullptr,"Minidump handler is uninintialized, make sure to call _enable_minidump_collection first");
handler->WriteMinidump();
}
#else
void _enable_minidump_collection(const std::string& dir) {
AT_ERROR(
"Minidump collection is currently only implemented for Linux platforms");
}
void _disable_minidump_collection() {
// Purposefully do nothing
}
const std::string& _get_minidump_directory() {
AT_ERROR(
"Minidump collection is currently only implemented for Linux platforms");
}
bool is_enabled() {
return false;
}
void write_minidump() {
AT_ERROR(
"Minidump collection is currently only implemented for Linux platforms");
}
#endif
} // namespace crash_handler
} // namespace torch