Learn notes--recognize important members of Xcode: Lldb debugger

Source: Internet
Author: User
Tags uikit

Prior to the Lldb debugger know less, usually mainly used to print the log and tentative with the mouse to view the property data and use P PO some simple command statements.

Today read some articles about LLDB, suddenly feel that before it too little understanding, originally it has so many functions.

Good memory than bad pen, I put the easy-to-use commands to record down, easy to view later.


First, the grammatical structure of LDB

The syntax structure of LLDB is as follows:
<command> [<subcommand> [<subcommand> ...]] <action> [-options [Option-value]] [argument [ Argument ...]
Translation:
< commands > [< > [< subcommands ...] < command actions > [command Options] [parameters [parameters]]
A Command object creates a context for the child Command object that follows it, and the subcommand creates a context for its subcommands, and so on.

For example, suppose we set a breakpoint on the main method, we use the following command:

Breakpoint Set-n Main
This command corresponds to the syntax above:

Command:breakpoint represents a breakpoint command
Action:set indicates what to do with the command
Option:-n means setting breakpoints based on Method name (--name)
Arguement: Method name Mian represents the parameter Mian

Ii. Raw (RAW) command
LLDB supports RAW (RAW) commands without command options, and the original command passes everything that follows the command as a parameter (arguement).
However, many of the original commands can also have command options, and when you use the command options, you need to add the command option to the command options and the parameters.

The expression command is an original command, for example:
1. Print a variable
(lldb) Expression count
(int) $ = 4
2. Print an object, you need to print the object with the-o command option, followed by the "--" delimiter, followed by the parameter
(LLDB) Expression-o-Self
<ViewController:0x7f9000f17660>

Third, the only matching principle
The LLDB command follows the unique matching principle, for example
(lldb) Breakpoint Set-n touchesbegan:withevent:
(LLDB) Bre-S-n touchesbegan:withevent:
The effect of these two commands is the same, because the letter "Bre" matches to the command only "breakpoint", according to the letter "s" match to the command operation only "set"

Iv. Common commands of LLDB
1.expression
The function of the expression command is to execute an expression and output the result returned by the expression. The complete syntax for expression is this:
Expression <cmd-options>--<expr>
The corresponding examples are as follows:
(LLDB) Expression-o-Self
<ViewController:0x7f9000f17660>
Expression < command options > "--" delimiter execution parameters or expressions

Expression is the most important command in LLDB. He can achieve 2 very important functions.
A. Execute an expression.
When the program is paused, the page properties can be modified directly through the LLDB debugger without having to rerun the program
Change color
(lldb) expression--Self.view.backgroundColor = [Uicolor Redcolor]
Refresh Interface
(lldb) expression--(void) [Catransaction flush]
B. Output return value
(lldb) expression--Self.view
(UIView *) $ = 0x00007fe322c18a10

2.P, print, call command
p, print, call. These three commands are all expressions--the alias (---that means no longer accepting command options) is a layer of expression--a wrapper
They also have two important functions of expression, a. Execution expression, B. Output return value
(LLDB) P Self.count
(cgfloat) $ = 30
(lldb) Print Self.count
(cgfloat) $ = 30
(LLDB) Call Self.count
(cgfloat) $ = 30
(lldb) expression--Self.count
(cgfloat) $ = 30

3.po
All objects in OC are represented by pointers, so when printed with P, print, call, expression-the object's pointer is printed, not the object itself. If we want to print the object. We need to use the command option:-O. For ease of use, LLDB is Expression-o--defines an alias: PO
Examples are as follows:
(LLDB) P self
(Singletonviewcontroller *) $ = 0x00007fda32a8f9d0
(LLDB) PO Self
<SingletonViewController:0x7fda32a8f9d0>
More expression learning can be obtained with help expression, and then carefully studied

4.thread
A. Thread BackTrace, BT
To get the stack information for the thread at this time while the program is paused, it can be obtained through the thread BackTrace command, which has the following syntax:
Thread BackTrace [-c <count>] [-S <frame-index>] [-e <boolean>]
-C: Set the number of frames for the print stack (frame)
-S: Set from which frame (frame) to start printing
-E: Whether to show additional backtracking
In fact, these command options are generally not necessary for us to use. When the program is tentative, you only need to call the thread BackTrace command to print the commonly used information.
The BT command is the same as the thread BackTrace command, for example:
(LLDB) Thread BackTrace
* Thread #1: tid = 0x3ae2, 0x0000000106ccc5a6 mytestworkproduct '-[singletonviewcontroller touchesbegan:withevent:] ( Self=0x00007fda32a8f9d0, _cmd= "touchesbegan:withevent:", Touches=1 element, event=0x00007fda307057b0) + at singletonviewcontroller.m:61, queue = ' com.apple.main-thread ', stop reason = Breakpoint 4.1
* Frame #0:0x0000000106ccc5a6 mytestworkproduct '-[singletonviewcontroller touchesbegan:withevent:] (self= 0x00007fda32a8f9d0, _cmd= "touchesbegan:withevent:", Touches=1 element, event=0x00007fda307057b0) + at singletonviewcontroller.m:61
Frame #1:0x0000000108972227 UIKit ' Forwardtouchmethod + 349
Frame #2:0x00000001089720b9 UIKit '-[uiresponder touchesbegan:withevent:] + 49
Frame #3:0x00000001087d3790 UIKit '-[uiwindow _sendtouchesforevent:] + 308
Frame #4:0x00000001087d46d4 UIKit '-[uiwindow sendevent:] + 865
Frame #5:0x000000010877fdc6 UIKit '-[uiapplication sendevent:] + 263
Frame #6:0x0000000106c15d73 mytestworkproduct '-[uiapplication (self=<unavailable>, _cmd=<unavailable>, event=<unavailable>) Btg_swizzlesendevent:] + at uiapplication+btgmethodswizzler.m:27 [opt]
Frame #7:0x0000000108759553 UIKit ' _uiapplicationhandleeventqueue + 6660
Frame #8:0x000000010b32f301 corefoundation ' __cfrunloop_is_calling_out_to_a_source0_perform_function__ + 17
Frame #9:0x000000010b32522c corefoundation ' __cfrunloopdosources0 + 556
Frame #10:0x000000010b3246e3 corefoundation ' __cfrunlooprun + 867
Frame #11:0x000000010b3240f8 corefoundation ' cfrunlooprunspecific + 488
Frame #12:0x000000010da06ad2 graphicsservices ' Gseventrunmodal + 161
Frame #13:0x000000010875ef09 UIKit ' Uiapplicationmain + 171
Frame #14:0x0000000106d4204a mytestworkproduct ' main (argc=1, Argv=0x00007fff590025d8) + 138 at Main.m:16
Frame #15:0x000000010c3c592d libdyld.dylib ' start + 1
Frame #16:0x000000010c3c592d libdyld.dylib ' start + 1
(LLDB) bt
* Thread #1: tid = 0x3ae2, 0x0000000106ccc5a6 mytestworkproduct '-[singletonviewcontroller touchesbegan:withevent:] ( Self=0x00007fda32a8f9d0, _cmd= "touchesbegan:withevent:", Touches=1 element, event=0x00007fda307057b0) + at singletonviewcontroller.m:61, queue = ' com.apple.main-thread ', stop reason = Breakpoint 4.1
* Frame #0:0x0000000106ccc5a6 mytestworkproduct '-[singletonviewcontroller touchesbegan:withevent:] (self= 0x00007fda32a8f9d0, _cmd= "touchesbegan:withevent:", Touches=1 element, event=0x00007fda307057b0) + at singletonviewcontroller.m:61
Frame #1:0x0000000108972227 UIKit ' Forwardtouchmethod + 349
Frame #2:0x00000001089720b9 UIKit '-[uiresponder touchesbegan:withevent:] + 49
Frame #3:0x00000001087d3790 UIKit '-[uiwindow _sendtouchesforevent:] + 308
Frame #4:0x00000001087d46d4 UIKit '-[uiwindow sendevent:] + 865
Frame #5:0x000000010877fdc6 UIKit '-[uiapplication sendevent:] + 263
Frame #6:0x0000000106c15d73 mytestworkproduct '-[uiapplication (self=<unavailable>, _cmd=<unavailable>, event=<unavailable>) Btg_swizzlesendevent:] + at uiapplication+btgmethodswizzler.m:27 [opt]
Frame #7:0x0000000108759553 UIKit ' _uiapplicationhandleeventqueue + 6660
Frame #8:0x000000010b32f301 corefoundation ' __cfrunloop_is_calling_out_to_a_source0_perform_function__ + 17
Frame #9:0x000000010b32522c corefoundation ' __cfrunloopdosources0 + 556
Frame #10:0x000000010b3246e3 corefoundation ' __cfrunlooprun + 867
Frame #11:0x000000010b3240f8 corefoundation ' cfrunlooprunspecific + 488
Frame #12:0x000000010da06ad2 graphicsservices ' Gseventrunmodal + 161
Frame #13:0x000000010875ef09 UIKit ' Uiapplicationmain + 171
Frame #14:0x0000000106d4204a mytestworkproduct ' main (argc=1, Argv=0x00007fff590025d8) + 138 at Main.m:16
Frame #15:0x000000010c3c592d libdyld.dylib ' start + 1
Frame #16:0x000000010c3c592d libdyld.dylib ' start + 1


B. Thread return
When debugging, there are times when you don't want the code to go along the way for a variety of reasons. Either return directly or bring a value directly back.
The syntax is: thread return [<expr>]
Examples are as follows:
(LLDB) Thread return
(LLDB) thread return NO

C. C, N, S, finish
In the Lldb debugger, there is a pause button in the upper corner, the right three order is: stepping, entering method, returning from the current method to the previous layer frame
These three buttons are usually manually clicked for debugging, in the Lldb debugger, there are corresponding commands to operate
The following are explained separately:
C/continue/thread continue: These three command effects all indicate that the program continues to run
N/next/thread step-over: These three command effects all represent a single step run
S/step/thread step-in: These three command effects all mean entering a method
Finish/step-out: Both command effects represent the immediate completion of the current method and return to the upper frame.

D. Thread other infrequently used commands
Thread list: List all the threads
Thread Select: Select a thread
Thread Info: Output information for the current thread
Additional commands can be learned using the command help thread

5. FRAME (frames)
In the 4th thread command, there is a thread backtrace command that prints out the stack information for the current thread, each of which is a frame
Examples are as follows:
Frame #0:0x0000000106ccc5a6 mytestworkproduct '-[singletonviewcontroller touchesbegan:withevent:] (self= 0x00007fda32a8f9d0, _cmd= "touchesbegan:withevent:", Touches=1 element, event=0x00007fda307057b0) + at singletonviewcontroller.m:61
Frame #1:0x0000000108972227 UIKit ' Forwardtouchmethod + 349
Frame #2:0x00000001089720b9 UIKit '-[uiresponder touchesbegan:withevent:] + 49

A. Frame variable print variable information in the current frame
Examples are as follows:
(LLDB) Frame variable
(Singletonviewcontroller *) self = 0x00007fda32a8f9d0
(SEL) _cmd = "Touchesbegan:withevent:"
(__nssetm *) touches = 0x00007fda32ad6a70 1 element
(Uitouchesevent *) event = 0x00007fda307057b0
(nsinteger) Count1 = 10
(nsinteger) Count2 = 20
(LLDB) Frame variable Self->_count
(cgfloat) Self->_count = 30

B. Frame other infrequently used commands
Frame info: View information about the current frame
Frame select: Select a Frame

6. Breakpoint Breakpoint command, the breakpoint command in LLDB is very powerful
A. Breakpoint set breakpoint settings, there are several different ways to set breakpoints:
Use-N to set breakpoints based on method name:
To Viewwillappear: Set a breakpoint in all classes:
(lldb) Breakpoint Set-n viewwillappear:
Breakpoint 13:33 locations.
Specifying a file with-F
You only need to set breakpoints for Viewdidload in the viewcontroller.m file:
(lldb) Breakpoint Set-f viewcontroller.m-n Viewdidload
Breakpoint 22:where = Tlldb '-[viewcontroller viewdidload] + at viewcontroller.m:22, address = 0x000000010272a6f4
It is important to note that if a method is not written in a file (such as in a category file, or in a parent file), you cannot set a breakpoint on this method after specifying the file.
Use-l to specify a line on a file to set breakpoints
Set breakpoints to VIEWCONTROLLER.M line 38th
(lldb) Breakpoint Set-f Viewcontroller.m-l 38
Breakpoint 23:where = Tlldb '-[viewcontroller text:] + PNS at viewcontroller.m:38, address = 0x000000010272a7d5
Set conditional breakpoints with-c
Text: The method accepts a RET parameter, we want to make ret = = yes when the program interrupts:
(LLDB) Breakpoint Set-n text:-c ret = = YES
Breakpoint 7:where = Tlldb '-[viewcontroller text:] + at viewcontroller.m:37, address = 0x0000000105ef37ce
Set a single breakpoint with-O
If we just had that breakpoint we just wanted him to be interrupted once:
(LLDB) Breakpoint set-n text:-O
' Breakpoint 3 ': where = Tlldb '-[viewcontroller text:] + at viewcontroller.m:37, address = 0x000000010b6f97ce

B. Breakpoint Command subcommand
After a program goes to a breakpoint, it is sometimes necessary to execute commands that can be manipulated. We can add to the breakpoint, delete the command
B1. Breakpoint Command Add commands
(lldb) Breakpoint command add-o "PO Self" 3
Explanation: Add a command for breakpoints with breakpoint number: 3. -O complete notation is--one-liner.
(lldb) Breakpoint command Add 3
Enter your debugger command (s). Type ' done ' to end.
> Frame variable
> Continue
> Done
Explanation: Add multiple commands for breakpoints with breakpoint ID: 3, which are "done" as Terminators.
B2. Breakpoint Command List 3
Get a breakpoint with Breakpoint ID: 3 List of commands
B3. Breakpoint Command Delete 3
Remove all commands for breakpoints with breakpoint ID: 3

C. Breakpoint List
Get a list of all breakpoints in a project

D. Breakpoint Disable/enable
Invalid/Effective Breakpoint
Examples are as follows:
(LLDB) Breakpoint Disable 1
1 breakpoints Disabled.
(LLDB) Breakpoint Disable 1
1 breakpoints Disabled.

F. Breakpoint Delete
Delete breakpoint
(lldb) Breakpoint Delete 1
Delete breakpoint with breakpoint ID: 1
(lldb) Breakpoint Delete
About to D Elete All breakpoints, does want to does that?: [y/n] Y
All breakpoints removed. (1 breakpoint)
Delete all breakpoints in the project
(LLDB) breakpoint delete-f
All breakpoints removed. (1 breakpoint)
Force all breakpoints, ignore prompt

7. Watchpoint
Breakpoint has a twin brother Watchpoint. If breakpoint is a breakpoint that takes effect on a method, Watchpoint is the breakpoint that takes effect on the address
In the development process, usually we call a property change is often used as a property of the set method, if the property is not through the set method, directly through the Self-> property directly modified, with the Set method is not, at this time can be watchpoint command to listen to the memory address of the property, Once the content of the address is modified, the program is automatically disconnected.
A. Watchpoint Set
Set up observation points
Watchpoint Set Variable < variable parameters > set observation points for variables
(LLDB) watchpoint Set Variable self->_string
Watchpoint created:watchpoint 1:addr = 0x7fcf3959c418 size = 8 state = enabled type = W
Watchpoint spec = ' self->_string '
New value:0x0000000000000000
Watchpoint set Expression < variable address parameter > Set observation point for variable address
(LLDB) P &_model
(Modek *) $ = 0x00007fe0dbf23280
(lldb) watchpoint set expression 0x00007fe0dbf23280
Watchpoint created:watchpoint 1:addr = 0x7fe0dbf23280 size = 8 state = enabled type = W
New value:0

B. Watchpoint command
Similar to breakpoint, you can add commands in Watchpoint

C. Watchpoint command Add
Add a command to an observation point
Watchpoint command add-o ' po self ' 1
Add a command for an observation point with ID: 1
(lldb) watchpoint command add 1
Enter your debugger command (s). Type ' done ' to end.
> PO Self
> Continue
> Done
Add more than one command for an observation point with ID: 1

D. Watchpoint command List
Querying the list of observation point commands
(lldb) watchpoint command List 1
Query observation points with an observation point ID: 1, command list

F. watchpoint command Delete
(lldb) watchpoint command Delete 1
To delete a command list for an observation point with an observation point ID of: 1

E. watchpoint list
Query a list of all observation points in a project

F. Watchpoint disable
(LLDB) watchpoint disable 1
Set the observation point ID to: 1 is invalid

G. Watchpoint enable
(LLDB) watchpoint enable 1
Set the observation point ID to: 1 to be effective

H. watchpoint Delete
(lldb) Watchpoint Delete 1
To delete an observation point with an observation point ID of: 1
(lldb) Watchpoint Delete
About-to-delete all watchpoints, does want to does that?: [y/n] Y
All watchpoints removed. (2 watchpoints)
Delete an observation point in a project

8. Target prints the parameter information corresponding to the location in the project file according to the parameters
For Target This command, the most commonly used is the target modules lookup. Since Lldb has taken an alias image for Target modules, we can write this command again as an image lookup.
A.image Lookup--address finds the location in the project file where the symbol is located, based on the memory address given. Abbreviated to Image Lookup-a
When a crash occurs, you can navigate to where the crash occurred by printing the memory address.
Examples are as follows:
2017-04-14 21:49:30.784 mytestworkproduct[955:18657]-[__nsdictionarym addobject:]: Unrecognized selector sent to Instance 0x7fc50b0e4e70
Libc++abi.dylib:terminate_handler unexpectedly threw an exception
(lldb) Image Lookup-a 0x7fc50b0e4e70
B. Image Lookup--name If you want to find the location of a method or symbol, you can find it by using this command. Shorthand: Image Lookup-n
Examples are as follows:
LLDB) Image Lookup-n didreceivememorywarning
Matches found in/users/xuyefeng/library/developer/xcode/deriveddata/ mytestworkproduct-etxrcvqebgprfcfjyibavnioxbey/build/products/debug-iphonesimulator/mytestworkproduct.app/ Mytestworkproduct:
ADDRESS:MYTESTWORKPRODUCT[0X000000010000B742] (Mytestworkproduct.__text.__text + 40370)
Summary:mytestworkproduct '-[btgbaseviewcontroller didreceivememorywarning] at btgbaseviewcontroller.m:71 Address: MYTESTWORKPRODUCT[0X000000010005DE80] (Mytestworkproduct.__text.__text + 378096)
Summary:mytestworkproduct '-[MODELDICTIONARYTRANSFERVC didreceivememorywarning] at ModelDictionaryTransferVC.m:22 ADDRESS:MYTESTWORKPRODUCT[0X000000010005DF00] (Mytestworkproduct.__text.__text + 378224)
Summary:mytestworkproduct '-[viewcontroller didreceivememorywarning] at viewcontroller.m:23 Address: MYTESTWORKPRODUCT[0X0000000100061510] (Mytestworkproduct.__text.__text + 392064)
C. Image lookup--type You can use this command if you want to find information about a class. Abbreviated to Image Lookup-t
Examples are as follows:
(lldb) Image lookup-t person
Best match found in/users/xuyefeng/library/developer/xcode/deriveddata/ mytestworkproduct-etxrcvqebgprfcfjyibavnioxbey/build/products/debug-iphonesimulator/mytestworkproduct.app/ Mytestworkproduct:
id = {0x000d796a}, name = "Person", byte-size = +, Decl = person.h:13, Compiler_type = "@interface person:nsobject{
NSString * _NAME;
NSString * _adress;
NSNumber * _age;
Nsinteger _id;
}
@property (getter = Name,setter = setname:,readwrite,nonatomic) NSString * name;
@property (getter = Adress,setter = setadress:,readwrite,nonatomic) NSString * adress;
@property (getter = Age,setter = setage:,readwrite,nonatomic) NSNumber * age;
@property (getter = Id,setter = setid:,assign,readwrite,nonatomic) Nsinteger ID;
@end "

D. Target Stop-hook, the target Stop-hook command is to allow you to execute some commands at each stop
Target Stop-hook Add, display how the command is added.
Examples are as follows:
(LLDB) Target Stop-hook add-o "frame variable"
Stop Hook #4 added.
Note: The full name of-O is--one-liner

F. Target Stop-hook List
Get a list of all commands
E. Target Stop-hook Delete & Undisplay
Delete List
G. Target Stop-hook Disable/enable
Invalidate the command

9. Extension
Enter e @import UIKit in the debugger to print out the view frame
Examples are as follows:
(LLDB) e @import UIKit
(LLDB) PO self.view.frame
(origin = (x = 0, y = 0), size = (width = 375, height = 667))


10. More
More commands to learn with help
The apropos command can blur the search command keyword.

Reference Source:

The story of the Little Wolf and Lldb: Http://www.jianshu.com/p/e89af3e9a8d7

Learn notes--recognize important members of Xcode: Lldb debugger

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.