コード例 #1
0
ファイル: MiniDump.cs プロジェクト: dsmalik/process-dump
        public static string Create(string processName, NativeMethods._MINIDUMP_TYPE dumpType)
        {
            Process process = Process.GetProcessesByName(processName).FirstOrDefault();

            if (process == null)
            {
                throw new InvalidOperationException($"Specify a valid process name - {processName} not found.");
            }

            return(Create(process, dumpType, TempPath));
        }
コード例 #2
0
ファイル: MiniDump.cs プロジェクト: dsmalik/process-dump
        public static string Create(int processId, NativeMethods._MINIDUMP_TYPE dumpType, string path)
        {
            Process process = Process.GetProcessById(processId);

            if (process == null)
            {
                throw new InvalidOperationException($"Specify a valid process id - {processId} not found.");
            }

            return(Create(process, dumpType, path));
        }
コード例 #3
0
ファイル: MiniDump.cs プロジェクト: dsmalik/process-dump
        public static string Create(Process process, NativeMethods._MINIDUMP_TYPE dumpType, string outputPath)
        {
            IntPtr hFile = IntPtr.Zero;

            if (IntPtr.Size == 4)
            {
                Console.WriteLine($"CreateMiniDump {dumpType} - Running as 32 bit, creating 32 bit dumps");
            }
            else
            {
                Console.WriteLine($"CreateMiniDump {dumpType} - Running as 64 bit, creating 64 bit dumps");
            }

            try
            {
                var dumpFileName = $@"{outputPath}\MiniDumpProcess-{dumpType.ToString()}.dmp";
                if (File.Exists(dumpFileName))
                {
                    File.Delete(dumpFileName);
                }

                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);
                    throw ex;
                }

                var exceptInfo = new NativeMethods.MINIDUMP_EXCEPTION_INFORMATION();

                if (!process.Is32BitProcess() && IntPtr.Size == 4)
                {
                    throw new InvalidOperationException("Can't create 32 bit dump of 64 bit process");
                }

                var isDumpWrittenSuccessfully = NativeMethods.MiniDumpWriteDump(process.Handle, process.Id, hFile,
                                                                                dumpType, ref exceptInfo, UserStreamParam: IntPtr.Zero, CallbackParam: IntPtr.Zero);

                if (!isDumpWrittenSuccessfully)
                {
                    var hr = Marshal.GetHRForLastWin32Error();
                    var ex = Marshal.GetExceptionForHR(hr);
                    throw ex;
                }

                return(dumpFileName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw ex;
            }
            finally
            {
                NativeMethods.CloseHandle(hFile);
            }
        }
コード例 #4
0
ファイル: MiniDump.cs プロジェクト: dsmalik/process-dump
 public static string Create(int processId, NativeMethods._MINIDUMP_TYPE dumpType)
 {
     return(Create(processId, dumpType, TempPath));
 }
