Dump文件的生成


一、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——MiniDumpWriteDumpSetHandleExceptionFilter。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.IO;
 5 using System.Linq;
 6 using System.Runtime.InteropServices;
 7 using System.Text;
 8 using System.Threading;
 9 using System.Threading.Tasks;
10 
11 namespace MyCommon
12 {
13      public static class MiniDump
14     {
15         /*
16          * 導入DbgHelp.dll
17          */
18         [DllImport("DbgHelp.dll")]
19         private static extern Boolean MiniDumpWriteDump(
20                                     IntPtr hProcess,
21                                     Int32 processId,
22                                     IntPtr fileHandle,
23                                     MiniDumpType dumpType, 
24                                     ref MinidumpExceptionInfo excepInfo,
25                                     IntPtr userInfo, 
26                                     IntPtr extInfo );
27 
28         /*
29          *  MINIDUMP_EXCEPTION_INFORMATION  這個宏的信息
30          */
31         struct MinidumpExceptionInfo
32         {
33             public Int32 ThreadId;
34             public IntPtr ExceptionPointers;
35             public Boolean ClientPointers;
36         }
37 
38         /*
39          * 自己包裝的一個函數
40          */
41         public static Boolean TryDump(String dmpPath, MiniDumpType dmpType)
42         {
43 
44             //使用文件流來創健 .dmp文件
45             using (FileStream stream = new FileStream(dmpPath, FileMode.Create))
46             {
47                 //取得進程信息
48                 Process process = Process.GetCurrentProcess();
49                 // MINIDUMP_EXCEPTION_INFORMATION 信息的初始化
50                 MinidumpExceptionInfo mei = new MinidumpExceptionInfo();
51                 mei.ThreadId = Thread.CurrentThread.ManagedThreadId;
52                 mei.ExceptionPointers = Marshal.GetExceptionPointers();
53                 mei.ClientPointers = true;
54                 
55                 //這里調用的Win32 API
56                 Boolean res = MiniDumpWriteDump(
57                                     process.Handle,
58                                     process.Id,
59                                     stream.SafeFileHandle.DangerousGetHandle(),
60                                     dmpType,
61                                     ref mei,
62                                     IntPtr.Zero,
63                                     IntPtr.Zero);
64 
65                 //清空 stream
66                 stream.Flush();
67                 stream.Close();
68                 return res;
69             }
70         }
71 
72         public enum MiniDumpType
73         {
74             None = 0x00010000,
75             Normal = 0x00000000,
76             WithDataSegs = 0x00000001,
77             WithFullMemory = 0x00000002,
78             WithHandleData = 0x00000004,
79             FilterMemory = 0x00000008,
80             ScanMemory = 0x00000010,
81             WithUnloadedModules = 0x00000020,
82             WithIndirectlyReferencedMemory = 0x00000040,
83             FilterModulePaths = 0x00000080,
84             WithProcessThreadData = 0x00000100,
85             WithPrivateReadWriteMemory = 0x00000200,
86             WithoutOptionalData = 0x00000400,
87             WithFullMemoryInfo = 0x00000800,
88             WithThreadInfo = 0x00001000,
89             WithCodeSegs = 0x00002000
90         }
91     }
92 }
View Code
 1 using MyCommon;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading;
 7 using System.Threading.Tasks;
 8 
 9 namespace DumpDemo
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( (obj, ars) => 
16             {
17                 MiniDump.TryDump("DumpDemo_Err.dmp", MyCommon.MiniDump.MiniDumpType.WithProcessThreadData);
18                 Console.WriteLine(ars);
19             });
20 
21             ExceptionFunc();
22 
23             Console.ReadKey();
24         }
25 
26         /// <summary>
27         /// 引發程序奔潰的異常代碼
28         /// </summary>
29         static void ExceptionFunc()
30         {
31             new Thread(arg=>Console.WriteLine(arg.ToString())).Start();
32         }
33     }
34 }
View Code

參考:https://www.cnblogs.com/yilang/p/11106495.html

Windbg常用命令:https://www.cnblogs.com/huangsitao/p/10299300.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM