Repository: pytorch
Issue: Cannot formatted print at::Tensor in gdb
Labels: module: cpp,triaged

### 🐛 Describe the bug

When debugging PyTorch inside gdb, I sourced `tools/gdb/pytorch-gdb.py` to print `at::Tensor` prettily. However, `gdb` shows `Couldn't find method at::Tensor::at::Tensor` unexpectedly. 

## Reproduce
1. First compile PyTorch in development mode.
```bash
USE_CUDA=0 DEBUG=1 USE_ROCM=0 CMAKE_PREFIX_PATH=/path/to/mambaforge/envs/pytorch-dev/ python setup.py --cmake develop 
```
2. Create a python file `a.py` for debug.
```python
import torch            
                        
a = torch.tensor([1, 1])
b = torch.tensor([1, 1])
c = torch.add(a, b)     
```
3. Open gdb and run
```bash
~/tmp cse_yuanx@login02
09:23:18 pytorch-dev ❯ gdb python                                (pytorch-dev)
GNU gdb (GDB) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-conda-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from python...
(gdb) break structured_ufunc_add_CPU::impl
Function "structured_ufunc_add_CPU::impl" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (structured_ufunc_add_CPU::impl) pending.
(gdb) run a.py
Starting program: /home/zhengf_lab/cse_yuanx/mambaforge/envs/pytorch-dev/bin/python a.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
warning: File "/opt/ohpc/pub/compiler/gcc/9.4.0/lib64/libstdc++.so.6.0.28-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
        add-auto-load-safe-path /opt/ohpc/pub/compiler/gcc/9.4.0/lib64/libstdc++.so.6.0.28-gdb.py
line to your configuration file "/home/zhengf_lab/cse_yuanx/.config/gdb/gdbinit".
To completely disable this security protection add
        set auto-load safe-path /
line to your configuration file "/home/zhengf_lab/cse_yuanx/.config/gdb/gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
        info "(gdb)Auto-loading safe path"
[New Thread 0x7fffc2b35700 (LWP 4044363)]
[New Thread 0x7fffc2334700 (LWP 4044364)]
[New Thread 0x7fffbfb33700 (LWP 4044365)]
[New Thread 0x7fffbb332700 (LWP 4044366)]
[New Thread 0x7fffbab31700 (LWP 4044367)]
[New Thread 0x7fffb8330700 (LWP 4044368)]
[New Thread 0x7fffb5b2f700 (LWP 4044369)]
[New Thread 0x7fffb332e700 (LWP 4044370)]
[New Thread 0x7fffb0b2d700 (LWP 4044371)]
[New Thread 0x7fffac32c700 (LWP 4044372)]
[New Thread 0x7fffa9b2b700 (LWP 4044373)]
[New Thread 0x7fffa732a700 (LWP 4044374)]
[New Thread 0x7fffa6b29700 (LWP 4044375)]
[New Thread 0x7fffa2328700 (LWP 4044376)]
[New Thread 0x7fff9fb27700 (LWP 4044377)]

Thread 1 "python" hit Breakpoint 1, 0x00007fffc9502b30 in at::native::structured_ufunc_add_CPU::impl(at::Tensor const&, at::Tensor const&, c10::Scalar const&, at::Tensor const&)@plt () from /home/zhengf_lab/cse_yuanx/codes/pytorch/torch/lib/libtorch_cpu.so
(gdb) n
Single stepping until exit from function _ZN2at6native24structured_ufunc_add_CPU4implERKNS_6TensorES4_RKN3c106ScalarES4_@plt,
which has no line number information.
at::native::structured_ufunc_add_CPU::impl (this=0x7fffffffb310, self=..., other=..., alpha=..., out=...) at /home/zhengf_lab/cse_yuanx/codes/pytorch/build/aten/src/ATen/UfuncCPU_add.cpp:29
29      TORCH_IMPL_FUNC(ufunc_add_CPU)(const at::Tensor & self, const at::Tensor & other, const at::Scalar & alpha, const at::Tensor & out) {
(gdb) l
24
25      using add_fn = void(*)(TensorIteratorBase&, const at::Scalar &);
26      DECLARE_DISPATCH(add_fn, add_stub);
27      DEFINE_DISPATCH(add_stub);
28
29      TORCH_IMPL_FUNC(ufunc_add_CPU)(const at::Tensor & self, const at::Tensor & other, const at::Scalar & alpha, const at::Tensor & out) {
30        add_stub(device_type(), *this, alpha);
31      }
32      }} // namespace at::native
(gdb) p self

    $1 = (const at::Tensor &) <error reading variable: Cannot access memory at address 0x0>
(gdb) p torch::gdb::tensor_repr(self)
Couldn't find method at::Tensor::at::Tensor
(gdb) source ~/codes/pytorch/tools/gdb/pytorch-gdb.py
(gdb) torch-tensor-repr self
Python Exception <class 'gdb.error'>: Couldn't find method at::Tensor::at::Tensor
Error occurred in Python: Couldn't find method at::Tensor::at::Tensor
(gdb)
```
I tried in gdb by directly invoking `torch::gdb::tensor_repr` and indirectly call `torch-tensor-repr`. But they both throw an error. I also tried in lldb but the same situation is encountered as well.

The expected output should be almost the same as what we would see when printing a Tensor in Python.



### Versions

```bash
Collecting environment information...
PyTorch version: 2.3.0a0+git42062e2
Is debug build: True
CUDA used to build PyTorch: Could not collect
ROCM used to build PyTorch: N/A

OS: Rocky Linux 8.5 (Green Obsidian) (x86_64)
GCC version: (GCC) 9.4.0
Clang version: 18.0.0git (https://github.com/llvm/llvm-project.git a762cc21556bbd90ae7d9ee13c33213501195f64)
CMake version: version 3.26.4
Libc version: glibc-2.28

Python version: 3.10.13 | packaged by conda-forge | (main, Oct 26 2023, 18:07:37) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-4.18.0-348.23.1.el8_5.x86_64-x86_64-with-glibc2.28
Is CUDA available: False
CUDA runtime version: 12.2.91
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              16
On-line CPU(s) list: 0-15
Thread(s) per core:  1
Core(s) per socket:  16
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               85
Model name:          Intel(R) Xeon(R) Gold 5218R CPU @ 2.10GHz
Stepping:            7
CPU MHz:             2099.999
BogoMIPS:            4199.99
Hypervisor vendor:   VMware
Virtualization type: full
L1d cache:           32K
L1i cache:           32K
L2 cache:            1024K
L3 cache:            28160K
NUMA node0 CPU(s):   0-15
Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves arat pku ospke avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] numpy==1.26.2
[pip3] optree==0.10.0
[pip3] pytorch-sphinx-theme==0.0.24
[pip3] torch==2.3.0a0+git42062e2
[conda] cudatoolkit               11.8.0              h4ba93d1_12    http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
[conda] magma-cuda121             2.6.1                         1    pytorch
[conda] mkl                       2024.0.0                 pypi_0    pypi
[conda] mkl-devel                 2024.0.0                 pypi_0    pypi
[conda] mkl-include               2024.0.0                 pypi_0    pypi
[conda] numpy                     1.26.2                   pypi_0    pypi
[conda] optree                    0.10.0                   pypi_0    pypi
[conda] pytorch-sphinx-theme      0.0.24                    dev_0    <develop>
[conda] torch                     2.3.0a0+git42062e2           dev_0    <develop>
```
```[tasklist]
### Tasks
```


cc @jbschlosser