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        self.frames.append({"pc": 0x0100001b00 })
50
51    def get_thread_id(self) -> int:
52        return 0x19
53
54    def get_name(self) -> str:
55        return DummyScriptedThread.__name__ + ".thread-1"
56
57    def get_state(self) -> int:
58        return lldb.eStateStopped
59
60    def get_stop_reason(self) -> Dict[str, Any]:
61        return { "type": lldb.eStopReasonSignal, "data": {
62            "signal": signal.SIGINT
63        } }
64
65    def get_register_context(self) -> str:
66        return struct.pack(
67                '21Q', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)
68
69
70def __lldb_init_module(debugger, dict):
71    if not 'SKIP_SCRIPTED_PROCESS_LAUNCH' in os.environ:
72        debugger.HandleCommand(
73            "process launch -C %s.%s" % (__name__,
74                                     DummyScriptedProcess.__name__))
75    else:
76        print("Name of the class that will manage the scripted process: '%s.%s'"
77                % (__name__, DummyScriptedProcess.__name__))
78