1 #ifndef Py_CPYTHON_PYSTATE_H 2 # error "this header file must not be included directly" 3 #endif 4 5 6 /* private interpreter helpers */ 7 8 PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *); 9 PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int); 10 11 PyAPI_FUNC(PyObject *) PyUnstable_InterpreterState_GetMainModule(PyInterpreterState *); 12 13 /* State unique per thread */ 14 15 /* Py_tracefunc return -1 when raising an exception, or 0 for success. */ 16 typedef int (*Py_tracefunc)(PyObject *, PyFrameObject *, int, PyObject *); 17 18 /* The following values are used for 'what' for tracefunc functions 19 * 20 * To add a new kind of trace event, also update "trace_init" in 21 * Python/sysmodule.c to define the Python level event name 22 */ 23 #define PyTrace_CALL 0 24 #define PyTrace_EXCEPTION 1 25 #define PyTrace_LINE 2 26 #define PyTrace_RETURN 3 27 #define PyTrace_C_CALL 4 28 #define PyTrace_C_EXCEPTION 5 29 #define PyTrace_C_RETURN 6 30 #define PyTrace_OPCODE 7 31 32 typedef struct _err_stackitem { 33 /* This struct represents a single execution context where we might 34 * be currently handling an exception. It is a per-coroutine state 35 * (coroutine in the computer science sense, including the thread 36 * and generators). 37 * 38 * This is used as an entry on the exception stack, where each 39 * entry indicates if it is currently handling an exception. 40 * This ensures that the exception state is not impacted 41 * by "yields" from an except handler. The thread 42 * always has an entry (the bottom-most one). 43 */ 44 45 /* The exception currently being handled in this context, if any. */ 46 PyObject *exc_value; 47 48 struct _err_stackitem *previous_item; 49 50 } _PyErr_StackItem; 51 52 typedef struct _stack_chunk { 53 struct _stack_chunk *previous; 54 size_t size; 55 size_t top; 56 PyObject * data[1]; /* Variable sized */ 57 } _PyStackChunk; 58 59 struct _ts { 60 /* See Python/ceval.c for comments explaining most fields */ 61 62 PyThreadState *prev; 63 PyThreadState *next; 64 PyInterpreterState *interp; 65 66 /* The global instrumentation version in high bits, plus flags indicating 67 when to break out of the interpreter loop in lower bits. See details in 68 pycore_ceval.h. */ 69 uintptr_t eval_breaker; 70 71 struct { 72 /* Has been initialized to a safe state. 73 74 In order to be effective, this must be set to 0 during or right 75 after allocation. */ 76 unsigned int initialized:1; 77 78 /* Has been bound to an OS thread. */ 79 unsigned int bound:1; 80 /* Has been unbound from its OS thread. */ 81 unsigned int unbound:1; 82 /* Has been bound aa current for the GILState API. */ 83 unsigned int bound_gilstate:1; 84 /* Currently in use (maybe holds the GIL). */ 85 unsigned int active:1; 86 /* Currently holds the GIL. */ 87 unsigned int holds_gil:1; 88 89 /* various stages of finalization */ 90 unsigned int finalizing:1; 91 unsigned int cleared:1; 92 unsigned int finalized:1; 93 94 /* padding to align to 4 bytes */ 95 unsigned int :23; 96 } _status; 97 #ifdef Py_BUILD_CORE 98 # define _PyThreadState_WHENCE_NOTSET -1 99 # define _PyThreadState_WHENCE_UNKNOWN 0 100 # define _PyThreadState_WHENCE_INIT 1 101 # define _PyThreadState_WHENCE_FINI 2 102 # define _PyThreadState_WHENCE_THREADING 3 103 # define _PyThreadState_WHENCE_GILSTATE 4 104 # define _PyThreadState_WHENCE_EXEC 5 105 #endif 106 int _whence; 107 108 /* Thread state (_Py_THREAD_ATTACHED, _Py_THREAD_DETACHED, _Py_THREAD_SUSPENDED). 109 See Include/internal/pycore_pystate.h for more details. */ 110 int state; 111 112 int py_recursion_remaining; 113 int py_recursion_limit; 114 115 int c_recursion_remaining; 116 int recursion_headroom; /* Allow 50 more calls to handle any errors. */ 117 118 /* 'tracing' keeps track of the execution depth when tracing/profiling. 119 This is to prevent the actual trace/profile code from being recorded in 120 the trace/profile. */ 121 int tracing; 122 int what_event; /* The event currently being monitored, if any. */ 123 124 /* Pointer to currently executing frame. */ 125 struct _PyInterpreterFrame *current_frame; 126 127 Py_tracefunc c_profilefunc; 128 Py_tracefunc c_tracefunc; 129 PyObject *c_profileobj; 130 PyObject *c_traceobj; 131 132 /* The exception currently being raised */ 133 PyObject *current_exception; 134 135 /* Pointer to the top of the exception stack for the exceptions 136 * we may be currently handling. (See _PyErr_StackItem above.) 137 * This is never NULL. */ 138 _PyErr_StackItem *exc_info; 139 140 PyObject *dict; /* Stores per-thread state */ 141 142 int gilstate_counter; 143 144 PyObject *async_exc; /* Asynchronous exception to raise */ 145 unsigned long thread_id; /* Thread id where this tstate was created */ 146 147 /* Native thread id where this tstate was created. This will be 0 except on 148 * those platforms that have the notion of native thread id, for which the 149 * macro PY_HAVE_THREAD_NATIVE_ID is then defined. 150 */ 151 unsigned long native_thread_id; 152 153 PyObject *delete_later; 154 155 /* Tagged pointer to top-most critical section, or zero if there is no 156 * active critical section. Critical sections are only used in 157 * `--disable-gil` builds (i.e., when Py_GIL_DISABLED is defined to 1). In the 158 * default build, this field is always zero. 159 */ 160 uintptr_t critical_section; 161 162 int coroutine_origin_tracking_depth; 163 164 PyObject *async_gen_firstiter; 165 PyObject *async_gen_finalizer; 166 167 PyObject *context; 168 uint64_t context_ver; 169 170 /* Unique thread state id. */ 171 uint64_t id; 172 173 _PyStackChunk *datastack_chunk; 174 PyObject **datastack_top; 175 PyObject **datastack_limit; 176 /* XXX signal handlers should also be here */ 177 178 /* The following fields are here to avoid allocation during init. 179 The data is exposed through PyThreadState pointer fields. 180 These fields should not be accessed directly outside of init. 181 This is indicated by an underscore prefix on the field names. 182 183 All other PyInterpreterState pointer fields are populated when 184 needed and default to NULL. 185 */ 186 // Note some fields do not have a leading underscore for backward 187 // compatibility. See https://bugs.python.org/issue45953#msg412046. 188 189 /* The thread's exception stack entry. (Always the last entry.) */ 190 _PyErr_StackItem exc_state; 191 192 PyObject *previous_executor; 193 194 uint64_t dict_global_version; 195 196 /* Used to store/retrieve `threading.local` keys/values for this thread */ 197 PyObject *threading_local_key; 198 199 /* Used by `threading.local`s to be remove keys/values for dying threads. 200 The PyThreadObject must hold the only reference to this value. 201 */ 202 PyObject *threading_local_sentinel; 203 }; 204 205 #ifdef Py_DEBUG 206 // A debug build is likely built with low optimization level which implies 207 // higher stack memory usage than a release build: use a lower limit. 208 # define Py_C_RECURSION_LIMIT 500 209 #elif defined(__s390x__) 210 # define Py_C_RECURSION_LIMIT 800 211 #elif defined(_WIN32) && defined(_M_ARM64) 212 # define Py_C_RECURSION_LIMIT 1000 213 #elif defined(_WIN32) 214 # define Py_C_RECURSION_LIMIT 3000 215 #elif defined(__ANDROID__) 216 // On an ARM64 emulator, API level 34 was OK with 10000, but API level 21 217 // crashed in test_compiler_recursion_limit. 218 # define Py_C_RECURSION_LIMIT 3000 219 #elif defined(_Py_ADDRESS_SANITIZER) 220 # define Py_C_RECURSION_LIMIT 4000 221 #elif defined(__wasi__) 222 // Based on wasmtime 16. 223 # define Py_C_RECURSION_LIMIT 5000 224 #else 225 // This value is duplicated in Lib/test/support/__init__.py 226 # define Py_C_RECURSION_LIMIT 10000 227 #endif 228 229 230 /* other API */ 231 232 /* Similar to PyThreadState_Get(), but don't issue a fatal error 233 * if it is NULL. */ 234 PyAPI_FUNC(PyThreadState *) PyThreadState_GetUnchecked(void); 235 236 // Alias kept for backward compatibility 237 #define _PyThreadState_UncheckedGet PyThreadState_GetUnchecked 238 239 240 // Disable tracing and profiling. 241 PyAPI_FUNC(void) PyThreadState_EnterTracing(PyThreadState *tstate); 242 243 // Reset tracing and profiling: enable them if a trace function or a profile 244 // function is set, otherwise disable them. 245 PyAPI_FUNC(void) PyThreadState_LeaveTracing(PyThreadState *tstate); 246 247 /* PyGILState */ 248 249 /* Helper/diagnostic function - return 1 if the current thread 250 currently holds the GIL, 0 otherwise. 251 252 The function returns 1 if _PyGILState_check_enabled is non-zero. */ 253 PyAPI_FUNC(int) PyGILState_Check(void); 254 255 /* The implementation of sys._current_frames() Returns a dict mapping 256 thread id to that thread's current frame. 257 */ 258 PyAPI_FUNC(PyObject*) _PyThread_CurrentFrames(void); 259 260 /* Routines for advanced debuggers, requested by David Beazley. 261 Don't use unless you know what you are doing! */ 262 PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Main(void); 263 PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Head(void); 264 PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Next(PyInterpreterState *); 265 PyAPI_FUNC(PyThreadState *) PyInterpreterState_ThreadHead(PyInterpreterState *); 266 PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *); 267 PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void); 268 269 /* Frame evaluation API */ 270 271 typedef PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, struct _PyInterpreterFrame *, int); 272 273 PyAPI_FUNC(_PyFrameEvalFunction) _PyInterpreterState_GetEvalFrameFunc( 274 PyInterpreterState *interp); 275 PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc( 276 PyInterpreterState *interp, 277 _PyFrameEvalFunction eval_frame); 278