-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
GH-126910: Add gdb support for unwinding JIT frames #146071
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
Open
diegorusso
wants to merge
6
commits into
python:main
Choose a base branch
from
diegorusso:add-gdb-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,164
−713
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5cd7ade
GH-126910 jit_unwind: refactor EH frame generation
diegorusso 669dfb9
GH-126910 jit_unwind: add GDB JIT interface and test
diegorusso 255c0b3
Fix make smelly
diegorusso ac018d6
Add __jit_debug_descriptor to ignored.tsv
diegorusso b0bab8c
📜🤖 Added by blurb_it.
blurb-it[bot] a0dff1f
Fix check C globals
diegorusso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| #ifndef Py_CORE_JIT_UNWIND_H | ||
| #define Py_CORE_JIT_UNWIND_H | ||
|
|
||
| #ifdef PY_HAVE_PERF_TRAMPOLINE | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| /* Return the size of the generated .eh_frame data for the given encoding. */ | ||
| size_t _PyJitUnwind_EhFrameSize(int absolute_addr); | ||
|
|
||
| /* | ||
| * Build DWARF .eh_frame data for JIT code; returns size written or 0 on error. | ||
| * absolute_addr selects the FDE address encoding: | ||
| * - 0: PC-relative offsets (perf jitdump synthesized DSO). | ||
| * - nonzero: absolute addresses (GDB JIT in-memory ELF). | ||
| */ | ||
| size_t _PyJitUnwind_BuildEhFrame(uint8_t *buffer, size_t buffer_size, | ||
| const void *code_addr, size_t code_size, | ||
| int absolute_addr); | ||
|
|
||
| void _PyJitUnwind_GdbRegisterCode(const void *code_addr, | ||
| unsigned int code_size, | ||
| const char *entry, | ||
| const char *filename); | ||
|
|
||
| #endif // PY_HAVE_PERF_TRAMPOLINE | ||
|
|
||
| #endif // Py_CORE_JIT_UNWIND_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Sample script for use by test_gdb.test_jit | ||
|
|
||
| import operator | ||
| import sys | ||
|
|
||
|
|
||
| def jit_bt_hot(depth, warming_up_caller=False): | ||
| if warming_up_caller: | ||
| return | ||
| if depth == 0: | ||
| id(42) | ||
| return | ||
|
|
||
| warming_up = True | ||
| while warming_up: | ||
| warming_up = sys._jit.is_enabled() & (not sys._jit.is_active()) | ||
| operator.call(jit_bt_hot, depth - 1, warming_up) | ||
|
|
||
|
|
||
| jit_bt_hot(10) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import os | ||
| import re | ||
| import sys | ||
| import unittest | ||
|
|
||
| from .util import setup_module, DebuggerTests | ||
|
|
||
|
|
||
| JIT_SAMPLE_SCRIPT = os.path.join(os.path.dirname(__file__), "gdb_jit_sample.py") | ||
|
|
||
|
|
||
| def setUpModule(): | ||
| setup_module() | ||
|
|
||
|
|
||
| @unittest.skipUnless( | ||
| hasattr(sys, "_jit") and sys._jit.is_available(), | ||
| "requires a JIT-enabled build", | ||
| ) | ||
| class JitBacktraceTests(DebuggerTests): | ||
| def test_bt_unwinds_through_jit_frames(self): | ||
| gdb_output = self.get_stack_trace( | ||
| script=JIT_SAMPLE_SCRIPT, | ||
| cmds_after_breakpoint=["bt"], | ||
| PYTHON_JIT="1", | ||
| ) | ||
| self.assertIn("py::jit_executor:<jit>", gdb_output) | ||
| self.assertIn("py::jit_shim:<jit>", gdb_output) | ||
| self.assertRegex( | ||
| gdb_output, | ||
| re.compile( | ||
| r"py::jit_executor:<jit>.*(_PyEval_EvalFrameDefault|_PyEval_Vector)", | ||
| re.DOTALL, | ||
| ), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core_and_Builtins/2026-03-17-20-30-17.gh-issue-126910.NaUwmD.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add support for unwinding JIT frames using GDB. Patch by Diego Russo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| #include "pycore_interpframe.h" | ||
| #include "pycore_interpolation.h" | ||
| #include "pycore_intrinsics.h" | ||
| #include "pycore_jit_unwind.h" | ||
| #include "pycore_lazyimportobject.h" | ||
| #include "pycore_list.h" | ||
| #include "pycore_long.h" | ||
|
|
@@ -60,6 +61,28 @@ jit_error(const char *message) | |
| PyErr_Format(PyExc_RuntimeWarning, "JIT %s (%d)", message, hint); | ||
| } | ||
|
|
||
| static void | ||
| jit_record_code(const void *code_addr, size_t code_size, | ||
| const char *entry, const char *filename) | ||
| { | ||
| #ifdef PY_HAVE_PERF_TRAMPOLINE | ||
| _PyPerf_Callbacks callbacks; | ||
| _PyPerfTrampoline_GetCallbacks(&callbacks); | ||
| if (callbacks.write_state == _Py_perfmap_jit_callbacks.write_state) { | ||
| _PyPerfJit_WriteNamedCode( | ||
| code_addr, (unsigned int)code_size, entry, filename); | ||
| return; | ||
| } | ||
| _PyJitUnwind_GdbRegisterCode( | ||
| code_addr, (unsigned int)code_size, entry, filename); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| #else | ||
| (void)code_addr; | ||
| (void)code_size; | ||
| (void)entry; | ||
| (void)filename; | ||
| #endif | ||
| } | ||
|
|
||
| static size_t _Py_jit_shim_size = 0; | ||
|
|
||
| static int | ||
|
|
@@ -731,6 +754,10 @@ _PyJIT_Compile(_PyExecutorObject *executor, const _PyUOpInstruction trace[], siz | |
| } | ||
| executor->jit_code = memory; | ||
| executor->jit_size = total_size; | ||
| jit_record_code(memory, | ||
| code_size + state.trampolines.size, | ||
| "jit_executor", | ||
| "<jit>"); | ||
| return 0; | ||
| } | ||
|
|
||
|
|
@@ -781,6 +808,10 @@ compile_shim(void) | |
| return NULL; | ||
| } | ||
| _Py_jit_shim_size = total_size; | ||
| jit_record_code(memory, | ||
| code_size + state.trampolines.size, | ||
| "jit_shim", | ||
| "<jit>"); | ||
| return (_PyJitEntryFuncPtr)memory; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this loop hang? When
warming_up=True, the call passeswarming_up_caller=Truewhich returns immediately at line 8, so the recursive body never actually executes. If the JIT does not activate via some other path, would this not spin forever until the timeout kills it? Should there be a max iteration count as a safety net?Also, line 16 uses bitwise
&instead ofand. Was that intentional? It meansis_active()is always evaluated even whenis_enabled()isFalse.