Browse Source

Methods for reading and writing of process memory. Test that breaks string immutability using memory write.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3585 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
David Srbecký 17 years ago
parent
commit
c19717ac7e
  1. 25
      src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process.cs
  2. 2
      src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Interop/CorDebug/ICorDebugProcess.cs
  3. 10
      src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Values/Value.cs
  4. 6
      src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Wrappers/CorDebug/Autogenerated/ICorDebugProcess.cs
  5. 1
      src/AddIns/Misc/Debugger/Debugger.Tests/Project/Debugger.Tests.csproj
  6. 85
      src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/MemoryReadWrite.cs

25
src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Control/Process.cs

@ -171,6 +171,31 @@ namespace Debugger @@ -171,6 +171,31 @@ namespace Debugger
}
}
}
/// <summary> Read the specified amount of memory at the given memory address </summary>
/// <returns> The content of the memory. The amount of the read memory may be less then requested. </returns>
public unsafe byte[] ReadMemory(ulong address, int size)
{
byte[] buffer = new byte[size];
int readCount;
fixed(byte* pBuffer = buffer) {
readCount = (int)corProcess.ReadMemory(address, (uint)size, new IntPtr(pBuffer));
}
if (readCount != size) Array.Resize(ref buffer, readCount);
return buffer;
}
/// <summary> Writes the given buffer at the specified memory address </summary>
/// <returns> The number of bytes written </returns>
public unsafe int WriteMemory(ulong address, byte[] buffer)
{
if (buffer.Length == 0) return 0;
int written;
fixed(byte* pBuffer = buffer) {
written = (int)corProcess.WriteMemory(address, (uint)buffer.Length, new IntPtr(pBuffer));
}
return written;
}
}
[Serializable]

2
src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Interop/CorDebug/ICorDebugProcess.cs

@ -55,7 +55,7 @@ namespace Debugger.Interop.CorDebug @@ -55,7 +55,7 @@ namespace Debugger.Interop.CorDebug
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)]
void ReadMemory([In] ulong address, [In] uint size, [Out] IntPtr buffer, [ComAliasName("Debugger.Interop.CorDebug.ULONG_PTR")] out uint read);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)]
void WriteMemory([In] ulong address, [In] uint size, [In] ref byte buffer, [ComAliasName("Debugger.Interop.CorDebug.ULONG_PTR")] out uint written);
void WriteMemory([In] ulong address, [In] uint size, [In] IntPtr buffer, [ComAliasName("Debugger.Interop.CorDebug.ULONG_PTR")] out uint written);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)]
void ClearCurrentException([In] uint threadID);
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime)]

10
src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Values/Value.cs

@ -91,6 +91,16 @@ namespace Debugger @@ -91,6 +91,16 @@ namespace Debugger
}
}
/// <summary>
/// Gets the address in memory where this value is stored
/// </summary>
[Debugger.Tests.IgnoreAttribute]
public ulong Address {
get {
return corValue.Address;
}
}
/// <summary> Gets value indication whether the value is a reference </summary>
/// <remarks> Value types also return true if they are boxed </remarks>
public bool IsReference {

6
src/AddIns/Misc/Debugger/Debugger.Core/Project/Src/Wrappers/CorDebug/Autogenerated/ICorDebugProcess.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
// <file>
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="David Srbecký" email="dsrbecky@gmail.com"/>
@ -237,10 +237,10 @@ namespace Debugger.Wrappers.CorDebug @@ -237,10 +237,10 @@ namespace Debugger.Wrappers.CorDebug
return read;
}
public uint WriteMemory(ulong address, uint size, ref byte buffer)
public uint WriteMemory(ulong address, uint size, System.IntPtr buffer)
{
uint written;
this.WrappedObject.WriteMemory(address, size, ref buffer, out written);
this.WrappedObject.WriteMemory(address, size, buffer, out written);
return written;
}

1
src/AddIns/Misc/Debugger/Debugger.Tests/Project/Debugger.Tests.csproj

@ -60,6 +60,7 @@ @@ -60,6 +60,7 @@
<Compile Include="Src\TestPrograms\Generics.cs" />
<Compile Include="Src\TestPrograms\HelloWorld.cs" />
<Compile Include="Src\TestPrograms\MainThreadExit.cs" />
<Compile Include="Src\TestPrograms\MemoryReadWrite.cs" />
<Compile Include="Src\TestPrograms\Metadata.cs" />
<Compile Include="Src\TestPrograms\MetadataIdentity.cs" />
<Compile Include="Src\TestPrograms\ObjectValue.cs" />

85
src/AddIns/Misc/Debugger/Debugger.Tests/Project/Src/TestPrograms/MemoryReadWrite.cs

@ -0,0 +1,85 @@ @@ -0,0 +1,85 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="David Srbecký" email="dsrbecky@gmail.com"/>
// <version>$Revision$</version>
// </file>
using System;
namespace Debugger.Tests.TestPrograms
{
public class MemoryReadWrite
{
public static void Main()
{
string hello = "Hello";
string world = " !";
System.Diagnostics.Debugger.Break();
System.Diagnostics.Debug.WriteLine(hello + " " + world);
}
}
}
#if TEST_CODE
namespace Debugger.Tests {
public partial class DebuggerTests
{
[NUnit.Framework.Test]
public void MemoryReadWrite()
{
StartTest("MemoryReadWrite.cs");
ulong addrHello = process.SelectedStackFrame.GetLocalVariableValue("hello").Address;
ulong addrWorld = process.SelectedStackFrame.GetLocalVariableValue("world").Address;
addrHello = DeRef(process.ReadMemory(addrHello, 4));
addrWorld = DeRef(process.ReadMemory(addrWorld, 4));
byte[] hello = process.ReadMemory(addrHello, 22);
byte[] world = process.ReadMemory(addrWorld, 24);
ObjectDump("hello", ToHex(hello));
ObjectDump("world", ToHex(world));
process.WriteMemory(addrWorld + 12, new byte[] {0x77, 0x0, 0x6F, 0x0, 0x72, 0x0, 0x6C, 0x0, 0x64, 0x0});
EndTest();
}
ulong DeRef(byte[] ptr)
{
return (ulong)(ptr[0] + (ptr[1] << 8) + (ptr[2] << 16) + (ptr[3] << 24));
}
string ToHex(byte[] buffer)
{
string hex = "";
foreach(byte b in buffer) {
hex += b.ToString("X") + " ";
}
return hex;
}
}
}
#endif
#if EXPECTED_OUTPUT
<?xml version="1.0" encoding="utf-8"?>
<DebuggerTests>
<Test
name="MemoryReadWrite.cs">
<ProcessStarted />
<ModuleLoaded>mscorlib.dll (No symbols)</ModuleLoaded>
<ModuleLoaded>MemoryReadWrite.exe (Has symbols)</ModuleLoaded>
<ModuleLoaded>System.dll (No symbols)</ModuleLoaded>
<DebuggingPaused>Break MemoryReadWrite.cs:18,4-18,40</DebuggingPaused>
<hello>EC 8 33 79 6 0 0 0 5 0 0 0 48 0 65 0 6C 0 6C 0 6F 0 </hello>
<world>EC 8 33 79 7 0 0 0 6 0 0 0 20 0 20 0 20 0 20 0 20 0 21 0 </world>
<ModuleLoaded>System.Configuration.dll (No symbols)</ModuleLoaded>
<ModuleLoaded>System.Xml.dll (No symbols)</ModuleLoaded>
<LogMessage>Hello world!\r\n</LogMessage>
<ProcessExited />
</Test>
</DebuggerTests>
#endif // EXPECTED_OUTPUT
Loading…
Cancel
Save