nodejs / nan

Native Abstractions for Node.js
MIT License
3.29k stars 505 forks source link

nodejs C++ addon exception catch #868

Closed wanghui-andy closed 5 years ago

wanghui-andy commented 5 years ago

i have written a C++ addon and compile it into .node file, somtimes it crashes, so i want to catch the exception, what i do now is tring to write dump file with windows API in my C++ file, but when i run my program, the exception-catch callback function never be triggered. What confuses me is that when i use the same code to catch exception and write dump file in .dll file, it works well, what should i do if i want to write dump file correctly when my C++ addon crashes? Thanks!

bnoordhuis commented 5 years ago

somtimes it crashes

Can you define 'crashes'?

the exception-catch callback function

It'd help if you posted or linked to your code.

wanghui-andy commented 5 years ago

somtimes it crashes

Can you define 'crashes'?

the exception-catch callback function

It'd help if you posted or linked to your code.

‘crashes’means exception, such as "printf("%s", 111)"

wanghui-andy commented 5 years ago

somtimes it crashes

Can you define 'crashes'?

the exception-catch callback function

It'd help if you posted or linked to your code.

the exception-catch code:

#pragma once
#include <windows.h>
#include < Dbghelp.h>
#include <iostream>  
#include <vector>  
using namespace std;

#pragma comment(lib, "Dbghelp.lib")

namespace NSDumpFile
{
    void CreateDumpFile(LPCWSTR lpstrDumpFilePathName, EXCEPTION_POINTERS *pException)
    {
        // 创建Dump文件  
        //  
        HANDLE hDumpFile = CreateFile(lpstrDumpFilePathName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

        // Dump信息  
        //  
        MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
        dumpInfo.ExceptionPointers = pException;
        dumpInfo.ThreadId = GetCurrentThreadId();
        dumpInfo.ClientPointers = TRUE;

        // 写入Dump文件内容  
        //  
        MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);

        CloseHandle(hDumpFile);
    }

    LPTOP_LEVEL_EXCEPTION_FILTER WINAPI MyDummySetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)
    {
        return NULL;
    }

    BOOL PreventSetUnhandledExceptionFilter()
    {
        HMODULE hKernel32 = LoadLibrary(_T("kernel32.dll"));
        if (hKernel32 == NULL)
            return FALSE;

        void *pOrgEntry = GetProcAddress(hKernel32, "SetUnhandledExceptionFilter");
        if (pOrgEntry == NULL)
            return FALSE;

        unsigned char newJump[100];
        DWORD dwOrgEntryAddr = (DWORD)pOrgEntry;
        dwOrgEntryAddr += 5; // add 5 for 5 op-codes for jmp far

        void *pNewFunc = &MyDummySetUnhandledExceptionFilter;
        DWORD dwNewEntryAddr = (DWORD)pNewFunc;
        DWORD dwRelativeAddr = dwNewEntryAddr - dwOrgEntryAddr;

        newJump[0] = 0xE9;  // JMP absolute
        memcpy(&newJump[1], &dwRelativeAddr, sizeof(pNewFunc));
        SIZE_T bytesWritten;
        BOOL bRet = WriteProcessMemory(GetCurrentProcess(), pOrgEntry, newJump, sizeof(pNewFunc) + 1, &bytesWritten);
        return bRet;
    }

    LONG WINAPI UnhandledExceptionFilterEx(struct _EXCEPTION_POINTERS *pException)
    {
        TCHAR szMbsFile[MAX_PATH] = { 0 };
        ::GetModuleFileName(NULL, szMbsFile, MAX_PATH);
        TCHAR* pFind = _tcsrchr(szMbsFile, '\\');
        if (pFind)
        {
            *(pFind + 1) = 0;
            _tcscat(szMbsFile, _T("CrashDumpFile.dmp"));
            CreateDumpFile(szMbsFile, pException);
        }

        // TODO: MiniDumpWriteDump
        //FatalAppExit(-1, _T("Fatal Error"));
        return EXCEPTION_CONTINUE_SEARCH;
    }

    void RunCrashHandler()
    {
        SetUnhandledExceptionFilter(UnhandledExceptionFilterEx);
        PreventSetUnhandledExceptionFilter();
    }
};

#define DeclareDumpFile() NSDumpFile::RunCrashHandler();
bnoordhuis commented 5 years ago

Okay, I see what you mean but I fail to see how any of it is related to nan. I think this is something you'll have to figure out yourself. I can't help you at least.

bnoordhuis commented 5 years ago

I see you've also opened https://github.com/nodejs/help/issues/2169. I'll close out this issue in favor of that one.

SongAdonis commented 2 years ago

@bnoordhuis @wanghui-andy Do you have any solution for this problem? I have the same question. And I have try SetErrorMode(0) or the other params, the exception-catch callback still cannot be triggered