Xcode里调试 自定义 signal的信号 回调函数的执行

 signal信号:

 signal是一种软中断信号,提供异步事件处理机制。

signal是进程间相互传递信息的一种粗糙方法,使用场景: 进程终止相关;

终端交互;  

编程错误或硬件错误相关,系统遇到不可恢复的错误时触发崩溃机制让程序退出,比如:除0、内存写入错误等。
        这里我们主要考虑系统遇到不可恢复的错误时即Crash时,信号相关的应用。signal信号处理是UNIX操作系统机制,可以基于signal来捕获Android Native Crash。

 signal注册和处理signal():
       注册signal handler;  

      调用成功时,会移除signo信号当前的操作,以handler指定的新信号处理程序替代;  

     信号处理函数返回void,因为没有地方给该函数返回。注册自定义信号处理函数,构造Crash后,发出信号并执行自定义信号处理逻辑。

 Xcode Debug运行时,添加断点,在Crash触发前,执行pro hand -p true -s false SIGABRT命令。
 

1、

 #define CALLSTACK_SIG SIGUSR1

 signal(CALLSTACK_SIG, thread_singal_handler);
 

2、

static void thread_singal_handler(int sig)
{
    NSLog(@"main thread catch signal: %d", sig);
    
    if (sig != CALLSTACK_SIG) {
        return;
    }
    
    NSArray* callStack = [NSThread callStackSymbols];
    
    id<PMainThreadWatcherDelegate> del = [PMainThreadWatcher sharedInstance].watchDelegate;
    if (del != nil && [del respondsToSelector:@selector(onMainThreadSlowStackDetected:)]) {
        [del onMainThreadSlowStackDetected:callStack];
    }
    else
    {
        NSLog(@"detect slow call stack on main thread! \n");
        for (NSString* call in callStack) {
            NSLog(@"%@\n", call);
        }
    }
    
    return;
}

问题:Xcode Debug 调试时  thread_singal_handler,不被执行。

解决:参考《Xcode里调试signal的信号回调处理函数》的设置 :

   pro hand -p true -s false SIGUSR1

就可以 执行 相应的回调函数 thread_singal_handler 了。

参考:iOS Mach异常和signal信号 https://www.jianshu.com/p/133fd6f20563

以下是《Xcode里调试signal的信号回调处理函数》中内容 (https://blog.csdn.net/skylin19840101/article/details/52935637

我们在我的代码里添加了异常捕获功能,包括对signal的捕获

但是在Xcode里调试的时候,程序不会进入bugrpt_signalHandler处理函数里面,为什么呢?

因为Xcode屏蔽了signal的回调,我们需要在lldb中输入以下命令,signal的回调就可以进来了

pro hand -p true -s false SIGABRT

注意:SIGABRT可以替换为你需要的任何signal类型,比如SIGSEGV

猜你喜欢

转载自blog.csdn.net/bravegogo/article/details/81168444