gdb special command to print tensors (#54339)

Summary:
This is something which I wrote because it was useful during my debugging sessions, but I think it might be generally useful to other people as well so I took the liberty of proposing an official `pytorch-gdb` extension.

`pytorch-gdb` is a gdb script written in python. Currently, it contains only one command: `torch-tensor-repr`, which prints a human-readable repr of an `at::Tensor` object. Example:
```
Breakpoint 1, at::native::neg (self=...) at [...]/pytorch/aten/src/ATen/native/UnaryOps.cpp:520
520     Tensor neg(const Tensor& self) { return unary_op_impl(self, at::neg_out); }
(gdb) # the default repr of 'self' is not very useful
(gdb) p self
$1 = (const at::Tensor &) 0x7ffff72ed780: {impl_ = {target_ = 0x5555559df6e0}}
(gdb) torch-tensor-repr self
Python-level repr of self:
tensor([1., 2., 3., 4.], dtype=torch.float64)
```

The idea is that by having an official place where to put these things, `pytorch-gdb` will slowly grow other useful features and make the pytorch debugging experience nicer and faster.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/54339

Reviewed By: bdhirsh

Differential Revision: D27253674

Pulled By: ezyang

fbshipit-source-id: dba219e126cc2fe66b2d26740f3a8e3b886e56f5
This commit is contained in:
Antonio Cuni
2021-03-23 12:27:51 -07:00
committed by Facebook GitHub Bot
parent 583c4bf7d3
commit 21a9a93eb4
4 changed files with 185 additions and 0 deletions

52
tools/gdb/pytorch-gdb.py Normal file
View File

@ -0,0 +1,52 @@
import gdb
import textwrap
class DisableBreakpoints:
"""
Context-manager to temporarily disable all gdb breakpoints, useful if
there is a risk to hit one during the evaluation of one of our custom
commands
"""
def __enter__(self):
self.disabled_breakpoints = []
for b in gdb.breakpoints():
if b.enabled:
b.enabled = False
self.disabled_breakpoints.append(b)
def __exit__(self, etype, evalue, tb):
for b in self.disabled_breakpoints:
b.enabled = True
class TensorRepr(gdb.Command):
"""
Print a human readable representation of the given at::Tensor.
Usage: torch-tensor-repr EXP
at::Tensor instances do not have a C++ implementation of a repr method: in
pytoch, this is done by pure-Python code. As such, torch-tensor-repr
internally creates a Python wrapper for the given tensor and call repr()
on it.
"""
__doc__ = textwrap.dedent(__doc__).strip()
def __init__(self):
gdb.Command.__init__(self, 'torch-tensor-repr',
gdb.COMMAND_USER, gdb.COMPLETE_EXPRESSION)
def invoke(self, args, from_tty):
args = gdb.string_to_argv(args)
if len(args) != 1:
print('Usage: torch-tensor-repr EXP')
return
name = args[0]
with DisableBreakpoints():
res = gdb.parse_and_eval('torch::gdb::tensor_repr(%s)' % name)
print('Python-level repr of %s:' % name)
print(res.string())
# torch::gdb::tensor_repr returns a malloc()ed buffer, let's free it
gdb.parse_and_eval('(void)free(%s)' % int(res))
TensorRepr()