How to execute a block (identified by pointer) fro

2019-05-14 21:00发布

问题:

I'm in the lldb debugger within the iOS simulator and I have the address for a block. I want to try to execute it. I tried the first thing that came to mind (see below) but it didn't work. Is there a way to do this?

(lldb) po 0x2c7140
(int) $2 = 2912576 <__NSGlobalBlock__: 0x2c7140>
(lldb) po 0x2c7140(NO, @"Test")
error: called object type 'int' is not a function or function pointer

I also tried call but apparently that is not a command in llvm? It was available in gdb.

(lldb) call (void)0x2c7140(NO, @"Test")
error: 'call' is not a valid command.

I realize just now that the first attempt would have failed anyhow since po isn't going to work with a void return value, but the question still stands...

回答1:

You need to cast your number to a block pointer.

expr ((void (^)(BOOL,NSString*))0x2c7140)(NO, @"Test")
       |        |    |          |        |
  Return type  Argument types  Address  Call

(expr is lldb's replacement for call)

I have not actually tested that this works, but have confirmed that lldb recognizes the cast. You may need to split it into two expr commands, one to do the cast and one to do the call:

expr (void (^)(BOOL,NSString*))0x2c7140
expr $n(NO, @"Test")

where $n is the identifier given to the result of the first expression, which will be a part of lldb's output. I believe you can simply use $ to mean "the previous result", but have not tested this.