iOS exception capture

Source: Internet
Author: User
Tags terminates

iOS exception capture

By to Chen Yu

Posted on 2015-08-29

Article Directory
  1. 1. Preface
  2. 2. I. System Crash
  3. 3. two. Handling Signal
    1. 3.1. here are some signal descriptions
    2. 3.2. Key points Note
  4. 4. three. Actual Combat
  5. 5. four. Crash callstack analysis-In one step analysis
  6. 6. Five. Demo Address
  7. 7. Six. References
Objective

Today, in the iOS Advanced group 237305299, a friend asked about the iOS exception capture problem, which has not been studied before, taking this opportunity to study. and wrote a demo, if necessary, can be downloaded at the bottom of the article.

Before reading the article, it is recommended that you read this article after reading a ramble about iOS crash Collection framework, to understand the principle.

Developing iOS apps, solving crash problems is always a challenge. Crash is divided into two types, one of which is caused by exc_bad_access, because it accesses memory addresses that are not part of the process, it is possible to access memory that has been freed, and the other is an uncaught objective-c exception (nsexception). Causes the program to send itself a SIGABRT signal and crashes. In fact, for the uncaught objective-c exception, we have a way to record it down, if the log is properly recorded, can solve most of the crash problem. Here, the UI thread and the background thread are explained separately

I. System crash

For the system crash, the program exception exits can be captured by the Uncaughtexceptionhandler mechanism, that is, the content outside the catch in the program is captured by the system's own error handling. All we have to do is replace the Exceptionhandler with a custom function.

Two. Handling signal

Exception handling using OBJECTIVE-C can not get signal, if we want to deal with it, we also use the UNIX standard signal mechanism, register SIGABRT, Sigbus, SIGSEGV and other signals occur when the processing function. In this function we can output the stack information, the version information and so on everything else we want.

Here are some signal descriptions

