Learn how to debug program bugs

Preface:---------- >Learn how to debug program bugs<----------
  • 1. How to debug the program in development ------------------ (Please see ==》Step 1 to Step 5)
  • 2. Which debugging tools have you used --------------------- Tencent's bugly
  • 3. After the program is online, how to collect error information--------- (Please see ==》Step 6)
  • 4. bugly demo --------------------------------- (please see ==》Step 6)
  • 5. NSLog debugging -------------------------- Please see the summary-NSLog debugging
case study:

step one

  1. Create a project called "Debug-01-crash".
  2. Suppose, we write a piece of wrong code in this project. as follows:


    Error code example


    Step 2:
    cmd + R, run it, the following error will be reported, as shown in the figure below: ( insert an empty object at the index position of 1 )


    Error resolution


    Step 3
    At this time, we will think where we made the mistake, where we inserted the empty object, and have the urge to go through bugs everywhere. But at this moment we assume that we will have hundreds of files, how do we find them?
    ------------- Using the call stack method -------------

    先执行的在下面,后执行的在上面。

    Call stack


    Detailed explanation of the call stack
    First of all, let’s take a look at what information the call stack brings us. For example, as shown in the figure above,

    ① 程序启动的时候调用       -[UIWindow makeKeyAndVisible]
      ====>(在往上看) 
    ② viewController调用     - [viewController viewDidLoad]
                            - [viewController demoCrash]
    
    ③ 然后看下面这个,在一个数组中试图插入一个对象
                            + [NSArray arrayWithObjects:count:]
    ④ 在看到这个              -[__NSPlaceholderarray initwithObjects:count:]  
    ⑤ 在往上看就崩溃了
     这时,我们就知道了,在 #demoCrash# 方法中出现了上面所描述的错误。
    ⑥ 分析完毕

    Step 4
    Find the error, and the positioning method is shown in the figure below:


    Find a way

    Positioning method


    Step 5
    What happens if the project is run on a real machine?

    ** 项目会提示错误信息,调用堆栈不会显示。**

    Real machine running-display results

How to collect error information after the program is online

Step 6
At this time, we are thinking about a problem. Any programmer may make mistakes when working on the project. For example, when the program is on the shelf, it is very likely that some mistakes have not been corrected, or during testing. Nothing was found, so it was repeated. At this time, when the user runs, a crash will occur.
===》Then we have a problem. After the program is online, how do we collect error information?
For this, we usually use a third-party framework, here we will use the bugly of QQ for demonstration.


bugly demo


Step 7 Let's scan with QQ and log in. Will enter the following interface.
Follow the steps below as shown in the figure:





After registration, we will be asked to download the SDK and view product information.
As follows:



Download SDK

Click to enter this interface

See the user guide Pod integration

pod integration


After pod integration ---> Initialize SDK


Initialize SDK


Then, we find the APPid


1

2

3


The appid fill to the project, as follows:


Code example


At this time, basically complete, let's run the program. You will see the picture below, and there is no error in it. After refreshing it, the crash log cannot be seen.



Reason: Normally our program runs and crashes, it will stay as shown in the picture below. It is a breakpoint. If the breakpoint stays here, the code behind the program will not be executed. If it is not executed, the error message will not be submitted.



We install the program on the real machine and run it, there will be a crash.
After refreshing, the interface on the picture will appear below, find the log, as shown below:



After clicking the error log, the following picture will appear:



Focus on here:



Through the above picture, we will find a detail?
After we imported Tencent's third-party framework, even if our program is on the shelves, we can know where the application will crash.

summary

1. Write in the first sentence


detail


2. Good words


Jin Yuliang
NSLog debugging

Step 1
Create a project named "NSLog" and create a PCH file. As shown below:


PCH creation


Cmd + B Compile it.
Suppose, we have used this program for two years, and we have been debugging with NSLog, and suddenly thought about it and replaced NSLog. Then we consider using PCH. As shown below:


1

2

3

4

5


main: If we want to make NSLog print more beautiful, we can set it as shown below:


Suggest

Guess you like

Origin blog.csdn.net/woruosuifenglang/article/details/54930032
Recommended