如何进行转储 [英] how to take Dump of a process

查看:64
本文介绍了如何进行转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的c#代码,用于转储进程Test_app(一个c ++应用程序).

但是问题是,当此程序(下面的代码)中发生异常而不是Test_app时,会生成转储.

我想在Test_app中发生异常时转储Test_app.
但是我做不到.

here is my c# code that takes dump of a process Test_app(a c++ application).

but the problem is that dump is generated when exception occurs in this program(code below) instead of Test_app.

I want to take dump of the Test_app when exception occurs in Test_app.
but i m unable to do so.

using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using System.Globalization;
using System.Diagnostics;
using System.Collections.Generic;


public class CallMyHelloWorld
{
    public static void Main()
    {

        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomainUnhandledException);


    string i;
    i = Console.ReadLine();
    int a = Convert.ToInt32(i);

    int j = 10 / a;
    Console.WriteLine("The Application succeeded");

    }

    public static class MINIDUMPTYPE
    {
        public const int MiniDumpNormal = 0x00000000;
        public const int MiniDumpWithDataSegs = 0x00000001;
        public const int MiniDumpWithFullMemory = 0x00000002;
        public const int MiniDumpWithHandleData = 0x00000004;
        public const int MiniDumpFilterMemory = 0x00000008;
        public const int MiniDumpScanMemory = 0x00000010;
        public const int MiniDumpWithUnloadedModules = 0x00000020;
        public const int MiniDumpWithIndirectlyReferencedMemory = 0x00000040;
        public const int MiniDumpFilterModulePaths = 0x00000080;
        public const int MiniDumpWithProcessThreadData = 0x00000100;
        public const int MiniDumpWithPrivateReadWriteMemory = 0x00000200;
        public const int MiniDumpWithoutOptionalData = 0x00000400;
        public const int MiniDumpWithFullMemoryInfo = 0x00000800;
        public const int MiniDumpWithThreadInfo = 0x00001000;
        public const int MiniDumpWithCodeSegs = 0x00002000;
    }

    [DllImport("dbghelp.dll")]
    public static extern bool MiniDumpWriteDump(IntPtr hProcess, 
                                                Int32 ProcessId, 
                                                IntPtr hFile, 
                                                int DumpType, 
                                                IntPtr ExceptionParam, 
                                                IntPtr UserStreamParam, 
                                                IntPtr CallackParam);
    private static void CurrentDomainUnhandledException(object sender, 
                                                        UnhandledExceptionEventArgs e)
    {
            CreateMiniDump();
    }

    private static void CreateMiniDump()
    {
        DateTime endTime = DateTime.Now;

        string dt = endTime.ToString("yyyy.MM.dd.HH.mm.ss", DateTimeFormatInfo.InvariantInfo);

        System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("Test_app");

       string dumpFileName = "C:\\HelloWorldDump" + process[0] + dt + ".dmp";

        FileStream fs = new FileStream(dumpFileName, FileMode.Create);


        MiniDumpWriteDump(process[0].Handle, 
                          process[0].Id, 
                          fs.SafeFileHandle.DangerousGetHandle(), 
                          MINIDUMPTYPE.MiniDumpWithFullMemory, 
                          IntPtr.Zero, 
                          IntPtr.Zero, 
                          IntPtr.Zero);

         }
}

推荐答案

您以编程方式启动了该过程吗?如果是这样,您可以查看退出代码,但是除非该子进程在失败时转储其自身的状态,否则我不确定您可以得到什么,因为一个进程也可以选择不提供退出代码.如果发生故障.

因此,除非您实际编写了要监视的过程,否则您实质上就是SOL.
Did you programatically start the process? If so, you can watch for the exit code, but unless that child process is dumping its own state when it fails, I''m not sure what you could get back, because a process could also choose not to provide an exit code in the event of a failure.

So, unless you actually wrote the process you''re watching, you''re essentially SOL.


要在Test_App中发生未处理的异常时获取调试转储,您必须调用Test_App进程(C ++应用程序)中未处理异常过滤器中的MiniDumpWriteDump函数.您正在做的是向生成小型转储的进程添加未处理的异常过滤器.

或者,您可以在C#应用程序中使用WIN32调试API(WaitForDebugEvent),但这会有些复杂.
To get a debug dump when an unhandled exception occurs in the Test_App, you have to call the MiniDumpWriteDump function from the Unhandled Exception filter in the Test_App process (the C++ application). What you are doing is adding unhandled exception filter to the process that generates the mini-dump.

Alternatively, you can use the WIN32 debugging API (WaitForDebugEvent) in your C# application but that will be a little complex.


感谢Rama Krishna的迅速答复.

如何在C#应用程序中使用WIN32调试API(WaitForDebugEvent).

您能给我提供代码吗?
Thank You Rama Krishna for the prompt reply.

how can I use WIN32 debugging API (WaitForDebugEvent) in my C# application.

Can You provide me with the code.


这篇关于如何进行转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