对我而言,Objective-C能够对周围环境作出反应,描述和混乱。这从基本层面开始,具有在任何时候引用_cmd
并且获得当前SEL
的坚定能力。从那里,您可以选择参加NSInvocation
咒语或运行时诡计。
现在,在一个区块内,您仍然可以调用_cmd
并对当前“上下文”进行模糊描述,即
__30-[RoomController awakeFromNib]_block_invoke123RoomController
描述性?是的。 信息性?好的......但不是那么有用。如何在块中获得动态和准确的运行时信息 ,特别是调用签名,args等?
I have found a useful little method to "describe" a block提前给出了一个很好的例子,说明我希望在块内获取信息的类型。
typedef void(^blockHead)(NSString*);
blockHead v = ^(NSString*sandy) { NSLog(@"damnDog",nil); };
Log([v blockDescription]);
[v blockDescription] = <NSMethodSignature: 0x7fd6fabc44d0>
number of arguments = 2
frame size = 224
is special struct return? NO
return value: -------- -------- -------- --------
type encoding (v) 'v'
flags {}
modifiers {}
frame {offset = 0, offset adjust = 0, size = 0, size adjust = 0}
memory {offset = 0, size = 0}
argument 0: -------- -------- -------- --------
type encoding (@) '@?'
flags {isObject, isBlock}
modifiers {}
frame {offset = 0, offset adjust = 0, size = 8, size adjust = 0}
memory {offset = 0, size = 8}
argument 1: -------- -------- -------- --------
type encoding (@) '@"NSString"'
flags {isObject}
modifiers {}
frame {offset = 8, offset adjust = 0, size = 8, size adjust = 0}
memory {offset = 0, size = 8}
class 'NSString'
答案 0 :(得分:5)
如果你深入挖掘,确实可以使用一些特定于目标的装配。
您将运行Objective-c代码有三种主要体系结构,它们是:
使用lldb调试器,以及大量的黑客攻击,我已经提出了每个平台正在使用的寄存器(用于保存块指针):
ecx
/ edi
rcx
/ rdi
r0
/ r4
在所有平台上,值似乎都在两个独立的寄存器中,可能一个来自调用点,另一个来自传递的参数。
使用这些信息,我制作了一些可以与GCC和Clang一起使用的宏,以便将所述寄存器的值转换为C变量:
#if TARGET_CPU_X86_64
// OSX, the block pointer is in the register 'rcx'.
// The 'mov' instruction does not clobber the register,
// So we can simply (ab)use that here.
#define BLOCK_GET_SELF() ({ id __block_self_tmp; __asm__("mov %%rcx, %0" : "=r"(__block_self_tmp)); __block_self_tmp; })
#elif TARGET_CPU_X86
// iOS Simulator, the block pointer is in the register 'ecx'.
// Same deal as with x86_64 code, except it's in a 32-bit register.
#define BLOCK_GET_SELF() ({ id __block_self_tmp; __asm__("mov %%ecx, %0" : "=r"(__block_self_tmp)); __block_self_tmp; })
#elif TARGET_CPU_ARM64
// iOS Device, ARM64 (iPhone 5S, iPad Mini 2, iPad Air).
// The block pointer is in the x0 register, and the x4 register.
// Similar code to the TARGET_CPU_ARM function.
#define BLOCK_GET_SELF() ({ id __block_self_tmp; __asm__("str x0, [%0]" :: "r"(&__block_self_tmp)); __block_self_tmp; })
#elif TARGET_CPU_ARM
// iOS Device, the block pointer is in register 'r0'.
// The 'mov' (move) instruction clobbers the r0 register
// (which messes up the debugger) for whatever reason,
// so we use the 'str' (store) instruction instead.
#define BLOCK_GET_SELF() ({ id __block_self_tmp; __asm__("str r0, [%0]" :: "r"(&__block_self_tmp)); __block_self_tmp; })
#endif
void blockTest() {
__block void *blockPtr = NULL;
void (^myBlock)() = ^{
id this = BLOCK_GET_SELF();
printf("this is:\t\t0x%.8lx\n", (uintptr_t) this);
printf("blockPtr is:\t0x%.8lx\n", (uintptr_t) blockPtr);
};
// example using dispatch
blockPtr = (__bridge void *) myBlock;
dispatch_async(dispatch_get_main_queue(), myBlock);
}
输出,运行iOS 7 Beta 2的iPhone 5:
this is: 0x17e7c890 blockPtr is: 0x17e7c890
请随时告诉我此代码的任何问题,我希望它可以帮助您!