This article transferred from: http://www.dreamingwish.com/article/lldb-usage-a.html
Lldb is an open source, Repl (read-eval-print-loop) feature built into Xcode that can install C + + or Python plugins.
This series of needles is intended for readers who already know what debugger is and have experience with GDB or LLDB. For the skilled user of lldb, it can also be used as a reference manual.
In this article, we tell about the commands built in Lldb.
Table of Lldb and gdb command names: http://lldb.llvm.org/lldb-gdb.html
1.help command
The Execute help command alone lists all the command lists, and the user-loaded plug-in is generally listed last.
The help that executes can print the Help information for the specified command, and for the commands provided by the plug-in, its assistance depends on the implementation of the plug-in itself.
Help print, for example, prints the use of the built-in command print.
2.print command
The simplified way of the Print command is Prin pri p, except that PR cannot be used as a check, because it is confused with the process, and fortunately P is implemented by LLDB as specifically print.
In fact, you will find that lldb for the short name of the command, is the head matching method, as long as not confused, you can simply call a command.
For example:
It is worth noting that the returned $ is the reference name of the command result, using $7 to print out the 106, of course, the $ can be used for any other expression or command to receive parameters.
3.expression command
The expression command can be used to modify the value of a variable, although in most cases it is more convenient to use the visual editor provided by Xcode.
1234 |
(lldb) p count (NSUInteger) $4 = 12 (lldb)e count = 42 (NSUInteger) $5 = 42 |
Actually, print is equivalent to expression----------------the command's argument is terminated, followed by--the input data of the command.
To print an object, you need to use E-o-anobj, and E-o-the abbreviation is our usual PO command:
12345 |
(lldb) po $8 ( foo, bar ) |
To print a variable in a specific format, use the p/
Command:
12345678 |
(lldb) p 16 16 (lldb)p/x 16 0x10 (lldb) p/t 16 0b00000000000000000000000000010000 (lldb) p/t ( char )16 0b00010000 |
or use P/C print character, p/s print C string, detailed format check here.
Now that we know that print is actually a shorthand for expression, it is possible to follow a complex expression after P (or PO) to evaluate the expression before printing the final result.
1234567891011 |
(lldb) e
int $a = 2
(lldb) p $a * 19
38
(lldb) e NSArray *$array = @[ @
"Saturday"
, @
"Sunday"
, @
"Monday" ]
(lldb) p [$array count]
3
(lldb) po [[$array objectAtIndex:0] uppercaseString]
SATURDAY
(lldb) p [[$array objectAtIndex:$a] characterAtIndex:0]
error: no known method
‘-characterAtIndex:‘
; cast the message send to the method‘s
return type
error: 1 errors parsing expression
|
It is important to note that once the above error is shown, LLDB cannot determine what data type the result of a step is evaluated in, it is necessary to force the type conversion to tell LLDB:
1234 |
(lldb) p ( char )[[$array objectAtIndex :$a] characterAtIndex : 0 ] ‘M‘ (lldb) p/d ( char )[[$array objectAtIndex :$a] characterAtIndex : 0 ] 77 |
4. Process Control Commands
In fact, it's easier to use Xcode's own visualizer to control "continue" "Pause" "Next" "Go" to "jump out", but here's a list of its corresponding command names:
Continue: Process continue, continue, C
Next: Thread Step-over, Next, n
Entry: Thread step-in, step, S
Jump: Thread step-out, Finish, F
5.thread return command
Executing the thread return command causes the current function to return immediately, that is, the subsequent code will not execute. Of course, executing this command may cause confusion in the arc's count tracking.
The thread return command requires a parameter to indicate the return value when the function is forced to return.
6. Breakpoint command
In general, it is easy to create new/delete "line breakpoints" in Xcode, but there are many advanced ways to use breakpoints:
Conditional breakpoints, condition execution, logging, auto-continuation, repeated breakpoint skipping.
Using the visual tools provided by Xcode is easy to do:
7. Execute arbitrary code in debugger
1234567 |
(lldb) e
char *$str = (
char *)malloc(
128
)
(lldb) e (
void
)strcpy($str,
"wxrld of warcraft"
)
(lldb) e $str[
1
] =
‘o‘
(
char
) $
0 =
‘o‘
(lldb) p $str
(
char *) $str = 0x00007fd04a900040
"world of warcraft"
(lldb) e (
void
)free($str)
|
So, in debugger, you can modify the color, size, and even create a controller to push the view.
8.watchpoint
Watchpoint can pause the program when a variable is written/read:
1234567891011 |
(lldb) watchpoint set expression -- (
int
*)&_abc4
Watchpoint created
: Watchpoint 7
: addr = 0x15e36d3c size =
4 state = enabled type = w
new value
: 0x00000000
(lldb) watchpoint set v -w read _abc4
Watchpoint created
: Watchpoint 8
: addr = 0x15e36d3c size =
4 state = enabled type = r
watchpoint spec =
‘_abc4‘
new value
:
0
(lldb) watchpoint set v -w read_write _abc3
Watchpoint created
: Watchpoint 9
: addr = 0x15e36d38 size =
4 state = enabled type = rw
watchpoint spec =
‘_abc3‘
new value
:
0
|
You can actually use Watchpoint to monitor the read and write of any memory.
You can also easily create watchpoint by using Xcode.
The watch in the Visual debug tool for Xcode is a write type Watchpoint (that is, the default)
In addition, the above statement is a shorthand for variable, similarly, set can be abbreviated to S,watch can be shortened to WA, and the parameters after-W are not abbreviated must be read, write, or Read_write.
Currently on ARM and x86, we create up to 4 watchpoint at a time and continue to create a prompt error.
9. Symbol Breakpoint
Creating a symbolic breakpoint with the Xcode visualizer is simple, fill in the symbol name in the Add symbolic breakpoint, and here is the notation for the OBJECTIVE-C function symbol breakpoint:
12 |
-[MyViewController viewDidAppear :] +[MyViewController sharedInstance ] |
10. View Memory
Using Xcode's visualizer to view memory, be aware of the difference between watch memory of "P" and watch Memory of "*p".
Manually execute the command to help X and/or helps memory.
Summary
The use of lldb is flexible, but Xcode's visual debugging tools have limited coverage, and some advanced usages still need to be manually typed into commands, even with Python scripts.
Transferred from: http://blog.sina.com.cn/s/blog_6dce99b10101gwwo.html
PO (Print object) is a command of LLDB, whose main function is to output information about objects in objective-c (objects), and another command similar to that is p(print), whose primary function is to output the native type (Boolean, Integer, float, etc) information.
Practical tip: Modify the value of a variable at run time
You can modify the value of a variable at run time by using the expr command.
Practical tip Two: Set the breakpoint trigger condition
Another important function of breakpoints is that you can set the conditions that trigger the breakpoint to take effect, so that we can analyze the specific data at run time to see if the app is running on the right track. Such as:
The above can be seen in the following statement
1 |
( BOOL )[(NSString*)[item valueForKey:@ "ID" ] isEqualToString:@ "93306" ] |
With this line of statement, we tell the compiler that when the ID equals 93306 in item, this breakpoint takes effect and enters the breakpoint debug mode.
Practical tips Three: Format output data
If you're tired of the endless NSLog in your code, fortunately we can edit breakpoints so that they output formatted strings just as they would normally be encoded. However, it is important to note that the usual encoding may use NSString's stringWithFormat: output format string, but this method seems to be in the breakpoint wood effect, you need to use the method of Alloc/init form, as follows:
1 |
po [[NSString alloc] initWithFormat:@ "Item index is: %d" , index] |
Run the app and you'll see the desired output in the console!
Simple! Powerful! This is lldb give you the choice, from this code can no longer have nslog the situation, the code becomes cleaner, the mood becomes more joyful!
LLDB also has a lot of powerful places, this tutorial only to uncover its veil, even so, still let us indulge.
Go Xcode on the use of LLDB