-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiniDump.cs
106 lines (92 loc) · 3.57 KB
/
MiniDump.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace MiniDumpStartupHook
{
internal static class MiniDump
{
public static void WriteDump(Process proc, string name, string outputFolder = null)
{
try
{
var dumpFileName = $"{proc.ProcessName}-{proc.Id}-{name}-{DateTime.UtcNow.Ticks}.dmp";
outputFolder ??= Settings.Path;
if (!string.IsNullOrWhiteSpace(outputFolder))
{
dumpFileName = Path.Combine(outputFolder, dumpFileName);
}
Console.WriteLine("Writing dump to: {0}", dumpFileName);
if (File.Exists(dumpFileName))
{
File.Delete(dumpFileName);
}
var hFile = NativeMethods.CreateFile(
dumpFileName,
NativeMethods.EFileAccess.GenericWrite,
NativeMethods.EFileShare.None,
lpSecurityAttributes: IntPtr.Zero,
dwCreationDisposition: NativeMethods.ECreationDisposition.CreateAlways,
dwFlagsAndAttributes: NativeMethods.EFileAttributes.Normal,
hTemplateFile: IntPtr.Zero);
if (hFile == NativeMethods.INVALID_HANDLE_VALUE)
{
var hr = Marshal.GetHRForLastWin32Error();
var ex = Marshal.GetExceptionForHR(hr);
Console.WriteLine(ex);
return;
}
NativeMethods._MINIDUMP_TYPE dumpType = NativeMethods._MINIDUMP_TYPE.MiniDumpWithFullMemory;
NativeMethods.MINIDUMP_EXCEPTION_INFORMATION exceptInfo = default;
if (!Is32BitProcess(proc) && IntPtr.Size == 4)
{
Console.WriteLine("Error: Can't create 32 bit dump of 64 bit process");
return;
}
var result = NativeMethods.MiniDumpWriteDump(
proc.Handle,
proc.Id,
hFile,
dumpType,
ref exceptInfo,
UserStreamParam: IntPtr.Zero,
CallbackParam: IntPtr.Zero);
if (result == false)
{
var hr = Marshal.GetHRForLastWin32Error();
var ex = Marshal.GetExceptionForHR(hr);
Console.WriteLine(ex);
return;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private static bool Is32BitProcess(Process proc)
{
bool fIs32bit = false;
// if we're runing on 32bit, default to true
if (IntPtr.Size == 4)
{
fIs32bit = true;
}
bool fIsRunningUnderWow64 = false;
// if machine is 32 bit then all procs are 32 bit
if (NativeMethods.IsWow64Process(NativeMethods.GetCurrentProcess(), out fIsRunningUnderWow64) && fIsRunningUnderWow64)
{
// current OS is 64 bit
if (NativeMethods.IsWow64Process(proc.Handle, out fIsRunningUnderWow64) && fIsRunningUnderWow64)
{
fIs32bit = true;
}
else
{
fIs32bit = false;
}
}
return fIs32bit;
}
}
}