Search code examples
c++pybind11

How to access pybind11 array_t<> data pointer in debugger


I would like to access the data pointer for a pybind11::array_t<T> typed NumPy array wrapper, while stepping in a debugger. As below, the debugger does not know the type of array_t<T>, so the data(...)/mutable_data(...) accessors are not available.

(lldb) p o
(pybind11::array_t<unsigned long long, 16>) $24 = {
  pybind11::array = {
    pybind11::buffer = {
      pybind11::object = {
        pybind11::handle = (m_ptr = '0x10872fe40')
      }
    }
  }
}
(lldb) p o.data()
error: no member named 'data' in 'pybind11::array_t<unsigned long long, 16>'
(lldb) p o.data(0)
error: no member named 'data' in 'pybind11::array_t<unsigned long long, 16>'

Solution

  • Based on the implementation of array_t::data in pybind11's numpy.h, a solution is to use the pybind11::detail::array_proxy to access the data pointer:

    (lldb) p ((uint64_t*)pybind11::detail::array_proxy(o.m_ptr)->data)[652]
    (uint64_t) $26 = 87112