|
| 1 | +from abc import ABCMeta, abstractmethod |
| 2 | + |
| 3 | +import lldb |
| 4 | + |
| 5 | + |
| 6 | +class ScriptedFrameProvider(metaclass=ABCMeta): |
| 7 | + """ |
| 8 | + The base class for a scripted frame provider. |
| 9 | +
|
| 10 | + A scripted frame provider allows you to provide custom stack frames for a |
| 11 | + thread, which can be used to augment or replace the standard unwinding |
| 12 | + mechanism. This is useful for: |
| 13 | +
|
| 14 | + - Providing frames for custom calling conventions or languages |
| 15 | + - Reconstructing missing frames from crash dumps or core files |
| 16 | + - Adding diagnostic or synthetic frames for debugging |
| 17 | + - Visualizing state machines or async execution contexts |
| 18 | +
|
| 19 | + Most of the base class methods are `@abstractmethod` that need to be |
| 20 | + overwritten by the inheriting class. |
| 21 | +
|
| 22 | + Example usage: |
| 23 | +
|
| 24 | + .. code-block:: python |
| 25 | +
|
| 26 | + # Attach a frame provider to a thread |
| 27 | + thread = process.GetSelectedThread() |
| 28 | + error = thread.SetScriptedFrameProvider( |
| 29 | + "my_module.MyFrameProvider", |
| 30 | + lldb.SBStructuredData() |
| 31 | + ) |
| 32 | + """ |
| 33 | + |
| 34 | + @abstractmethod |
| 35 | + def __init__(self, input_frames, args): |
| 36 | + """Construct a scripted frame provider. |
| 37 | +
|
| 38 | + Args: |
| 39 | + input_frames (lldb.SBFrameList): The frame list to use as input. |
| 40 | + This allows you to access frames by index. The frames are |
| 41 | + materialized lazily as you access them. |
| 42 | + args (lldb.SBStructuredData): A Dictionary holding arbitrary |
| 43 | + key/value pairs used by the scripted frame provider. |
| 44 | + """ |
| 45 | + self.input_frames = None |
| 46 | + self.args = None |
| 47 | + self.thread = None |
| 48 | + self.target = None |
| 49 | + self.process = None |
| 50 | + |
| 51 | + if isinstance(input_frames, lldb.SBFrameList) and input_frames.IsValid(): |
| 52 | + self.input_frames = input_frames |
| 53 | + self.thread = input_frames.GetThread() |
| 54 | + if self.thread and self.thread.IsValid(): |
| 55 | + self.process = self.thread.GetProcess() |
| 56 | + if self.process and self.process.IsValid(): |
| 57 | + self.target = self.process.GetTarget() |
| 58 | + |
| 59 | + if isinstance(args, lldb.SBStructuredData) and args.IsValid(): |
| 60 | + self.args = args |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + def get_frame_at_index(self, index): |
| 64 | + """Get a single stack frame at the given index. |
| 65 | +
|
| 66 | + This method is called lazily when a specific frame is needed in the |
| 67 | + thread's backtrace (e.g., via the 'bt' command). Each frame is |
| 68 | + requested individually as needed. |
| 69 | +
|
| 70 | + Args: |
| 71 | + index (int): The frame index to retrieve (0 for youngest/top frame). |
| 72 | +
|
| 73 | + Returns: |
| 74 | + Dict or None: A frame dictionary describing the stack frame, or None |
| 75 | + if no frame exists at this index. The dictionary should contain: |
| 76 | +
|
| 77 | + Required fields: |
| 78 | + - idx (int): The synthetic frame index (0 for youngest/top frame) |
| 79 | + - pc (int): The program counter address for the synthetic frame |
| 80 | +
|
| 81 | + Alternatively, you can return: |
| 82 | + - A ScriptedFrame object for full control over frame behavior |
| 83 | + - An integer representing an input frame index to reuse |
| 84 | + - None to indicate no more frames exist |
| 85 | +
|
| 86 | + Example: |
| 87 | +
|
| 88 | + .. code-block:: python |
| 89 | +
|
| 90 | + def get_frame_at_index(self, index): |
| 91 | + # Return None when there are no more frames |
| 92 | + if index >= self.total_frames: |
| 93 | + return None |
| 94 | +
|
| 95 | + # Re-use an input frame by returning its index |
| 96 | + if self.should_use_input_frame(index): |
| 97 | + return index # Returns input frame at this index |
| 98 | +
|
| 99 | + # Or create a custom frame dictionary |
| 100 | + if index == 0: |
| 101 | + return { |
| 102 | + "idx": 0, |
| 103 | + "pc": 0x100001234, |
| 104 | + } |
| 105 | +
|
| 106 | + return None |
| 107 | +
|
| 108 | + Note: |
| 109 | + The frames are indexed from 0 (youngest/top) to N (oldest/bottom). |
| 110 | + This method will be called repeatedly with increasing indices until |
| 111 | + None is returned. |
| 112 | + """ |
| 113 | + pass |
0 commit comments