Skip to content

Instantly share code, notes, and snippets.

@JonCole
Last active September 25, 2017 15:47
Show Gist options
  • Save JonCole/aa1c732b77bf4ce28d3d to your computer and use it in GitHub Desktop.
Save JonCole/aa1c732b77bf4ce28d3d to your computer and use it in GitHub Desktop.
Crash Dump instructions and Helper code for doing it programmatically.
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace System.Diagnostics
{
public static class CrashDumpHelper
{
/// <summary>
/// Creates a memory dump of the current process at the specified file location, overwiting any existing dump file...
/// </summary>
/// <returns>true if the dump was written successfully</returns>
public static bool CreateCrashDump(string path)
{
var targetProcess = Process.GetCurrentProcess();
if (File.Exists(path))
File.Delete(path);
IntPtr exceptionParam = IntPtr.Zero;
using (var file = new FileStream(path, FileMode.Create))
{
return MiniDumpWriteDump(targetProcess.Handle, (uint)targetProcess.Id, file.SafeFileHandle, MINIDUMP_TYPE.MiniDumpWithFullMemory, ref exceptionParam, IntPtr.Zero, IntPtr.Zero);
}
}
[DllImport("Dbghelp.dll")]
static extern bool MiniDumpWriteDump(IntPtr hProcess, uint ProcessId, SafeHandle hFile, MINIDUMP_TYPE DumpType, ref IntPtr ExceptionParam, IntPtr UserStreamParam, IntPtr CallbackParam);
enum MINIDUMP_TYPE : int
{
MiniDumpNormal = 0x00000000,
MiniDumpWithDataSegs = 0x00000001,
MiniDumpWithFullMemory = 0x00000002,
MiniDumpWithHandleData = 0x00000004,
MiniDumpFilterMemory = 0x00000008,
MiniDumpScanMemory = 0x00000010,
MiniDumpWithUnloadedModules = 0x00000020,
MiniDumpWithIndirectlyReferencedMemory = 0x00000040,
MiniDumpFilterModulePaths = 0x00000080,
MiniDumpWithProcessThreadData = 0x00000100,
MiniDumpWithPrivateReadWriteMemory = 0x00000200,
MiniDumpWithoutOptionalData = 0x00000400,
MiniDumpWithFullMemoryInfo = 0x00000800,
MiniDumpWithThreadInfo = 0x00001000,
MiniDumpWithCodeSegs = 0x00002000,
MiniDumpWithoutAuxiliaryState = 0x00004000,
MiniDumpWithFullAuxiliaryState = 0x00008000,
MiniDumpWithPrivateWriteCopyMemory = 0x00010000,
MiniDumpIgnoreInaccessibleMemory = 0x00020000,
MiniDumpWithTokenInformation = 0x00040000,
MiniDumpWithModuleHeaders = 0x00080000,
MiniDumpFilterTriage = 0x00100000,
MiniDumpValidTypeFlags = 0x001fffff
}
}
}

Azure App Service (WebSites)

  1. Identify the process that we want to dump out. You can do this by going to the Kudu console, then selecting Process explorer, and noting the PID column for the application in question. Please make sure you are looking at the non-scm w3wp.exe process. If it’s not there, you might need to start the application
  2. Launch the Debug Console – CMD
  3. Navigate to the d:\home\Logfiles directory
  4. Run this command: d:\devtools\sysinternals\procdump.exe -accepteula -ma -e 1 -f TimeoutException -n 3 where is the process id from the first step. NOTE: Sometimes pasting from an e-mail modifies the dashes so if you are having issues running this command, please re-type it instead of pasting it.

Web Role

  1. RDP into your web role instance.
  2. Download ProcDump.exe.
  3. Open a command prompt.
  4. Switch into the %windir%\System32\inetsrv folder (cd /D %windir%\System32\inetsrv). Run the command appcmd list wp. This will provide a list of running w3wp.exe processes. Example:

D:\Windows\System32\inetsrv>appcmd list wp WP "3204"> (applicationPool:51c661b0-eb07-423d-b60d-c9a98f1f47c0)

Your web role will have a long GUID in the applicationPool name. Note it’s process ID (PID). In the above output the PID is 3204. Now run ProcDump and use the PID to attach to this worker process:

Procdump -accepteula -ma -e 1 -f TimeoutException -n 3

This will capture 3 dumps of the process when System.TimeoutException errors are thrown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment