Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 21a9a93

Browse files
antocunifacebook-github-bot
authored andcommittedMar 23, 2021
gdb special command to print tensors (pytorch#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: pytorch#54339 Reviewed By: bdhirsh Differential Revision: D27253674 Pulled By: ezyang fbshipit-source-id: dba219e126cc2fe66b2d26740f3a8e3b886e56f5
1 parent 583c4bf commit 21a9a93

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed
 

‎.gdbinit

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# automatically load the pytoch-gdb extension.
2+
#
3+
# gdb automatically tries to load this file whenever it is executed from the
4+
# root of the pytorch repo, but by default it is not allowed to do so due to
5+
# security reasons. If you want to use pytorch-gdb, please add the following
6+
# line to your ~/.gdbinit (i.e., the .gdbinit file which is in your home
7+
# directory, NOT this file):
8+
# add-auto-load-safe-path /path/to/pytorch/.gdbinit
9+
#
10+
# Alternatively, you can manually load the pytorch-gdb commands into your
11+
# existing gdb session by doing the following:
12+
# (gdb) source /path/to/pytorch/tools/gdb/pytorch-gdb.py
13+
14+
source tools/gdb/pytorch-gdb.py

‎CONTRIBUTING.md

+63
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- [Use CCache](#use-ccache)
2727
- [Use a faster linker](#use-a-faster-linker)
2828
- [C++ frontend development tips](#c-frontend-development-tips)
29+
- [GDB integration](#gdb-integration)
2930
- [CUDA development tips](#cuda-development-tips)
3031
- [Windows development tips](#windows-development-tips)
3132
- [Known MSVC (and MSVC with NVCC) bugs](#known-msvc-and-msvc-with-nvcc-bugs)
@@ -735,6 +736,68 @@ framework, which you can read up about to learn how to configure the test runner
735736
submitting a new feature, we care very much that you write appropriate tests.
736737
Please follow the lead of the other tests to see how to write a new test case.
737738

739+
### GDB integration
740+
741+
If you are debugging pytorch inside GDB, you might be interested in
742+
[pytorch-gdb](tools/gdb/pytorch-gdb.py). This script introduces some
743+
pytorch-specific commands which you can use from the GDB prompt. In
744+
particular, `torch-tensor-repr` prints a human-readable repr of an at::Tensor
745+
object. Example of usage:
746+
747+
```
748+
$ gdb python
749+
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
750+
[...]
751+
(gdb) # insert a breakpoint when we call .neg()
752+
(gdb) break at::native:neg
753+
No source file named at::native.
754+
Make breakpoint pending on future shared library load? (y or [n]) y
755+
Breakpoint 1 (at::native:neg) pending.
756+
757+
(gdb) run
758+
[...]
759+
>>> import torch
760+
>>> t = torch.tensor([1, 2, 3, 4], dtype=torch.float64)
761+
>>> t
762+
tensor([1., 2., 3., 4.], dtype=torch.float64)
763+
>>> t.neg()
764+
765+
Breakpoint 1, at::native::neg (self=...) at [...]/pytorch/aten/src/ATen/native/UnaryOps.cpp:520
766+
520 Tensor neg(const Tensor& self) { return unary_op_impl(self, at::neg_out); }
767+
(gdb) # the default repr of 'self' is not very useful
768+
(gdb) p self
769+
$1 = (const at::Tensor &) @0x7ffff72ed780: {impl_ = {target_ = 0x5555559df6e0}}
770+
(gdb) torch-tensor-repr self
771+
Python-level repr of self:
772+
tensor([1., 2., 3., 4.], dtype=torch.float64)
773+
```
774+
775+
GDB tries to automatically load `pytorch-gdb` thanks to the
776+
[.gdbinit](.gdbinit) at the root of the pytorch repo. Howevever, auto-loadings is disabled by default, because of security reasons:
777+
778+
```
779+
$ gdb
780+
warning: File "/path/to/pytorch/.gdbinit" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
781+
To enable execution of this file add
782+
add-auto-load-safe-path /path/to/pytorch/.gdbinit
783+
line to your configuration file "/home/YOUR-USERNAME/.gdbinit".
784+
To completely disable this security protection add
785+
set auto-load safe-path /
786+
line to your configuration file "/home/YOUR-USERNAME/.gdbinit".
787+
For more information about this security protection see the
788+
"Auto-loading safe path" section in the GDB manual. E.g., run from the shell:
789+
info "(gdb)Auto-loading safe path"
790+
(gdb)
791+
```
792+
793+
As gdb itself suggests, the best way to enable auto-loading of `pytorch-gdb`
794+
is to add the following line to your `~/.gdbinit` (i.e., the `.gdbinit` file
795+
which is in your home directory, **not** `/path/to/pytorch/.gdbinit`):
796+
```
797+
add-auto-load-safe-path /path/to/pytorch/.gdbinit
798+
```
799+
800+
738801
## CUDA development tips
739802

740803
If you are working on the CUDA code, here are some useful CUDA debugging tips:

‎tools/gdb/pytorch-gdb.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import gdb
2+
import textwrap
3+
4+
class DisableBreakpoints:
5+
"""
6+
Context-manager to temporarily disable all gdb breakpoints, useful if
7+
there is a risk to hit one during the evaluation of one of our custom
8+
commands
9+
"""
10+
11+
def __enter__(self):
12+
self.disabled_breakpoints = []
13+
for b in gdb.breakpoints():
14+
if b.enabled:
15+
b.enabled = False
16+
self.disabled_breakpoints.append(b)
17+
18+
def __exit__(self, etype, evalue, tb):
19+
for b in self.disabled_breakpoints:
20+
b.enabled = True
21+
22+
class TensorRepr(gdb.Command):
23+
"""
24+
Print a human readable representation of the given at::Tensor.
25+
Usage: torch-tensor-repr EXP
26+
27+
at::Tensor instances do not have a C++ implementation of a repr method: in
28+
pytoch, this is done by pure-Python code. As such, torch-tensor-repr
29+
internally creates a Python wrapper for the given tensor and call repr()
30+
on it.
31+
"""
32+
__doc__ = textwrap.dedent(__doc__).strip()
33+
34+
def __init__(self):
35+
gdb.Command.__init__(self, 'torch-tensor-repr',
36+
gdb.COMMAND_USER, gdb.COMPLETE_EXPRESSION)
37+
38+
def invoke(self, args, from_tty):
39+
args = gdb.string_to_argv(args)
40+
if len(args) != 1:
41+
print('Usage: torch-tensor-repr EXP')
42+
return
43+
name = args[0]
44+
with DisableBreakpoints():
45+
res = gdb.parse_and_eval('torch::gdb::tensor_repr(%s)' % name)
46+
print('Python-level repr of %s:' % name)
47+
print(res.string())
48+
# torch::gdb::tensor_repr returns a malloc()ed buffer, let's free it
49+
gdb.parse_and_eval('(void)free(%s)' % int(res))
50+
51+
TensorRepr()
52+

‎torch/csrc/utils.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,59 @@ void THPPointer<THPStorage>::free() {
252252
}
253253

254254
template class THPPointer<THPStorage>;
255+
256+
namespace torch { namespace gdb {
257+
/* ~~~ misc debugging utilities ~~~
258+
*
259+
* torch::gdb::* functions are NOT meant to be called by general pytorch code,
260+
* but only from within a gdb session. As such, utils.h does not contain any
261+
* declaration for those.
262+
*/
263+
264+
// This is a helper needed by the torch-tensor-repr gdb command.
265+
// Return an human-readable representation of the given Tensor. The resulting
266+
// string is stored into a malloc()ed buffer. The caller is responsible to
267+
// free() it. We use malloc() instead of new[] because it's much easier to
268+
// call free than delete[] from withing gdb.
269+
// Currently the code for computing the repr of a tensor is written in Python,
270+
// so we need to wrap the Tensor into a Python object first.
271+
char *tensor_repr(at::Tensor tensor) {
272+
PyGILState_STATE gil = PyGILState_Ensure();
273+
PyObject *pytensor = NULL;
274+
PyObject *repr = NULL;
275+
Py_ssize_t bufsize;
276+
const char *buf = NULL;
277+
char *result = NULL;
278+
279+
pytensor = THPVariable_Wrap(at::Tensor(tensor));
280+
if (!pytensor)
281+
goto error;
282+
repr = PyObject_Repr(pytensor);
283+
if (!repr)
284+
goto error;
285+
buf = PyUnicode_AsUTF8AndSize(repr, &bufsize);
286+
if (!buf)
287+
goto error;
288+
result = static_cast<char*>(malloc(bufsize + 1)); // account for the trailing \0
289+
if (!result) {
290+
fprintf(stderr, "cannot allocate memory for the result\n");
291+
goto error;
292+
}
293+
strcpy(result, buf);
294+
Py_XDECREF(pytensor);
295+
Py_XDECREF(repr);
296+
PyGILState_Release(gil);
297+
return result;
298+
299+
error:
300+
fprintf(stderr, "torch::gdb::tensor_repr: unexpected error\n");
301+
if (PyErr_Occurred())
302+
PyErr_Print();
303+
Py_XDECREF(pytensor);
304+
Py_XDECREF(repr);
305+
free(result);
306+
PyGILState_Release(gil);
307+
return NULL;
308+
}
309+
310+
}} // namespace torch::gdb

0 commit comments

Comments
 (0)
Please sign in to comment.