Revert "Improve and expose cpp_backtrace to python binding (#84896)"

This reverts commit 73fbca1ea6ecc08ae4455a12b68fc2ead93a088c.

Reverted https://github.com/pytorch/pytorch/pull/84896 on behalf of https://github.com/kit1980 due to Broke libtorch and linux-binary-manywheel - 73fbca1ea6
This commit is contained in:
PyTorch MergeBot
2022-09-21 03:13:20 +00:00
parent 9fdd8a8b7f
commit 3122a96ee4
6 changed files with 7 additions and 121 deletions

View File

@ -924,7 +924,6 @@ add-auto-load-safe-path /path/to/pytorch/.gdbinit
### C++ stacktraces ### C++ stacktraces
Set `TORCH_SHOW_CPP_STACKTRACES=1` to get the C++ stacktrace when an error occurs in Python. Set `TORCH_SHOW_CPP_STACKTRACES=1` to get the C++ stacktrace when an error occurs in Python.
Set `TORCH_SHOW_CPP_STACKTRACES_WITH_LINENO=1` to get the C++ stacktrace with file and line number.
## CUDA development tips ## CUDA development tips

View File

@ -1,7 +1,6 @@
#include <c10/util/Backtrace.h> #include <c10/util/Backtrace.h>
#include <c10/util/Optional.h> #include <c10/util/Optional.h>
#include <c10/util/Type.h> #include <c10/util/Type.h>
#include <c10/util/env.h>
#include <c10/util/irange.h> #include <c10/util/irange.h>
#include <functional> #include <functional>
@ -22,14 +21,7 @@
#include <dlfcn.h> #include <dlfcn.h>
#include <unwind.h> #include <unwind.h>
#else #else
#include <dlfcn.h>
#include <execinfo.h> #include <execinfo.h>
#ifndef __APPLE__
// link.h is not available on IOS and Mac builds
#include <link.h>
#endif
#endif #endif
#endif #endif
@ -95,46 +87,6 @@ void dump_stack(
#if SUPPORTS_BACKTRACE #if SUPPORTS_BACKTRACE
namespace { namespace {
#if !defined(C10_ANDROID) && !defined(__APPLE__)
// converts a function's address in memory to its VMA address in the executable
// file. VMA is what addr2line expects
size_t ConvertToVMA(size_t addr) {
Dl_info info;
link_map* link_map;
dladdr1((void*)addr, &info, (void**)&link_map, RTLD_DL_LINKMAP);
return addr - link_map->l_addr;
}
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
std::string rstrip(const std::string& s) {
const std::string WHITESPACE = " \n\r\t\f\v";
size_t end = s.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
bool use_addr2line() {
static bool _use_addr2line = []() {
return c10::utils::check_env("TORCH_SHOW_CPP_STACKTRACES_WITH_LINENO") ==
true;
}();
return _use_addr2line;
}
#endif // !defined(C10_ANDROID) && !defined(__APPLE__)
struct FrameInformation { struct FrameInformation {
/// If available, the demangled name of the function at this frame, else /// If available, the demangled name of the function at this frame, else
/// whatever (possibly mangled) name we got from `backtrace()`. /// whatever (possibly mangled) name we got from `backtrace()`.
@ -147,10 +99,6 @@ struct FrameInformation {
/// NOTE: In debugger parlance, the "object file" refers to the ELF file that /// NOTE: In debugger parlance, the "object file" refers to the ELF file that
/// the symbol originates from, i.e. either an executable or a library. /// the symbol originates from, i.e. either an executable or a library.
std::string object_file; std::string object_file;
/// Source file name and line number
std::string source_file_lineno;
bool is_python_frame;
}; };
#ifndef C10_ANDROID #ifndef C10_ANDROID
@ -160,8 +108,7 @@ bool is_python_frame(const FrameInformation& frame) {
} }
c10::optional<FrameInformation> parse_frame_information( c10::optional<FrameInformation> parse_frame_information(
const std::string& frame_string, const std::string& frame_string) {
void* frame_pointer) {
FrameInformation frame; FrameInformation frame;
// This is the function name in the CXX ABI mangled format, e.g. something // This is the function name in the CXX ABI mangled format, e.g. something
@ -194,7 +141,6 @@ c10::optional<FrameInformation> parse_frame_information(
frame.object_file = frame_string.substr(0, function_name_start - 1); frame.object_file = frame_string.substr(0, function_name_start - 1);
frame.offset_into_function = frame.offset_into_function =
frame_string.substr(offset_start, offset_end - offset_start); frame_string.substr(offset_start, offset_end - offset_start);
frame.is_python_frame = is_python_frame(frame);
// NOTE: We don't need to parse the return address because // NOTE: We don't need to parse the return address because
// we already have it from the call to `backtrace()`. // we already have it from the call to `backtrace()`.
@ -225,30 +171,6 @@ c10::optional<FrameInformation> parse_frame_information(
} }
frame.function_name = demangle(mangled_function_name.c_str()); frame.function_name = demangle(mangled_function_name.c_str());
#if !defined(__APPLE__)
if (use_addr2line() && !frame.is_python_frame) {
Dl_info info;
if (dladdr(frame_pointer, &info)) {
char command[256];
size_t VMA_addr = ConvertToVMA((size_t)frame_pointer);
// Need to decrease the VMA address by 1 to get the correct line number
// https://stackoverflow.com/questions/11579509/wrong-line-numbers-from-addr2line/63841497#63841497
VMA_addr -= 1;
snprintf(
command,
sizeof(command),
"addr2line -e %s -C %zx",
info.dli_fname,
VMA_addr);
frame.source_file_lineno = rstrip(exec(command));
}
}
#endif // !defined(__APPLE__)
return frame; return frame;
} }
#endif /* !defined(C10_ANDROID) */ #endif /* !defined(C10_ANDROID) */
@ -361,10 +283,9 @@ std::string get_backtrace(
bool has_skipped_python_frames = false; bool has_skipped_python_frames = false;
for (const auto frame_number : c10::irange(callstack.size())) { for (const auto frame_number : c10::irange(callstack.size())) {
const auto frame = const auto frame = parse_frame_information(symbols[frame_number]);
parse_frame_information(symbols[frame_number], callstack[frame_number]);
if (skip_python_frames && frame && frame->is_python_frame) { if (skip_python_frames && frame && is_python_frame(*frame)) {
if (!has_skipped_python_frames) { if (!has_skipped_python_frames) {
stream << "<omitting python frames>\n"; stream << "<omitting python frames>\n";
has_skipped_python_frames = true; has_skipped_python_frames = true;
@ -376,17 +297,10 @@ std::string get_backtrace(
stream << "frame #" << frame_number << ": "; stream << "frame #" << frame_number << ": ";
if (frame) { if (frame) {
if (frame->source_file_lineno.empty()) { // <function_name> + <offset> (<return-address> in <object-file>)
// <function_name> + <offset> (<return-address> in <object-file>) stream << frame->function_name << " + " << frame->offset_into_function
stream << frame->function_name << " + " << frame->offset_into_function << " (" << callstack[frame_number] << " in " << frame->object_file
<< " (" << callstack[frame_number] << " in " << ")\n";
<< frame->object_file << ")\n";
} else {
// <function_name> (<return-address> in <filename>:<line-number>)
stream << frame->function_name << " (" << callstack[frame_number]
<< " in " << frame->source_file_lineno << ")\n";
}
} else { } else {
// In the edge-case where we couldn't parse the frame string, we can // In the edge-case where we couldn't parse the frame string, we can
// just use it directly (it may have a different format). // just use it directly (it may have a different format).

View File

@ -843,7 +843,6 @@ def _remove_meta_from_tls_dispatch_include() -> None: ...
# https://code.activestate.com/lists/python-dev/139675/ # https://code.activestate.com/lists/python-dev/139675/
def _to_dlpack(data: Tensor) -> Any: ... # THPModule_toDLPack def _to_dlpack(data: Tensor) -> Any: ... # THPModule_toDLPack
def _from_dlpack(data: Any) -> Tensor: ... # THPModule_fromDLPack def _from_dlpack(data: Any) -> Tensor: ... # THPModule_fromDLPack
def _get_cpp_backtrace(frames_to_skip: _int, maximum_number_of_frames: _int) -> str: ... # THPModule_getCppBacktrace
def set_flush_denormal(arg: _bool) -> _bool: ... # THPModule_setFlushDenormal def set_flush_denormal(arg: _bool) -> _bool: ... # THPModule_setFlushDenormal
def get_default_dtype() -> _dtype: ... # THPModule_getDefaultDtype def get_default_dtype() -> _dtype: ... # THPModule_getDefaultDtype
def _get_default_device() -> str: ... # THPModule_getDefaultDevice def _get_default_device() -> str: ... # THPModule_getDefaultDevice

View File

@ -425,19 +425,6 @@ PyObject* THPModule_fromDLPack(PyObject* _unused, PyObject* data) {
END_HANDLE_TH_ERRORS END_HANDLE_TH_ERRORS
} }
PyObject* THModule_getCppBacktrace(PyObject* _unused, PyObject* args) {
HANDLE_TH_ERRORS
size_t frames_to_skip;
size_t maximum_number_of_frames;
if (!PyArg_ParseTuple(
args, "LL", &frames_to_skip, &maximum_number_of_frames)) {
return nullptr;
}
return THPUtils_packString(
c10::get_backtrace(frames_to_skip, maximum_number_of_frames, true));
END_HANDLE_TH_ERRORS
}
PyObject* THPModule_setAllowTF32CuDNN(PyObject* _unused, PyObject* arg) { PyObject* THPModule_setAllowTF32CuDNN(PyObject* _unused, PyObject* arg) {
THPUtils_assert( THPUtils_assert(
PyBool_Check(arg), PyBool_Check(arg),
@ -879,7 +866,6 @@ static PyMethodDef TorchMethods[] = {
nullptr}, nullptr},
{"_to_dlpack", THPModule_toDLPack, METH_O, nullptr}, {"_to_dlpack", THPModule_toDLPack, METH_O, nullptr},
{"_from_dlpack", THPModule_fromDLPack, METH_O, nullptr}, {"_from_dlpack", THPModule_fromDLPack, METH_O, nullptr},
{"_get_cpp_backtrace", THModule_getCppBacktrace, METH_VARARGS, nullptr},
{"set_flush_denormal", THPModule_setFlushDenormal, METH_O, nullptr}, {"set_flush_denormal", THPModule_setFlushDenormal, METH_O, nullptr},
{"get_default_dtype", THPModule_getDefaultDtype, METH_NOARGS, nullptr}, {"get_default_dtype", THPModule_getDefaultDtype, METH_NOARGS, nullptr},
{"_get_default_device", THPModule_getDefaultDevice, METH_NOARGS, nullptr}, {"_get_default_device", THPModule_getDefaultDevice, METH_NOARGS, nullptr},

View File

@ -3,7 +3,6 @@ import sys
from .throughput_benchmark import ThroughputBenchmark from .throughput_benchmark import ThroughputBenchmark
from ._crash_handler import enable_minidumps, disable_minidumps, enable_minidumps_on_exceptions from ._crash_handler import enable_minidumps, disable_minidumps, enable_minidumps_on_exceptions
from .cpp_backtrace import get_cpp_backtrace
# Set the module for a given object for nicer printing # Set the module for a given object for nicer printing
def set_module(obj, mod): def set_module(obj, mod):

View File

@ -1,11 +0,0 @@
from torch._C import _get_cpp_backtrace
def get_cpp_backtrace(frames_to_skip=0, maximum_number_of_frames=64) -> str:
r"""
Returns a string containing the C++ stack trace of the current thread.
Args:
frames_to_skip (int): the number of frames to skip from the top of the stack
maximum_number_of_frames (int): the maximum number of frames to return
"""
return _get_cpp_backtrace(frames_to_skip, maximum_number_of_frames)