xref: /llvm-project/lldb/test/API/functionalities/gdb_remote_client/TestWasm.py (revision 4cc8f2a017c76af25234afc7c380550e9c93135c)
1import lldb
2import binascii
3from lldbsuite.test.lldbtest import *
4from lldbsuite.test.decorators import *
5from lldbsuite.test.gdbclientutils import *
6from lldbsuite.test.lldbgdbclient import GDBRemoteTestBase
7
8LLDB_INVALID_ADDRESS = lldb.LLDB_INVALID_ADDRESS
9load_address = 0x400000000
10
11def format_register_value(val):
12    """
13    Encode each byte by two hex digits in little-endian order.
14    """
15    result = ""
16    mask = 0xff
17    shift = 0
18    for i in range(0, 8):
19        x = (val & mask) >> shift
20        result += format(x, '02x')
21        mask <<= 8
22        shift += 8
23    return result
24
25
26class MyResponder(MockGDBServerResponder):
27    current_pc = load_address + 0x0a
28
29    def __init__(self, obj_path, module_name = ""):
30        self._obj_path = obj_path
31        self._module_name = module_name or obj_path
32        MockGDBServerResponder.__init__(self)
33
34    def respond(self, packet):
35        if packet[0:13] == "qRegisterInfo":
36            return self.qRegisterInfo(packet[13:])
37        return MockGDBServerResponder.respond(self, packet)
38
39    def qSupported(self, client_supported):
40        return "qXfer:libraries:read+;PacketSize=1000;vContSupported-"
41
42    def qHostInfo(self):
43        return ""
44
45    def QEnableErrorStrings(self):
46        return ""
47
48    def qfThreadInfo(self):
49        return "OK"
50
51    def qRegisterInfo(self, index):
52        if (index == 0):
53            return "name:pc;alt-name:pc;bitsize:64;offset:0;encoding:uint;format:hex;set:General Purpose Registers;gcc:16;dwarf:16;generic:pc;"
54        return "E45"
55
56    def qProcessInfo(self):
57        return "pid:1;ppid:1;uid:1;gid:1;euid:1;egid:1;name:%s;triple:%s;ptrsize:4" % (hex_encode_bytes("lldb"), hex_encode_bytes("wasm32-unknown-unknown-wasm"))
58
59    def haltReason(self):
60        return "T05thread:1;"
61
62    def readRegister(self, register):
63        return format_register_value(self.current_pc)
64
65    def qXferRead(self, obj, annex, offset, length):
66        if obj == "libraries":
67            xml = '<library-list><library name=\"%s\"><section address=\"%d\"/></library></library-list>' % (self._module_name, load_address)
68            return xml, False
69        else:
70            return None, False
71
72    def readMemory(self, addr, length):
73        if addr < load_address:
74            return "E02"
75        result = ""
76        with open(self._obj_path, mode='rb') as file:
77            file_content = bytearray(file.read())
78            addr_from = addr - load_address
79            addr_to = addr_from + min(length, len(file_content) - addr_from)
80            for i in range(addr_from, addr_to):
81                result += format(file_content[i], '02x')
82            file.close()
83        return result
84
85
86class TestWasm(GDBRemoteTestBase):
87
88    @skipIfAsan
89    @skipIfXmlSupportMissing
90    def test_load_module_with_embedded_symbols_from_remote(self):
91        """Test connecting to a WebAssembly engine via GDB-remote and loading a Wasm module with embedded DWARF symbols"""
92
93        yaml_path = "test_wasm_embedded_debug_sections.yaml"
94        yaml_base, ext = os.path.splitext(yaml_path)
95        obj_path = self.getBuildArtifact(yaml_base)
96        self.yaml2obj(yaml_path, obj_path)
97
98        self.server.responder = MyResponder(obj_path, "test_wasm")
99
100        target = self.dbg.CreateTarget("")
101        process = self.connect(target)
102        lldbutil.expect_state_changes(self, self.dbg.GetListener(), process, [lldb.eStateStopped])
103
104        num_modules = target.GetNumModules()
105        self.assertEquals(1, num_modules)
106
107        module = target.GetModuleAtIndex(0)
108        num_sections = module.GetNumSections()
109        self.assertEquals(5, num_sections)
110
111        code_section = module.GetSectionAtIndex(0)
112        self.assertEquals("code", code_section.GetName())
113        self.assertEquals(load_address | code_section.GetFileOffset(), code_section.GetLoadAddress(target))
114
115        debug_info_section = module.GetSectionAtIndex(1)
116        self.assertEquals(".debug_info", debug_info_section.GetName())
117        self.assertEquals(load_address | debug_info_section.GetFileOffset(), debug_info_section.GetLoadAddress(target))
118
119        debug_abbrev_section = module.GetSectionAtIndex(2)
120        self.assertEquals(".debug_abbrev", debug_abbrev_section.GetName())
121        self.assertEquals(load_address | debug_abbrev_section.GetFileOffset(), debug_abbrev_section.GetLoadAddress(target))
122
123        debug_line_section = module.GetSectionAtIndex(3)
124        self.assertEquals(".debug_line", debug_line_section.GetName())
125        self.assertEquals(load_address | debug_line_section.GetFileOffset(), debug_line_section.GetLoadAddress(target))
126
127        debug_str_section = module.GetSectionAtIndex(4)
128        self.assertEquals(".debug_str", debug_str_section.GetName())
129        self.assertEquals(load_address | debug_line_section.GetFileOffset(), debug_line_section.GetLoadAddress(target))
130
131
132    @skipIfAsan
133    @skipIfXmlSupportMissing
134    def test_load_module_with_stripped_symbols_from_remote(self):
135        """Test connecting to a WebAssembly engine via GDB-remote and loading a Wasm module with symbols stripped into a separate Wasm file"""
136
137        sym_yaml_path = "test_sym.yaml"
138        sym_yaml_base, ext = os.path.splitext(sym_yaml_path)
139        sym_obj_path = self.getBuildArtifact(sym_yaml_base) + ".wasm"
140        self.yaml2obj(sym_yaml_path, sym_obj_path)
141
142        yaml_path = "test_wasm_external_debug_sections.yaml"
143        yaml_base, ext = os.path.splitext(yaml_path)
144        obj_path = self.getBuildArtifact(yaml_base) + ".wasm"
145        self.yaml2obj(yaml_path, obj_path)
146
147        self.server.responder = MyResponder(obj_path, "test_wasm")
148
149        folder, _ = os.path.split(obj_path)
150        self.runCmd("settings set target.debug-file-search-paths " + os.path.abspath(folder))
151
152        target = self.dbg.CreateTarget("")
153        process = self.connect(target)
154        lldbutil.expect_state_changes(self, self.dbg.GetListener(), process, [lldb.eStateStopped])
155
156        num_modules = target.GetNumModules()
157        self.assertEquals(1, num_modules)
158
159        module = target.GetModuleAtIndex(0)
160        num_sections = module.GetNumSections()
161        self.assertEquals(5, num_sections)
162
163        code_section = module.GetSectionAtIndex(0)
164        self.assertEquals("code", code_section.GetName())
165        self.assertEquals(load_address | code_section.GetFileOffset(), code_section.GetLoadAddress(target))
166
167        debug_info_section = module.GetSectionAtIndex(1)
168        self.assertEquals(".debug_info", debug_info_section.GetName())
169        self.assertEquals(LLDB_INVALID_ADDRESS, debug_info_section.GetLoadAddress(target))
170
171        debug_abbrev_section = module.GetSectionAtIndex(2)
172        self.assertEquals(".debug_abbrev", debug_abbrev_section.GetName())
173        self.assertEquals(LLDB_INVALID_ADDRESS, debug_abbrev_section.GetLoadAddress(target))
174
175        debug_line_section = module.GetSectionAtIndex(3)
176        self.assertEquals(".debug_line", debug_line_section.GetName())
177        self.assertEquals(LLDB_INVALID_ADDRESS, debug_line_section.GetLoadAddress(target))
178
179        debug_str_section = module.GetSectionAtIndex(4)
180        self.assertEquals(".debug_str", debug_str_section.GetName())
181        self.assertEquals(LLDB_INVALID_ADDRESS, debug_line_section.GetLoadAddress(target))
182
183
184    @skipIfAsan
185    @skipIfXmlSupportMissing
186    def test_load_module_from_file(self):
187        """Test connecting to a WebAssembly engine via GDB-remote and loading a Wasm module from a file"""
188
189        yaml_path = "test_wasm_embedded_debug_sections.yaml"
190        yaml_base, ext = os.path.splitext(yaml_path)
191        obj_path = self.getBuildArtifact(yaml_base)
192        self.yaml2obj(yaml_path, obj_path)
193
194        self.server.responder = MyResponder(obj_path)
195
196        target = self.dbg.CreateTarget("")
197        process = self.connect(target)
198        lldbutil.expect_state_changes(self, self.dbg.GetListener(), process, [lldb.eStateStopped])
199
200        num_modules = target.GetNumModules()
201        self.assertEquals(1, num_modules)
202
203        module = target.GetModuleAtIndex(0)
204        num_sections = module.GetNumSections()
205        self.assertEquals(5, num_sections)
206
207        code_section = module.GetSectionAtIndex(0)
208        self.assertEquals("code", code_section.GetName())
209        self.assertEquals(load_address | code_section.GetFileOffset(), code_section.GetLoadAddress(target))
210
211        debug_info_section = module.GetSectionAtIndex(1)
212        self.assertEquals(".debug_info", debug_info_section.GetName())
213        self.assertEquals(LLDB_INVALID_ADDRESS, debug_info_section.GetLoadAddress(target))
214
215        debug_abbrev_section = module.GetSectionAtIndex(2)
216        self.assertEquals(".debug_abbrev", debug_abbrev_section.GetName())
217        self.assertEquals(LLDB_INVALID_ADDRESS, debug_abbrev_section.GetLoadAddress(target))
218
219        debug_line_section = module.GetSectionAtIndex(3)
220        self.assertEquals(".debug_line", debug_line_section.GetName())
221        self.assertEquals(LLDB_INVALID_ADDRESS, debug_line_section.GetLoadAddress(target))
222
223        debug_str_section = module.GetSectionAtIndex(4)
224        self.assertEquals(".debug_str", debug_str_section.GetName())
225        self.assertEquals(LLDB_INVALID_ADDRESS, debug_line_section.GetLoadAddress(target))
226
227