1import os,struct, signal
2
3from typing import Any, Dict
4
5import lldb
6from lldb.plugins.scripted_process import ScriptedProcess
7from lldb.plugins.scripted_process import ScriptedThread
8
9class DummyScriptedProcess(ScriptedProcess):
10    def __init__(self, target: lldb.SBTarget, args : lldb.SBStructuredData):
11        super().__init__(target, args)
12        self.threads[0] = DummyScriptedThread(self, None)
13
14    def get_memory_region_containing_address(self, addr: int) -> lldb.SBMemoryRegionInfo:
15        return None
16
17    def get_thread_with_id(self, tid: int):
18        return {}
19
20    def get_registers_for_thread(self, tid: int):
21        return {}
22
23    def read_memory_at_address(self, addr: int, size: int) -> lldb.SBData:
24        data = lldb.SBData().CreateDataFromCString(
25                                    self.target.GetByteOrder(),
26                                    self.target.GetCodeByteSize(),
27                                    "Hello, world!")
28        return data
29
30    def get_loaded_images(self):
31        return self.loaded_images
32
33    def get_process_id(self) -> int:
34        return 42
35
36    def should_stop(self) -> bool:
37        return True
38
39    def is_alive(self) -> bool:
40        return True
41
42    def get_scripted_thread_plugin(self):
43        return DummyScriptedThread.__module__ + "." + DummyScriptedThread.__name__
44
45
46class DummyScriptedThread(ScriptedThread):
47    def __init__(self, process, args):
48        super().__init__(process, args)
49
50    def get_thread_id(self) -> int:
51        return 0x19
52
53    def get_name(self) -> str:
54        return DummyScriptedThread.__name__ + ".thread-1"
55
56    def get_state(self) -> int:
57        return lldb.eStateStopped
58
59    def get_stop_reason(self) -> Dict[str, Any]:
60        return { "type": lldb.eStopReasonSignal, "data": {
61            "signal": signal.SIGINT
62        } }
63
64    def get_stackframes(self):
65        class ScriptedStackFrame:
66            def __init__(idx, cfa, pc, symbol_ctx):
67                self.idx = idx
68                self.cfa = cfa
69                self.pc = pc
70                self.symbol_ctx = symbol_ctx
71
72
73        symbol_ctx = lldb.SBSymbolContext()
74        frame_zero = ScriptedStackFrame(0, 0x42424242, 0x5000000, symbol_ctx)
75        self.frames.append(frame_zero)
76
77        return self.frame_zero[0:0]
78
79    def get_register_context(self) -> str:
80        return struct.pack(
81                '21Q', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)
82
83
84def __lldb_init_module(debugger, dict):
85    if not 'SKIP_SCRIPTED_PROCESS_LAUNCH' in os.environ:
86        debugger.HandleCommand(
87            "process launch -C %s.%s" % (__name__,
88                                     DummyScriptedProcess.__name__))
89    else:
90        print("Name of the class that will manage the scripted process: '%s.%s'"
91                % (__name__, DummyScriptedProcess.__name__))