Xcode Debugging Tips-B

Source: Internet
Author: User
Tags print object types of functions

With the release of Xcode 5, the LLDB debugger has replaced GDB as the default debugger in Xcode engineering. Together with the LLVM compiler, it gives us more extensive process control and debugging capabilities for data detection. LLDB provides the underlying debugging environment for Xcode, including the control Panel in the debug area embedded in the Xcode IDE, where we can call the LLDB command directly

1. Prerequisite 1.1 Print Variable: PRINT/PO
    • Print: Prints the value of a variable you can use the Print command, which, if you print a simple type, lists the type and value of the simple type. If it is an object, it will also print out the object pointer address;
    • Print Object: If we only want to view information about the value of an object, you can use the PO(abbreviation for print object ) command.
1.2 Viewing thread Status: Thread List

After the process has stopped, LLDB selects a current thread and the current frame in the thread (frame). Many commands for detecting status can be used with this thread or frame.
To detect the current state of the process, use this command, where an asterisk (*) indicates the thread #1为当前线程.

1.3 Getting the thread's trace stack: Thread BackTrace(abbreviated BT)

Using the command thread BackTrace, you can view the thread's trace stack, and you can use the command to see the call stack for all threads:thread BackTrace All (short BT all).

1.4 List the frame parameters and local variables: Frame Variable

Here frame refers to the current frame in the thread (frame)

1.5 Addressing: Image

Image We can use it to find the original address of an executable or shared library, and when our program crashes, we can find the exact location where the crash is located, as shown here:

This code will throw the following exception after it is run:

Now, we suspect that the wrong address is 0x000000010ce22e42 (which can be judged by the execution file name, or the minimum stack address). For further precise positioning, we can enter the following command:image lookup--address

As you can see, the location of the error is the .m No. 206 line of Userlistviewcontroller .

1.6 Help system: Help

The LLDB Help system allows us to understand what features LLDB provides and to see details of the LLDB command structure. Familiarity with the help system allows us to access the command documentation in the Help system.

We can simply call the help command to list all the top-level commands in the LLDB.

If the help is followed by a specific command, all information related to that command is listed as follows:

2. Tip 2.1 Change the value of the variable at run time: expression

How did you verify that the value of a variable caused the whole program not to work correctly? Modify the values of the variables in the code, and then cmd+r restart the app? Now you don't have to do this, just set a breakpoint when the program enters debug mode

Use the expression command to modify the value of a variable at run time

Once the command is complete, continue running the app, and you'll see the header background color turns yellow. This technique is useful for changing the content of a label text when debugging, and for displaying multiple lines/single-line effects.

2.2 Troubleshooting: Exception breakpoints

If an exception breakpoint is added, it will be interrupted every time the program has an exception. Generally used to catch unknown exceptions, run the following effect

2.3 Symbol Breakpoint

Symbolic breakpoint is simple to use, click on the "+" sign of the breakpoint interface and select "Add symbolic Breakpoint" to Create a breakpoint:

Here's the most important part, the editable entry for the breakpoint,

From the edit interface, you can see that breakpoints can be edited with the symbol, Module, Condition, Ignore, Action, Options.

    • Symbol

Breakpoint Trigger function. There are two types of functions, one is the C function style and one is the OC method style.
The C function style only needs to write the function name without writing the following () and arguments. such as NSLog.
The OC Method-style [className methodName] ClassName is the class name, andMethodName is the method name (the classification method and the instance method ). If the method that writes the tag of this class is overridden by the quilt class, the method of the subclass also triggers the breakpoint. For example [Uiviewcontroller viewdidload].

    • Module

Module filtering. You can avoid the same method names or function names in different libraries.

    • Condition

The trigger condition. Here you can add some specified trigger conditions, such as adding the first parameter cannot be nil. Here $arg 3 represents the first 1 parameters,$arg 4 represents the 2 parameters, and so on. The method can also be called to judge, but it must be a class method, and the return value must be of type bool .  

Example: Find the code that passes nil to [UIImage imagenamed:] . Here you need to set symbol to [UIImage imagenamed:], then condition set to $arg 3 = = nil. This will trigger a breakpoint if it encounters a nil pass in the run .

    • (4) Ignore

The number of triggering starts. Setting this value ignores the triggering of the previous specified number of times.

    • (5) Action

Triggers the activity. Here is the action to be performed when the breakpoint is triggered, you can add more than one, and the order of execution is from top to bottom. There are altogether 6 types of executables, such as

The main use of the following several times:

    1. Debugger command: Executes LLDB commands when a breakpoint is triggered . You can print objects, modify object values, and so on.
    2. Log Message: Logs are printed when a breakpoint is triggered. where @[email protected] Prints the object value,exp is the object name,%b indicates a broken name, and%h indicates the number of times the current breakpoint was triggered.
    3. Shell command: Executes shell commands when a breakpoint is triggered .
    • Options

Whether to enter the debug interface. Tick this breakpoint trigger does not enter the debug interface, the breakpoint print log or sound breakpoints are generally checked. Sometimes our program does not know where to go to crash, and crash is difficult to reproduce. It is a conservative practice to set breakpoints before the system throws an exception, specifically to set breakpoints at Objc_exception_throw . This way , in Debug mode, if the program is about to throw an exception, it can be interrupted at the point where the exception was thrown. The effect is similar to exception breakpoint.

2.4 watchpoints

Watuchpoints is a tool used to listen for changes in the value of a variable or change in memory address, triggering a pause in the debugger when a change occurs. For those states that do not know how to track accurately, this tool can be used to solve the problem. To set watchpoint, let debugger pause when the program runs to a stack frame containing the variable you want to see, and this time the variable is within the scope of the current stack frame. You can set watchpoint for this variable at this time .

You can set watchpoint in Xcode's GUI, keep the variables you want to watch in Xcode's Variables View, then right-click to set "Watch XXX". For example, to observe the title variable of self , click Watch "_button1clickcount" .

Xcode Debugging Tips-B

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.