1)SIGHUP
This signal is issued at the end of the user terminal connection (normal or abnormal), usually at the end of the control process of the terminal to notify the same session of the various jobs, when they are no longer associated with the control terminal.
When you log in to Linux, the system is assigned to a terminal (Session) of the logged-on user. All programs running at this terminal, including the foreground process group and the background process group, usually belong to this Session. When the user exits the Linux login, the foreground process group and the background will receive a sighup signal to the terminal output process. The default action for this signal is to terminate the process, so the foreground process group and the process that has terminal output in the background will be aborted. However, this signal can be captured, such as wget can capture the sighup signal, and ignore it, so even if you quit the Linux login, wget can continue to download.
In addition, for daemons with terminal disengagement, this signal is used to notify it to reread the configuration file.
2)SIGINT
A program termination (interrupt) signal, issued when the user types the intr character (usually ctrl-c), to inform the foreground process group that the process is terminated.
3)Sigquit
Similar to SIGINT, but controlled by the quit character (usually ctrl). The process generates a core file when it receives a sigquit exit, similar to a program error signal in this sense.
4)Sigill
An illegal instruction was executed. This is usually due to an error in the executable file itself or an attempt to execute a data segment. This signal can also be generated when a stack overflows.
5)SIGTRAP
Generated by a breakpoint instruction or other trap instruction. Used by debugger.
6)SIGABRT
Invokes the signal generated by the abort function.
7)Sigbus
Illegal address, including memory address alignment (alignment) error. For example, a four-word integer is accessed, but its address is not a multiple of 4. It differs from SIGSEGV in that the latter is triggered by illegal access to a legitimate storage address (such as Access does not belong to its own storage space or read-only storage space).
8)SIGFPE
Emitted when a fatal arithmetic operation error occurs. This includes not only floating-point arithmetic errors, but also all other arithmetic errors such as overflow and divisor 0.
9)SIGKILL
Used to immediately end the run of the program. This signal cannot be blocked, processed, or ignored. If an administrator discovers that a process is not terminating, try sending this signal.
10)SIGUSR1
Leave it to the user
11)SIGSEGV
An attempt was made to access memory that is not assigned to itself or to write data to a memory address that does not have write permissions.
12)SIGUSR2
Leave it to the user
13)Sigpipe
Pipe rupture. This signal is usually generated between interprocess communication, such as two processes with FIFO (pipeline) communication, the read pipeline is not opened or terminated unexpectedly to the pipeline to write, the writing process will receive sigpipe signal. In addition, two processes with the socket communication, the write process when the socket is written, the read process has been terminated.
14)SIGALRM
A clock timing signal that calculates the actual time or clock time. The alarm function uses this signal.
15)SIGTERM
The end of program (terminate) signal, unlike Sigkill, is that the signal can be blocked and processed. Typically used to require the program to exit gracefully, the shell command kill generates this signal by default. If the process does not stop, we will try to Sigkill.
17)SIGCHLD
The parent process receives this signal at the end of the child process.
If the parent process does not process the signal and does not wait for the (wait) child process, although the child process terminates, it also occupies the table entry in the kernel process table, when the child process is called a zombie process. In this case we should avoid (the parent process either ignores the sigchild signal, or catches it, or wait for its derived child process, or the parent process terminates first, and the termination of the child process is automatically taken over by the Init process).
18)Sigcont
Let a stop (stopped) process continue execution. This signal cannot be blocked. You can use a handler to get the program to do certain work when the stopped state changes to continue. For example, to re-display the prompt
19)SIGSTOP
Stop (stopped) the execution of the process. Notice the difference between it and terminate and interrupt: The process is not over yet, just pause execution. This signal cannot be blocked, processed or ignored.
20)SIGTSTP
Stops the process from running, but the signal can be processed and ignored. This signal is emitted when the user types Susp characters (usually ctrl-z)
21)Sigttin
When a background job wants to read data from the user terminal, all processes in the job receive a sigttin signal. By default, these processes stop executing.
22)Sigttou
Similar to Sigttin, but received when writing a terminal (or modifying terminal mode).
23)Sigurg
When there is "emergency" data or Out-of-band data arrives at the socket.
24)sigxcpu
CPU time resource limit exceeded. This restriction can be read/changed by Getrlimit/setrlimit.
25)Sigxfsz
When the process attempts to enlarge the file so that it exceeds the file size resource limit.
26)SIGVTALRM
Virtual clock signal. is similar to SIGALRM, but calculates the CPU time that is consumed by the process.
27)sigprof
Similar to SIGALRM/SIGVTALRM, but includes the CPU time used by the process and the time of the system call.
28)Sigwinch
emitted when the window size changes.
29)SIGIO
The file descriptor is ready to start the input/output operation.
60LSIGPWR
Power failure
31)Sigsys
An illegal system call.

Key points Note
    • In the signals listed above, the program can not capture, block or ignore the signal is:sigkill,sigstop
    • Signals that cannot be restored to the default action are: Sigill,sigtrap
    • The default causes the process to abort the signal is: Sigabrt,sigbus,sigfpe,sigill,sigiot,sigquit,sigsegv,sigtrap,sigxcpu,sigxfsz
      The signal that causes the process to exit by default is:
    • Sigalrm,sighup,sigint,sigkill,sigpipe,sigpoll,sigprof,sigsys,sigterm,sigusr1,sigusr2,sigvtalrm
    • The signal that causes the process to stop by default is: Sigstop,sigtstp,sigttin,sigttou
    • The default process ignores signals such as: Sigchld,sigpwr,sigurg,sigwinch
    • In addition, Sigio in SVR4 is exited, is ignored in 4.3BSD, Sigcont is continued when the process hangs, otherwise it is ignored and cannot be blocked.
Three. Actual combat

1. AppDelegate.m in

