我可以在信号处理程序中做什么? [英] What can I do in a signal handler?

查看:87
本文介绍了我可以在信号处理程序中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct sigaction newSignalAction;
memset(&newSignalAction, 0, sizeof(newSignalAction));
newSignalAction.sa_handler = &SignalHandler;
sigaction(SIGSEGV, &newSignalAction, NULL);

[TestFlight takeOff:@"etc etc etc"];

然后

void SignalHandler(int sig) {
    NSLog(@"Boom");
}

我试图执行 SIGSEGV 信号:

int *p = NULL;
*p = 1;

但我的处理程序未被调用。相反,Xcode指出行 * p = 1

But my handler is not called. Instead, Xcode points out the line *p = 1.

我读过你不能打印东西用信号处理程序。也许这就是问题所在。 可以我做什么?我听说你应该保存数据 - 所以我想我可以在Documents目录中创建文件。我也尝试这样做,但也没有创建文件。

I have read that you can't print stuff with a signal handler. Maybe that's the problem. What can I do then? I heard that you're supposed to save data - so I guess I can create files in the Documents directory. I tried to do that, too, but the files are not created either.

推荐答案

近似你无法做到信号处理程序中的任何内容问题是由信号中断的代码可能是持有锁。如果你的处理程序试图采用相同的锁,那么它会死锁。

To a close approximation you can't do anything in a signal handler. The problem is that the code interrupted by the signal might be holding locks. If your handler tries to take the same locks then it deadlocks.

sigaction 手册页列出了官方批准在信号处理程序中使用的函数。清单很简短。 NSLog()和Objective-C方法调度都不在该列表中。

The sigaction man page lists the functions that are officially approved for use in a signal handler. The list is short. Neither NSLog() nor Objective-C method dispatch is in that list.

但是,如果 NSLog()没有死锁,那么它应该像往常一样打印输出。也许你的崩溃是 SIGBUS 而不是 SIGSEGV 。也许编译器优化器正在转换你的 * NULL = 1 ,以便它以不同的方式崩溃。也许TestFlight会安装自己的信号处理程序来替换你的信号处理程序。

However, if NSLog() doesn't deadlock then it should print output as usual. Perhaps your crash is SIGBUS instead of SIGSEGV. Perhaps the compiler optimizer is transforming your *NULL=1 so that it crashes in a different way. Perhaps TestFlight installs its own signal handler that replaces your signal handler.

这篇关于我可以在信号处理程序中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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