xref: /llvm-project/lldb/test/API/functionalities/scripted_process/invalid_scripted_process.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
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
9
10class InvalidScriptedProcess(ScriptedProcess):
11    def __init__(self, exe_ctx: lldb.SBExecutionContext, args: lldb.SBStructuredData):
12        super().__init__(exe_ctx, args)
13        self.threads[0] = InvalidScriptedThread(self, None)
14
15    def read_memory_at_address(
16        self, addr: int, size: int, error: lldb.SBError
17    ) -> lldb.SBData:
18        error.SetErrorString("This is an invalid scripted process!")
19        return lldb.SBData()
20
21    def get_loaded_images(self):
22        return self.loaded_images
23
24    def get_process_id(self) -> int:
25        return 666
26
27    def should_stop(self) -> bool:
28        return True
29
30    def is_alive(self) -> bool:
31        return True
32
33    def get_scripted_thread_plugin(self):
34        return InvalidScriptedThread.__module__ + "." + InvalidScriptedThread.__name__
35
36
37class InvalidScriptedThread(ScriptedThread):
38    def __init__(self, process, args):
39        super().__init__(process, args)
40
41    def get_thread_id(self) -> int:
42        return 0x19
43
44    def get_name(self) -> str:
45        return InvalidScriptedThread.__name__ + ".thread-1"
46
47    def get_state(self) -> int:
48        return lldb.eStateInvalid
49
50    def get_stop_reason(self) -> Dict[str, Any]:
51        return {"type": lldb.eStopReasonSignal, "data": {"signal": signal.SIGTRAP}}
52
53    def get_register_context(self) -> str:
54        return None
55
56
57def __lldb_init_module(debugger, dict):
58    if not "SKIP_SCRIPTED_PROCESS_LAUNCH" in os.environ:
59        debugger.HandleCommand(
60            "process launch -C %s.%s" % (__name__, InvalidScriptedProcess.__name__)
61        )
62    else:
63        print(
64            "Name of the class that will manage the scripted process: '%s.%s'"
65            % (__name__, InvalidScriptedProcess.__name__)
66        )
67