1
2
3
4
5
6
7
8
-(BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions {
Override point for customization after application launch.

Installsignalhandler (); //Semaphore truncation
Installuncaughtexceptionhandler (); //System exception capture

YES;
}

2. SignalHandler.m the implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
void Signalexceptionhandler (int signal)
{
nsmutablestring *MSTR = [[Nsmutablestring alloc] init];
[MSTR appendString:@ "stack:\n"];
void* callstack[128];
int I, frames = BackTrace (callstack,128);
char** STRs = Backtrace_symbols (callstack, frames);
for (i =0; I <frames; ++i) {
[MSTR AppendFormat:@ "%s\n", Strs[i]];
}
[Signalhandler Savecreash:mstr];

}

void Installsignalhandler (void)
{
signal (SIGHUP, Signalexceptionhandler);
signal (SIGINT, signalexceptionhandler);
signal (sigquit, Signalexceptionhandler);
signal (SIGABRT, signalexceptionhandler);
signal (Sigill, Signalexceptionhandler);
signal (SIGSEGV, signalexceptionhandler);
signal (SIGFPE, Signalexceptionhandler);
signal (Sigbus, signalexceptionhandler);
signal (sigpipe, Signalexceptionhandler);
}
/span>

The error type can be seen in the above description, Signalexceptionhandler is the callback when the signal is wrong. This method can be recalled when there is a signal error.

3. UncaughtExceptionHandler.m the implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void HandleException (NSException *exception)
{
Stack information for exceptions
Nsarray *stackarray = [exception callstacksymbols];
Why the exception occurred
NSString *reason = [exception reason];
//exception name
nsstring *name = [ Exception name];
nsstring *exceptioninfo = [nsstring Stringwithformat:@ "Exception reason:%@\nexception name:%@\nexception stack:%@", Name, Reason, Stackarray];
nslog (@ "%@", exceptioninfo);
[Uncaughtexceptionhandler Savecreash:exceptioninfo];
void Installuncaughtexceptionhandler (void)
{
Span class= "built_in" >nssetuncaughtexceptionhandler (&handleexception);
}
/span>

4. Test – The key to stepping on the pit
The most critical step here, Signalhandler do not test in the debug environment. Because the system debug will take precedence to intercept. After we have to run it once, turn off the debug state. You should click on our build app to run it directly on the simulator. While the Uncaughtexceptionhandler can be captured in the debug state

1
2
3
4
5
6
7
8
9
10
11
12
- (ibaction) ButtonClick: (uibutton *) Sender {
//1. Semaphore
1,2};
//cause SIGABRT error because there is no space in memory, where free is the object in the stack
Ptest->a = 5;
-(ibaction) buttonocexception: (uibutton *) sender
{
//2.ios crash
Span class= "line" > nsarray *array= @[@ "Tom", @ "xxx ", @" ooo "];
[array Objectatindex:5];

/span>

"File Structure"
Operation

Four. Crash callstack analysis-in-step analysis
Properties Description
0x8badf00d In the start-up, end-of-stop, or respond to system events spent a long time, meaning "ate bad food."
0xdeadfa11 The user is forced to quit, meaning "dead fall". (When the system is unresponsive, the user presses the power switch and home)
0xbaaaaaad The user presses the home key and the volume key to get the current memory status, which does not represent a crash
0xbad22222 VoIP should be used because the recovery is too frequent cause crash
0xc00010ff Because it was too hot to be killed, meaning "cool off"
0xdead10cc Because in the background still occupy system resources (, such as Communication Records) was killed, meaning "dead lock"
Five. Demo Address

Ioscrashuncaught Download

Six. References

1. Debugging skills after the program crash
2.iOS Development Socket Program is SIGPIPE signal terminate problem
3. Beauty, Miss.
4. How to locate obj-c field pointer random Crash (a): increase the crash rate of the wild hands first
5. How to locate obj-c field pointer random Crash (ii): Make the non-mandatory crash into a must
6. How to locate obj-c field pointer random Crash (iii): Add Black technology let crash tell

iOS exception capture

Related Article

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.