aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-09-04[lldb] Introduce ScriptedFrame affordance (#149622)Med Ismail Bennani1-0/+5
This patch introduces a new scripting affordance in lldb: `ScriptedFrame`. This allows user to produce mock stackframes in scripted threads and scripted processes from a python script. With this change, StackFrame can be synthetized from different sources: - Either from a dictionary containing a load address, and a frame index, which is the legacy way. - Or by creating a ScriptedFrame python object. One particularity of synthezising stackframes from the ScriptedFrame python object, is that these frame have an optional PC, meaning that they don't have a report a valid PC and they can act as shells that just contain static information, like the frame function name, the list of variables or registers, etc. It can also provide a symbol context. rdar://157260006 Signed-off-by: Med Ismail Bennani <ismail@bennani.ma> Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2025-08-14[lldb] Use (only) PyImport_AppendInittab to patch readline (#153329)Jonas Devlieghere1-11/+1
The current implementation tries to (1) patch the existing readline module definition if it's already present in the inittab and (2) append our patched readline module to the inittab. The former (1) uses the non-stable Python API and I can't find a situation where this is necessary. We do this work before initialization, so for the readline module to exist, it either needs to be added by Python itself (which doesn't seem to be the case), or someone would have had to have added it without initializing.
2025-08-14[lldb] Use PyThread_get_thread_ident instead of accessing PyThreadState ↵Jonas Devlieghere1-2/+2
(#153460) Use `PyThread_get_thread_ident`, which is part of the Stable API, instead of accessing a member of the PyThreadState, which is opaque when using the Stable API.
2025-08-08[lldb] Only use PyConfig when LLDB_EMBED_PYTHON_HOME is enabled (#152588)Jonas Devlieghere1-19/+21
PyConfig and friends are not part of the stable API. We could switch back to Py_SetPythonHome, which has been deprecated, but still part of the stable API. For now, limit the use of PyConfig to when LLDB_EMBED_PYTHON_HOME is enabled, which essentially means Windows. Changing the order doesn't seem to matter.
2025-08-04[lldb] Eliminate PyGILState_Check (NFC) (#152006)Jonas Devlieghere1-2/+3
Eliminate calls to PyGILState_Check, which is not part of the Python Limited C API. In the Locker, we can use PyGILState_Ensure directly. We could do something similar to replace the assert, but I don't think it's worth it. We don't assert that we hold the GIL anywhere else.
2025-08-04[lldb] Simplify Python Locker log messages (NFC)Jonas Devlieghere1-11/+10
Eliminate the `log` variable by inlining the GetLog call and use "locked" and "unlocked" directly, as requested by Ismail in #151780.
2025-08-04[lldb] Eliminate InitializePythonRAII::InitializeThreadsPrivate (NFC) (#151780)Jonas Devlieghere1-41/+10
The behavior of thread initialization changed in Python 3.7. The minimum supported Python version is now 3.8. That means that `PyEval_ThreadsInitialized` always returns true and `PyEval_InitThreads` is never called. The helper function existed to coordinate initializing the threads and acquiring the GIL, which is no longer necessary. With that, there's no point in having the helper at all. This PR eliminates the function and inlines to GIL acquisition into the caller.
2025-08-01[lldb] Reimplment PyRun_SimpleString using the Python stable C API (#151777)Jonas Devlieghere1-24/+24
Reimplment `PyRun_SimpleString` using the Python stable C API and the `RunString` helper. Part of https://github.com/llvm/llvm-project/issues/151617
2025-07-28Switch the ScriptedBreakpointResolver over to the ScriptedInterface form ↵jimingham1-69/+5
(#150720) This is NFC, I'm modernizing the interface before I add to it in a subsequent commit.
2025-07-25[lldb] Use std::make_shared where possible (NFC) (#150714)Jonas Devlieghere1-2/+2
This is a continuation of 68fd102, which did the same thing but only for StopInfo. Using make_shared is both safer and more efficient: - With make_shared, the object and the control block are allocated together, which is more efficient. - With make_shared, the enable_shared_from_this base class is properly linked to the control block before the constructor finishes, so shared_from_this() will be safe to use (though still not recommended during construction).
2025-05-04[lldb] Remove unused local variables (NFC) (#138457)Kazu Hirata1-8/+0
2025-04-30[lldb] Upgrade `GetIndexOfChildWithName` to use `llvm::Expected` (#136693)Charles Zablit1-5/+7
This patch replaces the use of `UINT32_MAX` as the error return value of `GetIndexOfChildWithName` with `llvm::Expected`. # Tasks to do in another PR 1. Replace `CalculateNumChildrenIgnoringErrors` with `CalculateNumChildren`. See [this comment](https://github.com/llvm/llvm-project/pull/136693#discussion_r2056319358). 2. Update `lldb_private::formatters::ExtractIndexFromString` to use `llvm::Expected`. See [this comment](https://github.com/llvm/llvm-project/pull/136693#discussion_r2054217536). 3. Create a new class which carries both user and internal errors. See [this comment](https://github.com/llvm/llvm-project/pull/136693#discussion_r2056439608).
2025-04-18[lldb] Fix Python GIL-not-held issue in CreateStructuredDataFromScriptObject ↵Vladislav Dzhidzhoev1-1/+1
(#136309) TestStructuredDataAPI.py fails with Python debug build ver. 3.12+ due to call to Py_XINCREF while GIL is not held.
2025-04-01Add a new affordance that the Python module in a dSYM (#133290)jimingham1-1/+7
So the dSYM can be told what target it has been loaded into. When lldb is loading modules, while creating a target, it will run "command script import" on any Python modules in Resources/Python in the dSYM. However, this happens WHILE the target is being created, so it is not yet in the target list. That means that these scripts can't act on the target that they a part of when they get loaded. This patch adds a new python API that lldb will call: __lldb_module_added_to_target if it is defined in the module, passing in the Target the module was being added to, so that code in these dSYM's don't have to guess.
2025-03-25Fix the managing of the session dictionary when you have nested wrappers ↵jimingham1-5/+9
(#132846) Since the inner wrapper call might have removed one of the entries from the global dict that the outer wrapper ALSO was going to delete, make sure that we check that the key is still in the global dict before trying to act on it.
2025-02-19[lldb] Fix header include order in ScriptInterpreterPython.cppJonas Devlieghere1-4/+3
Should fix the following compile error on Windows: C:\Python312\include\pyconfig.h(225): error C2371: 'pid_t': redefinition; different basic types C:\buildbot\as-builder-10\lldb-x-aarch64\llvm-project\lldb\include\lldb/Host/windows/PosixApi.h(80): note: see declaration of 'pid_t'
2025-02-19[lldb] Synchronize the debuggers output & error streamsJonas Devlieghere1-16/+19
This patch improves the synchronization of the debugger's output and error streams using two new abstractions: `LockableStreamFile` and `LockedStreamFile`. - `LockableStreamFile` is a wrapper around a `StreamFile` and a mutex. Client cannot use the `StreamFile` without calling `Lock`, which returns a `LockedStreamFile`. - `LockedStreamFile` is an RAII object that locks the stream for the duration of its existence. As long as you hold on to the returned object you are permitted to write to the stream. The destruction of the object automatically flush the output stream.
2025-02-12[lldb] Remove Debugger::Get{Output,Error}Stream (NFC) (#126821)Jonas Devlieghere1-2/+2
Remove Debugger::GetOutputStream and Debugger::GetErrorStream in preparation for replacing both with a new variant that needs to be locked and hence can't be handed out like we do right now. The patch replaces most uses with GetAsyncOutputStream and GetAsyncErrorStream respectively. There methods return new StreamSP objects that automatically get flushed on destruction. See #126630 for more details.
2025-01-29[lldb][NFC] Format part of ScriptInterpreterPython.cppDavid Spickett1-6/+8
Was flagged in https://github.com/llvm/llvm-project/pull/124735 but done separately so it didn't get in the way of that.
2025-01-29[lldb] Make Python >= 3.8 required for LLDB 21 (#124735)David Spickett1-26/+1
As decided on https://discourse.llvm.org/t/rfc-lets-document-and-enforce-a-minimum-python-version-for-lldb/82731. LLDB 20 recommended `>= 3.8` but did not remove support for anything earlier. Now we are in what will become LLDB 21, so I'm removing that support and making `>= 3.8` required. See https://docs.python.org/3/c-api/apiabiversion.html#c.PY_VERSION_HEX for the format of PY_VERSION_HEX.
2024-10-31[lldb] Use PY_VERSION_HEX to simplify conditional compilation (NFC) (#114346)Jonas Devlieghere1-9/+8
Use PY_VERSION_HEX to simplify conditional compilation depending on the Python version. This also adds a static_assert to lldb-python to error out with a meaningful diagnostic when you try building LLDB with an older Python version in preparation for [1]. [1] https://discourse.llvm.org/t/rfc-lets-document-and-enforce-a-minimum-python-version-for-lldb/82731/15
2024-10-30[lldb] Fix formatting and whitespace in ScriptInterpreterPython (NFC)Jonas Devlieghere1-40/+35
2024-10-30[lldb] Use Py_InitializeFromConfig with Python >= 3.8 (NFC) (#114112)Jonas Devlieghere1-28/+35
This fixes the deprecation warning for Py_SetPythonHome, which was deprecated in Python 3.11. With this patch, when building against Python 3.8 or later, we now use Py_InitializeFromConfig instead. Fixes #113475
2024-10-30Revert "[lldb] Use Py_InitializeFromConfig with Python >= 3.8 (NFC)" (#114290)Jonas Devlieghere1-40/+28
Reverts llvm/llvm-project#114112 because this triggers a compile error: ``` no known conversion from 'str_type' (aka 'wchar_t *') to 'const char *' for 3rd argument 221 | PyAPI_FUNC(PyStatus) PyConfig_SetBytesString( | ^ 222 | PyConfig *config, 223 | wchar_t **config_str, 224 | const char *str); | ~~~~~~~~~~~~~~~ 1 error generated. ```
2024-10-30[lldb] Use Py_InitializeFromConfig with Python >= 3.8 (NFC) (#114112)Jonas Devlieghere1-28/+40
This fixes the deprecation warning for Py_SetPythonHome, which was deprecated in Python 3.11. With this patch, when building against Python 3.8 or later, we now use Py_InitializeFromConfig instead. Fixes #113475
2024-10-24[lldb] Move ValueObject into its own library (NFC) (#113393)Jonas Devlieghere1-1/+1
ValueObject is part of lldbCore for historical reasons, but conceptually it deserves to be its own library. This does introduce a (link-time) circular dependency between lldbCore and lldbValueObject, which is unfortunate but probably unavoidable because so many things in LLDB rely on ValueObject. We already have cycles and these libraries are never built as dylibs so while this doesn't improve the situation, it also doesn't make things worse. The header includes were updated with the following command: ``` find . -type f -exec sed -i.bak "s%include \"lldb/Core/ValueObject%include \"lldb/ValueObject/ValueObject%" '{}' \; ```
2024-09-24Add the ability to define custom completers to the parsed_cmd template. ↵jimingham1-0/+40
(#109062) If your arguments or option values are of a type that naturally uses one of our common completion mechanisms, you will get completion for free. But if you have your own custom values or if you want to do fancy things like have `break set -s foo.dylib -n ba<TAB>` only complete on symbols in foo.dylib, you can use this new mechanism to achieve that.
2024-09-20[lldb/Interpreter] Introduce ScriptedStopHook{,Python}Interface & make use ↵Med Ismail Bennani1-51/+5
of it (#109498) This patch re-lands #105449 and fixes the various test failures. --------- Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2024-09-20Revert "[lldb] Fix SWIG wrapper compilation error"David Spickett1-5/+51
...and "[lldb/Interpreter] Introduce `ScriptedStopHook{,Python}Interface` & make use of it (#105449)" This reverts commit 76b827bb4d5b4cc4d3229c4c6de2529e8b156810, and commit 1e131ddfa8f1d7b18c85c6e4079458be8b419421 because the first commit caused the test command-stop-hook-output.test to fail.
2024-09-19[lldb/Interpreter] Introduce `ScriptedStopHook{,Python}Interface` & make use ↵Med Ismail Bennani1-51/+5
of it (#105449) This patch introduces new `ScriptedStopHook{,Python}Interface` classes that make use of the Scripted Interface infrastructure and makes use of it in `StopHookScripted`. It also relax the requirement on the number of argument for initializing scripting extension if the size of the interface parameter pack contains 1 less element than the extension maximum number of positional arguments for this initializer. This addresses the cases where the embedded interpreter session dictionary is passed to the extension initializer which is not used most of the time. --------- Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2024-09-05[lldb] Make conversions from llvm::Error explicit with Status::FromEr… ↵Adrian Prantl1-5/+5
(#107163) …ror() [NFC]
2024-08-27[lldb] Turn lldb_private::Status into a value type. (#106163)Adrian Prantl1-46/+51
This patch removes all of the Set.* methods from Status. This cleanup is part of a series of patches that make it harder use the anti-pattern of keeping a long-lives Status object around and updating it while dropping any errors it contains on the floor. This patch is largely NFC, the more interesting next steps this enables is to: 1. remove Status.Clear() 2. assert that Status::operator=() never overwrites an error 3. remove Status::operator=() Note that step (2) will bring 90% of the benefits for users, and step (3) will dramatically clean up the error handling code in various places. In the end my goal is to convert all APIs that are of the form ` ResultTy DoFoo(Status& error) ` to ` llvm::Expected<ResultTy> DoFoo() ` How to read this patch? The interesting changes are in Status.h and Status.cpp, all other changes are mostly ` perl -pi -e 's/\.SetErrorString/ = Status::FromErrorString/g' $(git grep -l SetErrorString lldb/source) ` plus the occasional manual cleanup.
2024-08-23Revert "Revert "[lldb] Extend frame recognizers to hide frames from ↵Adrian Prantl1-0/+29
backtraces (#104523)"" This reverts commit 547917aebd1e79a8929b53f0ddf3b5185ee4df74.
2024-08-22Revert "[lldb] Extend frame recognizers to hide frames from backtraces ↵Dmitri Gribenko1-29/+0
(#104523)" This reverts commit f01f80ce6ca7640bb0e267b84b1ed0e89b57e2d9. This commit introduces an msan violation. See the discussion on https://github.com/llvm/llvm-project/pull/104523.
2024-08-20[lldb] Extend frame recognizers to hide frames from backtraces (#104523)Adrian Prantl1-0/+29
Compilers and language runtimes often use helper functions that are fundamentally uninteresting when debugging anything but the compiler/runtime itself. This patch introduces a user-extensible mechanism that allows for these frames to be hidden from backtraces and automatically skipped over when navigating the stack with `up` and `down`. This does not affect the numbering of frames, so `f <N>` will still provide access to the hidden frames. The `bt` output will also print a hint that frames have been hidden. My primary motivation for this feature is to hide thunks in the Swift programming language, but I'm including an example recognizer for `std::function::operator()` that I wished for myself many times while debugging LLDB. rdar://126629381 Example output. (Yes, my proof-of-concept recognizer could hide even more frames if we had a method that returned the function name without the return type or I used something that isn't based off regex, but it's really only meant as an example). before: ``` (lldb) thread backtrace --filtered=false * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1 * frame #0: 0x0000000100001f04 a.out`foo(x=1, y=1) at main.cpp:4:10 frame #1: 0x0000000100003a00 a.out`decltype(std::declval<int (*&)(int, int)>()(std::declval<int>(), std::declval<int>())) std::__1::__invoke[abi:se200000]<int (*&)(int, int), int, int>(__f=0x000000016fdff280, __args=0x000000016fdff224, __args=0x000000016fdff220) at invoke.h:149:25 frame #2: 0x000000010000399c a.out`int std::__1::__invoke_void_return_wrapper<int, false>::__call[abi:se200000]<int (*&)(int, int), int, int>(__args=0x000000016fdff280, __args=0x000000016fdff224, __args=0x000000016fdff220) at invoke.h:216:12 frame #3: 0x0000000100003968 a.out`std::__1::__function::__alloc_func<int (*)(int, int), std::__1::allocator<int (*)(int, int)>, int (int, int)>::operator()[abi:se200000](this=0x000000016fdff280, __arg=0x000000016fdff224, __arg=0x000000016fdff220) at function.h:171:12 frame #4: 0x00000001000026bc a.out`std::__1::__function::__func<int (*)(int, int), std::__1::allocator<int (*)(int, int)>, int (int, int)>::operator()(this=0x000000016fdff278, __arg=0x000000016fdff224, __arg=0x000000016fdff220) at function.h:313:10 frame #5: 0x0000000100003c38 a.out`std::__1::__function::__value_func<int (int, int)>::operator()[abi:se200000](this=0x000000016fdff278, __args=0x000000016fdff224, __args=0x000000016fdff220) const at function.h:430:12 frame #6: 0x0000000100002038 a.out`std::__1::function<int (int, int)>::operator()(this= Function = foo(int, int) , __arg=1, __arg=1) const at function.h:989:10 frame #7: 0x0000000100001f64 a.out`main(argc=1, argv=0x000000016fdff4f8) at main.cpp:9:10 frame #8: 0x0000000183cdf154 dyld`start + 2476 (lldb) ``` after ``` (lldb) bt * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1 * frame #0: 0x0000000100001f04 a.out`foo(x=1, y=1) at main.cpp:4:10 frame #1: 0x0000000100003a00 a.out`decltype(std::declval<int (*&)(int, int)>()(std::declval<int>(), std::declval<int>())) std::__1::__invoke[abi:se200000]<int (*&)(int, int), int, int>(__f=0x000000016fdff280, __args=0x000000016fdff224, __args=0x000000016fdff220) at invoke.h:149:25 frame #2: 0x000000010000399c a.out`int std::__1::__invoke_void_return_wrapper<int, false>::__call[abi:se200000]<int (*&)(int, int), int, int>(__args=0x000000016fdff280, __args=0x000000016fdff224, __args=0x000000016fdff220) at invoke.h:216:12 frame #6: 0x0000000100002038 a.out`std::__1::function<int (int, int)>::operator()(this= Function = foo(int, int) , __arg=1, __arg=1) const at function.h:989:10 frame #7: 0x0000000100001f64 a.out`main(argc=1, argv=0x000000016fdff4f8) at main.cpp:9:10 frame #8: 0x0000000183cdf154 dyld`start + 2476 Note: Some frames were hidden by frame recognizers ```
2024-08-20[lldb] Fix windows debug build after 9d07f43 (#104896)Med Ismail Bennani1-4/+1
This patch tries to fix an issue with the windows debug builds where the PDB file for python scripted interfaces cannot be opened since its path length exceed the windows `MAX_PATH` limit: https://github.com/llvm/llvm-project/pull/101672#issuecomment-2289481324 This patch addresses the issue by building all the interfaces as a single library plugin that initiliazes each component as part of its `Initialize` method, instead of building each interface as its own library plugin. This keeps the build artifact path length smaller while respecting the naming convention and without making any exception in the build system. Fixes #104895. Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2024-08-02Reland "[lldb] Reland 2402b3213c2f with `/H` to debug the windows build ↵David Spickett1-1/+1
issue (#101672) This reverts commit 9effefbae8d96006a4dd29bb9ab8532fd408559d. With the include order in ScriptedProcessPythonInterface.cpp fixed (though I cannot explain exactly why it works) and removes the /H flag intended for debugging this issue. I think it is something to do with Process.h pulling in PosixApi.h somewhere along the line, and including Process.h after lldb-python.h means that NO_PID_T is defined to prevent a redefinition of pid_t.
2024-07-31Revert "[lldb] Reland 2402b3213c2f with `/H` to debug the windows build issue"Haowei Wu1-1/+1
This reverts commit e72cdae47b4e263ea97b2bdd75cf44c1510cf3be, which broke LLVM's lldb builder for Windows msvc.
2024-07-30[lldb] Reland 2402b3213c2f with `/H` to debug the windows build issueMed Ismail Bennani1-1/+1
This patch relands 2402b3213c2f to investigate the ambigious typedef issue happening on the windows bots: https://lab.llvm.org/buildbot/#/builders/141/builds/1175/ However this patch adds the `/H` compiler flag when building the ScriptedProcessPythonInterface library to be able to investigate the include order issue. This patch will be revert after 1 failing run on the windows bot. Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2024-07-30Revert "[lldb] Reland 2402b3213c2f with `-H` to debug the windows build issue"Med Ismail Bennani1-1/+1
This reverts commit c35c4c72e4977258fc1da940e0470e8d0671bf07.
2024-07-30[lldb] Reland 2402b3213c2f with `-H` to debug the windows build issueMed Ismail Bennani1-1/+1
This patch relands 2402b3213c2f to investigate the ambigious typedef issue happening on the windows bots: https://lab.llvm.org/buildbot/#/builders/141/builds/1175/ However this patch adds the `-H` compiler flag when building the ScriptedProcessPythonInterface library to be able to investigate the include order issue. This patch will be revert after 1 failing run on the windows bot. Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2024-07-29Revert "[lldb] Reland 2402b3213c2f with `-H -MM` to debug the windows build ↵Med Ismail Bennani1-1/+1
issue" This reverts commit 6bd5fd35063cd14b0a56bc6f7fb1e7b6e1ac56d5.
2024-07-29[lldb] Reland 2402b3213c2f with `-H -MM` to debug the windows build issueMed Ismail Bennani1-1/+1
This patch relands 2402b3213c2f to investigate the ambigious typedef issue happening on the windows bots: https://lab.llvm.org/buildbot/#/builders/141/builds/1175/ However this patch adds the `-H` & `-MM` compiler flags when building the ScriptedProcessPythonInterface library to be able to investigate the include order issue. This patch will be revert after 1 failing run on the windows bot. Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2024-07-27Revert "[lldb] Add ScriptedProcess to `scripting template list`"Med Ismail Bennani1-1/+1
This reverts commit 2402b3213c2f10d5033b31df3a0e59e4ad151fbd since it breaks the windows bot: https://lab.llvm.org/buildbot/#/builders/141/builds/1175/
2024-07-27[lldb] Add ScriptedProcess to `scripting template list`Med Ismail Bennani1-1/+1
This patch is a follow-up to bccff3baeff8 which adds the `ScriptedProcess` extension to the `scripting template list` command as well as its description. Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2024-07-27[lldb] Add OperatingSystem to `scripting template list`Med Ismail Bennani1-2/+1
This patch is a follow-up to bccff3baeff8 which adds the `OperatingSystem` extension to the `scripting template list` command as well as its description. Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2024-07-27[lldb] Add ScriptedPlatform to `scripting template list`Med Ismail Bennani1-1/+1
This patch is a follow-up to bccff3baeff8 which adds the `ScriptedPlatform` extension to the `scripting template list` command as well as its description. Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2024-07-27[lldb/Commands] Add `scripting template list` command with auto discoveryMed Ismail Bennani1-1/+1
This patch introduces a new `template` multiword sub-command to the `scripting` top-level command. As the name suggests, this sub-command operates on scripting templates, and currently has the ability to automatically discover the various scripting extensions that lldb supports. This was previously reviewed in #97273. Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
2024-07-25[lldb] Revert scripting template list patches (#100673)Med Ismail Bennani1-12/+7
Reverts https://github.com/llvm/llvm-project/pull/97273 since it broke the windows bot: https://lab.llvm.org/buildbot/#/builders/141/builds/1025/steps/4/logs/stdio
2024-07-25[lldb/Plugins] Fix build failure on windows following 2914a4b88837Med Ismail Bennani1-3/+8
This patch tries to fix the following build failure on windows: https://lab.llvm.org/buildbot/#/builders/141/builds/1083 This started happening following 2914a4b88837, and it seems to be caused by some special `#include` ordering for the lldb-python header on Windows. Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>