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