1.通过lldb指令给未来的某个方法添加断点,比如给test函数添加断点

breakpoint set -n test

其中n == name

2.通过help指令查看breakpoint set有哪些用法

help breakpoit set
-A ( --all-files )
All files are searched for source pattern matches.

-C <command> ( --command <command> )
A command to run when the breakpoint is hit, can be provided more
than once, the commands will get run in order left to right.

-D ( --dummy-breakpoints )
Act on Dummy breakpoints - i.e. breakpoints set before a file is
provided, which prime new targets.

-E <source-language> ( --language-exception <source-language> )
Set the breakpoint on exceptions thrown by the specified language
(without options, on throw but not catch.)

3.expression : 执行表达式

通过lldb指令,修改一个view的背景色未红色

expression self.view.backgroundColor = [UIColor redColor]

po self.view.backgroundColor = [UIColor redColor]

p self.view.backgroundColor = [UIColor blueColor]

print self.view.backgroundColor = [UIColor greenColor]

call self.view.backgroundColor = [UIColor redColor]

3.1.调试时动态执行指定表达式,如何修改变量值

demo

expr a=20

expression a=20

po a=50

过掉断点,输出40

打印变量的值可以使用print命令,print命令的简化方式有prin pri p,幸运的是p被lldb实现为特指print po命令是”exp -O —“命令的别名,使用”exp -O —”能达到同样的效果 如果我们只想查看对象的值的信息,则可以使用po(print object的缩写)命令

4.打印当前对象的几种方法

expression self
expression -- self
expression -O -- self
p self
po self
print self
call self

5.各个指令打印对象的区别

定义一个数组

NSArray *array = @[@1, @2, @3];
p array 

输出

(__NSArrayI *) $0 = 0x00006000016e0e10 @"3 elements"
po array

输出

<__NSArrayI 0x6000016e0e10>(
1,
2,
3
)

expression array /print/call和p输出相同 expression -O – array 和po输出相同

5.打印线程的堆栈信息thread backtrace

比如断点在test函数,执行指令thread backtrace

thread backtrace

thread backtrace 可以简写bt 因此

bt

输出

* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
* frame #0: 0x0000000100a5dcff 链式编程和函数式编程`test at ViewController.m:81
frame #1: 0x0000000100a5dc8b 链式编程和函数式编程`-[ViewController touchesBegan:withEvent:](self=0x00007fc7f040cef0, _cmd="touchesBegan:withEvent:", touches=1 element, event=0x0000600002ab8ea0) at ViewController.m:76
frame #2: 0x0000000104a40f01 UIKitCore`forwardTouchMethod + 353
frame #3: 0x0000000104a40d8f UIKitCore`-[UIResponder touchesBegan:withEvent:] + 49
frame #4: 0x0000000104e6f08f UIKitCore`-[UIWindow _sendTouchesForEvent:] + 2052
frame #5: 0x0000000104e70a30 UIKitCore`-[UIWindow sendEvent:] + 4080
frame #6: 0x0000000104676e10 UIKitCore`-[UIApplication sendEvent:] + 352
frame #7: 0x00000001045af0d0 UIKitCore`__dispatchPreprocessedEventFromEventQueue + 3024
frame #8: 0x00000001045b1cf2 UIKitCore`__handleEventQueueInternal + 5948
frame #9: 0x0000000101d48b31 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #10: 0x0000000101d483a3 CoreFoundation`__CFRunLoopDoSources0 + 243
frame #11: 0x0000000101d42a4f CoreFoundation`__CFRunLoopRun + 1263
frame #12: 0x0000000101d42221 CoreFoundation`CFRunLoopRunSpecific + 625
frame #13: 0x000000010a44f1dd GraphicsServices`GSEventRunModal + 62
frame #14: 0x000000010465b115 UIKitCore`UIApplicationMain + 140
frame #15: 0x0000000100a5e4b0 链式编程和函数式编程`main(argc=1, argv=0x00007ffeef1a2f68) at main.m:14
frame #16: 0x000000010374b551 libdyld.dylib`start + 1
frame #17: 0x000000010374b551 libdyld.dylib`start + 1

方法的调用顺序从高到底,如图

6.让函数直接返回某个值,不会执行断点后面的的代码

比如test函数

void test() {

int a = 10;
int b = 20;
NSLog(@"%d",a + b);
}

断点在test函数的第一行,如果不想打印NSLog,可以执行lldb指令

thread return 

如果函数有返回值,可以直接返回值,比如

thread return YES

7.打印当前栈帧的变量

如图 demo

使用

frame variable

打印出

(int) a = -1056893662
(int) b = 1359413436

