Skip to content

Commit 5b326d6

Browse files
zeshengzongpytorchmergebot
authored andcommitted
Add gdb print methods support same as pytorch-lldb (pytorch#140935)
`pytorch-lldb` support pretty printing size and key_set of tensor via pytorch#97101 Add same pretty printing for gdb debugging. **Test Result** ```bash $ gdb python (gdb) break at::native::negative (gdb) r >>> import torch >>> t = torch.tensor([1, 2, 3, 4], dtype=torch.float64) >>> t.negative() Thread 1 "python" hit Breakpoint 1, at::native::negative (self=...) at /home/zong/code/pytorch/aten/src/ATen/native/UnaryOps.cpp:854 854 Tensor negative(const Tensor& self) { return self.neg(); } ``` **Before** ```bash (gdb) p self.key_set() $2 = {repr_ = 1271310352385} (gdb) p self.sizes() $3 = {Data = 0x9cb488, Length = 1} ``` **After** ```bash (gdb) torch-int-array-ref-repr self.sizes() [4] (gdb) torch-dispatch-keyset-repr self.key_set() DispatchKeySet(CPU, ADInplaceOrView, AutogradCPU, AutocastCPU) ``` ```bash $ lintrunner ``` ![image](https://github.com/user-attachments/assets/b720e284-13b1-4581-ae3a-963f6482fdb2) Pull Request resolved: pytorch#140935 Approved by: https://github.com/drisspg
1 parent 98e6e69 commit 5b326d6

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tools/gdb/pytorch-gdb.py

+49
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,53 @@ def invoke(self, args: str, from_tty: bool) -> None:
5555
gdb.parse_and_eval(f"(void)free({int(res)})")
5656

5757

58+
class IntArrayRefRepr(gdb.Command): # type: ignore[misc, no-any-unimported]
59+
"""
60+
Print human readable representation of c10::IntArrayRef
61+
"""
62+
63+
def __init__(self) -> None:
64+
gdb.Command.__init__(
65+
self, "torch-int-array-ref-repr", gdb.COMMAND_USER, gdb.COMPLETE_EXPRESSION
66+
)
67+
68+
def invoke(self, args: str, from_tty: bool) -> None:
69+
args = gdb.string_to_argv(args)
70+
if len(args) != 1:
71+
print("Usage: torch-int-array-ref-repr EXP")
72+
return
73+
name = args[0]
74+
with DisableBreakpoints():
75+
res = gdb.parse_and_eval(f"torch::gdb::int_array_ref_string({name})")
76+
res = str(res)
77+
print(res[res.find('"') + 1 : -1])
78+
79+
80+
class DispatchKeysetRepr(gdb.Command): # type: ignore[misc, no-any-unimported]
81+
"""
82+
Print human readable representation of c10::DispatchKeyset
83+
"""
84+
85+
def __init__(self) -> None:
86+
gdb.Command.__init__(
87+
self,
88+
"torch-dispatch-keyset-repr",
89+
gdb.COMMAND_USER,
90+
gdb.COMPLETE_EXPRESSION,
91+
)
92+
93+
def invoke(self, args: str, from_tty: bool) -> None:
94+
args = gdb.string_to_argv(args)
95+
if len(args) != 1:
96+
print("Usage: torch-dispatch-keyset-repr EXP")
97+
return
98+
keyset = args[0]
99+
with DisableBreakpoints():
100+
res = gdb.parse_and_eval(f"torch::gdb::dispatch_keyset_string({keyset})")
101+
res = str(res)
102+
print(res[res.find('"') + 1 : -1])
103+
104+
58105
TensorRepr()
106+
IntArrayRefRepr()
107+
DispatchKeysetRepr()

0 commit comments

Comments
 (0)