[lldb/script] Add scripting extension template generator (#209647)

This patch adds a `scripting extension generate <ExtensionType>...`
command that introspects a Python extension base class and emits a
skeleton subclass with `# TODO: Implement` stubs for its abstract
methods (or, with `-a`, every method), then opens the result in an
editor.

Generated imports use `from <module> import <class>`, and the generated
`__init__` forwards its arguments to `super().__init__(...)` since every
base class relies on its constructor to set up attributes
(`self.target`, `self.process`, ...) that inherited, non-overridden
methods depend on. When the host can't open an external editor (e.g.
non-macOS), the command reports that as a message rather than an error,
since the file is already written.

To catch regressions in the generator itself instead of just checking
that a file was produced, this patch adds
`TestScriptingExtensionGenerate.py`, which generates a template for
every extension kind on the fly and drives it through a live lldb
session (`process launch -C`, `breakpoint set -P`, `target stop-hook add
-P`, etc.).

Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
(cherry picked from commit c4962f5721a36e198234019b846d3788738f766a)
This commit is contained in:
Med Ismail Bennani authored and Tobias Hieta committed 2026-07-23 08:09:06 +02:00
1 parent e28ea20ce0
commit 3df0bbdc25
13 files changed
+961 -12

No files matched your search

@@ -16,6 +16,7 @@
#include "lldb/lldb-private.h"
#include <memory>
#include <set>
#include <string>
#include <vector>
@@ -41,7 +42,15 @@ public:
: ScriptInterpreter(debugger, lldb::eScriptLanguagePython),
IOHandlerDelegateMultiline("DONE") {}
llvm::Expected<std::string>
ExtensionToImportPath(lldb::ScriptedExtension extension) override;
StructuredData::DictionarySP GetInterpreterInfo() override;
llvm::Expected<FileSpec>
GenerateExtensionTemplate(const std::string &name,
std::vector<ExtensionTemplateRequest> &extensions,
bool generate_non_abstract_methods,
std::string output_file) override;
static void Initialize();
static void Terminate();
static llvm::StringRef GetPluginNameStatic() { return "script-python"; }
@@ -50,6 +59,14 @@ public:
static void SharedLibraryDirectoryHelper(FileSpec &this_file);
protected:
llvm::Error
ParseExtensionSchema(Stream &s, llvm::StringRef output_script_prefix,
const llvm::SmallVector<llvm::StringRef> &extension_path,
bool generate_non_abstract_methods,
std::set<std::string> &typing_imports);
llvm::Expected<StructuredData::ObjectSP>
GetExtensionSchema(const llvm::SmallVector<llvm::StringRef> &extension_path);
static void ComputePythonDirForApple(llvm::SmallVectorImpl<char> &path);
static void ComputePythonDir(llvm::SmallVectorImpl<char> &path);
};