Extend Lldb with Python

Source: Internet
Author: User
Tags python script uikit

Xcode integrates the LLDB, further simplifying the program debugging process. Although LLDB is powerful, its commands are limited. Fortunately, Lldb contains support for Python, making it possible to expand the lldb. I like to use the image Lookup command during the development process, but it is time consuming to execute only one piece at a time, so I always want to find a way to execute the batch. So I put my eyes on lldb python ...

Issue: Batch execution of Image lookup-a (1) to write a Python script (layne_command.py) with the following code:
#coding =utf-8# Custom lldb command import lldbimport commandsimport optparseimport shlexdef 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, ARG s) = 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]" Descriptio      n = "' 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 parserdef __lldb_init_module (debugger, internal_dict): Debugger. Handlecommand (' command script add-f layne_command.layne_imagelookup layne_imagelookup ') print (' The ' Layne_imagelooku P "Python command has been installed and are ready for use. ')

Then save as file layne_command.py, put in the following directory (your own designation): ~/python/lldb/layne_command.py
Description :
① #coding =utf-8 Specifies the Python script encoding, otherwise the Chinese in the run-time comment will be given an error.
② when the script is run, the entry is the __lldb_init_module(debugger,internal_dict) first to execute __lldb_init_module(debugger,internal_dict) the contents of the function. Debugger. Handlecommand is the primary way to execute LLDB commands in Python.
layne_imagelookup is a function that executes the image Lookup command in bulk, and is also the name of a custom new LLDB command.
optparse and shlex is the two important libraries used to parse the parameters. The parser is generated by Optparse.

(2) Referencing Python script in Xcode

When crash in Xcode, the LLDB console appears below, entering the following command:
command script import ~/Python/lldb/layne_command.py
A one-line prompt will appear after the carriage return: the"layne_imagelookup" python command has been installedand are ready to use (this hint is defined beforehand in the Layne_comm and.py). You can then use Layne_imagelookup as commands in the LLDB console po , using the method: If the memory address that appears when Crash is
0x1111111 0x2222222 0x3333333 0x4444444 0x5555555
The previous practice was to use the image lookup-a command for each address:

(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

Now just need to:

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

The results will be separated by "************", such as:

(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:u Ikit '-[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 _sched UleFirstCommitForScene:transition:firstActivation:completion:] + 249*ADDRESS:UIKIT[0X0000000000A2F998] (uikit.__text.__text + 10673352) Summary : UIKit '-[_uicanvaslifecyclesettingsdiffaction PerformActionsForCanvas:withUpdatedScene:settingsDiff: Fromsettings:transitioncontext:] + 231
(3) Automatic loading of Python scripts

There is a drawback to manually loading a custom Python script: Once the program is running again, if you want to use a custom command again, you must call
command script import ~/Python/lldb/layne_command.pyBefore you can use the Layne_imagelookup command. So configure it here to automatically load it.
principle : When Xcode starts, it reads a default file: ~/.lldbinit just write the command to command script import ~/Python/lldb/layne_command.py the file.
① open terminal, use VIM to open the file ~/.lldbinit (if not, VIM will be created automatically).
② writes the command command script import ~/Python/lldb/layne_command.py ~/.lldbinit to the file and saves the exit. (Note: The path to the layne_command.py must be correct!) )
As soon as Xcode starts up, you can use Layne_imagelookup in the LLDB console.

Extend Lldb with Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.