1 """Test reading c-strings from memory via SB API."""
6 from lldbsuite
.test
.decorators
import *
7 from lldbsuite
.test
.lldbtest
import *
8 from lldbsuite
.test
import lldbutil
11 class TestReadMemCString(TestBase
):
12 NO_DEBUG_INFO_TESTCASE
= True
14 def test_read_memory_c_string(self
):
15 """Test corner case behavior of SBProcess::ReadCStringFromMemory"""
17 self
.dbg
.SetAsync(False)
19 self
.main_source
= "main.c"
20 self
.main_source_path
= os
.path
.join(self
.getSourceDir(), self
.main_source
)
21 self
.main_source_spec
= lldb
.SBFileSpec(self
.main_source_path
)
22 self
.exe
= self
.getBuildArtifact("read-mem-cstring")
24 (target
, process
, thread
, bkpt
) = lldbutil
.run_to_source_breakpoint(
25 self
, "breakpoint here", self
.main_source_spec
, None, self
.exe
28 frame
= thread
.GetFrameAtIndex(0)
32 empty_str_addr
= frame
.FindVariable("empty_string").GetValueAsUnsigned(err
)
33 self
.assertSuccess(err
)
34 self
.assertNotEqual(empty_str_addr
, lldb
.LLDB_INVALID_ADDRESS
)
36 one_letter_str_addr
= frame
.FindVariable(
38 ).GetValueAsUnsigned(err
)
39 self
.assertSuccess(err
)
40 self
.assertNotEqual(one_letter_str_addr
, lldb
.LLDB_INVALID_ADDRESS
)
42 invalid_memory_str_addr
= frame
.FindVariable(
43 "invalid_memory_string"
44 ).GetValueAsUnsigned(err
)
45 self
.assertSuccess(err
)
46 self
.assertNotEqual(invalid_memory_str_addr
, lldb
.LLDB_INVALID_ADDRESS
)
48 # Important: An empty (0-length) c-string must come back as a Python string, not a
50 empty_str
= process
.ReadCStringFromMemory(empty_str_addr
, 2048, err
)
51 self
.assertSuccess(err
)
52 self
.assertEqual(empty_str
, "")
54 one_letter_string
= process
.ReadCStringFromMemory(
55 one_letter_str_addr
, 2048, err
57 self
.assertSuccess(err
)
58 self
.assertEqual(one_letter_string
, "1")
60 invalid_memory_string
= process
.ReadCStringFromMemory(
61 invalid_memory_str_addr
, 2048, err
63 self
.assertTrue(err
.Fail())
64 self
.assertTrue(invalid_memory_string
== "" or invalid_memory_string
== None)