Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-115946: Add a new interface for stack retrieval for external profilers and debuggers #115947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ struct _ts {
pycore_ceval.h. */
uintptr_t eval_breaker;

Py_ssize_t n_eval_frames;
Py_ssize_t eval_stack_size;
PyObject** eval_stack;

struct {
/* Has been initialized to a safe state.

Expand Down
14 changes: 14 additions & 0 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ static inline void _PyFrame_StackPush(_PyInterpreterFrame *f, PyObject *value) {
f->stacktop++;
}

static inline void _PyFrame_PushState(PyThreadState *tstate, _PyInterpreterFrame *f) {
if (tstate->n_eval_frames >= tstate->eval_stack_size) {
tstate->eval_stack_size *= 2;
tstate->eval_stack = PyMem_RawRealloc(tstate->eval_stack, tstate->eval_stack_size * sizeof(PyObject*));
if (tstate->eval_stack == NULL) {
Py_FatalError("Failed to allocate eval stack");
}
}
tstate->eval_stack[tstate->n_eval_frames] = f->f_executable;
tstate->n_eval_frames += 1;
}

#define FRAME_SPECIALS_SIZE ((int)((sizeof(_PyInterpreterFrame)-1)/sizeof(PyObject *)))

static inline int
Expand Down Expand Up @@ -270,6 +282,7 @@ _PyFrame_PushUnchecked(PyThreadState *tstate, PyFunctionObject *func, int null_l
tstate->datastack_top += code->co_framesize;
assert(tstate->datastack_top < tstate->datastack_limit);
_PyFrame_Initialize(new_frame, func, NULL, code, null_locals_from);
_PyFrame_PushState(tstate, new_frame);
return new_frame;
}

Expand All @@ -294,6 +307,7 @@ _PyFrame_PushTrampolineUnchecked(PyThreadState *tstate, PyCodeObject *code, int
frame->instr_ptr = _PyCode_CODE(code);
frame->owner = FRAME_OWNED_BY_THREAD;
frame->return_offset = 0;
_PyFrame_PushState(tstate, frame);
return frame;
}

Expand Down
3 changes: 3 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,8 @@ clear_gen_frame(PyThreadState *tstate, _PyInterpreterFrame * frame)
void
_PyEval_FrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame * frame)
{
tstate->n_eval_frames -= 1;
assert(tstate->n_eval_frames >= 0);
if (frame->owner == FRAME_OWNED_BY_THREAD) {
clear_thread_frame(tstate, frame);
}
Expand All @@ -1722,6 +1724,7 @@ _PyEvalFramePushAndInit(PyThreadState *tstate, PyFunctionObject *func,
goto fail;
}
_PyFrame_Initialize(frame, func, locals, code, 0);
_PyFrame_PushState(tstate, frame);
if (initialize_locals(tstate, func, frame->localsplus, args, argcount, kwnames)) {
assert(frame->owner == FRAME_OWNED_BY_THREAD);
clear_thread_frame(tstate, frame);
Expand Down
15 changes: 14 additions & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,9 @@ init_threadstate(_PyThreadStateImpl *_tstate,
}

tstate->_status.initialized = 1;
tstate->n_eval_frames = 0;
tstate->eval_stack_size = tstate->py_recursion_limit * 2;
tstate->eval_stack = NULL;
}

static void
Expand Down Expand Up @@ -1428,7 +1431,6 @@ new_threadstate(PyInterpreterState *interp, int whence)

init_threadstate(tstate, interp, id, whence);
add_threadstate(interp, (PyThreadState *)tstate, old_head);

HEAD_UNLOCK(runtime);
if (!used_newtstate) {
// Must be called with lock unlocked to avoid re-entrancy deadlock.
Expand All @@ -1440,6 +1442,15 @@ new_threadstate(PyInterpreterState *interp, int whence)
_Py_qsbr_register(tstate, interp, qsbr_idx);
#endif

// Initialize eval stack
PyThreadState* result = (PyThreadState*)(tstate);
if (result && result->eval_stack == NULL) {
result->eval_stack = PyMem_RawMalloc(result->eval_stack_size * sizeof(PyObject*));
if (result->eval_stack == NULL) {
PyMem_RawFree(result);
return NULL;
}
}
return (PyThreadState *)tstate;
}

Expand Down Expand Up @@ -1650,6 +1661,8 @@ tstate_delete_common(PyThreadState *tstate)
clear_datastack(tstate);

tstate->_status.finalized = 1;
tstate->n_eval_frames = -1;
PyMem_RawFree(tstate->eval_stack);
}

static void
Expand Down
Empty file.