Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CrashDump.cs
Go to the documentation of this file.
1using System;
4using System.IO;
7using ReLogic.OS;
8
9namespace Terraria.Utilities;
10
11public static class CrashDump
12{
13 [Flags]
14 public enum Options : uint
15 {
16 Normal = 0u,
17 WithDataSegs = 1u,
18 WithFullMemory = 2u,
19 WithHandleData = 4u,
20 FilterMemory = 8u,
21 ScanMemory = 0x10u,
22 WithUnloadedModules = 0x20u,
24 FilterModulePaths = 0x80u,
25 WithProcessThreadData = 0x100u,
27 WithoutOptionalData = 0x400u,
28 WithFullMemoryInfo = 0x800u,
29 WithThreadInfo = 0x1000u,
30 WithCodeSegs = 0x2000u,
31 WithoutAuxiliaryState = 0x4000u,
32 WithFullAuxiliaryState = 0x8000u,
34 IgnoreInaccessibleMemory = 0x20000u,
35 ValidTypeFlags = 0x3FFFFu
36 }
37
38 private enum ExceptionInfo
39 {
40 None,
42 }
43
44 [StructLayout(LayoutKind.Sequential, Pack = 4)]
46 {
47 public uint ThreadId;
48
50
51 [MarshalAs(UnmanagedType.Bool)]
52 public bool ClientPointers;
53 }
54
55 public static bool WriteException(Options options, string outputDirectory = ".")
56 {
57 return Write(options, ExceptionInfo.Present, outputDirectory);
58 }
59
60 public static bool Write(Options options, string outputDirectory = ".")
61 {
62 return Write(options, ExceptionInfo.None, outputDirectory);
63 }
64
65 private static string CreateDumpName()
66 {
67 DateTime dateTime = DateTime.Now.ToLocalTime();
68 return string.Format("{0}_{1}_{2}_{3}.dmp", Main.dedServ ? "TerrariaServer" : "Terraria", Main.versionNumber, dateTime.ToString("MM-dd-yy_HH-mm-ss-ffff", CultureInfo.InvariantCulture), Thread.CurrentThread.ManagedThreadId);
69 }
70
71 private static bool Write(Options options, ExceptionInfo exceptionInfo, string outputDirectory)
72 {
74 {
75 return false;
76 }
77 string path = Path.Combine(outputDirectory, CreateDumpName());
78 if (!Utils.TryCreatingDirectory(outputDirectory))
79 {
80 return false;
81 }
82 using FileStream fileStream = File.Create(path);
83 return Write(fileStream.SafeFileHandle, options, exceptionInfo);
84 }
85
86 private static bool Write(SafeHandle fileHandle, Options options, ExceptionInfo exceptionInfo)
87 {
89 {
90 return false;
91 }
92 Process currentProcess = Process.GetCurrentProcess();
93 IntPtr handle = currentProcess.Handle;
94 uint id = (uint)currentProcess.Id;
96 expParam.ThreadId = GetCurrentThreadId();
97 expParam.ClientPointers = false;
98 expParam.ExceptionPointers = IntPtr.Zero;
99 if (exceptionInfo == ExceptionInfo.Present)
100 {
101 expParam.ExceptionPointers = Marshal.GetExceptionPointers();
102 }
103 bool flag = false;
104 if (expParam.ExceptionPointers == IntPtr.Zero)
105 {
106 return MiniDumpWriteDump(handle, id, fileHandle, (uint)options, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
107 }
108 return MiniDumpWriteDump(handle, id, fileHandle, (uint)options, ref expParam, IntPtr.Zero, IntPtr.Zero);
109 }
110
111 [DllImport("dbghelp.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
112 private static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, ref MiniDumpExceptionInformation expParam, IntPtr userStreamParam, IntPtr callbackParam);
113
114 [DllImport("dbghelp.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
115 private static extern bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, IntPtr expParam, IntPtr userStreamParam, IntPtr callbackParam);
116
117 [DllImport("kernel32.dll", ExactSpelling = true)]
118 private static extern uint GetCurrentThreadId();
119}
static bool IsWindows
Definition Platform.cs:19
static Process GetCurrentProcess()
Definition Process.cs:1107
static CultureInfo InvariantCulture
static FileStream Create(string path)
Definition File.cs:73
static string Combine(string path1, string path2)
Definition Path.cs:304
static Thread CurrentThread
Definition Thread.cs:312
static string versionNumber
Definition Main.cs:303
static bool dedServ
Definition Main.cs:1226
static bool Write(SafeHandle fileHandle, Options options, ExceptionInfo exceptionInfo)
Definition CrashDump.cs:86
static string CreateDumpName()
Definition CrashDump.cs:65
static uint GetCurrentThreadId()
static bool WriteException(Options options, string outputDirectory=".")
Definition CrashDump.cs:55
static bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, IntPtr expParam, IntPtr userStreamParam, IntPtr callbackParam)
static bool Write(Options options, string outputDirectory=".")
Definition CrashDump.cs:60
static bool Write(Options options, ExceptionInfo exceptionInfo, string outputDirectory)
Definition CrashDump.cs:71
static bool MiniDumpWriteDump(IntPtr hProcess, uint processId, SafeHandle hFile, uint dumpType, ref MiniDumpExceptionInformation expParam, IntPtr userStreamParam, IntPtr callbackParam)
static bool TryCreatingDirectory(string folderPath)
Definition Utils.cs:754
static DateTime Now
Definition DateTime.cs:103
override string ToString()
Definition DateTime.cs:1109
static readonly IntPtr Zero
Definition IntPtr.cs:18