用MDbgEng从内存中读取对象 [英] Reading objects from memory with MDbgEng
问题描述
我想帮助 @mark 在一个问题中他是要求API从.NET崩溃转储文件中转储许多对象。
所以我用以下代码 mdbgeng
,但不幸的是,当尝试枚举内存中的对象时,它失败了一个 NotImplementedException
。
使用系统;
使用System.Runtime.InteropServices;
使用Microsoft.Samples.Debugging.CorDebug;
使用Microsoft.Samples.Debugging.CorDebug.Utility;
使用Microsoft.Samples.Debugging.MdbgEngine;
使用Microsoft.Samples.Debugging.Native;
命名空间DumpHeapFromDotNet
{
类程序
{
static void Main(string [] args)
{
var libraryProvider =新的LibraryProvider();
var dumpReader = new DumpReader(args [0]);
var dataTarget = new DumpDataTarget(dumpReader);
foreach(dumpReader.EnumerateModules()中的var模块)
{
var clrDebugging = new CLRDebugging();
版本actualVersion;
ClrDebuggingProcessFlags标志;
CorProcess proc;
var hr =(HResult)clrDebugging.TryOpenVirtualProcess(module.BaseAddress,dataTarget,libraryProvider,
new Version(4,6,int.MaxValue,int.MaxValue),out actualVersion,out flags,out proc );
if(hr< 0)
{
switch(hr)
{
case HResult.CORDBG_E_NOT_CLR:
Console.WriteLine module.FullName +不是.NET模块);
break;
case HResult.CORDBG_E_LIBRARY_PROVIDER_ERROR:
Console.WriteLine(module.FullName +can not provide library);
break;
case HResult.CORDBG_E_UNSUPPORTED_DEBUGGING_MODEL:
case HResult.CORDBG_E_UNSUPPORTED_FORWARD_COMPAT:
break;
默认值:
Marshal.ThrowExceptionForHR((int)hr);
break;
}
}
else
{
var objects = proc.Objects; // NotImplementedException
foreach(对象中的CorObjectValue o)
{
// TODO:将文件的详细信息写入
}
}
}
Console.ReadLine();
}
}
}
我正在使用的转储一个具有完整内存的.NET 4.6.1076.0转储(可以将文件名作为参数传递):
0:000> lm vm clr
[...]
ProductVersion:4.6.1076.0
FileVersion:4.6.1076.0由:NETFXREL3STAGE
0:000> .dumpdebug
-----用户迷你转储分析
MINIDUMP_HEADER:
版本A793(61B1)
NumberOfStreams 11
标志1806
0002 MiniDumpWithFullMemory
0004 MiniDumpWithHandleData
0800 MiniDumpWithFullMemoryInfo
1000 MiniDumpWithThreadInfo
I怀疑它与缺少 mscordacwks
或类似的东西有关,因为我刚刚在同一台机器上创建了与我用于此示例的.NET框架相同的转储。 p>
真的没有实现,还是我做别的什么错?
我正在搞砸MDBG,我已经尝试在实际应用程序上检查描述的行为,而不是在转储上。我收到了相同的未实现的异常。在MSDN上查找文档我发现确认,这个方法未实现。
I wanted to help out @mark in a question where he is asking for an API to dump many objects from a .NET crash dump file.
So I wrote the following code using mdbgeng
, but unfortunately it fails with a NotImplementedException
when trying to enumerate the objects in memory.
using System;
using System.Runtime.InteropServices;
using Microsoft.Samples.Debugging.CorDebug;
using Microsoft.Samples.Debugging.CorDebug.Utility;
using Microsoft.Samples.Debugging.MdbgEngine;
using Microsoft.Samples.Debugging.Native;
namespace DumpHeapFromDotNet
{
class Program
{
static void Main(string[] args)
{
var libraryProvider = new LibraryProvider();
var dumpReader = new DumpReader(args[0]);
var dataTarget = new DumpDataTarget(dumpReader);
foreach (var module in dumpReader.EnumerateModules())
{
var clrDebugging = new CLRDebugging();
Version actualVersion;
ClrDebuggingProcessFlags flags;
CorProcess proc;
var hr = (HResult) clrDebugging.TryOpenVirtualProcess(module.BaseAddress, dataTarget, libraryProvider,
new Version(4, 6, int.MaxValue, int.MaxValue), out actualVersion, out flags, out proc);
if (hr < 0)
{
switch (hr)
{
case HResult.CORDBG_E_NOT_CLR:
Console.WriteLine(module.FullName + " is not a .NET module");
break;
case HResult.CORDBG_E_LIBRARY_PROVIDER_ERROR:
Console.WriteLine(module.FullName + " could not provide library");
break;
case HResult.CORDBG_E_UNSUPPORTED_DEBUGGING_MODEL:
case HResult.CORDBG_E_UNSUPPORTED_FORWARD_COMPAT:
break;
default:
Marshal.ThrowExceptionForHR((int)hr);
break;
}
}
else
{
var objects = proc.Objects; // NotImplementedException
foreach (CorObjectValue o in objects)
{
// TODO: Write details of object to file here
}
}
}
Console.ReadLine();
}
}
}
The dump I was using is a .NET 4.6.1076.0 dump with full memory (you can pass a file name as an argument):
0:000> lm vm clr
[...]
ProductVersion: 4.6.1076.0
FileVersion: 4.6.1076.0 built by: NETFXREL3STAGE
0:000> .dumpdebug
----- User Mini Dump Analysis
MINIDUMP_HEADER:
Version A793 (61B1)
NumberOfStreams 11
Flags 1806
0002 MiniDumpWithFullMemory
0004 MiniDumpWithHandleData
0800 MiniDumpWithFullMemoryInfo
1000 MiniDumpWithThreadInfo
I doubt it has something to do with missing mscordacwks
or similar, since I just created the dump on the same machine with the same .NET framework as I used for this sample.
Is it really not implemented yet, or am I doing something else wrong?
I'm currently messing with MDBG and I have tried to check the described behavior on real application, not on the dump. I received exatly the same not implemented exception. Looking for the documentation on MSDN I've found the confirmation, that this method is not implemented.
这篇关于用MDbgEng从内存中读取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!