| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008-2010 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Text; |
|---|
| 25 | using System.Runtime.InteropServices; |
|---|
| 26 | using Microsoft.Win32.SafeHandles; |
|---|
| 27 | |
|---|
| 28 | namespace Eraser.Util |
|---|
| 29 | { |
|---|
| 30 | /// <summary> |
|---|
| 31 | /// Stores DLL references for this class. |
|---|
| 32 | /// </summary> |
|---|
| 33 | internal static partial class NativeMethods |
|---|
| 34 | { |
|---|
| 35 | /// <summary> |
|---|
| 36 | /// Writes user-mode minidump information to the specified file. |
|---|
| 37 | /// </summary> |
|---|
| 38 | /// <param name="hProcess">A handle to the process for which the information |
|---|
| 39 | /// is to be generated.</param> |
|---|
| 40 | /// <param name="ProcessId">The identifier of the process for which the information |
|---|
| 41 | /// is to be generated.</param> |
|---|
| 42 | /// <param name="hFile">A handle to the file in which the information is to be |
|---|
| 43 | /// written.</param> |
|---|
| 44 | /// <param name="DumpType">The type of information to be generated. This parameter |
|---|
| 45 | /// can be one or more of the values from the MINIDUMP_TYPE enumeration.</param> |
|---|
| 46 | /// <param name="ExceptionParam">A pointer to a MiniDumpExceptionInfo structure |
|---|
| 47 | /// describing the client exception that caused the minidump to be generated. |
|---|
| 48 | /// If the value of this parameter is NULL, no exception information is included |
|---|
| 49 | /// in the minidump file.</param> |
|---|
| 50 | /// <param name="UserStreamParam">Not supported. Use IntPtr.Zero</param> |
|---|
| 51 | /// <param name="CallbackParam">Not supported. Use IntPtr.Zero</param> |
|---|
| 52 | /// <returns>If the function succeeds, the return value is true; otherwise, the |
|---|
| 53 | /// return value is false. To retrieve extended error information, call GetLastError. |
|---|
| 54 | /// Note that the last error will be an HRESULT value.</returns> |
|---|
| 55 | [DllImport("dbghelp.dll", SetLastError = true)] |
|---|
| 56 | [return: MarshalAs(UnmanagedType.Bool)] |
|---|
| 57 | public static extern bool MiniDumpWriteDump(IntPtr hProcess, uint ProcessId, |
|---|
| 58 | SafeFileHandle hFile, MiniDumpType DumpType, |
|---|
| 59 | ref MiniDumpExceptionInfo ExceptionParam, IntPtr UserStreamParam, |
|---|
| 60 | IntPtr CallbackParam); |
|---|
| 61 | |
|---|
| 62 | /// <summary> |
|---|
| 63 | /// Identifies the type of information that will be written to the minidump file |
|---|
| 64 | /// by the MiniDumpWriteDump function. |
|---|
| 65 | /// </summary> |
|---|
| 66 | public enum MiniDumpType |
|---|
| 67 | { |
|---|
| 68 | /// <summary> |
|---|
| 69 | /// Include just the information necessary to capture stack traces for all |
|---|
| 70 | /// existing threads in a process. |
|---|
| 71 | /// </summary> |
|---|
| 72 | MiniDumpNormal = 0x00000000, |
|---|
| 73 | |
|---|
| 74 | /// <summary> |
|---|
| 75 | /// Include the data sections from all loaded modules. This results in the |
|---|
| 76 | /// inclusion of global variables, which can make the minidump file significantly |
|---|
| 77 | /// larger. For per-module control, use the ModuleWriteDataSeg enumeration |
|---|
| 78 | /// value from MODULE_WRITE_FLAGS. |
|---|
| 79 | /// </summary> |
|---|
| 80 | MiniDumpWithDataSegs = 0x00000001, |
|---|
| 81 | |
|---|
| 82 | /// <summary> |
|---|
| 83 | /// Include all accessible memory in the process. The raw memory data is |
|---|
| 84 | /// included at the end, so that the initial structures can be mapped directly |
|---|
| 85 | /// without the raw memory information. This option can result in a very large |
|---|
| 86 | /// file. |
|---|
| 87 | /// </summary> |
|---|
| 88 | MiniDumpWithFullMemory = 0x00000002, |
|---|
| 89 | |
|---|
| 90 | /// <summary> |
|---|
| 91 | /// Include high-level information about the operating system handles that are |
|---|
| 92 | /// active when the minidump is made. |
|---|
| 93 | /// </summary> |
|---|
| 94 | MiniDumpWithHandleData = 0x00000004, |
|---|
| 95 | |
|---|
| 96 | /// <summary> |
|---|
| 97 | /// Stack and backing store memory written to the minidump file should be |
|---|
| 98 | /// filtered to remove all but the pointer values necessary to reconstruct a |
|---|
| 99 | /// stack trace. Typically, this removes any private information. |
|---|
| 100 | /// </summary> |
|---|
| 101 | MiniDumpFilterMemory = 0x00000008, |
|---|
| 102 | |
|---|
| 103 | /// <summary> |
|---|
| 104 | /// Stack and backing store memory should be scanned for pointer references |
|---|
| 105 | /// to modules in the module list. If a module is referenced by stack or backing |
|---|
| 106 | /// store memory, the ModuleWriteFlags member of the MINIDUMP_CALLBACK_OUTPUT |
|---|
| 107 | /// structure is set to ModuleReferencedByMemory. |
|---|
| 108 | /// </summary> |
|---|
| 109 | MiniDumpScanMemory = 0x00000010, |
|---|
| 110 | |
|---|
| 111 | /// <summary> |
|---|
| 112 | /// Include information from the list of modules that were recently unloaded, |
|---|
| 113 | /// if this information is maintained by the operating system. |
|---|
| 114 | /// </summary> |
|---|
| 115 | MiniDumpWithUnloadedModules = 0x00000020, |
|---|
| 116 | |
|---|
| 117 | /// <summary> |
|---|
| 118 | /// Include pages with data referenced by locals or other stack memory. |
|---|
| 119 | /// This option can increase the size of the minidump file significantly. |
|---|
| 120 | /// </summary> |
|---|
| 121 | MiniDumpWithIndirectlyReferencedMemory = 0x00000040, |
|---|
| 122 | |
|---|
| 123 | /// <summary> |
|---|
| 124 | /// Filter module paths for information such as user names or important |
|---|
| 125 | /// directories. This option may prevent the system from locating the image |
|---|
| 126 | /// file and should be used only in special situations. |
|---|
| 127 | /// </summary> |
|---|
| 128 | MiniDumpFilterModulePaths = 0x00000080, |
|---|
| 129 | |
|---|
| 130 | /// <summary> |
|---|
| 131 | /// Include complete per-process and per-thread information from the operating |
|---|
| 132 | /// system. |
|---|
| 133 | /// </summary> |
|---|
| 134 | MiniDumpWithProcessThreadData = 0x00000100, |
|---|
| 135 | |
|---|
| 136 | /// <summary> |
|---|
| 137 | /// Scan the virtual address space for PAGE_READWRITE memory to be included. |
|---|
| 138 | /// </summary> |
|---|
| 139 | MiniDumpWithPrivateReadWriteMemory = 0x00000200, |
|---|
| 140 | |
|---|
| 141 | /// <summary> |
|---|
| 142 | /// Reduce the data that is dumped by eliminating memory regions that are not |
|---|
| 143 | /// essential to meet criteria specified for the dump. This can avoid dumping |
|---|
| 144 | /// memory that may contain data that is private to the user. However, it is |
|---|
| 145 | /// not a guarantee that no private information will be present. |
|---|
| 146 | /// </summary> |
|---|
| 147 | MiniDumpWithoutOptionalData = 0x00000400, |
|---|
| 148 | |
|---|
| 149 | /// <summary> |
|---|
| 150 | /// Include memory region information. For more information, see |
|---|
| 151 | /// MINIDUMP_MEMORY_INFO_LIST. |
|---|
| 152 | /// </summary> |
|---|
| 153 | MiniDumpWithFullMemoryInfo = 0x00000800, |
|---|
| 154 | |
|---|
| 155 | /// <summary> |
|---|
| 156 | /// Include thread state information. For more information, see |
|---|
| 157 | /// MINIDUMP_THREAD_INFO_LIST. |
|---|
| 158 | /// </summary> |
|---|
| 159 | MiniDumpWithThreadInfo = 0x00001000, |
|---|
| 160 | |
|---|
| 161 | /// <summary> |
|---|
| 162 | /// Include all code and code-related sections from loaded modules to capture |
|---|
| 163 | /// executable content. For per-module control, use the ModuleWriteCodeSegs |
|---|
| 164 | /// enumeration value from MODULE_WRITE_FLAGS. |
|---|
| 165 | /// </summary> |
|---|
| 166 | MiniDumpWithCodeSegs = 0x00002000, |
|---|
| 167 | |
|---|
| 168 | /// <summary> |
|---|
| 169 | /// Turns off secondary auxiliary-supported memory gathering. |
|---|
| 170 | /// </summary> |
|---|
| 171 | MiniDumpWithoutAuxiliaryState = 0x00004000, |
|---|
| 172 | |
|---|
| 173 | /// <summary> |
|---|
| 174 | /// Requests that auxiliary data providers include their state in the dump |
|---|
| 175 | /// image; the state data that is included is provider dependent. This option |
|---|
| 176 | /// can result in a large dump image. |
|---|
| 177 | /// </summary> |
|---|
| 178 | MiniDumpWithFullAuxiliaryState = 0x00008000, |
|---|
| 179 | |
|---|
| 180 | /// <summary> |
|---|
| 181 | /// Scans the virtual address space for PAGE_WRITECOPY memory to be included. |
|---|
| 182 | /// </summary> |
|---|
| 183 | MiniDumpWithPrivateWriteCopyMemory = 0x00010000, |
|---|
| 184 | |
|---|
| 185 | /// <summary> |
|---|
| 186 | /// If you specify MiniDumpWithFullMemory, the MiniDumpWriteDump function will |
|---|
| 187 | /// fail if the function cannot read the memory regions; however, if you include |
|---|
| 188 | /// MiniDumpIgnoreInaccessibleMemory, the MiniDumpWriteDump function will |
|---|
| 189 | /// ignore the memory read failures and continue to generate the dump. Note that |
|---|
| 190 | /// the inaccessible memory regions are not included in the dump. |
|---|
| 191 | /// </summary> |
|---|
| 192 | MiniDumpIgnoreInaccessibleMemory = 0x00020000, |
|---|
| 193 | |
|---|
| 194 | /// <summary> |
|---|
| 195 | /// Adds security token related data. This will make the "!token" extension work |
|---|
| 196 | /// when processing a user-mode dump. |
|---|
| 197 | /// </summary> |
|---|
| 198 | MiniDumpWithTokenInformation = 0x00040000 |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | /// <summary> |
|---|
| 202 | /// Contains the exception information written to the minidump file by the |
|---|
| 203 | /// MiniDumpWriteDump function. |
|---|
| 204 | /// </summary> |
|---|
| 205 | [StructLayout(LayoutKind.Sequential, Pack = 4)] |
|---|
| 206 | public struct MiniDumpExceptionInfo |
|---|
| 207 | { |
|---|
| 208 | /// <summary> |
|---|
| 209 | /// The identifier of the thread throwing the exception. |
|---|
| 210 | /// </summary> |
|---|
| 211 | public uint ThreadId; |
|---|
| 212 | |
|---|
| 213 | /// <summary> |
|---|
| 214 | /// A pointer to an EXCEPTION_POINTERS structure specifying a |
|---|
| 215 | /// computer-independent description of the exception and the processor |
|---|
| 216 | /// context at the time of the exception. |
|---|
| 217 | /// </summary> |
|---|
| 218 | public IntPtr ExceptionPointers; |
|---|
| 219 | |
|---|
| 220 | /// <summary> |
|---|
| 221 | /// Determines where to get the memory regions pointed to by the |
|---|
| 222 | /// ExceptionPointers member. Set to TRUE if the memory resides in the |
|---|
| 223 | /// process being debugged (the target process of the debugger). Otherwise, |
|---|
| 224 | /// set to FALSE if the memory resides in the address space of the calling |
|---|
| 225 | /// program (the debugger process). If you are accessing local memory (in |
|---|
| 226 | /// the calling process) you should not set this member to TRUE. |
|---|
| 227 | /// </summary> |
|---|
| 228 | [MarshalAs(UnmanagedType.Bool)] |
|---|
| 229 | public bool ClientPointers; |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | } |
|---|