-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
GH-126910: Add GNU backtrace support for unwinding JIT frames #149104
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
Changes from 3 commits
db967dc
0979429
e8de85d
ef9ea52
01df239
cf16da0
5cd2e87
75b3be8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #ifndef Py_INTERNAL_JIT_PUBLISH_H | ||
| #define Py_INTERNAL_JIT_PUBLISH_H | ||
|
|
||
| #ifndef Py_BUILD_CORE | ||
| # error "this header requires Py_BUILD_CORE define" | ||
| #endif | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| typedef struct _PyJitCodeRegistration _PyJitCodeRegistration; | ||
|
|
||
| #ifdef _Py_JIT | ||
|
|
||
| /* Return a teardown handle when any backend stores registration state. | ||
| * A NULL result is valid when publication succeeded only through backends | ||
| * with no unregister step, such as perf map output. | ||
| */ | ||
| _PyJitCodeRegistration *_PyJit_RegisterCode(const void *code_addr, | ||
| size_t code_size, | ||
| const char *entry, | ||
| const char *filename); | ||
|
|
||
| void _PyJit_UnregisterCode(_PyJitCodeRegistration *registration); | ||
|
|
||
| #endif // _Py_JIT | ||
|
|
||
| #endif // Py_INTERNAL_JIT_PUBLISH_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,9 +10,14 @@ | |
|
|
||
| #if defined(_Py_JIT) && defined(__linux__) && defined(__ELF__) | ||
| # define PY_HAVE_JIT_GDB_UNWIND | ||
| # if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) | ||
|
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. This needs a separate configure link probe. For example, Linux builds with Please add a configure check for
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. For example this can break in:
Contributor
Author
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. ok, it makes sense.
Contributor
Author
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. link probe implemented here: ef9ea52 |
||
| # define PY_HAVE_JIT_GNU_BACKTRACE_UNWIND | ||
| # endif | ||
| #endif | ||
|
|
||
| #if defined(PY_HAVE_PERF_TRAMPOLINE) || defined(PY_HAVE_JIT_GDB_UNWIND) | ||
| #if defined(PY_HAVE_PERF_TRAMPOLINE) \ | ||
| || defined(PY_HAVE_JIT_GDB_UNWIND) \ | ||
| || defined(PY_HAVE_JIT_GNU_BACKTRACE_UNWIND) | ||
|
|
||
| #if defined(PY_HAVE_JIT_GDB_UNWIND) | ||
| extern PyMutex _Py_jit_debug_mutex; | ||
|
|
@@ -63,6 +68,13 @@ void *_PyJitUnwind_GdbRegisterCode(const void *code_addr, | |
|
|
||
| void _PyJitUnwind_GdbUnregisterCode(void *handle); | ||
|
|
||
| #endif // defined(PY_HAVE_PERF_TRAMPOLINE) || defined(PY_HAVE_JIT_GDB_UNWIND) | ||
| #if defined(PY_HAVE_JIT_GNU_BACKTRACE_UNWIND) | ||
| void *_PyJitUnwind_GnuBacktraceRegisterCode(const void *code_addr, | ||
| size_t code_size); | ||
|
|
||
| void _PyJitUnwind_GnuBacktraceUnregisterCode(void *handle); | ||
| #endif | ||
|
|
||
| #endif // JIT unwind support | ||
|
|
||
| #endif // Py_INTERNAL_JIT_UNWIND_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,7 +70,7 @@ def _frame_pointers_expected(machine): | |
| return None | ||
|
|
||
|
|
||
| def _build_stack_and_unwind(): | ||
| def _build_stack_and_unwind(unwinder): | ||
| import operator | ||
|
|
||
| def build_stack(n, unwinder, warming_up_caller=False): | ||
|
|
@@ -89,7 +89,7 @@ def build_stack(n, unwinder, warming_up_caller=False): | |
| result = operator.call(build_stack, n - 1, unwinder, warming_up) | ||
| return result | ||
|
|
||
| stack = build_stack(10, _testinternalcapi.manual_frame_pointer_unwind) | ||
| stack = build_stack(10, unwinder) | ||
| return stack | ||
|
|
||
|
|
||
|
|
@@ -112,8 +112,9 @@ def _classify_stack(stack, jit_enabled): | |
| return annotated, python_frames, jit_frames, other_frames | ||
|
|
||
|
|
||
| def _annotate_unwind(): | ||
| stack = _build_stack_and_unwind() | ||
| def _annotate_unwind(unwinder_name="manual_frame_pointer_unwind"): | ||
| unwinder = getattr(_testinternalcapi, unwinder_name) | ||
| stack = _build_stack_and_unwind(unwinder) | ||
| jit_enabled = hasattr(sys, "_jit") and sys._jit.is_enabled() | ||
| jit_backend = _testinternalcapi.get_jit_backend() | ||
| ranges = _testinternalcapi.get_jit_code_ranges() if jit_enabled else [] | ||
|
|
@@ -132,13 +133,14 @@ def _annotate_unwind(): | |
| "jit_frames": jit_frames, | ||
| "other_frames": other_frames, | ||
| "jit_backend": jit_backend, | ||
| "unwinder": unwinder_name, | ||
| }) | ||
|
|
||
|
|
||
| def _manual_unwind_length(**env): | ||
| def _unwind_result(unwinder_name, **env): | ||
| code = ( | ||
| "from test.test_frame_pointer_unwind import _annotate_unwind; " | ||
| "print(_annotate_unwind());" | ||
| f"print(_annotate_unwind({unwinder_name!r}));" | ||
| ) | ||
| run_env = os.environ.copy() | ||
| run_env.update(env) | ||
|
|
@@ -197,7 +199,7 @@ def test_manual_unwind_respects_frame_pointers(self): | |
|
|
||
| for env, using_jit in envs: | ||
| with self.subTest(env=env): | ||
| result = _manual_unwind_length(**env) | ||
| result = _unwind_result("manual_frame_pointer_unwind", **env) | ||
| jit_frames = result["jit_frames"] | ||
| python_frames = result.get("python_frames", 0) | ||
| jit_backend = result.get("jit_backend") | ||
|
|
@@ -240,5 +242,51 @@ def test_manual_unwind_respects_frame_pointers(self): | |
| ) | ||
|
|
||
|
|
||
| @support.requires_gil_enabled("test requires the GIL enabled") | ||
| @unittest.skipIf(support.is_wasi, "test not supported on WASI") | ||
| @unittest.skipUnless(sys.platform == "linux", "GNU backtrace unwinding test requires Linux") | ||
| class GnuBacktraceUnwindTests(unittest.TestCase): | ||
|
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. The new test only asserts
Contributor
Author
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. Addressed here: 01df239 |
||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| try: | ||
| _testinternalcapi.gnu_backtrace_unwind() | ||
| except RuntimeError as exc: | ||
| if "not supported" in str(exc): | ||
| self.skipTest("gnu backtrace unwinding not supported on this platform") | ||
| raise | ||
|
|
||
| def test_gnu_backtrace_unwinds_through_jit_frames(self): | ||
| jit_available = hasattr(sys, "_jit") and sys._jit.is_available() | ||
| envs = [({"PYTHON_JIT": "0"}, False)] | ||
| if jit_available: | ||
| envs.append(({"PYTHON_JIT": "1"}, True)) | ||
|
|
||
| for env, using_jit in envs: | ||
| with self.subTest(env=env): | ||
| result = _unwind_result("gnu_backtrace_unwind", **env) | ||
| python_frames = result.get("python_frames", 0) | ||
| jit_frames = result.get("jit_frames", 0) | ||
| jit_backend = result.get("jit_backend") | ||
|
|
||
| self.assertGreater( | ||
| python_frames, | ||
| 0, | ||
| f"expected to find Python frames in GNU backtrace with env {env}", | ||
| ) | ||
| if using_jit and jit_backend == "jit": | ||
| self.assertGreater( | ||
| jit_frames, | ||
| 0, | ||
| f"expected GNU backtrace to include JIT frames with env {env}", | ||
| ) | ||
| else: | ||
| self.assertEqual( | ||
| jit_frames, | ||
| 0, | ||
| f"unexpected JIT frames counted in GNU backtrace with env {env}", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add support for unwinding JIT frames using GNU backtrace. Patch by Diego Russo |
Uh oh!
There was an error while loading. Please reload this page.
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.
_PyJit_RegisterCodereturns NULL for three different reasons (perf-only success, calloc failure, all backends failed) and the caller can't tell them apart. Could you renameregistered->any_registeredand add a one-liner near the perf branch noting it's intentionally not counted? The deleted comment fromjit_record_codeabout partial-failure being non-fatal would also be nice to restore.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.
Addressed here: cf16da0