How to make lldb ignore EXC_BAD_ACCESS exception?

前端 未结 4 1425
粉色の甜心
粉色の甜心 2021-02-04 15:41

I am writing a program on Mac OSX depending on the sigaction/sa_handler mechanism. Run a code snippet from user and get ready to catch signals/exceptions at any time. The progra

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 16:39

    We can do it easily. Just add this code.

    #include 
    #include 
    #include 
    
    int ret = task_set_exception_ports(
                                       mach_task_self(),
                                       EXC_MASK_BAD_ACCESS,
                                       MACH_PORT_NULL,//m_exception_port,
                                       EXCEPTION_DEFAULT,
                                       0);
    

    Don't forget to do this

    proc hand -p true -s false SIGSEGV 
    proc hand -p true -s false SIGBUS
    

    Full code:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    
    static void handler(int signo, siginfo_t *sigaction, void *context)
    {
        printf("in handler.\n");
        signal(signo, SIG_DFL);
    }
    
    static void gen_exception()
    {
        printf("gen_exception in.\n");
        *(int *)0 = 0;
        printf("gen_exception out.\n");
    }
    
    void *gen_exception_thread(void *parg)
    {
        gen_exception();
        return 0;
    }
    
    int main()
    {
        task_set_exception_ports(
                                 mach_task_self(),
                                 EXC_MASK_BAD_ACCESS,
                                 MACH_PORT_NULL,//m_exception_port,
                                 EXCEPTION_DEFAULT,
                                 0);
        
        
        struct sigaction sa;
        sa.sa_sigaction = handler;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = SA_SIGINFO;
    
        if(sigaction(/*SIGBUS*/SIGSEGV, &sa, NULL) == -1) {
            printf("sigaction fails.\n");
            return 0;
        }
    
        pthread_t id;
        pthread_create(&id, NULL, gen_exception_thread, NULL);
        pthread_join(id, NULL);
    
        return 0;
    }
    

    Refer to (Chinese article): https://zhuanlan.zhihu.com/p/33542591

提交回复
热议问题