From 253db767430741bd389807b40ca3dad6eb16146a Mon Sep 17 00:00:00 2001 From: Rakesh Ganesh Date: Wed, 18 Jun 2025 16:34:12 +0000 Subject: [PATCH] MIDebugEngine: Implement memory edit and write for GPU Currently we can edit Memory only when the kernels are offloaded to CPU. Extending this to work with the GPUs. Signed-off-by: intel-rganesh rakesh.ganesh@intel.com --- src/MIDebugEngine/AD7.Impl/AD7Engine.cs | 22 +++++++++++++++++- .../Engine.Impl/DebuggedProcess.cs | 23 +++++++++++++++++++ src/MIDebugEngine/Engine.Impl/Structures.cs | 1 + 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/MIDebugEngine/AD7.Impl/AD7Engine.cs b/src/MIDebugEngine/AD7.Impl/AD7Engine.cs index b9e60a869..d3721bc79 100755 --- a/src/MIDebugEngine/AD7.Impl/AD7Engine.cs +++ b/src/MIDebugEngine/AD7.Impl/AD7Engine.cs @@ -1111,7 +1111,27 @@ public int ReadAt(IDebugMemoryContext2 pStartContext, uint dwCount, byte[] rgbMe public int WriteAt(IDebugMemoryContext2 pStartContext, uint dwCount, byte[] rgbMemory) { - throw new NotImplementedException(); + try + { + if (!(pStartContext is AD7MemoryAddress memContext)) + return Constants.E_INVALIDARG; + + ulong address = memContext.Address; + bool isWriteSuccess = false; + DebuggedProcess.WorkerThread.RunOperation(async () => + { + isWriteSuccess = await DebuggedProcess.WriteProcessMemory(address, dwCount, rgbMemory); + }); + + if (!isWriteSuccess) + return Constants.E_FAIL; + + return Constants.S_OK; + } + catch + { + return Constants.E_FAIL; + } } #endregion diff --git a/src/MIDebugEngine/Engine.Impl/DebuggedProcess.cs b/src/MIDebugEngine/Engine.Impl/DebuggedProcess.cs index aaabb974a..ba77db62c 100755 --- a/src/MIDebugEngine/Engine.Impl/DebuggedProcess.cs +++ b/src/MIDebugEngine/Engine.Impl/DebuggedProcess.cs @@ -2119,6 +2119,29 @@ internal async Task ReadProcessMemory(ulong address, uint count, byte[] by return toRead; } + internal async Task WriteProcessMemory(ulong address, uint dwCount, byte[] rgbMemory) + { + try + { + // Convert bytes to hex string + StringBuilder hex = new StringBuilder((int)dwCount * 2); + for (int i = 0; i < dwCount; i++) + hex.AppendFormat(CultureInfo.InvariantCulture, "{0:x2}", rgbMemory[i]); + + // Send the MI command + string cmd = $"-data-write-memory-bytes {EngineUtils.AsAddr(address, this.Is64BitArch)} {hex}"; + var result = await this.CmdAsync(cmd, ResultClass.None); + + // Check result + return result.ResultClass != ResultClass.error; + } + catch (MIException ex) + { + Logger.WriteLine(LogLevel.Warning, $"WriteProcessMemory failed: {ex.Message}"); + return false; + } + } + internal async Task> FindValidMemoryRange(ulong address, uint count, int offset) { var ret = new Tuple(0, 0); // init to an empty range diff --git a/src/MIDebugEngine/Engine.Impl/Structures.cs b/src/MIDebugEngine/Engine.Impl/Structures.cs index 2cfebba42..9610cbe77 100644 --- a/src/MIDebugEngine/Engine.Impl/Structures.cs +++ b/src/MIDebugEngine/Engine.Impl/Structures.cs @@ -111,6 +111,7 @@ public class Constants public const int E_NOTIMPL = unchecked((int)0x80004001); public const int E_FAIL = unchecked((int)0x80004005); public const int E_ABORT = unchecked((int)(0x80004004)); + public const int E_INVALIDARG = unchecked((int)(0x80070057)); public const int RPC_E_SERVERFAULT = unchecked((int)(0x80010105)); };