Files
Med Ismail Bennani 694db4d296 [lldb/script] Add type hints to scripted extension base classes (#209680)
This patch adds type annotations to every method (parameters and return
types) and every `self.<name>` attribute of the scripted extension base
classes shipped under `lldb/examples/python/templates/`. Extension
authors reading the base classes -- or an IDE navigating them -- can now
see the `lldb.SB*` contract each method exposes without having to
cross-reference the docstring.

`scripting extension generate` also picks the annotations up: derived
class docstrings advertise each inherited attribute together with its
declared type, and the generated method signatures carry their parameter
and return annotations too.

The classes covered are `ScriptedProcess`, `ScriptedThread`,
`ScriptedFrame`, `PassthroughScriptedProcess`,
`PassthroughScriptedThread`, `ScriptedFrameProvider`,
`ScriptedBreakpointResolver`, `ScriptedThreadPlan`, `ScriptedHook`,
`ScriptedPlatform`, and `OperatingSystem`.

Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
(cherry picked from commit f10ede511119d7f31f2c6aaca3cbcb1ea00164ce)
2026-07-23 08:09:06 +02:00

68 lines
2.2 KiB
Python

from abc import ABCMeta, abstractmethod
import lldb
class ScriptedHook(metaclass=ABCMeta):
"""
The base class for a scripted target hook.
A single `ScriptedHook` subclass backs both `target hook add -P` and
`target stop-hook add -P`. `handle_stop` is required so a hook can
always be attached as a stop-hook; `handle_module_loaded` and
`handle_module_unloaded` are optional and only called for hooks
registered via `target hook add -P`.
"""
target: lldb.SBTarget
args: lldb.SBStructuredData
@abstractmethod
def __init__(self, target: lldb.SBTarget, args: lldb.SBStructuredData):
"""Construct a scripted hook.
Args:
target (lldb.SBTarget): The target owning this hook.
args (lldb.SBStructuredData): A Dictionary holding arbitrary
key/value pairs used by the scripted hook.
"""
self.target = target
self.args = args
def handle_module_loaded(self, stream: lldb.SBStream) -> None:
"""Called whenever a module is loaded into the target.
Args:
stream (lldb.SBStream): The stream to which the hook can write
output that will be reported to the user.
"""
pass
def handle_module_unloaded(self, stream: lldb.SBStream) -> None:
"""Called whenever a module is unloaded from the target.
Args:
stream (lldb.SBStream): The stream to which the hook can write
output that will be reported to the user.
"""
pass
@abstractmethod
def handle_stop(
self, exe_ctx: lldb.SBExecutionContext, stream: lldb.SBStream
) -> bool:
"""Called whenever the process stops, before control is returned to
the user.
Args:
exe_ctx (lldb.SBExecutionContext): The execution context at the
point of the stop.
stream (lldb.SBStream): The stream to which the hook can write
output that will be reported to the user.
Returns:
bool: `True` if the process should stop and control should be
returned to the user, `False` if the process should keep running.
"""
pass