一、简介
关于Xcode调试,相信大家很多会用断点调试,今天无意间在苹果开发的群里看到了po,瞬间心中有个疑问:po是什么?下面我就百度搜索了一下,介绍一点皮毛。
首先是LLDB,它的全名是lower level debug,意思就是底层调试器。原来苹果用的是GDB,后来发现GDB有一些问题无法解决,所以就用了LLDB,它是高性能的调试器,包括了完整的LLVM编译器,其中LLVM包括了Clang表达式解析器和反汇编程序,它可以理解OC语法。进而进行调试。
其次就是po,网上搜了一下还有p命令。po全称:print object。顾名思义就是打印出一个NSObject,意思是在控制台,你可以使用po命令打印出来一个继承与NSObject的类。例如
(lldb) po self.accountTextField
<UITextField: 0x7a08be30; frame = (48 30; 205 35); text = '01001'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; tag = 1; gestureRecognizers = <NSArray: 0x78ec3a30>; layer = <CALayer: 0x7a08c090>>
这个是我在控制台输入打印出来我这个控制器中的accountTextField显示出来的东西,可以看到里面都是你自己定义的这个对象的基本信息。
还有一个是p命令,p全称是:print。它的用途就是打印出来一些值,例如int float等这样的数值。例如:
(lldb) p n
(NSInteger) $0 = 34
(lldb)
我在一个Controller中定义了一个NSInteger n;然后在控制台输入p n,就会把现在这个状态时n的值给打印出来。
还有一个是expr。expr的全称应该是express吧。不太确定,但是它的功能是可以在调试时动态的制定制定的表达式,并将结果打印出来,你可以在控制台进行表达式操作。很有用的命令。例如
(lldb) expr n==34
(bool) $1 = true
还是刚才定义的n,你可以在控制台输入 expr n==34,然后就可以判断出来n和34的大小,当然这只是简单地调试,你还可以写其他的比较复杂的表达式。
还有一个bt命令。bt全称就是thread backtrace.它的作用是打印调用的堆栈。在后面加上参数all即可打印出所有的thread的堆栈。例如:
(lldb) bt
* thread #1: tid = 0x1d37f5, 0x0003f508 NotePad`__35-[LoginViewController loginRequest]_block_invoke(.block_descriptor=0x7c8b8b90, gets=0x7be65a80, error=0x00000000, code=0) + 104 at LoginViewController.m:100, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1 * frame #0: 0x0003f508 NotePad`__35-[LoginViewController loginRequest]_block_invoke(.block_descriptor=0x7c8b8b90, gets=0x7be65a80, error=0x00000000, code=0) + 104 at LoginViewController.m:100 frame #1: 0x00048d41 NotePad`__35+[RequestService login:userID:pwd:]_block_invoke(.block_descriptor=0x7c8b8b70, operation=0x7c8934a0, responseObject=0x7be65a80) + 225 at RequestService.m:24 frame #2: 0x0008f1bb NotePad`__64-[AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke54(.block_descriptor=<unavailable>) + 43 at AFHTTPRequestOperation.m:137 frame #3: 0x025755ea libdispatch.dylib`_dispatch_call_block_and_release + 15 frame #4: 0x02597bef libdispatch.dylib`_dispatch_client_callout + 14 frame #5: 0x0257d6bb libdispatch.dylib`_dispatch_main_queue_callback_4CF + 993 frame #6: 0x00ae513e CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14 frame #7: 0x00aa3f10 CoreFoundation`__CFRunLoopRun + 2256 frame #8: 0x00aa337b CoreFoundation`CFRunLoopRunSpecific + 443 frame #9: 0x00aa31ab CoreFoundation`CFRunLoopRunInMode + 123 frame #10: 0x04ada2c1 GraphicsServices`GSEventRunModal + 192 frame #11: 0x04ada0fe GraphicsServices`GSEventRun + 104 frame #12: 0x011b79b6 UIKit`UIApplicationMain + 1526 frame #13: 0x0006f33d NotePad`main(argc=1, argv=0xbffd2784) + 141 at main.m:14 frame #14: 0x025c3ac9 libdyld.dylib`start + 1
(lldb) bt all
* thread #1: tid = 0x1d37f5, 0x0003f508 NotePad`__35-[LoginViewController loginRequest]_block_invoke(.block_descriptor=0x7c8b8b90, gets=0x7be65a80, error=0x00000000, code=0) + 104 at LoginViewController.m:100, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1 * frame #0: 0x0003f508 NotePad`__35-[LoginViewController loginRequest]_block_invoke(.block_descriptor=0x7c8b8b90, gets=0x7be65a80, error=0x00000000, code=0) + 104 at LoginViewController.m:100 frame #1: 0x00048d41 NotePad`__35+[RequestService login:userID:pwd:]_block_invoke(.block_descriptor=0x7c8b8b70, operation=0x7c8934a0, responseObject=0x7be65a80) + 225 at RequestService.m:24 frame #2: 0x0008f1bb NotePad`__64-[AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke54(.block_descriptor=<unavailable>) + 43 at AFHTTPRequestOperation.m:137 frame #3: 0x025755ea libdispatch.dylib`_dispatch_call_block_and_release + 15 frame #4: 0x02597bef libdispatch.dylib`_dispatch_client_callout + 14 frame #5: 0x0257d6bb libdispatch.dylib`_dispatch_main_queue_callback_4CF + 993 frame #6: 0x00ae513e CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14 frame #7: 0x00aa3f10 CoreFoundation`__CFRunLoopRun + 2256 frame #8: 0x00aa337b CoreFoundation`CFRunLoopRunSpecific + 443 frame #9: 0x00aa31ab CoreFoundation`CFRunLoopRunInMode + 123 frame #10: 0x04ada2c1 GraphicsServices`GSEventRunModal + 192 frame #11: 0x04ada0fe GraphicsServices`GSEventRun + 104 frame #12: 0x011b79b6 UIKit`UIApplicationMain + 1526 frame #13: 0x0006f33d NotePad`main(argc=1, argv=0xbffd2784) + 141 at main.m:14 frame #14: 0x025c3ac9 libdyld.dylib`start + 1 thread #2: tid = 0x1d386e, 0x028c78ce libsystem_kernel.dylib`kevent64 + 10, queue = 'com.apple.libdispatch-manager' frame #0: 0x028c78ce libsystem_kernel.dylib`kevent64 + 10 frame #1: 0x025850f0 libdispatch.dylib`_dispatch_mgr_invoke + 245 frame #2: 0x02584e48 libdispatch.dylib`_dispatch_mgr_thread + 60 thread #3: tid = 0x1d3870, 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 10 frame #0: 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 10 frame #1: 0x028f32b1 libsystem_pthread.dylib`_pthread_wqthread + 939 frame #2: 0x028f0e2e libsystem_pthread.dylib`start_wqthread + 30 thread #4: tid = 0x1d3871, 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 10 frame #0: 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 10 frame #1: 0x028f32b1 libsystem_pthread.dylib`_pthread_wqthread + 939 frame #2: 0x028f0e2e libsystem_pthread.dylib`start_wqthread + 30 thread #5: tid = 0x1d3872, 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 10 frame #0: 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 10 frame #1: 0x028f32b1 libsystem_pthread.dylib`_pthread_wqthread + 939 frame #2: 0x028f0e2e libsystem_pthread.dylib`start_wqthread + 30 thread #6: tid = 0x1d387e, 0x028c09ce libsystem_kernel.dylib`mach_msg_trap + 10, name = 'AFNetworking' frame #0: 0x028c09ce libsystem_kernel.dylib`mach_msg_trap + 10 frame #1: 0x028bfa70 libsystem_kernel.dylib`mach_msg + 68 frame #2: 0x00aa47d6 CoreFoundation`__CFRunLoopServiceMachPort + 214 frame #3: 0x00aa3bb8 CoreFoundation`__CFRunLoopRun + 1400 frame #4: 0x00aa337b CoreFoundation`CFRunLoopRunSpecific + 443 frame #5: 0x00aa31ab CoreFoundation`CFRunLoopRunInMode + 123 frame #6: 0x00417498 Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 309 frame #7: 0x004b6dcb Foundation`-[NSRunLoop(NSRunLoop) run] + 82 frame #8: 0x0004bbbb NotePad`+[AFURLConnectionOperation networkRequestThreadEntryPoint:](self=0x000c6374, _cmd=0x000a2e2b, object=0x00000000) + 315 at AFURLConnectionOperation.m:169 frame #9: 0x00415607 Foundation`-[NSThread main] + 76 frame #10: 0x00415560 Foundation`__NSThread__main__ + 1326 frame #11: 0x028f2e13 libsystem_pthread.dylib`_pthread_body + 138 frame #12: 0x028f2d89 libsystem_pthread.dylib`_pthread_start + 162 frame #13: 0x028f0e52 libsystem_pthread.dylib`thread_start + 34 thread #7: tid = 0x1d3881, 0x028c09ce libsystem_kernel.dylib`mach_msg_trap + 10, name = 'com.apple.NSURLConnectionLoader' frame #0: 0x028c09ce libsystem_kernel.dylib`mach_msg_trap + 10 frame #1: 0x028bfa70 libsystem_kernel.dylib`mach_msg + 68 frame #2: 0x00aa47d6 CoreFoundation`__CFRunLoopServiceMachPort + 214 frame #3: 0x00aa3bb8 CoreFoundation`__CFRunLoopRun + 1400 frame #4: 0x00aa337b CoreFoundation`CFRunLoopRunSpecific + 443 frame #5: 0x00aa31ab CoreFoundation`CFRunLoopRunInMode + 123 frame #6: 0x03a45386 CFNetwork`+[NSURLConnection(Loader) _resourceLoadLoop:] + 469 frame #7: 0x00415607 Foundation`-[NSThread main] + 76 frame #8: 0x00415560 Foundation`__NSThread__main__ + 1326 frame #9: 0x028f2e13 libsystem_pthread.dylib`_pthread_body + 138 frame #10: 0x028f2d89 libsystem_pthread.dylib`_pthread_start + 162 frame #11: 0x028f0e52 libsystem_pthread.dylib`thread_start + 34 thread #8: tid = 0x1d3884, 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 10 frame #0: 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 10 frame #1: 0x028f32b1 libsystem_pthread.dylib`_pthread_wqthread + 939 frame #2: 0x028f0e2e libsystem_pthread.dylib`start_wqthread + 30 thread #9: tid = 0x1d3885, 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 10 frame #0: 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 10 frame #1: 0x028f32b1 libsystem_pthread.dylib`_pthread_wqthread + 939 frame #2: 0x028f0e2e libsystem_pthread.dylib`start_wqthread + 30 thread #10: tid = 0x1d3888, 0x028c684e libsystem_kernel.dylib`__select + 10, name = 'com.apple.CFSocket.private' frame #0: 0x028c684e libsystem_kernel.dylib`__select + 10 frame #1: 0x00aecb87 CoreFoundation`__CFSocketManager + 919 frame #2: 0x028f2e13 libsystem_pthread.dylib`_pthread_body + 138 frame #3: 0x028f2d89 libsystem_pthread.dylib`_pthread_start + 162 frame #4: 0x028f0e52 libsystem_pthread.dylib`thread_start + 34
还有br命令。br全称是breakpoint list简写。你可以加入l 即br l.这样可以打印出现在项目中已经打得断点。输入显示的效果如下:
(lldb) br
The following subcommands are supported: clear -- Clears a breakpoint or set of breakpoints in the executable. command -- A set of commands for adding, removing and examining bits of code to be executed when the breakpoint is hit (breakpoint 'commands'). delete -- Delete the specified breakpoint(s). If no breakpoints are specified, delete them all. disable -- Disable the specified breakpoint(s) without removing it/them. If no breakpoints are specified, disable them all. enable -- Enable the specified disabled breakpoint(s). If no breakpoints are specified, enable all of them. list -- List some or all breakpoints at configurable levels of detail. modify -- Modify the options on a breakpoint or set of breakpoints in the executable. If no breakpoint is specified, acts on the last created breakpoint. With the exception of -e, -d and -i, passing an empty argument clears the modification. set -- Sets a breakpoint or set of breakpoints in the executable. For more help on any particular subcommand, type 'help <command> <subcommand>’.
这里可以看出关于br的命令参数。例如输入br l,输出效果如下
(lldb) br l
Current breakpoints: 1: file = '/zhanggui/NotePad/NotePad/ViewController/LocalPasswordViewController/LocalPasswordViewController.m', line = 49, locations = 0 (pending) 2: file = '/zhanggui/NotePad/NotePad/ViewController/LoginViewController/LoginViewController.m', line = 100, locations = 1, resolved = 1, hit count = 1 2.1: where = NotePad`__35-[LoginViewController loginRequest]_block_invoke + 104 at LoginViewController.m:100, address = 0x0003f508, resolved, hit count = 1
这里就可以打印出来项目中所有的断点信息,例如在哪个控制器中在哪一行等。
此外还有:process continue l,thread step-in l,thread step-inst l,thread step-over l,thread step-over-inst l,thread step-out l,thread list等。大家可以自行测试一下看看什么功能。
这里再补充一下po命令。
1、po $eax。其中$eax是cup的一个寄存器。在一个异常的情况下,这个寄存器将会包含一个异常对象的指针。注意:$eax只会在模拟器里工作,如果在设备上测试,需要使用$r0寄存器。
当你输入po $eax时,如果你的程序没有异常抛出。控制台输出的是:
(lldb) po $eax
<nil>
这很正常,因为$eax包含的是异常对象的指针,你的程序没有异常,所以为空。但是当我抛出异常时,例如showSegue的identifier不存在时,这样就会产生异常,但是我在再次使用po $eax时,打印出来的信息却是:
(lldb) po $eax
error: Couldn't materialize: couldn't read the value of register eax Errored out in Execute, couldn't PrepareToExecuteJITExpression
调试我查看了好多资料,也没有找到原因,所以就暂时先放着了。(谁知道的可以告诉我,万分感谢)。
常规来说,用po [$eax name]可以显示出正在处理的异常的名字。
用po [$eax reason]可以显示出来错误信息。
但是由于我po $eax还没有看出结果,所以上面刚说的那两个也没有测试。