Xcode调试LLDB

时间:2022-08-08 06:29:38

一、简介

关于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 = ( ;  ); text = ''; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; tag = ; gestureRecognizers = <NSArray: 0x78ec3a30>; layer = <CALayer: 0x7a08c090>>

这个是我在控制台输入打印出来我这个控制器中的accountTextField显示出来的东西,可以看到里面都是你自己定义的这个对象的基本信息。

还有一个是p命令,p全称是:print。它的用途就是打印出来一些值,例如int float等这样的数值。例如:

    (lldb) p n

  (NSInteger) $ = 

    (lldb)

   我在一个Controller中定义了一个NSInteger n;然后在控制台输入p n,就会把现在这个状态时n的值给打印出来。

还有一个是expr。expr的全称应该是express吧。不太确定,但是它的功能是可以在调试时动态的制定制定的表达式,并将结果打印出来,你可以在控制台进行表达式操作。很有用的命令。例如

        (lldb) expr n==34

 (bool) $ = true

还是刚才定义的n,你可以在控制台输入 expr n==34,然后就可以判断出来n和34的大小,当然这只是简单地调试,你还可以写其他的比较复杂的表达式。

还有一个bt命令。bt全称就是thread backtrace.它的作用是打印调用的堆栈。在后面加上参数all即可打印出所有的thread的堆栈。例如:

(lldb) bt

* thread #: tid = 0x1d37f5, 0x0003f508 NotePad`__35-[LoginViewController loginRequest]_block_invoke(.block_descriptor=0x7c8b8b90, gets=0x7be65a80, error=0x00000000, code=) +  at LoginViewController.m:, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1

  * frame #: 0x0003f508 NotePad`__35-[LoginViewController loginRequest]_block_invoke(.block_descriptor=0x7c8b8b90, gets=0x7be65a80, error=0x00000000, code=) +  at LoginViewController.m:

    frame #: 0x00048d41 NotePad`__35+[RequestService login:userID:pwd:]_block_invoke(.block_descriptor=0x7c8b8b70, operation=0x7c8934a0, responseObject=0x7be65a80) +  at RequestService.m:

    frame #: 0x0008f1bb NotePad`__64-[AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke54(.block_descriptor=<unavailable>) +  at AFHTTPRequestOperation.m:

    frame #: 0x025755ea libdispatch.dylib`_dispatch_call_block_and_release + 

    frame #: 0x02597bef libdispatch.dylib`_dispatch_client_callout + 

    frame #: 0x0257d6bb libdispatch.dylib`_dispatch_main_queue_callback_4CF + 

    frame #: 0x00ae513e CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 

    frame #: 0x00aa3f10 CoreFoundation`__CFRunLoopRun + 

    frame #: 0x00aa337b CoreFoundation`CFRunLoopRunSpecific + 

    frame #: 0x00aa31ab CoreFoundation`CFRunLoopRunInMode + 

    frame #: 0x04ada2c1 GraphicsServices`GSEventRunModal + 

    frame #: 0x04ada0fe GraphicsServices`GSEventRun + 

    frame #: 0x011b79b6 UIKit`UIApplicationMain + 

    frame #: 0x0006f33d NotePad`main(argc=, argv=0xbffd2784) +  at main.m:

    frame #: 0x025c3ac9 libdyld.dylib`start + 

(lldb) bt all

