iOS crash debugging usage and tips Summary

Source: Internet
Author: User

The program often crashes during the iOS development and debugging process and on-line. Simple crash It's okay to say that complex crashes need to be analyzed by parsing the crash file, parsing the crash file is more common in iOS development.

There are many blogs on the web that parse the crash information, but most of the quality is uneven, or some details are not noticed. Write a blog today to summarize my use of crash debugging and skills, if there are errors or omissions, please also point, thank you!

Get crash information

There are many ways to get crash information in iOS, it is more common to make the third-party analysis tools such as UF, Baidu, or collect crash information and upload the company server. Here are some of our common crash Analysis methods:

    • Use the Friends League, Baidu and other third-party crash statistics tools.

    • Implement in-app crash collection yourself and upload the server.

    • View the crash information for a device directly in the xcode-devices.

    • Use the crash Crash collection service provided by Apple.

Collect crash information

Apple provides us with exception-handling classes, nsexception classes. This class can create an exception object, or it can get an exception object from this class.

What we use most in this class is a C function that gets the crash information, which we can use to collect the exception when the program has an exception.

12345678910111213 // 将系统提供的获取崩溃信息函数写在这个方法中,以保证在程序开始运行就具有获取崩溃信息的功能  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     // 将下面C函数的函数地址当做参数     NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);     returnYES;  }  // 设置一个C函数,用来接收崩溃信息  void UncaughtExceptionHandler(NSException *exception){      // 可以通过exception对象获取一些崩溃信息,我们就是通过这些崩溃信息来进行解析的,例如下面的symbols数组就是我们的崩溃堆栈。      NSArray *symbols = [exception callStackSymbols];      NSString *reason = [exception reason];      NSString *name = [exception name];  }

We can also get the function pointers for the crash statistics in the following ways:

1   NSUncaughtExceptionHandler *handler = NSGetUncaughtExceptionHandler();

DSYM symbol Set

For crash analysis, the first thing to understand is a concept, the symbol set.

    • The symbol set is a file that we have packaged with an IPA file, and the. app file has a suffix named. dsym, and this file must be packaged with Xcode.

    • Each. dsym file has a UUID that corresponds to the UUID in the. app file, which represents an application. Each crash message in the. dsym file also has a separate UUID that is used to proofread the program's UUID.

    • The crash information we get without using the. dsym file is inaccurate.

    • The symbolic set stores information about the file name, method name, and line number, corresponding to the 16-function address of the executable file, by parsing the crash. Crash files can be accurately known for specific crash information.

Each time we archive a package, a dSYM file will be generated with it. Each time we publish a version, we need to back up this file to facilitate later debugging. When you symbolize the crash information, you must use the dSYM file generated by the current app's packaged computer, and other computer-generated files may cause problems with inaccurate parsing.

Archive

When the program crashes, we can get to the crash stack, but the error stack is a 16-based address at the beginning of 0x. We need to use the Symbolicatecrash tool from Xcode to symbolize the. Crash and. dsym files to get detailed information about the crash.

Crash Analysis

Command line parsing crash file

Parsing a crash file with a command-line tool that comes with your Mac requires three files

    • Symbolicatecrash,xcode comes with a crash analysis tool that allows you to locate the location of the crash more precisely, replacing the address at the beginning of the 0x with the code of the response and the exact number of lines.

    • The dSYM file produced when we packaged.

    • The crash file that is generated when the crash occurs.

When I parse the crash information, I first set up a crash folder on the desktop and then I will. Crash,. DSYM, Symbolicatecrash are placed in this folder, so enter this folder, a direct line of the command is resolved.

Symbolicatecrash we can be found below the path, I use Xcode7, other versions of the Xcode path is different, please Google yourself.

1 /Applications/Xcode.app/Contents/SharedFrameworks/DTDeviceKitBase.framework/Versions/A/Resources/symbolicatecrash

Then in Window->organizer->archives, select the archive version right-click and select the show in Finder to get the dSYM file.

dSYM file

Place the. Crash,. DSYM, Symbolicatecrash three files in the Crash folder that we created on the desktop.

Crash folder

Open the command line tool and go to the crash folder

cd/users/username/desktop/Crash folder

Parsing crash files with commands

