Files
clang-p2996/lldb/test/API/python_api/process/read-mem-cstring/TestReadMemCString.py
Jonas Devlieghere 2238dcc393 [NFC][Py Reformat] Reformat python files in lldb
This is an ongoing series of commits that are reformatting our Python
code. Reformatting is done with `black` (23.1.0).

If you end up having problems merging this commit because you have made
changes to a python file, the best way to handle that is to run `git
checkout --ours <yourfile>` and then reformat it with black.

RFC: https://discourse.llvm.org/t/rfc-document-and-standardize-python-code-style

Differential revision: https://reviews.llvm.org/D151460
2023-05-25 12:54:09 -07:00

65 lines
2.2 KiB
Python

"""Test reading c-strings from memory via SB API."""
import os
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
class TestReadMemCString(TestBase):
NO_DEBUG_INFO_TESTCASE = True
def test_read_memory_c_string(self):
"""Test corner case behavior of SBProcess::ReadCStringFromMemory"""
self.build()
self.dbg.SetAsync(False)
self.main_source = "main.c"
self.main_source_path = os.path.join(self.getSourceDir(), self.main_source)
self.main_source_spec = lldb.SBFileSpec(self.main_source_path)
self.exe = self.getBuildArtifact("read-mem-cstring")
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
self, "breakpoint here", self.main_source_spec, None, self.exe
)
frame = thread.GetFrameAtIndex(0)
err = lldb.SBError()
empty_str_addr = frame.FindVariable("empty_string").GetValueAsUnsigned(err)
self.assertSuccess(err)
self.assertNotEqual(empty_str_addr, lldb.LLDB_INVALID_ADDRESS)
one_letter_str_addr = frame.FindVariable(
"one_letter_string"
).GetValueAsUnsigned(err)
self.assertSuccess(err)
self.assertNotEqual(one_letter_str_addr, lldb.LLDB_INVALID_ADDRESS)
invalid_memory_str_addr = frame.FindVariable(
"invalid_memory_string"
).GetValueAsUnsigned(err)
self.assertSuccess(err)
self.assertNotEqual(invalid_memory_str_addr, lldb.LLDB_INVALID_ADDRESS)
# Important: An empty (0-length) c-string must come back as a Python string, not a
# None object.
empty_str = process.ReadCStringFromMemory(empty_str_addr, 2048, err)
self.assertSuccess(err)
self.assertEqual(empty_str, "")
one_letter_string = process.ReadCStringFromMemory(
one_letter_str_addr, 2048, err
)
self.assertSuccess(err)
self.assertEqual(one_letter_string, "1")
invalid_memory_string = process.ReadCStringFromMemory(
invalid_memory_str_addr, 2048, err
)
self.assertTrue(err.Fail())
self.assertTrue(invalid_memory_string == "" or invalid_memory_string == None)