fc26c3b6c86d60fd0c0a6638f4b319e88daec922
[openbsd] /
1 """Test reading c-strings from memory via SB API."""
2
3
4 import os
5 import lldb
6 from lldbsuite.test.decorators import *
7 from lldbsuite.test.lldbtest import *
8 from lldbsuite.test import lldbutil
9
10
11 class TestReadMemCString(TestBase):
12
13     mydir = TestBase.compute_mydir(__file__)
14     NO_DEBUG_INFO_TESTCASE = True
15
16     def test_read_memory_c_string(self):
17         """Test corner case behavior of SBProcess::ReadCStringFromMemory"""
18         self.build()
19         self.dbg.SetAsync(False)
20
21         self.main_source = "main.c"
22         self.main_source_path = os.path.join(self.getSourceDir(),
23                                              self.main_source)
24         self.main_source_spec = lldb.SBFileSpec(self.main_source_path)
25         self.exe = self.getBuildArtifact("read-mem-cstring")
26
27         (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
28             self, 'breakpoint here', self.main_source_spec, None, self.exe)
29
30         frame = thread.GetFrameAtIndex(0)
31
32         err = lldb.SBError()
33
34         empty_str_addr = frame.FindVariable("empty_string").GetValueAsUnsigned(err)
35         self.assertTrue(err.Success())
36         self.assertTrue(empty_str_addr != lldb.LLDB_INVALID_ADDRESS)
37
38         one_letter_str_addr = frame.FindVariable("one_letter_string").GetValueAsUnsigned(err)
39         self.assertTrue(err.Success())
40         self.assertTrue(one_letter_str_addr != lldb.LLDB_INVALID_ADDRESS)
41
42         invalid_memory_str_addr = frame.FindVariable("invalid_memory_string").GetValueAsUnsigned(err)
43         self.assertTrue(err.Success())
44         self.assertTrue(invalid_memory_str_addr != lldb.LLDB_INVALID_ADDRESS)
45
46         # Important:  An empty (0-length) c-string must come back as a Python string, not a
47         # None object.
48         empty_str = process.ReadCStringFromMemory(empty_str_addr, 2048, err)
49         self.assertTrue(err.Success())
50         self.assertTrue(empty_str == "")
51
52         one_letter_string = process.ReadCStringFromMemory(one_letter_str_addr, 2048, err)
53         self.assertTrue(err.Success())
54         self.assertTrue(one_letter_string == "1")
55
56         invalid_memory_string = process.ReadCStringFromMemory(invalid_memory_str_addr, 2048, err)
57         self.assertTrue(err.Fail())
58         self.assertTrue(invalid_memory_string == "" or invalid_memory_string == None)