Skip to content

Commit 6d93690

Browse files
gh-125604: Move _Py_AuditHookEntry, etc. Out of pycore_runtime.h (gh-125605)
This is essentially a cleanup, moving a handful of API declarations to the header files where they fit best, creating new ones when needed. We do the following: * add pycore_debug_offsets.h and move _Py_DebugOffsets, etc. there * inline struct _getargs_runtime_state and struct _gilstate_runtime_state in _PyRuntimeState * move struct _reftracer_runtime_state to the existing pycore_object_state.h * add pycore_audit.h and move to it _Py_AuditHookEntry , _PySys_Audit(), and _PySys_ClearAuditHooks * add audit.h and cpython/audit.h and move the existing audit-related API there *move the perfmap/trampoline API from cpython/sysmodule.h to cpython/ceval.h, and remove the now-empty cpython/sysmodule.h
1 parent 2e950e3 commit 6d93690

26 files changed

+429
-346
lines changed

Include/Python.h

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
#include "pylifecycle.h"
125125
#include "ceval.h"
126126
#include "sysmodule.h"
127+
#include "audit.h"
127128
#include "osmodule.h"
128129
#include "intrcheck.h"
129130
#include "import.h"

Include/audit.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef Py_AUDIT_H
2+
#define Py_AUDIT_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
8+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
9+
PyAPI_FUNC(int) PySys_Audit(
10+
const char *event,
11+
const char *argFormat,
12+
...);
13+
14+
PyAPI_FUNC(int) PySys_AuditTuple(
15+
const char *event,
16+
PyObject *args);
17+
#endif
18+
19+
20+
#ifndef Py_LIMITED_API
21+
# define Py_CPYTHON_AUDIT_H
22+
# include "cpython/audit.h"
23+
# undef Py_CPYTHON_AUDIT_H
24+
#endif
25+
26+
27+
#ifdef __cplusplus
28+
}
29+
#endif
30+
#endif /* !Py_AUDIT_H */

Include/cpython/audit.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef Py_CPYTHON_AUDIT_H
2+
# error "this header file must not be included directly"
3+
#endif
4+
5+
6+
typedef int(*Py_AuditHookFunction)(const char *, PyObject *, void *);
7+
8+
PyAPI_FUNC(int) PySys_AddAuditHook(Py_AuditHookFunction, void*);

Include/cpython/ceval.h

