xref: /llvm-project/lldb/test/API/functionalities/scripted_process/dummy_scripted_process.py (revision c1928033047409f977b26ffc938d59188f1ced97)
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, exe_ctx: lldb.SBExecutionContext, args : lldb.SBStructuredData):
11        super().__init__(exe_ctx, 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, error: lldb.SBError) -> lldb.SBData:
24        data = lldb.SBData().CreateDataFromCString(
25                                    self.target.GetByteOrder(),
26                                    self.target.GetCodeByteSize(),
27                                    "Hello, world!")
28
29        return data
30
31    def get_loaded_images(self):
32        return self.loaded_images
33
34    def get_process_id(self) -> int:
35        return 42
36
37    def should_stop(self) -> bool:
38        return True
39
40    def is_alive(self) -> bool:
41        return True
42
43    def get_scripted_thread_plugin(self):
44        return DummyScriptedThread.__module__ + "." + DummyScriptedThread.__name__
45
46    def my_super_secret_method(self):
47        if hasattr(self, 'my_super_secret_member'):
48            return self.my_super_secret_member
49        else:
50            return None
51
52
53class DummyScriptedThread(ScriptedThread):
54    def __init__(self, process, args):
55        super().__init__(process, args)
56        self.frames.append({"pc": 0x0100001b00 })
57
58    def get_thread_id(self) -> int:
59        return 0x19
60
61    def get_name(self) -> str:
62        return DummyScriptedThread.__name__ + ".thread-1"
63
64    def get_state(self) -> int:
65        return lldb.eStateStopped
66
67    def get_stop_reason(self) -> Dict[str, Any]:
68        return { "type": lldb.eStopReasonSignal, "data": {
69            "signal": signal.SIGINT
70        } }
71
72    def get_register_context(self) -> str:
73        return struct.pack(
74                '21Q', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)
75
76
77def __lldb_init_module(debugger, dict):
78    if not 'SKIP_SCRIPTED_PROCESS_LAUNCH' in os.environ:
79        debugger.HandleCommand(
80            "process launch -C %s.%s" % (__name__,
81                                     DummyScriptedProcess.__name__))
82    else:
83        print("Name of the class that will manage the scripted process: '%s.%s'"
84                % (__name__, DummyScriptedProcess.__name__))
85