|
| 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 = lldb.SBError() |
| 29 | + thread.SetScriptedFrameProvider( |
| 30 | + "my_module.MyFrameProvider", |
| 31 | + lldb.SBStructuredData() |
| 32 | + ) |
| 33 | + """ |
| 34 | + |
| 35 | + @abstractmethod |
| 36 | + def __init__(self, thread, args): |
| 37 | + """Construct a scripted frame provider. |
| 38 | +
|
| 39 | + Args: |
| 40 | + thread (lldb.SBThread): The thread for which to provide frames. |
| 41 | + args (lldb.SBStructuredData): A Dictionary holding arbitrary |
| 42 | + key/value pairs used by the scripted frame provider. |
| 43 | + """ |
| 44 | + self.thread = None |
| 45 | + self.args = None |
| 46 | + self.target = None |
| 47 | + self.process = None |
| 48 | + |
| 49 | + if isinstance(thread, lldb.SBThread) and thread.IsValid(): |
| 50 | + self.thread = thread |
| 51 | + self.process = thread.GetProcess() |
| 52 | + if self.process and self.process.IsValid(): |
| 53 | + self.target = self.process.GetTarget() |
| 54 | + |
| 55 | + if isinstance(args, lldb.SBStructuredData) and args.IsValid(): |
| 56 | + self.args = args |
| 57 | + |
| 58 | + def get_merge_strategy(self): |
| 59 | + """Get the merge strategy for how scripted frames should be integrated. |
| 60 | +
|
| 61 | + The merge strategy determines how the scripted frames are combined with the |
| 62 | + real unwound frames from the thread's normal unwinder. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + int: One of the following lldb.ScriptedFrameProviderMergeStrategy values: |
| 66 | +
|
| 67 | + - lldb.eScriptedFrameProviderMergeStrategyReplace: Replace the entire stack |
| 68 | + with scripted frames. The thread will only show frames provided |
| 69 | + by this provider. |
| 70 | +
|
| 71 | + - lldb.eScriptedFrameProviderMergeStrategyPrepend: Prepend scripted frames |
| 72 | + before the real unwound frames. Useful for adding synthetic frames |
| 73 | + at the top of the stack while preserving the actual callstack below. |
| 74 | +
|
| 75 | + - lldb.eScriptedFrameProviderMergeStrategyAppend: Append scripted frames |
| 76 | + after the real unwound frames. Useful for showing additional context |
| 77 | + after the actual callstack ends. |
| 78 | +
|
| 79 | + - lldb.eScriptedFrameProviderMergeStrategyReplaceByIndex: Replace specific |
| 80 | + frames at given indices with scripted frames, keeping other real frames |
| 81 | + intact. The idx field in each frame dictionary determines which real |
| 82 | + frame to replace (e.g., idx=0 replaces frame 0, idx=2 replaces frame 2). |
| 83 | +
|
| 84 | + The default implementation returns Replace strategy. |
| 85 | +
|
| 86 | + Example: |
| 87 | +
|
| 88 | + .. code-block:: python |
| 89 | +
|
| 90 | + def get_merge_strategy(self): |
| 91 | + # Only show our custom frames |
| 92 | + return lldb.eScriptedFrameProviderMergeStrategyReplace |
| 93 | +
|
| 94 | + def get_merge_strategy(self): |
| 95 | + # Add diagnostic frames on top of real stack |
| 96 | + return lldb.eScriptedFrameProviderMergeStrategyPrepend |
| 97 | +
|
| 98 | + def get_merge_strategy(self): |
| 99 | + # Replace frame 0 and frame 2 with custom frames, keep others |
| 100 | + return lldb.eScriptedFrameProviderMergeStrategyReplaceByIndex |
| 101 | + """ |
| 102 | + return lldb.eScriptedFrameProviderMergeStrategyReplace |
| 103 | + |
| 104 | + @abstractmethod |
| 105 | + def get_stackframes(self): |
| 106 | + """Get the list of stack frames to provide. |
| 107 | +
|
| 108 | + This method is called when the thread's backtrace is requested |
| 109 | + (e.g., via the 'bt' command). The returned frames will be integrated |
| 110 | + with the real frames according to the mode returned by get_mode(). |
| 111 | +
|
| 112 | + Returns: |
| 113 | + List[Dict]: A list of frame dictionaries, where each dictionary |
| 114 | + describes a single stack frame. Each dictionary should contain: |
| 115 | +
|
| 116 | + Required fields: |
| 117 | + - idx (int): The frame index (0 for innermost/top frame) |
| 118 | + - pc (int): The program counter address for this frame |
| 119 | +
|
| 120 | + Alternatively, you can return a list of ScriptedFrame objects |
| 121 | + for more control over frame behavior. |
| 122 | +
|
| 123 | + Example: |
| 124 | +
|
| 125 | + .. code-block:: python |
| 126 | +
|
| 127 | + def get_stackframes(self): |
| 128 | + frames = [] |
| 129 | +
|
| 130 | + # Frame 0: Current function |
| 131 | + frames.append({ |
| 132 | + "idx": 0, |
| 133 | + "pc": 0x100001234, |
| 134 | + }) |
| 135 | +
|
| 136 | + # Frame 1: Caller |
| 137 | + frames.append({ |
| 138 | + "idx": 1, |
| 139 | + "pc": 0x100001000, |
| 140 | + }) |
| 141 | +
|
| 142 | + return frames |
| 143 | +
|
| 144 | + Note: |
| 145 | + The frames are indexed from 0 (innermost/newest) to N (outermost/oldest). |
| 146 | + """ |
| 147 | + pass |
0 commit comments