iOS development process commonly used in crash


introduction

iOS development, we encounter a situation abnormal exit the program, if it is in the process of debugging, you may print or critical information by way of breakpoint debugging facilities, but for some non-complex module will now collapse anomalies, such way sometimes difficult to locate the problem, but also for applications has been published on-line, in this way it is powerless.

Usually we see Crash divided into two types, one is the system memory error, triggering EXC _ BAD _ ACCESS caused the program is running in accessing the wrong memory address, and the other is the emergence of abnormal signal can not be processed , causing the program to send itself to the SIGABRT signal collapse, here is what I usually use more crash approach, to share with you.


crash caused by memory errors: Part 1

➸ 1.1 Causes

  1. Access memory address does not belong in this process.
  2. Access has been freed memory (repeat release already freed memory).

iOS development to inherit from NSObject, memory management using reference counting mechanism for non-NSObject object reference counting mechanism does not work, we need to manage the use of memory recovery. Reference counting principle is that when an object is held (The retain), its reference count (retainCount) + 1'd, labeled once released (release), retainCount -1 once, when retainCount is 0, the object is released.

When using manual reference count (MRC) development, developers need to explicitly call retain or release. Apple's implementation after iOS5 Automatic Reference Counting (ARC), the developer does not have to explicitly call retain or release, and automatically added by the compiler, in a convenient developers, but also reduces the difficulty of developing, while memory problems when harder to locate the error.


Common treatment methods ➸ 1.2

Xcode 1.2.1 Adding global exception breakpoint

① The navigator view switch to the breakpoint navigator view at:


② bottom left, click the + sign, select Exception Breakpoint this option.


exception breakpoint can edit many functions, such as executing the script, the output log, select only the abnormal processing objective-c, etc., function is very rich.

The next time we run the program crashes, the program will automatically stop at the collapse of the code, to help us find the problem.


Object Debugger 1.2.2 zombies

The global exception breakpoint will normally locate the cause of the crash specific tag. However, if not in the current call stack collapse, the system can only tell us the crash address, but can not locate the specific code, so that we would not be able to modify the error. This is similar to the following:

 

In this case we can debug (Zombie Objects) provided by Xcode zombie objects to try to find the problem.

first or open Xcode upper left corner of the screen to select Xcode-> Preferencese , the behavior tab, set about output, when debugging output for more information, the following screenshot, the hook:


② menu Product> Scheme> Edit Scheme in the red circle inside the three options are on the hook:


③ After turning on the option, at run time, if access to the object has been released , it will give more accurate positioning information that can help identify the problem.

The principle of this feature is released (retainCount 0), using a built-in objects Zombie replace the original objects in the object to be released. No matter what the message is sent (function call) to the object, it will trigger an exception thrown debugging information.

Note: Remember the problem is repaired, turn off this feature! It will cause the program memory footprint abnormal.


④ can also be through the system terminal prints out using a terminal call information mallochistory commands, such as "mallochistory 30495 0x60005ef76fd0", where 30495 is the process pid, pid can be viewed according to Xcode console log, or obtained through the Activity Monitor, according to this record can roughly determine the position of the error code.

The following tips will be similar to the code, according to some key information, you can identify the specific location of the error.



1.2.3 use NSSetUncaughtExceptionHandler treatment

Before the two methods can be said for the APP online powerless, okay, iOS provides processing API exception occurs, NSSetUncaughtExceptionHandler, we can add this Handler when the program starts, the abnormal occurrence of such a program when he It can make the necessary information to this part of the process, timely feedback to the developers. Note that, by using NSSetUncaughtExceptionHandler crash can be used to treat abnormal, the system will crash report provided by the global exception handler NSSetUncaughtExceptionHandler method. If the custom NSSetUncaughtExceptionHandler monitor events will lead to a third-party monitor (such as Bugly) failure, it has been integrated platform for third-party monitor small partners need to pay attention.

Register Globals handler to handle exceptions , the program starts or other entry registration:


② When the online program appears crash, the code will be executed to handle previously registered in , an error message will be stored locally.


③ saved through the online app dSYM symbol table lookup problem .

Construction iOS symbol table generated when it is the memory address of the function name, file name, line number mapping table. Symbol table elements are as follows:

When an application crash, we can use the stack information obtained during the crash stack information corresponding to the source code, you can still see the error code number in the line, we are able to quickly locate the error code locations to quickly solve the problem.

Get to the error log dSYM symbol table and before the program crashes, we can locate the problem.


④ use atos command to locate the problem.

atos command to load a particular module symbolic address atos [the -arch schema name] [-o Symbol Table] [-l module address] [Method Address]

Calculated using the terminal, the first to get the hex address range.

Terminal code execution:

In this way, you can locate a problem code.



Part 2: Mach-induced abnormal signal and crash signal

➸ 2.1 Mach and signal

Mach is a microkernel core Mac OS and iOS operating systems, Mach abnormality is the lowest level of the kernel-level exception, so when an abnormal APP, the first to be able to listen to an exception is Mach.

The first captured Mach exception all exceptions will next converted to the corresponding Unix signals, and delivered to the thread error. After which you can register the type of signal you want to listen, to capture the signal. Exception handling is using Objective-C can not get the signal, if you want to deal with it, we have to use the standard unix signal mechanism, the registered handler when SIGABRT, SIGBUS, SIGSEGV signal generation and so on. The function we can output a stack information, and other information versions of everything we want. Follows, that the monitor signal SIGSEGV, SIGSEGV signal is generated when there is, it will callback method mySignalHandler: Signal (SIGSEGV, mySignalHandler).


➸ 2.2 signal Signal Descriptions

The default method of signal processing a total of five, respectively Terminate (terminate process, namely knot East Process), Ignore (ignore the signal), Dump (terminate process and dump core: the end of the process and generates core dump, the memory information process print it out), Stop (pause running processes, used for debugging) and Cont (resume the suspended process before running a multi-for debugging) to represent.

Signal Signal Type:

Signal Name

The default processing

Explanation

SIGABRT

Dump

Program termination command

SIGALRM

Terminate

Timeout signal

SEAL

Dump

Program illegal command signal

Sigःuf

Terminate

Program abort signal terminal

SIGINT

Terminate

Programs keyboard interrupt signal

SIGKILL

Terminate

Program forced end signal

SIGTERM

Terminate

Program termination signal

SIGSTOP

Stop

Keyboard program abort signal

SIGSEGV

Dump

Program invalid memory abort signal

SIGBUS

Dump

Program memory unaligned byte abort signal

SIGPIPE

Terminate

Socket program failed to send stop signal

If there is no handler set corresponding to a signal, it will use the default handler, otherwise the signal was intercepted and call the appropriate handler process. In the absence of the handler, the program can specify two behaviors: ignore the signal SIG_IGN or use the default handler SIG_DFL. But there are two signals can not be intercepted and handled: SIGKILL, SIGSTOP.


➸ 2.3 signal Signal Processing

registered global handler handle the exception signal is started in the program or other entry registration.

About the type of error can be seen above description, SignalExceptionHandler is an error signal when the callback. When a signal is wrong, you can call back to this method.


SignalHandler Do not test in debug environment . Because debug the system will give priority to intercept. I run once in the simulator, turn off the debug state, then click on our build up the app directly on the emulator to run. Logs obtained as follows:


Part 3: summary

For applications crash, there are many excellent platform for third-party (League of Friends, bugly, etc.) and provide the log function RBI, has been able to meet the daily needs of the development, but learning these common crash was able to help us understand the iOS operating mechanism, these are development Some crash often see, the actual process may be complicated, need to use a variety of ways to locate the problem and flexible to use.




Guess you like

Origin blog.csdn.net/weixin_34408717/article/details/91397292