[Lldb-commits] [lldb] [lldb/script] Migrate ParsedCommand & raw commands onto ScriptedPythonInterface (PR #210430)

via lldb-commits lldb-commits at lists.llvm.org
Tue Jul 21 15:50:25 PDT 2026


================
@@ -0,0 +1,86 @@
+from abc import ABCMeta, abstractmethod
+from typing import Optional
+
+import lldb
+
+
+class ScriptedCommand(metaclass=ABCMeta):
+    """
+    The base class for a scripted (raw) command.
+
+    A raw command receives the unparsed argument string exactly as the user
+    typed it, and is responsible for any parsing it needs. For a command
+    with a table-driven option/argument parser, see `ParsedCommand` instead.
+    Register it with `command script add -c <ClassName> ...`.
+
+    Most of the base class methods are `@abstractmethod` that need to be
+    overwritten by the inheriting class.
+    """
+
+    def __init__(self, debugger: lldb.SBDebugger):
+        """Construct a scripted command.
+
+        Args:
+            debugger (lldb.SBDebugger): The debugger this command is being
+                added to.
+        """
+        pass
+
+    @abstractmethod
+    def __call__(
+        self,
+        debugger: lldb.SBDebugger,
+        args: str,
+        exe_ctx: lldb.SBExecutionContext,
+        result: lldb.SBCommandReturnObject,
+    ) -> None:
+        """Execute the command.
+
+        Args:
+            debugger (lldb.SBDebugger): The debugger the command runs
----------------
jimingham wrote:

The implementation of the command might want to use the debugger it was registered in, e.g.:

debugger.HandleCommand()

So it has to be able to get to the debugger from somewhere.

We could have added a `"lldb.GetDebuggerOwningCurrentlyExecutingScriptInterpreter()` and then everyone could use that to fetch the debugger to use.  I'm not sure how hard that would be to write, but anyway, we didn't do it that way.

The `__init__` needs to receive a debugger, since it has no other way to find it.

The callback doesn't, however, since it should have stashed ways the debugger it was passed in the `__init__`.  I think we probably carried this over from the function version - which does need the debugger (*) - without thinking.

(*) You might think you could get it from the Target in the ExecutionContext, but I'm pretty sure if there are no Targets, we don't put the dummy target in the ExecutionContext, so that's not guaranteed to work.

https://github.com/llvm/llvm-project/pull/210430


More information about the lldb-commits mailing list