有没有一种方法可以获取在处理信号期间访问的指针
Is there a way to get the pointer that was access during the handling of a signal?
大约代码是这样的:
#include <signal.h>
void SegmentationFaultHandler( int signal ) {
if ( signal == SIGSEGV ) {
// how to check here if it's actual null pointer?
Throw( NullPointerException, "Object pointer not set to an instance of an object." );
}
else
Throw( InvalidOperationException, "Signal has been intercepted by wrong function." );
}
int main( ) {
signal( SIGSEGV, SegmentationFaultHandler );
try {
int *i = null;
*i = 0;
...
如果我没有仔细检查指针,或者只是访问了未初始化的数据并尊重它,我该如何检查?
我知道这是可能的,因为调试器可以知道程序试图访问哪个地址。
似乎可以使用在signal.h中声明的_pxcptinfoptrs全局变量在处理程序中检索PEXCEPTION_POINTERS。然后使用以下示例中的指针。
static void sigsegv_handler(int signo)
{
PEXCEPTION_POINTERS excptr = _pxcptinfoptrs;
if (excptr != NULL) {
}
// ...
}
矢量异常处理程序
在windows下,您可以使用矢量异常处理程序。您的处理程序将如下所示:
LONG WINAPI ExceptionFilter( struct _EXCEPTION_POINTERS * pExceptionPointers ) {
然后:
pExceptionPointers->ExceptionRecord->ExceptionCode
是您的异常代码,exception_ACCESS_VIOLATION是您访问无效内存时的异常代码。
pExceptionPointers->ExceptionRecord->ExceptionInformation[0] == 0
在完成读取操作时为true
pExceptionPointers->ExceptionRecord->ExceptionInformation[0] == 1
用于写操作
pExceptionPointers->ExceptionRecord->ExceptionInformation[1]
是发生异常时正在读取/写入的地址
结构化异常过滤
如果您不能使用向量异常处理程序,那么您可以在代码的最低级别添加__try/__except,即在main()中或执行线程函数的位置:
__try {
// Code which might cause access violation or any other hardware exceptions
}
__except(ExceptionFilter(GetExceptionInformation())) {
}
相关文章:
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 有可能在信号处理程序中设置promise吗
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 调试和自由执行中的信号处理
- 通过安装信号处理程序关闭多线程应用程序
- 为什么我的信号处理程序只执行一次?
- GTK C++:找不到信号处理程序 您是否使用 -rdynamic 进行了编译?
- C++ 在信号处理程序后继续执行
- 没有信号处理程序的POSIX定时器的目的是什么?
- 为什么这个信号处理程序不能捕获 SIGHUP 或 SIGQUIT?
- 如何在C++中使用 std::bind 函数作为信号处理程序?
- 在 C++17 中,是否未定义使用无锁原子学保护从信号处理程序传递的数据?
- 如何将信号处理程序添加为方法
- 窗口上信号处理程序的异步安全写入函数
- 使用信号处理程序处理从 FIFO 接收的数据
- gtkmm/glade - 将信号处理程序连接到 MenuShell
- GTK+3 编译错误"找不到信号处理程序",您是否使用 -rdynamic 进行了编译?
- Linux 中的信号处理程序使用 sigaction (C++)
- 使用 enum 参数将 C++ 对象信号连接到 QML 信号处理程序