Skip to content

Commit 4afb8ba

Browse files
committed
gh-146462: add dict introspection debug offsets
1 parent 54607ee commit 4afb8ba

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

Doc/howto/remote_debugging.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ To read and check the debug offsets, follow these steps:
365365
fields in memory. If any check fails, the debugger should stop the operation
366366
to avoid reading memory in the wrong format.
367367

368+
For object introspection, ``_Py_DebugOffsets`` also exposes layout metadata for
369+
``PyTypeObject`` and ``PyHeapTypeObject``. In particular,
370+
``type_object.tp_basicsize``, ``type_object.tp_dictoffset``, and
371+
``heap_type_object.ht_cached_keys`` let read-only tools locate instance
372+
dictionary storage and recover the inline-values key order for heap types.
373+
368374
The following is an example implementation that reads and checks
369375
``_Py_DebugOffsets``::
370376

Include/internal/pycore_debug_offsets.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,16 @@ typedef struct _Py_DebugOffsets {
158158
uint64_t tp_name;
159159
uint64_t tp_repr;
160160
uint64_t tp_flags;
161+
uint64_t tp_basicsize;
162+
uint64_t tp_dictoffset;
161163
} type_object;
162164

165+
// PyHeapTypeObject offset;
166+
struct _heap_type_object {
167+
uint64_t size;
168+
uint64_t ht_cached_keys;
169+
} heap_type_object;
170+
163171
// PyTuple object offset;
164172
struct _tuple_object {
165173
uint64_t size;
@@ -329,6 +337,12 @@ typedef struct _Py_DebugOffsets {
329337
.tp_name = offsetof(PyTypeObject, tp_name), \
330338
.tp_repr = offsetof(PyTypeObject, tp_repr), \
331339
.tp_flags = offsetof(PyTypeObject, tp_flags), \
340+
.tp_basicsize = offsetof(PyTypeObject, tp_basicsize), \
341+
.tp_dictoffset = offsetof(PyTypeObject, tp_dictoffset), \
342+
}, \
343+
.heap_type_object = { \
344+
.size = sizeof(PyHeapTypeObject), \
345+
.ht_cached_keys = offsetof(PyHeapTypeObject, ht_cached_keys), \
332346
}, \
333347
.tuple_object = { \
334348
.size = sizeof(PyTupleObject), \
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added ``PyTypeObject.tp_basicsize``, ``PyTypeObject.tp_dictoffset``, and
2+
``PyHeapTypeObject.ht_cached_keys`` offsets to :c:type:`!_Py_DebugOffsets` to
3+
support version-independent read-only dict introspection tools.

Modules/_remote_debugging/debug_offsets_validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#define FIELD_SIZE(type, member) sizeof(((type *)0)->member)
3232

3333
enum {
34-
PY_REMOTE_DEBUG_OFFSETS_TOTAL_SIZE = 840,
34+
PY_REMOTE_DEBUG_OFFSETS_TOTAL_SIZE = 872,
3535
PY_REMOTE_ASYNC_DEBUG_OFFSETS_TOTAL_SIZE = 104,
3636
};
3737

0 commit comments

Comments
 (0)