./symbolicatecrash./*.crash./*.app.dsym > Symbol.crash

If the above command is unsuccessful, use the command to check the environment variables

Xcode-select-print-path

return Result:

/applications/xcode.app/contents/developer/

If it is not the result above, you need to use the following command to set the exported environment variables, and then repeat the above resolved operation.

Export Developer_dir=/applications/xcode.app/contents/developer

When the parsing is complete, a new one is generated. Crash file, this file is the crash details. The red part of the figure is the part where our code crashes.

Parse the completed results

Note that there will be no crash information generated in the following situations:

    • Memory access error (not wild pointer error)

    • Low memory, when the program memory use too much can cause low system memory problems, the system will recycle the program memory

    • Triggering a watchdog mechanism for some reason

View device crash information through Xcode

In addition to the above system analysis tools for analysis, if we are directly using the phone connection crashes or crashes after connecting the phone, choose window-> devices----Choose your own phone---view device logs can view our crash information.

View Device Logs

As long as the application on the phone is the computer installed packaging, such a crash information system has been symbolized for us, we just have to go in and wait for a while (do not believe that the progress of the refresh, not accurate), if still not signed, we select the file, Then right-click to select Re-sysbomlicate.

If you are using another computer for packaging, we can export the crash file here, and we will parse it by the command line.

Using the third-party crash analysis tool

Now there are a lot of third-party tools can do crash statistics analysis, using more is the Friend League crash statistics, AU crash statistics are integrated in the Friends League SDK, the specific use of the official documents directly to see the best method, the following list of friends of the crash statistics document address.

League of Friends collapse Statistics Official document

I do not recommend this third party, but recommend a better third-party-BUGHD. The biggest difference between this third party and the Friend Alliance is that it can be analyzed in combination with dSYM analysis, displayed on Web pages, and can count crashes, crash devices, system versions, and so on.

Here are some of the crashes that my company uses to BUGHD statistics

Bughd

The BUGHD server has helped us to use dSYM to complete the crash symbolization. We can view the detailed crash stack by clicking on a crash, as well as the crash device distribution and system distribution.

Detailed distribution

Apple comes with crash statistics tool

Apple has integrated the crash stats feature in Xcode for us, and in window->organizer->crashes we can see

Crashes

Apple's own crash statistics tool is not recommended, and if you want to use this feature, you'll need to set it up in your iphone.

Diagnostics and usage data--------diagnostics and usage, privacy

Select Auto-send and share with developers

Malicious coverage of third-party tools

The crash collection statistic function should only be called once, and it is best to use a third party if you are using a third party, so that the way we get the crash stats is unique.

Third-party statistical tools do not use as much as possible, using multiple crashes to collect third parties can cause malicious overwriting of nssetuncaughtexceptionhandler () function pointers, causing some third parties to not receive crash information.

Many third-party crashes now collect tools to ensure that they are able to collect the best possible information about the crash, which would be a malicious overwrite of the Nssetuncaughtexceptionhandler () function pointer. Because this function passes the function address as a parameter, it is overwritten as long as the call is repeated so that the stability of the crash collection is not guaranteed.

When we parse the crash information, we see that the crash stack only crashes in the main.m file, and it is possible to determine that the crash is not caused by a bug in the main.m file, it is almost certain that the Nssetuncaughtexceptionhandler () function pointer is maliciously overwritten.

A maliciously overwritten crash stack

Related: 1.symbolicatecrash where in the XOCDE compile the app will also generate a dsym as the suffix of the file, this file will record the app's crash log, need to be viewed through Symbolicatecrash, But the tool changed the location of the store when it xccode4.3.  1. Make a patch for Xcode: command line run  /usr/bin/xcode-select-print-path  if output "/developer" or other non-"/applications/ Xcode.app/contents/developer/", run the following command: sudo/usr/bin/xcode-select-switch/applications/xcode.app/contents/ Developer/ 2. Find Symbolicatecrash:find/applications/xcode.app-name symbolicatecrash-type f get path, Mine is applications/xcode.app/contents/developer/platforms/iphoneos.platform/developer/library/privateframeworks/. dtdevicekit.framework/versions/a/resources/symbolicatecrash  switches to his directory before executing Symbolicatecrash:cd  applications/xcode.app/contents/developer/platforms/iphoneos.platform/developer/library/privateframeworks/ dtdevicekit.framework/versions/a/resources/  4.3 before 1. Direct Find/developer-name Symbolicatecrash-type F  2. Switch to the directory, after:./symbolicatecrash/somepath/mycrashlogfile.crash/somepath/myappname.app.dsym

Link:

iOS crash debugging usage and tips Summary

Where's Symbolicatecrash?

iOS crash debugging usage and tips Summary

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.