+18
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,21 @@ _PyEval_RequestCodeExtraIndex(freefunc f) {
2323

2424
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
2525
PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
26+
27+
28+
// Trampoline API
29+
30+
typedef struct {
31+
FILE* perf_map;
32+
PyThread_type_lock map_lock;
33+
} PerfMapState;
34+
35+
PyAPI_FUNC(int) PyUnstable_PerfMapState_Init(void);
36+
PyAPI_FUNC(int) PyUnstable_WritePerfMapEntry(
37+
const void *code_addr,
38+
unsigned int code_size,
39+
const char *entry_name);
40+
PyAPI_FUNC(void) PyUnstable_PerfMapState_Fini(void);
41+
PyAPI_FUNC(int) PyUnstable_CopyPerfMapFile(const char* parent_filename);
42+
PyAPI_FUNC(int) PyUnstable_PerfTrampoline_CompileCode(PyCodeObject *);
43+
PyAPI_FUNC(int) PyUnstable_PerfTrampoline_SetPersistAfterFork(int enable);

Include/cpython/sysmodule.h

-22
This file was deleted.

Include/internal/pycore_audit.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef Py_INTERNAL_AUDIT_H
2+
#define Py_INTERNAL_AUDIT_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
12+
/* Runtime audit hook state */
13+
14+
typedef struct _Py_AuditHookEntry {
15+
struct _Py_AuditHookEntry *next;
16+
Py_AuditHookFunction hookCFunction;
17+
void *userData;
18+
} _Py_AuditHookEntry;
19+
20+
21+
extern int _PySys_Audit(
22+
PyThreadState *tstate,
23+
const char *event,
24+
const char *argFormat,
25+
...);
26+
27+
// _PySys_ClearAuditHooks() must not be exported: use extern rather than
28+
// PyAPI_FUNC(). We want minimal exposure of this function.
29+
extern void _PySys_ClearAuditHooks(PyThreadState *tstate);
30+
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif
35+
#endif /* !Py_INTERNAL_AUDIT_H */
+269
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
#ifndef Py_INTERNAL_DEBUG_OFFSETS_H
2+
#define Py_INTERNAL_DEBUG_OFFSETS_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
12+
#define _Py_Debug_Cookie "xdebugpy"
13+
14+
#ifdef Py_GIL_DISABLED
15+
# define _Py_Debug_gilruntimestate_enabled offsetof(struct _gil_runtime_state, enabled)
16+
# define _Py_Debug_Free_Threaded 1
17+
#else
18+
# define _Py_Debug_gilruntimestate_enabled 0
19+
# define _Py_Debug_Free_Threaded 0
20+
#endif
21+
22+
23+
typedef struct _Py_DebugOffsets {
24+
char cookie[8];
25+
uint64_t version;
26+
uint64_t free_threaded;
27+
// Runtime state offset;
28+
struct _runtime_state {
29+
uint64_t size;
30+
uint64_t finalizing;
31+
uint64_t interpreters_head;
32+
} runtime_state;
33+
34+
// Interpreter state offset;
35+
struct _interpreter_state {
36+
uint64_t size;
37+
uint64_t id;
38+
uint64_t next;
39+
uint64_t threads_head;
40+
uint64_t gc;
41+
uint64_t imports_modules;
42+
uint64_t sysdict;
43+
uint64_t builtins;
44+
uint64_t ceval_gil;
45+
uint64_t gil_runtime_state;
46+
uint64_t gil_runtime_state_enabled;
47+
uint64_t gil_runtime_state_locked;
48+
uint64_t gil_runtime_state_holder;
49+
} interpreter_state;
50+
51+
// Thread state offset;
52+
struct _thread_state{
53+
uint64_t size;
54+
uint64_t prev;
55+
uint64_t next;
56+
uint64_t interp;
57+
uint64_t current_frame;
58+
uint64_t thread_id;
59+
uint64_t native_thread_id;
60+
uint64_t datastack_chunk;
61+
uint64_t status;
62+
} thread_state;
63+
64+
// InterpreterFrame offset;
65+
struct _interpreter_frame {
66+
uint64_t size;
67+
uint64_t previous;
68+
uint64_t executable;
69+
uint64_t instr_ptr;
70+
uint64_t localsplus;
71+
uint64_t owner;
72+
} interpreter_frame;
73+
74+
// Code object offset;
75+
struct _code_object {
76+
uint64_t size;
77+
uint64_t filename;
78+
uint64_t name;
79+
uint64_t qualname;
80+
uint64_t linetable;
81+
uint64_t firstlineno;
82+
uint64_t argcount;
83+
uint64_t localsplusnames;
84+
uint64_t localspluskinds;
85+
uint64_t co_code_adaptive;
86+
} code_object;
87+
88+
// PyObject offset;
89+
struct _pyobject {
90+
uint64_t size;
91+
uint64_t ob_type;
92+
} pyobject;
93+
94+
// PyTypeObject object offset;
95+
struct _type_object {
96+
uint64_t size;
97+
uint64_t tp_name;
98+
uint64_t tp_repr;
99+
uint64_t tp_flags;
100+
} type_object;
101+
102+
// PyTuple object offset;
103+
struct _tuple_object {
104+
uint64_t size;
105+
uint64_t ob_item;
106+
uint64_t ob_size;
107+
} tuple_object;
108+
109+
// PyList object offset;
110+
struct _list_object {
111+
uint64_t size;
112+
uint64_t ob_item;
113+
uint64_t ob_size;
114+
} list_object;
115+
116+
// PyDict object offset;
117+
struct _dict_object {
118+
uint64_t size;
119+
uint64_t ma_keys;
120+
uint64_t ma_values;
121+
} dict_object;
122+
123+
// PyFloat object offset;
124+
struct _float_object {
125+
uint64_t size;
126+
uint64_t ob_fval;
127+
} float_object;
128+
129+
// PyLong object offset;
130+
struct _long_object {
131+
uint64_t size;
132+
uint64_t lv_tag;
133+
uint64_t ob_digit;
134+
} long_object;
135+
136+
// PyBytes object offset;
137+
struct _bytes_object {
138+
uint64_t size;
139+
uint64_t ob_size;
140+
uint64_t ob_sval;
141+
} bytes_object;
142+
143+
// Unicode object offset;
144+
struct _unicode_object {
145+
uint64_t size;
146+
uint64_t state;
147+
uint64_t length;
148+
uint64_t asciiobject_size;
149+
} unicode_object;
150+
151+
// GC runtime state offset;
152+
struct _gc {
153+
uint64_t size;
154+
uint64_t collecting;
155+
} gc;
156+
} _Py_DebugOffsets;
157+
158+
159+
#define _Py_DebugOffsets_INIT(debug_cookie) { \
160+
.cookie = debug_cookie, \
161+
.version = PY_VERSION_HEX, \
162+
.free_threaded = _Py_Debug_Free_Threaded, \
163+
.runtime_state = { \
164+
.size = sizeof(_PyRuntimeState), \
165+
.finalizing = offsetof(_PyRuntimeState, _finalizing), \
166+
.interpreters_head = offsetof(_PyRuntimeState, interpreters.head), \
167+
}, \
168+
.interpreter_state = { \
169+
.size = sizeof(PyInterpreterState), \
170+
.id = offsetof(PyInterpreterState, id), \
171+
.next = offsetof(PyInterpreterState, next), \
172+
.threads_head = offsetof(PyInterpreterState, threads.head), \
173+
.gc = offsetof(PyInterpreterState, gc), \
174+
.imports_modules = offsetof(PyInterpreterState, imports.modules), \
175+
.sysdict = offsetof(PyInterpreterState, sysdict), \
176+
.builtins = offsetof(PyInterpreterState, builtins), \
177+
.ceval_gil = offsetof(PyInterpreterState, ceval.gil), \
178+
.gil_runtime_state = offsetof(PyInterpreterState, _gil), \
179+
.gil_runtime_state_enabled = _Py_Debug_gilruntimestate_enabled, \
180+
.gil_runtime_state_locked = offsetof(PyInterpreterState, _gil.locked), \
181+
.gil_runtime_state_holder = offsetof(PyInterpreterState, _gil.last_holder), \
182+
}, \
183+
.thread_state = { \
184+
.size = sizeof(PyThreadState), \
185+
.prev = offsetof(PyThreadState, prev), \
186+
.next = offsetof(PyThreadState, next), \
187+
.interp = offsetof(PyThreadState, interp), \
188+
.current_frame = offsetof(PyThreadState, current_frame), \
189+
.thread_id = offsetof(PyThreadState, thread_id), \
190+
.native_thread_id = offsetof(PyThreadState, native_thread_id), \
191+
.datastack_chunk = offsetof(PyThreadState, datastack_chunk), \
192+
.status = offsetof(PyThreadState, _status), \
193+
}, \
194+
.interpreter_frame = { \
195+
.size = sizeof(_PyInterpreterFrame), \
196+
.previous = offsetof(_PyInterpreterFrame, previous), \
197+
.executable = offsetof(_PyInterpreterFrame, f_executable), \
198+
.instr_ptr = offsetof(_PyInterpreterFrame, instr_ptr), \
199+
.localsplus = offsetof(_PyInterpreterFrame, localsplus), \
200+
.owner = offsetof(_PyInterpreterFrame, owner), \
201+
}, \
202+
.code_object = { \
203+
.size = sizeof(PyCodeObject), \
204+
.filename = offsetof(PyCodeObject, co_filename), \
205+
.name = offsetof(PyCodeObject, co_name), \
206+
.qualname = offsetof(PyCodeObject, co_qualname), \
207+
.linetable = offsetof(PyCodeObject, co_linetable), \
208+
.firstlineno = offsetof(PyCodeObject, co_firstlineno), \
209+
.argcount = offsetof(PyCodeObject, co_argcount), \
210+
.localsplusnames = offsetof(PyCodeObject, co_localsplusnames), \
211+
.localspluskinds = offsetof(PyCodeObject, co_localspluskinds), \
212+
.co_code_adaptive = offsetof(PyCodeObject, co_code_adaptive), \
213+
}, \
214+
.pyobject = { \
215+
.size = sizeof(PyObject), \
216+
.ob_type = offsetof(PyObject, ob_type), \
217+
}, \
218+
.type_object = { \
219+
.size = sizeof(PyTypeObject), \
220+
.tp_name = offsetof(PyTypeObject, tp_name), \
221+
.tp_repr = offsetof(PyTypeObject, tp_repr), \
222+
.tp_flags = offsetof(PyTypeObject, tp_flags), \
223+
}, \
224+
.tuple_object = { \
225+
.size = sizeof(PyTupleObject), \
226+
.ob_item = offsetof(PyTupleObject, ob_item), \
227+
.ob_size = offsetof(PyTupleObject, ob_base.ob_size), \
228+
}, \
229+
.list_object = { \
230+
.size = sizeof(PyListObject), \
231+
.ob_item = offsetof(PyListObject, ob_item), \
232+
.ob_size = offsetof(PyListObject, ob_base.ob_size), \
233+
}, \
234+
.dict_object = { \
235+
.size = sizeof(PyDictObject), \
236+
.ma_keys = offsetof(PyDictObject, ma_keys), \
237+
.ma_values = offsetof(PyDictObject, ma_values), \
238+
}, \
239+
.float_object = { \
240+
.size = sizeof(PyFloatObject), \
241+
.ob_fval = offsetof(PyFloatObject, ob_fval), \
242+
}, \
243+
.long_object = { \
244+
.size = sizeof(PyLongObject), \
245+
.lv_tag = offsetof(PyLongObject, long_value.lv_tag), \
246+
.ob_digit = offsetof(PyLongObject, long_value.ob_digit), \
247+
}, \
248+
.bytes_object = { \
249+
.size = sizeof(PyBytesObject), \
250+
.ob_size = offsetof(PyBytesObject, ob_base.ob_size), \
251+
.ob_sval = offsetof(PyBytesObject, ob_sval), \
252+
}, \
253+
.unicode_object = { \
254+
.size = sizeof(PyUnicodeObject), \
255+
.state = offsetof(PyUnicodeObject, _base._base.state), \
256+
.length = offsetof(PyUnicodeObject, _base._base.length), \
257+
.asciiobject_size = sizeof(PyASCIIObject), \
258+
}, \
259+
.gc = { \
260+
.size = sizeof(struct _gc_runtime_state), \
261+
.collecting = offsetof(struct _gc_runtime_state, collecting), \
262+
}, \
263+
}
264+
265+
266+
#ifdef __cplusplus
267+
}
268+
#endif
269+
#endif /* !Py_INTERNAL_DEBUG_OFFSETS_H */

0 commit comments

Comments
 (0)