美文网首页iOSer 干货部落
利用chisel自定义lldb命令

利用chisel自定义lldb命令

作者: LuckLarry | 来源:发表于2016-02-01 16:12 被阅读82次

    自定义chisel命令

    1,chisel自带的命令就很强大,但是还无法满足某些特殊的需求,如打印内容,显示格式,这就需要我们对chisel命令进行自定义了。基本使用可以看这篇文章
    2,本文写的小例子是打印模型对象的值,一般用po命令打印出来的话是该模型对象的地址,而这不是我们想要的。
    3,首先自定义命令要求懂一点点Python的知识,直接上主要的pthyon代码。全部代码已经放在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的类,需要实现 name description run 方法来分别告诉名称、描述、和执行实体。
    创建好脚本后,然后在前面安装时创建的 ~/.lldbinit文件中添加一行:(其中命令保存在了/magical/commands/)
    script fblldb.loadCommandsInDirectory('/magical/commands/')
    主要就是使用runtime技术动态获取模型属性。这个还要感谢锤神的指点

    测试结果 测试的模型

    参考文章:

    好玩的debugDescription & runtime(debug模式下调试model)
    Dancing in the Debugger — A Waltz with LLDB

    相关文章

      网友评论

        本文标题:利用chisel自定义lldb命令

        本文链接:https://www.haomeiwen.com/subject/eauskttx.html