美文网首页初见
KSCrash源码学习

KSCrash源码学习

作者: advanceye | 来源:发表于2020-06-18 19:06 被阅读0次

KSCrash监控的crash有以下几种类型:

/** Various aspects of the system that can be monitored:
 * - Mach kernel exception
 * - Fatal signal
 * - Uncaught C++ exception
 * - Uncaught Objective-C NSException
 * - Deadlock on the main thread
 * - User reported custom exception
 */
typedef enum
{
    /* Captures and reports Mach exceptions. */
    KSCrashMonitorTypeMachException      = 0x01,
    
    /* Captures and reports POSIX signals. */
    KSCrashMonitorTypeSignal             = 0x02,
    
    /* Captures and reports C++ exceptions.
     * Note: This will slightly slow down exception processing.
     */
    KSCrashMonitorTypeCPPException       = 0x04,
    
    /* Captures and reports NSExceptions. */
    KSCrashMonitorTypeNSException        = 0x08,
    
    /* Detects and reports a deadlock in the main thread. */
    KSCrashMonitorTypeMainThreadDeadlock = 0x10,
    
    /* Accepts and reports user-generated exceptions. */
    KSCrashMonitorTypeUserReported       = 0x20,
    
    /* Keeps track of and injects system information. */
    KSCrashMonitorTypeSystem             = 0x40,
    
    /* Keeps track of and injects application state. */
    KSCrashMonitorTypeApplicationState   = 0x80,
    
    /* Keeps track of zombies, and injects the last zombie NSException. */
    KSCrashMonitorTypeZombie             = 0x100,
} KSCrashMonitorType;

#define KSCrashMonitorTypeAll              \
(                                          \
    KSCrashMonitorTypeMachException      | \
    KSCrashMonitorTypeSignal             | \
    KSCrashMonitorTypeCPPException       | \
    KSCrashMonitorTypeNSException        | \
    KSCrashMonitorTypeMainThreadDeadlock | \
    KSCrashMonitorTypeUserReported       | \
    KSCrashMonitorTypeSystem             | \
    KSCrashMonitorTypeApplicationState   | \
    KSCrashMonitorTypeZombie               \
)

分别通过不同的KSCrashMonitor类绑定:


image.png

主要hook以下四种crash的绑定方法:

// KSCrashMonitor_Signal.c -- POSIX signals
if(sigaction(fatalSignals[i], &action, &g_previousSignalHandlers[i]) != 0) {
   ...
}

// KSCrashMonitor_MachException.c -- Mach exceptions
    kr = task_get_exception_ports(thisTask,
                                  mask,
                                  g_previousExceptionPorts.masks,
                                  &g_previousExceptionPorts.count,
                                  g_previousExceptionPorts.ports,
                                  g_previousExceptionPorts.behaviors,
                                  g_previousExceptionPorts.flavors);
    ......

    if(g_exceptionPort == MACH_PORT_NULL)
    {
        kr = mach_port_allocate(thisTask,
                                MACH_PORT_RIGHT_RECEIVE,
                                &g_exceptionPort);
        ......
        kr = mach_port_insert_right(thisTask,
                                    g_exceptionPort,
                                    g_exceptionPort,
                                    MACH_MSG_TYPE_MAKE_SEND);
        ......
    }

// KSCrashMonitor_NSException.m -- NSExceptions
NSSetUncaughtExceptionHandler(&handleUncaughtException);

// KSCrashMonitor_CPPException.cpp -- C++
g_originalTerminateHandler = std::set_terminate(CPPExceptionTerminate);

其余几种:
KSCrashMonitorTypeUserReported:用户主动上报;
KSCrashMonitorTypeSystem:系统信息;
KSCrashMonitorTypeMainThreadDeadlock:主线程死锁 (通过开一个子线程,通过sleep(5s)实现每隔5s判断一个标记位是否在主线程被置回,来判断主线程是否无响应,用于判断死锁)

参考资料:

  1. 漫谈iOS Crash收集框架
    https://blog.csdn.net/weixin_36139431/article/details/95358484
    http://www.cocoachina.com/articles/12301
  2. KSCrash源码分析:
    https://www.jianshu.com/p/3ec5f16ac526
    https://programmer.group/kscrash-source-code-analysis.html
  3. 深入解析Mac OS X & iOS 操作系统 学习笔记(十二)
    https://www.jianshu.com/p/cc655bfdac13

相关文章

网友评论

    本文标题:KSCrash源码学习

    本文链接:https://www.haomeiwen.com/subject/vcqpxktx.html