Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- Consider this example program:
- #import <Foundation/Foundation.h>
- @interface Foo: NSObject
- @end
- @implementation Foo
- @end
- typedef void (^MyBlock)(int y);
- MyBlock makeBlock(Foo *foo, int x) {
- return ^(int y){
- NSLog(@"foo=%@ x+y=%d\n", foo, x + y);
- };
- }
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- Foo *foo = [Foo new];
- MyBlock block = makeBlock(foo, 7);
- block(8); // <------------ breakpoint here
- }
- return 0;
- }
- 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:
- (lldb) pblock block
- /Users/mayoff/TestProjects/blockTest2/blockTest2/main.m:12
- (__block_literal_1) *$0 = {
- __isa = 0x00007fffa2380160
- __flags = -1023410172
- __reserved = 0
- __FuncPtr = 0x0000000100000dc0 (blockTest2`__makeBlock_block_invoke at main.m:12)
- __descriptor = 0x0000000100001060
- foo = 0x0000000100580c10
- x = 7
- }
- 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.
- 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