由于a和b还未赋值,因此值是乱七八糟 执行两次

thread step-in

再次查看当前栈帧的变量

(int) a = 10
(int) b = 20

8.程序崩溃的时候,使用image查看崩溃所在的具体位置

代码示例

NSArray *array = @[@1, @2, @3];
NSNumber *num = array[3];

抛出如下异常

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndexedSubscript:]: index 3 beyond bounds [0 .. 2]'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000108ca429b __exceptionPreprocess + 331
1   libobjc.A.dylib                     0x0000000108240735 objc_exception_throw + 48
2   CoreFoundation                      0x0000000108bee8fc _CFThrowFormattedException + 194
3   CoreFoundation                      0x0000000108d26b60 +[__NSArrayI allocWithZone:] + 0
4   ÈìæºèÁºñÁ®ãÂíåÂáΩÊïºèÁºñÁ®ã      0x000000010791cbfe -[ViewController touchesBegan:withEvent:] + 350
5   UIKitCore                           0x000000010b8fff01 forwardTouchMethod + 353
6   UIKitCore                           0x000000010b8ffd8f -[UIResponder touchesBegan:withEvent:] + 49
7   UIKitCore                           0x000000010bd2e08f -[UIWindow _sendTouchesForEvent:] + 2052
8   UIKitCore                           0x000000010bd2fa30 -[UIWindow sendEvent:] + 4080
9   UIKitCore                           0x000000010b535e10 -[UIApplication sendEvent:] + 352
10  UIKitCore                           0x000000010b46e0d0 __dispatchPreprocessedEventFromEventQueue + 3024
11  UIKitCore                           0x000000010b470cf2 __handleEventQueueInternal + 5948
12  CoreFoundation                      0x0000000108c07b31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
13  CoreFoundation                      0x0000000108c073a3 __CFRunLoopDoSources0 + 243
14  CoreFoundation                      0x0000000108c01a4f __CFRunLoopRun + 1263
15  CoreFoundation                      0x0000000108c01221 CFRunLoopRunSpecific + 625
16  GraphicsServices                    0x000000011130f1dd GSEventRunModal + 62
17  UIKitCore                           0x000000010b51a115 UIApplicationMain + 140
18  ÈìæºèÁºñÁ®ãÂíåÂáΩÊïºèÁºñÁ®ã      0x000000010791d490 main + 112
19  libdyld.dylib                       0x000000010a60a551 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

可以看到应该在这个方法[ViewController touchesBegan:withEvent:]崩溃

image lookup --address 0x000000010791cbfe

输出

Address: [0x0000000100002bfe] (链式编程和函数式编程.__TEXT.__text + 5278)
Summary: -[ViewController touchesBegan:withEvent:] + 350 at ViewController.m:72

由此可以定位到ViewController.m第72行

9.使用image lookup查看UIKit或者Foundation框架系统文件类型,属性列表

比如查看NSCache的属性列表

image lookup --type NSCache

输出

Best match found in /Users/fashion/Library/Developer/Xcode/DerivedData/ejfremvnlsyrrdfudwjwfiqagnfc/Build/Products/Debug-iphonesimulator
id = {0x000072a7}, name = "NSCache", byte-size = 64, decl = NSCache.h:13, compiler_type = "@interface NSCache : NSObject{
id _delegate;
void *[5] _private;
void * _reserved;
}
@property ( getter = name,setter = setName:,readwrite,copy ) NSString * name;
@property ( getter = delegate,setter = setDelegate:,assign,readwrite ) id delegate;
@property ( getter = totalCostLimit,setter = setTotalCostLimit:,assign,readwrite ) NSUInteger totalCostLimit;
@property ( getter = countLimit,setter = setCountLimit:,assign,readwrite ) NSUInteger countLimit;
@property ( getter = evictsObjectsWithDiscardedContent,setter = setEvictsObjectsWithDiscardedContent:,assign,readwrite ) BOOL evictsObjectsWithDiscardedContent;
@end"

10.image其他指令有哪些

输入

image

输出

Commands for accessing information for one or more target modules.

Syntax: target modules <sub-command> ...

The following subcommands are supported:

add          -- Add a new module to the current target's modules.
dump         -- Commands for dumping information about one or more target
modules.
list         -- List current executable and dependent shared library
images.
load         -- Set the load addresses for one or more sections in a
target module.
lookup       -- Look up information within executable and dependent
shared library images.
search-paths -- Commands for managing module search paths for a target.
show-unwind  -- Show synthesized unwind instructions for a function.

For more help on any particular subcommand, type 'help <command> <subcommand>'.

11.Xcode界面上lldb对应的指令 demo demo demo demo