[Lldb-commits] [PATCH] D144224: [lldb] Extend SWIG SBProcess interface with WriteMemoryAsCString method

Med Ismail Bennani via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Fri Mar 3 18:52:54 PST 2023


mib updated this revision to Diff 502332.
mib added a comment.

Handle empty string case and add tests


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144224/new/

https://reviews.llvm.org/D144224

Files:
  lldb/bindings/interface/SBProcessExtensions.i
  lldb/test/API/python_api/process/TestProcessAPI.py


Index: lldb/test/API/python_api/process/TestProcessAPI.py
===================================================================
--- lldb/test/API/python_api/process/TestProcessAPI.py
+++ lldb/test/API/python_api/process/TestProcessAPI.py
@@ -186,6 +186,32 @@
             exe=False,
             startstr=b'a')
 
+        # Get the SBValue for the global variable 'my_cstring'.
+        val = frame.FindValue("my_cstring", lldb.eValueTypeVariableGlobal)
+        self.DebugSBValue(val)
+
+        addr = val.AddressOf().GetValueAsUnsigned()
+
+        # Write an empty string to memory
+        bytes_written = process.WriteMemoryAsCString(addr, "", error)
+        self.assertEqual(bytes_written, 0)
+        if not error.Success():
+            self.fail("SBProcess.WriteMemoryAsCString() failed")
+
+        message = "Hello!"
+        bytes_written = process.WriteMemoryAsCString(addr, message, error)
+        self.assertEqual(bytes_written, len(message) + 1)
+        if not error.Success():
+            self.fail("SBProcess.WriteMemoryAsCString() failed")
+
+        cstring = process.ReadCStringFromMemory(
+            val.AddressOf().GetValueAsUnsigned(), 256, error)
+        if not error.Success():
+            self.fail("SBProcess.ReadCStringFromMemory() failed")
+
+        self.assertEqual(cstring, message)
+
+
     def test_access_my_int(self):
         """Test access 'my_int' using Python SBProcess.GetByteOrder() and other APIs."""
         self.build()
Index: lldb/bindings/interface/SBProcessExtensions.i
===================================================================
--- lldb/bindings/interface/SBProcessExtensions.i
+++ lldb/bindings/interface/SBProcessExtensions.i
@@ -2,6 +2,18 @@
 %extend lldb::SBProcess {
 #ifdef SWIGPYTHON
     %pythoncode %{
+        def WriteMemoryAsCString(self, addr, str, error):
+            '''
+              WriteMemoryAsCString(self, addr, str, error):
+                This functions the same as `WriteMemory` except a null-terminator is appended
+                to the end of the buffer if it is not there already.
+            '''
+            if not str or len(str) == 0:
+                return 0
+            if not str[-1] == '\0':
+                str += '\0'
+            return self.WriteMemory(addr, str, error)
+
         def __get_is_alive__(self):
             '''Returns "True" if the process is currently alive, "False" otherwise'''
             s = self.GetState()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144224.502332.patch
Type: text/x-patch
Size: 2445 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230304/2e5f59d9/attachment.bin>


More information about the lldb-commits mailing list