自定義chisel命令
1,chisel自帶的命令就很強(qiáng)大矢腻,但是還無(wú)法滿足某些特殊的需求敲茄,如打印內(nèi)容,顯示格式副签,這就需要我們對(duì)chisel命令進(jìn)行自定義了遥椿。基本使用可以看這篇文章
2,本文寫的小例子是打印模型對(duì)象的值淆储,一般用po
命令打印出來(lái)的話是該模型對(duì)象的地址冠场,而這不是我們想要的。
3,首先自定義命令要求懂一點(diǎn)點(diǎn)Python的知識(shí)本砰,直接上主要的pthyon代碼碴裙。全部代碼已經(jīng)放在gitbub
def run(self, arguments, options):
cls = arguments[0]
ilass = arguments[0]
if not self.isClassObject(cls):
cls = runtimeHelpers.object_getClass(cls)
if not self.isClassObject(cls):
raise Exception('Invalid argument. Please specify an instance or a Class.')
menber_json = self.get_oc_menbers_json(cls,ilass)
print menber_json
def get_oc_menbers_json(mSelf,klass,ilass):
str = """
unsigned int menberCount;
NSMutableDictionary *result = (id)[NSMutableDictionary dictionary];
objc_property_t *properties = (objc_property_t *)class_copyPropertyList((Class)$cls, &menberCount);
for (int i = 0; i<menberCount; i++)
{
char *name = (char *)property_getName(properties[i]);
NSString *ret_name = (NSString *)[NSString stringWithUTF8String:name];
id value = (id)[(NSObject *)$ils valueForKey:ret_name]?:@"nil";
[result setObject:value forKey:ret_name];
}
//free(properties);
RETURN(result);
"""
command = string.Template(str).substitute(cls=klass,ils=ilass)
return fb.evaluate(command)`
其中定義了PrintInstanceDump
的類,需要實(shí)現(xiàn) name description run
方法來(lái)分別告訴名稱点额、描述舔株、和執(zhí)行實(shí)體。
創(chuàng)建好腳本后还棱,然后在前面安裝時(shí)創(chuàng)建的 ~/.lldbinit文件中添加一行:(其中命令保存在了/magical/commands/
)
script fblldb.loadCommandsInDirectory('/magical/commands/')
主要就是使用runtime技術(shù)動(dòng)態(tài)獲取模型屬性载慈。這個(gè)還要感謝錘神的指點(diǎn)
測(cè)試結(jié)果
測(cè)試的模型
參考文章:
好玩的debugDescription & runtime(debug模式下調(diào)試model)
Dancing in the Debugger — A Waltz with LLDB