[lldb/Plugins] Add memory writing capabilities to Scripted Process

This patch adds memory writing capabilities to the Scripted Process plugin.

This allows to user to get a target address and a memory buffer on the
python scripted process implementation that the user can make processing
on before performing the actual write.

This will also be used to write trap instruction to a real process
memory to set a breakpoint.

Signed-off-by: Med Ismail Bennani <medismail.bennani@gmail.com>
This commit is contained in:
Med Ismail Bennani
2023-03-03 19:30:56 -08:00
parent e02a355f98
commit f190ec6882
13 changed files with 107 additions and 8 deletions

View File

@@ -150,7 +150,6 @@ class ScriptedProcesTestCase(TestBase):
self.assertEqual(process_1.GetNumThreads(), 1)
# ... then try reading from target #1 process ...
addr = 0x500000000
message = "Hello, target 1"
buff = process_1.ReadCStringFromMemory(addr, len(message) + 1, error)
self.assertSuccess(error)
@@ -158,12 +157,22 @@ class ScriptedProcesTestCase(TestBase):
# ... now, reading again from target #0 process to make sure the call
# gets dispatched to the right target.
addr = 0x500000000
message = "Hello, target 0"
buff = process_0.ReadCStringFromMemory(addr, len(message) + 1, error)
self.assertSuccess(error)
self.assertEqual(buff, message)
# Let's write some memory.
message = "Hello, world!"
bytes_written = process_0.WriteMemoryAsCString(addr, message, error)
self.assertSuccess(error)
self.assertEqual(bytes_written, len(message) + 1)
# ... and check if that memory was saved properly.
buff = process_0.ReadCStringFromMemory(addr, len(message) + 1, error)
self.assertSuccess(error)
self.assertEqual(buff, message)
thread = process_0.GetSelectedThread()
self.assertTrue(thread, "Invalid thread.")
self.assertEqual(thread.GetThreadID(), 0x19)

View File

@@ -7,20 +7,29 @@ from lldb.plugins.scripted_process import ScriptedProcess
from lldb.plugins.scripted_process import ScriptedThread
class DummyScriptedProcess(ScriptedProcess):
memory = None
def __init__(self, exe_ctx: lldb.SBExecutionContext, args : lldb.SBStructuredData):
super().__init__(exe_ctx, args)
self.threads[0] = DummyScriptedThread(self, None)
def read_memory_at_address(self, addr: int, size: int, error: lldb.SBError) -> lldb.SBData:
self.memory = {}
addr = 0x500000000
debugger = self.target.GetDebugger()
index = debugger.GetIndexOfTarget(self.target)
self.memory[addr] = "Hello, target " + str(index)
def read_memory_at_address(self, addr: int, size: int, error: lldb.SBError) -> lldb.SBData:
data = lldb.SBData().CreateDataFromCString(
self.target.GetByteOrder(),
self.target.GetCodeByteSize(),
"Hello, target " + str(index))
self.memory[addr])
return data
def write_memory_at_address(self, addr, data, error):
self.memory[addr] = data.GetString(error, 0)
return len(self.memory[addr]) + 1
def get_loaded_images(self):
return self.loaded_images