xref: /llvm-project/lldb/examples/python/templates/scripted_thread_plan.py (revision 622df0ee9226b90e924538909337d55333d5d2fa)
1from abc import abstractmethod
2
3import lldb
4
5
6class ScriptedThreadPlan:
7    """
8    Class that provides data for an instance of a LLDB 'ScriptedThreadPlan' plug-in class used to construct custom stepping logic.
9
10    """
11
12    def __init__(self, thread_plan: lldb.SBThreadPlan):
13        """Initialization needs a valid lldb.SBThreadPlan object. This plug-in will get created after a live process is valid and has stopped.
14
15        Args:
16            thread_plan (lldb.SBThreadPlan): The underlying `ThreadPlan` that is pushed onto the plan stack.
17        """
18        self.thread_plan = thread_plan
19
20    def explains_stop(self, event: lldb.SBEvent) -> bool:
21        """Each plan is asked from youngest to oldest if it "explains" the stop. The first plan to claim the stop wins.
22
23        Args:
24            event (lldb.SBEvent): The process stop event.
25
26        Returns:
27            bool: `True` if this stop could be claimed by this thread plan, `False` otherwise.
28            Defaults to `True`.
29        """
30        return True
31
32    def is_stale(self) -> bool:
33        """If your plan is no longer relevant (for instance, you were stepping in a particular stack frame, but some other operation pushed that frame off the stack) return True and your plan will get popped.
34
35        Returns:
36            bool: `True` if this thread plan is stale, `False` otherwise.
37            Defaults to `False`.
38        """
39        return False
40
41    def should_stop(self, event: lldb.SBEvent) -> bool:
42        """Whether this thread plan should stop and return control to the user.
43        If your plan is done at this point, call SetPlanComplete on your thread plan instance. Also, do any work you need here to set up the next stage of stepping.
44
45        Args:
46            event (lldb.SBEvent): The process stop event.
47
48        Returns:
49            bool: `True` if this plan wants to stop and return control to the user at this point, `False` otherwise.
50            Defaults to `False`.
51        """
52        self.thread_plan.SetPlanComplete(True)
53        return True
54
55    def should_step(self) -> bool:
56        """Whether this thread plan should instruction step one instruction, or continue till the next breakpoint is hit.
57
58        Returns:
59            bool: `True` if this plan will instruction step one instruction, `False` otherwise.
60            Defaults to `True`.
61        """
62        return True
63
64    def stop_description(self, stream: lldb.SBStream) -> None:
65        """Customize the thread plan stop reason when the thread plan is complete.
66
67        Args:
68            stream (lldb.SBStream): The stream containing the stop description.
69        """
70        pass
71