Dump文件的生成
原文网址:https://www.bbsmax.com/A/xl567mo4dr/
一、Windows系统的任务管理器里抓dump
启动任务管理器,选中某个进程,右键,弹出菜单"创建转储文件"


注意事项:
当你在64位Windows系统上抓32位进程的dmup文件时,如果用的是64位任务管理器,那么在用Windbg加载后,要用!wow64exts.sw切换到X86模式下,如果不想做这步切换,就要用32位的任务管理器来生成dmp文件。32位任务管理器在C:\Windows\SysWOW64\Taskmgr.exe
适合的场景:在任务管理器里还能看到进程,当程序出现业务问题、性能问题、失去响应;当程序崩溃跳出系统错误提示框的时候,特别适合应用在客户机出现上述问题时使用。因为我们不用传其他工具到客户机上。
二、修改注册表
@echo off
echo 正在启用Dump...
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps"
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps" /v DumpFolder /t REG_EXPAND_SZ /d "D:\CrashDumps" /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps" /v DumpType /t REG_DWORD /d 2 /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps" /v DumpCount /t REG_DWORD /d 10 /f
echo Dump已经启用
pause
@echo on
将上述内存保存为*.bat,然后执行,就开始了自动抓dmp文件的功能,只要有程序崩溃,就会在指定的目录下生成。
键值说明:
名称:DumpCount,类型:REG_DWORD,最大保留Dump个数,默认为10.
名称:DumpType,类型:REG_DWORD,Dump类型(1-Mini dump, 2-Full dump),默认为1.
名称:DumpFolder,类型:REG_EXPAND_SZ,Dump文件保存的位置。
当不需要自动抓取时,可以将下面的内容
@echo off
echo 正在关闭Dump...
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps" /f
echo Dump已经关闭
pause
@echo on
保存为bat执行,就不会在自动产生了
适合的场景:无法稳定重现的崩溃问题抓取
三、编程
直接使用Windows的API——MiniDumpWriteDump和SetHandleExceptionFilter。
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MyCommon
- {
- public static class MiniDump
- {
- /*
- * 导入DbgHelp.dll
- */
- [DllImport("DbgHelp.dll")]
- private static extern Boolean MiniDumpWriteDump(
- IntPtr hProcess,
- Int32 processId,
- IntPtr fileHandle,
- MiniDumpType dumpType,
- ref MinidumpExceptionInfo excepInfo,
- IntPtr userInfo,
- IntPtr extInfo );
- /*
- * MINIDUMP_EXCEPTION_INFORMATION 这个宏的信息
- */
- struct MinidumpExceptionInfo
- {
- public Int32 ThreadId;
- public IntPtr ExceptionPointers;
- public Boolean ClientPointers;
- }
- /*
- * 自己包装的一个函数
- */
- public static Boolean TryDump(String dmpPath, MiniDumpType dmpType)
- {
- //使用文件流来创健 .dmp文件
- using (FileStream stream = new FileStream(dmpPath, FileMode.Create))
- {
- //取得进程信息
- Process process = Process.GetCurrentProcess();
- // MINIDUMP_EXCEPTION_INFORMATION 信息的初始化
- MinidumpExceptionInfo mei = new MinidumpExceptionInfo();
- mei.ThreadId = Thread.CurrentThread.ManagedThreadId;
- mei.ExceptionPointers = Marshal.GetExceptionPointers();
- mei.ClientPointers = true;
- //这里调用的Win32 API
- Boolean res = MiniDumpWriteDump(
- process.Handle,
- process.Id,
- stream.SafeFileHandle.DangerousGetHandle(),
- dmpType,
- ref mei,
- IntPtr.Zero,
- IntPtr.Zero);
- //清空 stream
- stream.Flush();
- stream.Close();
- return res;
- }
- }
- public enum MiniDumpType
- {
- None = 0x00010000,
- Normal = 0x00000000,
- WithDataSegs = 0x00000001,
- WithFullMemory = 0x00000002,
- WithHandleData = 0x00000004,
- FilterMemory = 0x00000008,
- ScanMemory = 0x00000010,
- WithUnloadedModules = 0x00000020,
- WithIndirectlyReferencedMemory = 0x00000040,
- FilterModulePaths = 0x00000080,
- WithProcessThreadData = 0x00000100,
- WithPrivateReadWriteMemory = 0x00000200,
- WithoutOptionalData = 0x00000400,
- WithFullMemoryInfo = 0x00000800,
- WithThreadInfo = 0x00001000,
- WithCodeSegs = 0x00002000
- }
- }
- }
- using MyCommon;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace DumpDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( (obj, ars) =>
- {
- MiniDump.TryDump("DumpDemo_Err.dmp", MyCommon.MiniDump.MiniDumpType.WithProcessThreadData);
- Console.WriteLine(ars);
- });
- ExceptionFunc();
- Console.ReadKey();
- }
- /// <summary>
- /// 引发程序奔溃的异常代码
- /// </summary>
- static void ExceptionFunc()
- {
- new Thread(arg=>Console.WriteLine(arg.ToString())).Start();
- }
- }
- }
参考:https://www.cnblogs.com/yilang/p/11106495.html
Windbg常用命令:https://www.cnblogs.com/huangsitao/p/10299300.html

浙公网安备 33010602011771号