使用Python扩展lldb

Xcode集成了LLDB,进一步简化了程序调试流程。虽然LLDB很强大,可是它的命令颇有限。所幸的是,lldb包含了对python的支持,使得lldb的拓展成为可能。本人在开发过程当中很喜欢使用image lookup 命令,可是苦于每次只能执行一条,至关耗时,所以一直想要找到一种批量执行的方法。因而将目光放到了lldb python上......python

问题:批量执行image lookup -a

(1)编写python脚本(layne_command.py),代码以下:

#coding=utf-8
#自定义lldb命令 
import lldb
import commands
import optparse
import shlex

def layne_imagelookup(debugger, command, result, internal_dict):
    target = debugger.GetSelectedTarget()
    process = target.GetProcess()
    thread = process.GetSelectedThread()

    command_args = shlex.split(command)
    parser = create_custom_parser()

    try:
        (options, args) = parser.parse_args(command_args)
    except:
        result.SetError ("option parsing failed")
        return
    if args:
        for address in args:
            print("*************************************")
            debugger.HandleCommand('image lookup -a %s'%(address))

def create_custom_parser():
    usage = "usage: %prog [options]"
    description = '''Parse Symbols to Human-readable Format.'''  
    parser = optparse.OptionParser(description=description, prog='print_frame',usage=usage)
    # parser.add_option('-p','--parse',type='string',dest = 'parse',help='parse symbols.');
    return parser

def __lldb_init_module(debugger, internal_dict):  
    debugger.HandleCommand('command script add -f layne_command.layne_imagelookup layne_imagelookup')  
    print('The "layne_imagelookup" python command has been installed and is ready for use.')

而后保存为文件layne_command.py,放到以下目录(本身指定):~/Python/lldb/layne_command.py
说明:
①#coding=utf-8指定python脚本编码,不然运行时注释中的中文将会报错。
②运行脚本时入口为 __lldb_init_module(debugger,internal_dict) , 即先执行函数 __lldb_init_module(debugger,internal_dict) 中的内容。debugger.HandleCommand是python中执行lldb命令的主要方式。
layne_imagelookup是批量执行image lookup命令的函数,也是自定义的新的lldb命令的名称。
optparseshlex是用于解析参数的两个重要的库。经过optparse来生成解析器。 vim

(2)xcode中引用python脚本

在xcode中crash的时候,下方会出现lldb控制台,输入以下命令:
command script import ~/Python/lldb/layne_command.py
回车以后将会出现一行提示:The "layne_imagelookup" python command has been installed and is ready for use(这个提示是事先定义在layne_command.py中的)。而后就能够在lldb控制台像po命令那样使用layne_imagelookup了,使用方法:假如crash的时候出现的内存地址为
0x1111111 0x2222222 0x3333333 0x4444444 0x5555555
之前的作法是对每一个地址使用image lookup -a命令:xcode

(lldb)image lookup -a 0x1111111   
(lldb)image lookup -a 0x2222222
(lldb)image lookup -a 0x3333333
(lldb)image lookup -a 0x4444444
(lldb)image lookup -a 0x5555555

如今只须要:app

(lldb)layne_imagelookup 0x1111111 0x2222222 0x3333333 0x4444444 0x5555555

结果将会以"************"分隔开显示,如:ide

(lldb) layne_imagelookup 0x0000000107bcd914 0x000000010de2435a 0x000000010de2b245 0x000000010e1e6865 0x000000010e832998
*************************************
      Address: Maketion[0x0000000100004914] (Maketion.__TEXT.__text + 8356)
      Summary: Maketion`-[AppDelegate application:didFinishLaunchingWithOptions:] + 196 at AppDelegate.m:251
*************************************
      Address: UIKit[0x000000000002135a] (UIKit.__TEXT.__text + 128650)
      Summary: UIKit`-[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 267
*************************************
      Address: UIKit[0x0000000000028245] (UIKit.__TEXT.__text + 157045)
      Summary: UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 1720
*************************************
      Address: UIKit[0x00000000003e3865] (UIKit.__TEXT.__text + 4070293)
      Summary: UIKit`-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 249
*************************************
      Address: UIKit[0x0000000000a2f998] (UIKit.__TEXT.__text + 10673352)
      Summary: UIKit`-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 231

(3)自动加载python脚本

手动加载自定义的python脚本有个缺点:程序再次运行以后,若还想使用自定义的命令,则必须再次调用
command script import ~/Python/lldb/layne_command.py,而后才能使用layne_imagelookup命令。所以这里配置一下使其自动加载。
原理:xcode启动的时候会读取一个默认文件:~/.lldbinit,只须要将命令command script import ~/Python/lldb/layne_command.py写入这个文件便可。
①打开Terminal,使用vim打开文件~/.lldbinit(若没有,vim会自动建立)。
②将命令command script import ~/Python/lldb/layne_command.py写入文件~/.lldbinit,保存退出。 (注意:layne_command.py的路径必须正确!)
之后只要xcode启动起来就能够在lldb控制台使用layne_imagelookup.函数

相关文章
相关标签/搜索