使用FileMapping传递_EXCEPTION_POINTERS *时遇到问题 [英] Trouble passing _EXCEPTION_POINTERS * using FileMapping

查看:157
本文介绍了使用FileMapping传递_EXCEPTION_POINTERS *时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个进程外异常处理程序,我创建了一个看门狗进程,当子进程引发异常时,它会执行专门的异常处理。我已经成功通过事件调用看门狗进程。我遇到的问题是尝试将异常信息指针传递给其他进程。



我在这里登陆将指针传递给用exec()生成的进程,并且知道共享内存中的传递指针有这个问题: / p>

如果你使用共享内存,你不能传递指针。指针将包含虚拟地址,这是一个进程之间的不同,你必须交换



如果不使用共享内存,则不能交换任何类型的指针:其他进程将无法访问您的进程的内存。



现在我该如何克服这一点?



流程1:

  struct mytest 
{
_EXCEPTION_POINTERS * except;
DWORD ThreadId;
DWORD ProcessId;
}

OpenFileMapping();

void * pBuf = MapViewOfFile();

mytest passdata;

CopyMemory(pBuf,& passdata,sizeof(passdata));

UnMapView();

CloseHandle();

(例如)流程2:

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

会崩溃。我明白这是因为虚拟地址是特定于进程的。但在这种情况下如何传递异常信息到不同的进程和写一个minidump?



PS:我甚至尝试单独传递PEXCEPTION_RECORD结构,但不起作用。

解决方案

右,你不能在另一个进程中解引用指针,它只在崩溃的进程中有效。它只是足够好传递给MiniDumpWriteDump(),MINIDUMP_EXCEPTION_INFORMATION.ExceptionPointers字段。技术上你可以使用ReadProcessMemory(),但这样做对于崩溃的进程是不必要的风险。简单的解决方案是在存储异常代码并由异常过滤器写入的结构中添加一个额外的字段。

  mytest passdata ; 
passdata.except = ExceptionInfo;
//注意:添加字段
passdata.ExceptionCode = ExceptionInfo-> ExceptionRecord-> ExceptionCode;
passdata.ThreadId = GetCurrentThreadId();
//等..

同时避免调用winapi函数,如OpenFileMapping和MapViewOfFile,太冒险了。当程序因进程堆的堆损坏而崩溃时,它们往往会死锁。崩溃的常见原因和死锁,因为堆锁仍然保持。只需在程序初始化时执行此操作。你不需要费心清理,Windows会照顾它,当你的看门狗进程终止崩溃的进程,在采取minidump后。


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 successfully invoked the watchdog process through events . The problem i am facing is while trying to pass the exception information pointers to the other process .

I landed here Passing a pointer to process spawned with exec() and came to know that passing pointers in shared memory has this issue :

"If you use shared memory, you can't pass the pointer. The pointer will contain the virtual address, which is different from one process to another. You have to exchange offset values, based on the start of the shared memory area.

If you don't use shared memory, you can't exchange pointers of any kind: The other process won't be able to access the memory of your process."

Now how can i overcome this ?

Process 1 :

    struct mytest
    {
      _EXCEPTION_POINTERS * except ;
      DWORD ThreadId ;
      DWORD ProcessId ;
    }

    OpenFileMapping ( ) ;

    void * pBuf = MapViewOfFile ( ) ;

    mytest passdata ;

    CopyMemory ( pBuf , &passdata , sizeof ( passdata ) ) ;

    UnMapView ( ) ;

    CloseHandle ( ) ;

(For ex)Process 2 :

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

would crash . I understand this is because virtual address is process specific . But in this case how to pass exception information to different process and write a minidump ??

P.S : I even tried passing PEXCEPTION_RECORD structures seperately but does not work .

解决方案

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.

这篇关于使用FileMapping传递_EXCEPTION_POINTERS *时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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