コード例 #5
0
        void ProcessTraceEvent(TraceEvent traceEvent)
        {
            if (traceEvent.ProcessID == currentProcessId)
            {
                return;
            }

            if ((ushort)traceEvent.ID == ModuleDCStart_V2)
            {
                var processToUse = Process.GetProcessById(traceEvent.ProcessID);

                if (Path.GetFileNameWithoutExtension(processName) == processToUse.ProcessName.ToLowerInvariant() &&
                    moduleName == Path.GetFileName((string)traceEvent.PayloadByName("ModuleILPath")).ToLowerInvariant()
                    )
                {
                    startWatch.Stop();
                    session.Dispose();

                    switch (actionType)
                    {
                    case "ttd":

                        Console.WriteLine("Process and Module found. Attaching TTD now ...");

                        try
                        {
                            var process = Process.Start("TTTRacer.exe", string.Format("-dumpFull -attach {0}", processToUse.Id));
                            process.WaitForExit();

                            if (process.HasExited)
                            {
                                Console.WriteLine("TTD exited with exit code: {0}", process.ExitCode);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("ERROR: {0}", ex.ToString());
                        }

                        break;

                    case "dmp":
                    default:

                        IntPtr hFile = IntPtr.Zero;

                        try
                        {
                            var dumpFileName = string.Concat(Path.GetFileNameWithoutExtension(processName), "_", DateTime.Now.ToString("yyyyMMddHHmmssfff"), ".dmp");

                            if (File.Exists(dumpFileName))
                            {
                                File.Delete(dumpFileName);
                            }

                            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);
                                throw ex;
                            }

                            NativeMethods._MINIDUMP_TYPE dumpType = NativeMethods._MINIDUMP_TYPE.MiniDumpWithFullMemory;

                            var exceptInfo = new NativeMethods.MINIDUMP_EXCEPTION_INFORMATION();

                            if (!Is32BitProcess(processToUse) && IntPtr.Size == 4)
                            {
                                throw new InvalidOperationException("Can't create 32 bit dump of 64 bit process");
                            }

                            Console.WriteLine("Process and Module found. Writing dump file now ...");

                            var result = NativeMethods.MiniDumpWriteDump(
                                processToUse.Handle,
                                processToUse.Id,
                                hFile,
                                dumpType,
                                ref exceptInfo,
                                UserStreamParam: IntPtr.Zero,
                                CallbackParam: IntPtr.Zero
                                );

                            if (result == false)
                            {
                                var hr = Marshal.GetHRForLastWin32Error();
                                var ex = Marshal.GetExceptionForHR(hr);
                                throw ex;
                            }

                            Console.WriteLine("Dump Created. Dump is located here: {0}", dumpFileName);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("ERROR: {0}", ex.ToString());
                        }
                        finally
                        {
                            NativeMethods.CloseHandle(hFile);
                        }

                        break;
                    }

                    Stop();
                }
            }
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += (ol, el) =>
            {
                IntPtr hFile = IntPtr.Zero;
                try
                {
                    if (IntPtr.Size == 4)
                    {
                        this.Title = "CreateMiniDump Running as 32 bit, creating 32 bit dumps";
                    }
                    else
                    {
                        this.Title = "CreateMiniDump Running as 64 bit, creating 64 bit dumps";
                    }
                    this.Height = 800;
                    this.Width  = 800;
                    var sp = new StackPanel()
                    {
                        Orientation = Orientation.Vertical
                    };
                    this.Content = sp;
                    var dumpFileName = System.IO.Path.Combine(
                        System.IO.Path.GetTempPath(),
                        "testdump.dmp");
                    var txtDumpFile = new TextBox()
                    {
                        Text = dumpFileName
                    };
                    sp.Children.Add(txtDumpFile);

                    var txtProcName = new TextBox()
                    {
                        ToolTip = "Process name without extension",
                        Text    = "devenv"
                    };
                    sp.Children.Add(txtProcName);
                    var lstDumpType = new ListBox()
                    {
                        // allow multi select
                        SelectionMode = SelectionMode.Extended
                    };
                    lstDumpType.ItemsSource = Enum.GetValues(typeof(NativeMethods._MINIDUMP_TYPE));
                    // set initial value
                    // for a dump with memory info we want these:
                    foreach (var val in new[] {
                        NativeMethods._MINIDUMP_TYPE.MiniDumpWithFullMemory,
                        NativeMethods._MINIDUMP_TYPE.MiniDumpWithFullMemoryInfo,
                        NativeMethods._MINIDUMP_TYPE.MiniDumpWithHandleData,
                        NativeMethods._MINIDUMP_TYPE.MiniDumpWithThreadInfo
                    })
                    {
                        var nval = (int)(Math.Log((int)val) / Math.Log(2)) + 1;
                        lstDumpType.SelectedItems.Add(
                            lstDumpType.Items[nval]
                            );
                    }
                    sp.Children.Add(lstDumpType);

                    var btnGo = new Button()
                    {
                        Content = "_Create Dump",
                        Width   = 200
                    };
                    sp.Children.Add(btnGo);
                    var txtStatus = new TextBox()
                    {
                        IsUndoEnabled = false,
                        VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
                        MaxHeight = 400
                    };
                    sp.Children.Add(txtStatus);
                    btnGo.Click += (ob, eb) =>
                    {
                        try
                        {
                            var sw = new Stopwatch();
                            sw.Start();
                            dumpFileName = txtDumpFile.Text.Trim();
                            if (System.IO.File.Exists(dumpFileName))
                            {
                                System.IO.File.Delete(dumpFileName);
                            }
                            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);
                                throw ex;
                            }
                            NativeMethods._MINIDUMP_TYPE dumpType = NativeMethods._MINIDUMP_TYPE.MiniDumpNormal;       // 0
                            foreach (var item in lstDumpType.SelectedItems)
                            {
                                var dt = (NativeMethods._MINIDUMP_TYPE)item;
                                dumpType |= dt;
                            }

                            var exceptInfo = new NativeMethods.MINIDUMP_EXCEPTION_INFORMATION();
                            var proc       = Process.GetProcessesByName(
                                txtProcName.Text.Trim()
                                ).FirstOrDefault();

                            if (!proc.Is32BitProcess() && IntPtr.Size == 4)
                            {
                                throw new InvalidOperationException(
                                          "Can't create 32 bit dump of 64 bit process"
                                          );
                            }

                            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);
                                throw ex;
                            }

                            txtStatus.Text = string.Format(
                                "Dump Created. Pid= {0} {1}\r File size = {2:n0}\rElapsed = {3:n3}\r",
                                proc.Id,
                                dumpType,
                                (new System.IO.FileInfo(dumpFileName)).Length,
                                sw.Elapsed.TotalSeconds
                                );
                        }
                        catch (Exception ex)
                        {
                            txtStatus.Text = ex.ToString();
                        }
                        finally
                        {
                            NativeMethods.CloseHandle(hFile);
                        }
                    };
                }
                catch (Exception ex)
                {
                    this.Content = ex.ToString();
                }
            };
        }