* thread #: tid = 0x1d37f5, 0x0003f508 NotePad`__35-[LoginViewController loginRequest]_block_invoke(.block_descriptor=0x7c8b8b90, gets=0x7be65a80, error=0x00000000, code=) +  at LoginViewController.m:, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1

  * frame #: 0x0003f508 NotePad`__35-[LoginViewController loginRequest]_block_invoke(.block_descriptor=0x7c8b8b90, gets=0x7be65a80, error=0x00000000, code=) +  at LoginViewController.m:

    frame #: 0x00048d41 NotePad`__35+[RequestService login:userID:pwd:]_block_invoke(.block_descriptor=0x7c8b8b70, operation=0x7c8934a0, responseObject=0x7be65a80) +  at RequestService.m:

    frame #: 0x0008f1bb NotePad`__64-[AFHTTPRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke54(.block_descriptor=<unavailable>) +  at AFHTTPRequestOperation.m:

    frame #: 0x025755ea libdispatch.dylib`_dispatch_call_block_and_release + 

    frame #: 0x02597bef libdispatch.dylib`_dispatch_client_callout + 

    frame #: 0x0257d6bb libdispatch.dylib`_dispatch_main_queue_callback_4CF + 

    frame #: 0x00ae513e CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 

    frame #: 0x00aa3f10 CoreFoundation`__CFRunLoopRun + 

    frame #: 0x00aa337b CoreFoundation`CFRunLoopRunSpecific + 

    frame #: 0x00aa31ab CoreFoundation`CFRunLoopRunInMode + 

    frame #: 0x04ada2c1 GraphicsServices`GSEventRunModal + 

    frame #: 0x04ada0fe GraphicsServices`GSEventRun + 

    frame #: 0x011b79b6 UIKit`UIApplicationMain + 

    frame #: 0x0006f33d NotePad`main(argc=, argv=0xbffd2784) +  at main.m:

    frame #: 0x025c3ac9 libdyld.dylib`start + 

  thread #: tid = 0x1d386e, 0x028c78ce libsystem_kernel.dylib`kevent64 + , queue = 'com.apple.libdispatch-manager'

    frame #: 0x028c78ce libsystem_kernel.dylib`kevent64 + 

    frame #: 0x025850f0 libdispatch.dylib`_dispatch_mgr_invoke + 

    frame #: 0x02584e48 libdispatch.dylib`_dispatch_mgr_thread + 

  thread #: tid = 0x1d3870, 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 

    frame #: 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 

    frame #: 0x028f32b1 libsystem_pthread.dylib`_pthread_wqthread + 

    frame #: 0x028f0e2e libsystem_pthread.dylib`start_wqthread + 

  thread #: tid = 0x1d3871, 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 

    frame #: 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 

    frame #: 0x028f32b1 libsystem_pthread.dylib`_pthread_wqthread + 

    frame #: 0x028f0e2e libsystem_pthread.dylib`start_wqthread + 

  thread #: tid = 0x1d3872, 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 

    frame #: 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 

    frame #: 0x028f32b1 libsystem_pthread.dylib`_pthread_wqthread + 

    frame #: 0x028f0e2e libsystem_pthread.dylib`start_wqthread + 

  thread #: tid = 0x1d387e, 0x028c09ce libsystem_kernel.dylib`mach_msg_trap + , name = 'AFNetworking'

    frame #: 0x028c09ce libsystem_kernel.dylib`mach_msg_trap + 

    frame #: 0x028bfa70 libsystem_kernel.dylib`mach_msg + 

    frame #: 0x00aa47d6 CoreFoundation`__CFRunLoopServiceMachPort + 

    frame #: 0x00aa3bb8 CoreFoundation`__CFRunLoopRun + 

    frame #: 0x00aa337b CoreFoundation`CFRunLoopRunSpecific + 

    frame #: 0x00aa31ab CoreFoundation`CFRunLoopRunInMode + 

    frame #: 0x00417498 Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 

    frame #: 0x004b6dcb Foundation`-[NSRunLoop(NSRunLoop) run] + 

    frame #: 0x0004bbbb NotePad`+[AFURLConnectionOperation networkRequestThreadEntryPoint:](self=0x000c6374, _cmd=0x000a2e2b, object=0x00000000) +  at AFURLConnectionOperation.m:

    frame #: 0x00415607 Foundation`-[NSThread main] + 

    frame #: 0x00415560 Foundation`__NSThread__main__ + 

    frame #: 0x028f2e13 libsystem_pthread.dylib`_pthread_body + 

    frame #: 0x028f2d89 libsystem_pthread.dylib`_pthread_start + 

    frame #: 0x028f0e52 libsystem_pthread.dylib`thread_start + 

  thread #: tid = 0x1d3881, 0x028c09ce libsystem_kernel.dylib`mach_msg_trap + , name = 'com.apple.NSURLConnectionLoader'

    frame #: 0x028c09ce libsystem_kernel.dylib`mach_msg_trap + 

    frame #: 0x028bfa70 libsystem_kernel.dylib`mach_msg + 

    frame #: 0x00aa47d6 CoreFoundation`__CFRunLoopServiceMachPort + 

    frame #: 0x00aa3bb8 CoreFoundation`__CFRunLoopRun + 

    frame #: 0x00aa337b CoreFoundation`CFRunLoopRunSpecific + 

    frame #: 0x00aa31ab CoreFoundation`CFRunLoopRunInMode + 

    frame #: 0x03a45386 CFNetwork`+[NSURLConnection(Loader) _resourceLoadLoop:] + 

    frame #: 0x00415607 Foundation`-[NSThread main] + 

    frame #: 0x00415560 Foundation`__NSThread__main__ + 

    frame #: 0x028f2e13 libsystem_pthread.dylib`_pthread_body + 

    frame #: 0x028f2d89 libsystem_pthread.dylib`_pthread_start + 

    frame #: 0x028f0e52 libsystem_pthread.dylib`thread_start + 

  thread #: tid = 0x1d3884, 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 

    frame #: 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 

    frame #: 0x028f32b1 libsystem_pthread.dylib`_pthread_wqthread + 

    frame #: 0x028f0e2e libsystem_pthread.dylib`start_wqthread + 

  thread #: tid = 0x1d3885, 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 

    frame #: 0x028c6e6a libsystem_kernel.dylib`__workq_kernreturn + 

    frame #: 0x028f32b1 libsystem_pthread.dylib`_pthread_wqthread + 

    frame #: 0x028f0e2e libsystem_pthread.dylib`start_wqthread + 

  thread #: tid = 0x1d3888, 0x028c684e libsystem_kernel.dylib`__select + , name = 'com.apple.CFSocket.private'

    frame #: 0x028c684e libsystem_kernel.dylib`__select + 

    frame #: 0x00aecb87 CoreFoundation`__CFSocketManager + 

    frame #: 0x028f2e13 libsystem_pthread.dylib`_pthread_body + 

    frame #: 0x028f2d89 libsystem_pthread.dylib`_pthread_start + 

    frame #: 0x028f0e52 libsystem_pthread.dylib`thread_start + 

还有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:

: file = '/zhanggui/NotePad/NotePad/ViewController/LocalPasswordViewController/LocalPasswordViewController.m', line = , locations =  (pending)

: file = '/zhanggui/NotePad/NotePad/ViewController/LoginViewController/LoginViewController.m', line = , locations = , resolved = , hit count = 

  2.1: where = NotePad`__35-[LoginViewController loginRequest]_block_invoke +  at LoginViewController.m:, address = 0x0003f508, resolved, hit count =  

这里就可以打印出来项目中所有的断点信息,例如在哪个控制器中在哪一行等。

此外还有: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还没有看出结果,所以上面刚说的那两个也没有测试。