Trouble passing _EXCEPTION_POINTERS * using FileMapping

前端 未结 2 566
萌比男神i
萌比男神i 2021-01-21 17:30

I wanted to do a out-of-process exception handler and i had created a watch-dog process which does dedicated exception handling when child process raises exception. I had succes

相关标签:
2条回答
  • 2021-01-21 17:50

    Right, you cannot dereference the pointer in another process, it is only valid in the crashed process. It is only good enough to pass to MiniDumpWriteDump(), MINIDUMP_EXCEPTION_INFORMATION.ExceptionPointers field. Technically you could use ReadProcessMemory() but doing so for a crashed process is unnecessarily risky. The simple solution is to add an extra field to your structure that stores the exception code and written by your exception filter.

    mytest passdata ;
    passdata.except = ExceptionInfo;
    // Note: added field
    passdata.ExceptionCode = ExceptionInfo->ExceptionRecord->ExceptionCode;
    passdata.ThreadId = GetCurrentThreadId();
    // etc..
    

    Also avoid calling winapi functions like OpenFileMapping and MapViewOfFile, it is too risky. They tend to deadlock when the program crashed due to heap corruption of the process heap. A common reason to crash and a deadlock because the heap lock is still held. Just do this at program initialization. You don't need to bother cleaning up either, Windows takes care of it when your watchdog process terminates the crashed process after taking the minidump.

    0 讨论(0)
  • 2021-01-21 17:53

    I will put this together as an answer, although it really should be a comment to Hans's answer (and the comments there) but it seems some explanation is necessary:

    The code posted in the question correctly passes the value(s) of the struct mytest structure into shared memory.

    The second code snippet:

    (For ex)Process 2 :

    cout << passdata->except->ExceptionRecord->ExceptionCode << endl ;
    

    Shows a misunderstanding though: While you can read the value of the pointer passdata.except, in process 2 this is just an arbitrary 32/64 bit value, it is not a valid pointer.

    You can pass this to MiniDumpWriteDump, this function will eveluate this pointer value in the context of the target process (proc 1). But you cannot dereference it in process #2.

    Hans's example gives the solution, iff you need the value of ExeptionCode in process #2, then you need to dereference the pointer in proc#1 and put the value into the data you write to shared memory.

    0 讨论(0)
提交回复
热议问题