From f11b7c4385f950b45e1a9fca4f4e9c0bcdb0137e Mon Sep 17 00:00:00 2001 From: Manjusaka Date: Sun, 6 Oct 2024 17:47:32 +0800 Subject: [PATCH 1/5] Fix `function__return` and `function__entry` dTrace probe missing after `GH-103083` --- ...4-10-06-17-46-24.gh-issue-98894.uG2s-h.rst | 2 ++ Python/bytecodes.c | 2 ++ Python/ceval.c | 36 ++++++++++++++++++- Python/ceval_macros.h | 6 ++++ Python/generated_cases.c.h | 6 ++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst new file mode 100644 index 00000000000000..181a13e78a26a6 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst @@ -0,0 +1,2 @@ +Fix `function__return` and `function__entry` dTrace probe missing after +`GH-103083` diff --git a/Python/bytecodes.c b/Python/bytecodes.c index a0edf17d747e77..5267e9f0c4eee6 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -986,6 +986,7 @@ dummy_func( _PyStackRef temp = retval; DEAD(retval); SAVE_STACK(); + DTRACE_FUNCTION_EXIT(); assert(EMPTY()); _Py_LeaveRecursiveCallPy(tstate); // GH-99729: We need to unlink the frame *before* clearing it: @@ -1173,6 +1174,7 @@ dummy_func( _PyStackRef temp = retval; DEAD(retval); SAVE_STACK(); + DTRACE_FUNCTION_EXIT(); tstate->exc_info = gen->gi_exc_state.previous_item; gen->gi_exc_state.previous_item = NULL; _Py_LeaveRecursiveCallPy(tstate); diff --git a/Python/ceval.c b/Python/ceval.c index ba5c70b25f0a89..a2c2603315bd8e 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -275,6 +275,8 @@ static void monitor_throw(PyThreadState *tstate, _Py_CODEUNIT *instr); static int get_exception_handler(PyCodeObject *, int, int*, int*, int*); +static void dtrace_function_entry(_PyInterpreterFrame *); +static void dtrace_function_return(_PyInterpreterFrame *); static _PyInterpreterFrame * _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, _PyStackRef func, PyObject *locals, Py_ssize_t nargs, PyObject *callargs, PyObject *kwargs, _PyInterpreterFrame *previous); @@ -820,9 +822,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int _Py_Instrument(_PyFrame_GetCode(frame), tstate->interp); monitor_throw(tstate, frame, frame->instr_ptr); /* TO DO -- Monitor throw entry. */ + DTRACE_FUNCTION_ENTRY(); goto resume_with_error; } - /* Local "register" variables. * These are cached values from the frame and code object. */ _Py_CODEUNIT *next_instr; @@ -840,6 +842,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int } next_instr = frame->instr_ptr; + DTRACE_FUNCTION_ENTRY(); resume_frame: stack_pointer = _PyFrame_GetStackPointer(frame); @@ -3068,6 +3071,37 @@ PyUnstable_Eval_RequestCodeExtraIndex(freefunc free) return new_index; } + +static void +dtrace_function_entry(_PyInterpreterFrame *frame) +{ + const char *filename; + const char *funcname; + int lineno; + + PyCodeObject *code = _PyFrame_GetCode(frame); + filename = PyUnicode_AsUTF8(code->co_filename); + funcname = PyUnicode_AsUTF8(code->co_name); + lineno = PyUnstable_InterpreterFrame_GetLine(frame); + + PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno); +} + +static void +dtrace_function_return(_PyInterpreterFrame *frame) +{ + const char *filename; + const char *funcname; + int lineno; + + PyCodeObject *code = _PyFrame_GetCode(frame); + filename = PyUnicode_AsUTF8(code->co_filename); + funcname = PyUnicode_AsUTF8(code->co_name); + lineno = PyUnstable_InterpreterFrame_GetLine(frame); + + PyDTrace_FUNCTION_RETURN(filename, funcname, lineno); +} + /* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions for the limited API. */ diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index e0e9cc156ed62f..16a29e2f621835 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -290,6 +290,12 @@ GETITEM(PyObject *v, Py_ssize_t i) { #define CONSTS() _PyFrame_GetCode(frame)->co_consts #define NAMES() _PyFrame_GetCode(frame)->co_names +#define DTRACE_FUNCTION_EXIT() \ + if (PyDTrace_FUNCTION_RETURN_ENABLED()) { \ + dtrace_function_return(frame); \ + } + + #define DTRACE_FUNCTION_ENTRY() \ if (PyDTrace_FUNCTION_ENTRY_ENABLED()) { \ dtrace_function_entry(frame); \ diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index a9c127784edc83..704e86bc22306d 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -4911,6 +4911,7 @@ stack_pointer += -1; assert(WITHIN_STACK_BOUNDS()); _PyFrame_SetStackPointer(frame, stack_pointer); + DTRACE_FUNCTION_EXIT(); assert(EMPTY()); _Py_LeaveRecursiveCallPy(tstate); // GH-99729: We need to unlink the frame *before* clearing it: @@ -4956,6 +4957,7 @@ stack_pointer += -1; assert(WITHIN_STACK_BOUNDS()); _PyFrame_SetStackPointer(frame, stack_pointer); + DTRACE_FUNCTION_EXIT(); assert(EMPTY()); _Py_LeaveRecursiveCallPy(tstate); // GH-99729: We need to unlink the frame *before* clearing it: @@ -5015,6 +5017,7 @@ stack_pointer += -1; assert(WITHIN_STACK_BOUNDS()); _PyFrame_SetStackPointer(frame, stack_pointer); + DTRACE_FUNCTION_EXIT(); tstate->exc_info = gen->gi_exc_state.previous_item; gen->gi_exc_state.previous_item = NULL; _Py_LeaveRecursiveCallPy(tstate); @@ -6969,6 +6972,7 @@ #endif _PyStackRef temp = retval; _PyFrame_SetStackPointer(frame, stack_pointer); + DTRACE_FUNCTION_EXIT(); assert(EMPTY()); _Py_LeaveRecursiveCallPy(tstate); // GH-99729: We need to unlink the frame *before* clearing it: @@ -7033,6 +7037,7 @@ stack_pointer += -1; assert(WITHIN_STACK_BOUNDS()); _PyFrame_SetStackPointer(frame, stack_pointer); + DTRACE_FUNCTION_EXIT(); assert(EMPTY()); _Py_LeaveRecursiveCallPy(tstate); // GH-99729: We need to unlink the frame *before* clearing it: @@ -8171,6 +8176,7 @@ stack_pointer += -1; assert(WITHIN_STACK_BOUNDS()); _PyFrame_SetStackPointer(frame, stack_pointer); + DTRACE_FUNCTION_EXIT(); tstate->exc_info = gen->gi_exc_state.previous_item; gen->gi_exc_state.previous_item = NULL; _Py_LeaveRecursiveCallPy(tstate); From ac683c0b235da83206164f73779c7ad78fad2cc4 Mon Sep 17 00:00:00 2001 From: Manjusaka Date: Sun, 6 Oct 2024 17:55:33 +0800 Subject: [PATCH 2/5] Update news --- .../2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst index 181a13e78a26a6..787a4f875aba16 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst @@ -1,2 +1,2 @@ -Fix `function__return` and `function__entry` dTrace probe missing after -`GH-103083` +Fix ``function__return`` and ``function__entry`` dTrace probe missing after +``GH-103083`` From b4d457f622c53edb4a486bc10cf62ef85ad88325 Mon Sep 17 00:00:00 2001 From: Manjusaka Date: Sun, 6 Oct 2024 18:46:08 +0800 Subject: [PATCH 3/5] Update generate case --- Python/executor_cases.c.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 7631ff75ecafd6..f279f0a5b655c8 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -1260,6 +1260,7 @@ stack_pointer += -1; assert(WITHIN_STACK_BOUNDS()); _PyFrame_SetStackPointer(frame, stack_pointer); + DTRACE_FUNCTION_EXIT(); assert(EMPTY()); _Py_LeaveRecursiveCallPy(tstate); // GH-99729: We need to unlink the frame *before* clearing it: @@ -1403,6 +1404,7 @@ stack_pointer += -1; assert(WITHIN_STACK_BOUNDS()); _PyFrame_SetStackPointer(frame, stack_pointer); + DTRACE_FUNCTION_EXIT(); tstate->exc_info = gen->gi_exc_state.previous_item; gen->gi_exc_state.previous_item = NULL; _Py_LeaveRecursiveCallPy(tstate); From 481051017a3ebf66ae330ed24f887343c53523fe Mon Sep 17 00:00:00 2001 From: Nadeshiko Manju Date: Sun, 6 Oct 2024 19:04:48 +0800 Subject: [PATCH 4/5] Update Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst index 787a4f875aba16..d0bf4410dc01de 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-06-17-46-24.gh-issue-98894.uG2s-h.rst @@ -1,2 +1 @@ -Fix ``function__return`` and ``function__entry`` dTrace probe missing after -``GH-103083`` +Fix ``function__return`` and ``function__entry`` dtrace probe missing after :gh:`103083`. From 128e85641682b70741620c1e50a7b81c3f898063 Mon Sep 17 00:00:00 2001 From: Manjusaka Date: Sun, 6 Oct 2024 20:02:23 +0800 Subject: [PATCH 5/5] fix jit ci --- Python/ceval.c | 3 +-- Python/ceval_macros.h | 5 +++++ Tools/jit/template.c | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Python/ceval.c b/Python/ceval.c index a2c2603315bd8e..5074b14fd23dcb 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -275,8 +275,6 @@ static void monitor_throw(PyThreadState *tstate, _Py_CODEUNIT *instr); static int get_exception_handler(PyCodeObject *, int, int*, int*, int*); -static void dtrace_function_entry(_PyInterpreterFrame *); -static void dtrace_function_return(_PyInterpreterFrame *); static _PyInterpreterFrame * _PyEvalFramePushAndInit_Ex(PyThreadState *tstate, _PyStackRef func, PyObject *locals, Py_ssize_t nargs, PyObject *callargs, PyObject *kwargs, _PyInterpreterFrame *previous); @@ -825,6 +823,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int DTRACE_FUNCTION_ENTRY(); goto resume_with_error; } + /* Local "register" variables. * These are cached values from the frame and code object. */ _Py_CODEUNIT *next_instr; diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index 16a29e2f621835..9f1f4cb2ce0926 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -41,6 +41,8 @@ * the CFG. */ +#include "pycore_frame.h" + #ifdef WITH_DTRACE #define OR_DTRACE_LINE | (PyDTrace_LINE_ENABLED() ? 255 : 0) #else @@ -290,6 +292,9 @@ GETITEM(PyObject *v, Py_ssize_t i) { #define CONSTS() _PyFrame_GetCode(frame)->co_consts #define NAMES() _PyFrame_GetCode(frame)->co_names +static void dtrace_function_entry(_PyInterpreterFrame *); +static void dtrace_function_return(_PyInterpreterFrame *); + #define DTRACE_FUNCTION_EXIT() \ if (PyDTrace_FUNCTION_RETURN_ENABLED()) { \ dtrace_function_return(frame); \ diff --git a/Tools/jit/template.c b/Tools/jit/template.c index 6cf15085f79933..b812cd322006ab 100644 --- a/Tools/jit/template.c +++ b/Tools/jit/template.c @@ -18,6 +18,7 @@ #include "pycore_sliceobject.h" #include "pycore_descrobject.h" #include "pycore_stackref.h" +#include "pydtrace.h" #include "ceval_macros.h"