1d879a630SGreg Clayton#!/usr/bin/python 2d879a630SGreg Clayton 3d879a630SGreg Clayton#---------------------------------------------------------------------- 4d879a630SGreg Clayton# Be sure to add the python path that points to the LLDB shared library. 5d879a630SGreg Clayton# 6e2841639SGreg Clayton# # To use this in the embedded python interpreter using "lldb" just 7e2841639SGreg Clayton# import it with the full path using the "command script import" 8e2841639SGreg Clayton# command 9e2841639SGreg Clayton# (lldb) command script import /path/to/cmdtemplate.py 10d879a630SGreg Clayton#---------------------------------------------------------------------- 11d879a630SGreg Clayton 12d879a630SGreg Claytonimport lldb 13d879a630SGreg Claytonimport commands 14d879a630SGreg Claytonimport optparse 15d879a630SGreg Claytonimport shlex 16d879a630SGreg Clayton 17f453907aSGreg Claytondef create_ls_options(): 18d879a630SGreg Clayton usage = "usage: %prog [options] <PATH> [PATH ...]" 19f453907aSGreg Clayton description='''This command lets you run the /bin/ls shell command from 20f453907aSGreg Claytonwithin lldb. This code is designed to demonstrate the best principles that 21f453907aSGreg Claytonshould be used when creating a new LLDB command through python. 22f453907aSGreg ClaytonCreating the options in a separate function allows the parser to be 23f453907aSGreg Claytoncreated without running the command. The usage string is generated by the 24f453907aSGreg Claytonoptparse module and can be used to populate the ls.__doc__ documentation 25f453907aSGreg Claytonstring in the command interpreter function prior to registering the 26f453907aSGreg Claytoncommand with LLDB. The allows the output of "ls --help" to exactly match 27f453907aSGreg Claytonthe output of "help ls" when both commands are run from within LLDB. 28f453907aSGreg Clayton''' 29d879a630SGreg Clayton parser = optparse.OptionParser(description=description, prog='ls',usage=usage) 30d879a630SGreg Clayton parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False) 31f453907aSGreg Clayton return parser 32f453907aSGreg Clayton 33f453907aSGreg Claytondef ls(debugger, command, result, dict): 34f453907aSGreg Clayton # Use the Shell Lexer to properly parse up command options just like a 35f453907aSGreg Clayton # shell would 36f453907aSGreg Clayton command_args = shlex.split(command) 37f453907aSGreg Clayton parser = create_ls_options() 38d879a630SGreg Clayton try: 39d879a630SGreg Clayton (options, args) = parser.parse_args(command_args) 40d879a630SGreg Clayton except: 41*6308f582SEnrico Granata # if you don't handle exceptions, passing an incorrect argument to the OptionParser will cause LLDB to exit 42*6308f582SEnrico Granata # (courtesy of OptParse dealing with argument errors by throwing SystemExit) 436c00a015SJim Ingham result.SetStatus (lldb.eReturnStatusFailed) 44d879a630SGreg Clayton return 45d879a630SGreg Clayton 46d879a630SGreg Clayton for arg in args: 47d879a630SGreg Clayton if options.verbose: 4887a59368SJim Ingham result.PutCString(commands.getoutput('/bin/ls "%s"' % arg)) 49d879a630SGreg Clayton else: 5087a59368SJim Ingham result.PutCString(commands.getoutput('/bin/ls -lAF "%s"' % arg)) 51d879a630SGreg Clayton 5287a59368SJim Inghamdef __lldb_init_module (debugger, dict): 5387a59368SJim Ingham # This initializer is being run from LLDB in the embedded command interpreter 54f453907aSGreg Clayton # Make the options so we can generate the help text for the new LLDB 55f453907aSGreg Clayton # command line command prior to registering it with LLDB below 56f453907aSGreg Clayton parser = create_ls_options() 57f453907aSGreg Clayton ls.__doc__ = parser.format_help() 58d879a630SGreg Clayton # Add any commands contained in this module to LLDB 5987a59368SJim Ingham debugger.HandleCommand('command script add -f cmdtemplate.ls ls') 60f453907aSGreg Clayton print 'The "ls" command has been installed, type "help ls" or "ls --help" for detailed help.' 61