summaryrefslogtreecommitdiff
path: root/lldb/bindings
diff options
context:
space:
mode:
authorMed Ismail Bennani <medismail.bennani@gmail.com>2023-03-03 18:52:15 -0800
committerMed Ismail Bennani <medismail.bennani@gmail.com>2023-03-03 19:33:01 -0800
commite6cac17b563f2e2bc7d04347b0b40a9fe12334c9 (patch)
treee4f2ac75ed4233b950633d28a06f51e025b405a8 /lldb/bindings
parentc7af9ae577bb04c5fe120fc07844a500818c8f47 (diff)
downloadllvm-e6cac17b563f2e2bc7d04347b0b40a9fe12334c9.tar.gz
[lldb] Extend SWIG SBProcess interface with WriteMemoryAsCString method
This patch tries to address an interoperability issue when writing python string into the process memory. Since the python string is not null-terminated, it would still be written to memory however, when trying to read it again with `SBProcess::ReadCStringFromMemory`, the memory read would fail, since the read string doens't contain a null-terminator, and therefore is not a valid C string. To address that, this patch extends the `SBProcess` SWIG interface to expose a new `WriteMemoryAsCString` method that is only exposed to the SWIG target language. That method checks that the buffer to write is null-terminated and otherwise, it appends a null byte at the end of it. Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
Diffstat (limited to 'lldb/bindings')
-rw-r--r--lldb/bindings/interface/SBProcessExtensions.i12
1 files changed, 12 insertions, 0 deletions
diff --git a/lldb/bindings/interface/SBProcessExtensions.i b/lldb/bindings/interface/SBProcessExtensions.i
index c6660d7f42e1..fe9e6415b618 100644
--- a/lldb/bindings/interface/SBProcessExtensions.i
+++ b/lldb/bindings/interface/SBProcessExtensions.i
@@ -2,6 +2,18 @@ STRING_EXTENSION_OUTSIDE(SBProcess)
%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()