iOS crash pre-logging implementation

Source: Internet
Author: User

How to use signal

Because the error is thrown signal, it must be done specifically for signal processing.

in computer science, Signal ( English:signals) is a restricted way of interprocess communication in Unix, Unix-like, and other POSIX-compliant operating systems. It is an asynchronous notification mechanism used to alert a process that an event has occurred. When a signal is sent to a process, the operating system interrupts the normal process of control, at which point any non-atomic operations are interrupted. If a process defines a handler for a signal, it is executed, otherwise the default handler function is executed.

In Project engineering, you use the

#include <sys/signal.h>

A large number of system signal identifiers are defined in the Sys/signal file, and the signal processing functions can be set by the signal () system. If the corresponding processing function is not set for a signal, the default handler function is used, otherwise the signal is intercepted by the process and the corresponding handler function is called. In the absence of a handler function, the program can specify two behaviors: ignore the signal sig_ign or use the default handler function SIG_DFL.

However, there are two signals that cannot be intercepted and processed: SIGKILL, SIGSTOP.

// defines a callback function that receives a signal void HandleException (int  signo) {printf ("Lanou's sig is:%d", Signo);} // registers the callback function of the alerm signal signal (SIGALRM, handleexception);
types of signal signals

    • sigabrt--Program abort Command abort signal
    • sigalrm--Program Timeout signal
    • sigfpe--program floating point anomaly signal
    • sigill--Program Illegal Command signal
    • sighup--Program Terminal Stop signal
    • sigint--Program Keyboard Interrupt Signal
    • sigkill--program end receive abort signal
    • sigterm--Program Kill stop Signal
    • sigstop--Program Keyboard Abort signal
    • sigsegv--Program Invalid memory abort signal
    • sigbus--Program Memory byte misaligned abort signal
    • sigpipe--program socket send failed abort signal
How signal signals are used in IOS development

Create a Signalhandler static class

#import <Foundation/Foundation.h><sys/signal.h> @interface signalhandler:nsobject // The static method registers the signal processing function + (void) registersignalhandler; @end

signalhandler.mm

#import"SignalHandler.h"#import<UIKit/UIKit.h>#include<libkern/OSAtomic.h>#include<execinfo.h>//number of exceptions currently handledvolatileint32_t Uncaughtexceptioncount =0;//maximum number of exceptions that can be handledvolatileint32_t Uncaughtexceptionmaximum =Ten; //callback function after capturing the signalvoidHandleException (intSigno) {int32_t Exceptioncount= OSAtomicIncrement32 (&uncaughtexceptioncount); //do not handle more than 10 exceptionsif(Exceptioncount >uncaughtexceptionmaximum) {        return; } nsmutabledictionary*userinfo =[nsmutabledictionary dictionarywithobject:[nsnumber Numberwithint:signo] ForKey:@"Signal"]; //Create an OC exception objectNSException *ex = [NSException exceptionwithname:@"Signalexceptionname"Reason:[nsstring stringWithFormat:@"Signal%d was raised.\n", Signo] userinfo:userinfo]; //get the singleton Signalhandler and handle the ex exception in the blocking main thread, because this processing design UI[[Signalhandler Instance] Performselectoronmainthread: @selector (handleexception:) Withobject:ex Waituntildone: YES];} @implementation Signalhandler{bool isdismissed=NO;StaticSignalhandler *s_signalhandler =Nil;}+(instancetype) instance{Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{        if(S_signalhandler = =Nil) {S_signalhandler=[[Signalhandler alloc] init];    }    }); returnS_signalhandler;} + (void) Registersignalhandler {//program Abort signal that occurs due to abort () function call by the registry programsignal (SIGABRT, handleexception); //The program that the registration program is generating due to illegal instruction stops the signalsignal (Sigill, handleexception); //program abort signal due to invalid memory reference by registry programsignal (SIGSEGV, handleexception); //program abort signal due to floating-point exception in registration programsignal (SIGFPE, handleexception); //program abort signal due to misalignment of memory address by registry programsignal (Sigbus, handleexception); //Program abort signal caused by failure to send message via portsignal (sigpipe, handleexception);}//the method used to handle exceptions, where records and processing crashes- (void) HandleException: (NSException *) Exception {Cfrunloopref Runloop=cfrunloopgetcurrent (); Cfarrayref Allmodes=cfrunloopcopyallmodes (Runloop); Uialertview*alertview = [[Uialertview alloc] Initwithtitle:exception.descriptionMessage@"Click the button to end the program" Delegate: Self Cancelbuttontitle:@"Cancel"Otherbuttontitles:nil];     [Alertview show]; //when receiving an exception handling message, let the program start Runloop to prevent the program from dying until isdimissed = YES while(!isdismissed) {         for(NSString *modeinch(Nsarray *) {allmodes) {Cfrunloopruninmode (cfstringref) mode,0.001,false); }    }    //Restore the default processing function for each signal SIG_DFLcfrelease (allmodes);    Nssetuncaughtexceptionhandler (NULL);    Signal (SIGABRT, SIG_DFL);    Signal (Sigill, SIG_DFL);    Signal (SIGSEGV, SIG_DFL);    Signal (SIGFPE, SIG_DFL);    Signal (Sigbus, SIG_DFL); Signal (Sigpipe, SIG_DFL);} - (void) Alertview: (Uialertview *) Analertview Clickedbuttonatindex: (Nsinteger) anindex {isdismissed=YES;} @end

iOS crash pre-logging implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.