Guest User

Untitled

a guest
Jun 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. The attached lldb command `pblock` command lets you peek inside an Objective-C block. It tries to tell you where to find the source code for the block, and the values captured by the block when it was created.
  2.  
  3. Consider this example program:
  4.  
  5. #import <Foundation/Foundation.h>
  6.  
  7. @interface Foo: NSObject
  8. @end
  9.  
  10. @implementation Foo
  11. @end
  12.  
  13. typedef void (^MyBlock)(int y);
  14.  
  15. MyBlock makeBlock(Foo *foo, int x) {
  16. return ^(int y){
  17. NSLog(@"foo=%@ x+y=%d\n", foo, x + y);
  18. };
  19. }
  20.  
  21. int main(int argc, const char * argv[]) {
  22. @autoreleasepool {
  23. Foo *foo = [Foo new];
  24. MyBlock block = makeBlock(foo, 7);
  25. block(8); // <------------ breakpoint here
  26. }
  27. return 0;
  28. }
  29.  
  30. Suppose you put a breakpoint at the call to `block`, stopping before the block is called. Then in the debugger, you can use `pblock` to find out what's about to happen:
  31.  
  32. (lldb) pblock block
  33. /Users/mayoff/TestProjects/blockTest2/blockTest2/main.m:12
  34. (__block_literal_1) *$0 = {
  35. __isa = 0x00007fffa2380160
  36. __flags = -1023410172
  37. __reserved = 0
  38. __FuncPtr = 0x0000000100000dc0 (blockTest2`__makeBlock_block_invoke at main.m:12)
  39. __descriptor = 0x0000000100001060
  40. foo = 0x0000000100580c10
  41. x = 7
  42. }
  43.  
  44. The `pblock` command prints (if available) the source file and line number where the block body was defined, and the contents of the block literal, if debug information for it is available. Everything following the `__descriptor` field of the block literal is a value captured by the block when it was created.
  45.  
  46. Thanks to Jim Ingham and John McCall at WWDC 2018 for lots of help making this work.
Add Comment
Please, Sign In to add comment