cmdtemplate.py revision 062a836a9f99e4df0249bec17f05ed2efb2ee9fb
1#!/usr/bin/python
2
3#----------------------------------------------------------------------
4# Be sure to add the python path that points to the LLDB shared library.
5#
6# To use this in the embedded python interpreter using "lldb":
7#   % cd /path/containing/cmdtemplate.py
8#   % lldb
9#   (lldb) script import cmdtemplate
10#
11# For the shells csh, tcsh:
12#   ( setenv PYTHONPATH /path/to/LLDB.framework/Resources/Python ; ./cmdtemplate.py )
13#
14# For the shells sh, bash:
15#   PYTHONPATH=/path/to/LLDB.framework/Resources/Python ./cmdtemplate.py
16#----------------------------------------------------------------------
17
18import lldb
19import commands
20import optparse
21import shlex
22
23def ls(debugger, command, result, dict):
24    command_args = shlex.split(command)
25    usage = "usage: %prog [options] <PATH> [PATH ...]"
26    description='''This command lets you run the /bin/ls command from within lldb as a quick and easy example.'''
27    parser = optparse.OptionParser(description=description, prog='ls',usage=usage)
28    parser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='display verbose debug info', default=False)
29    try:
30        (options, args) = parser.parse_args(command_args)
31    except:
32        return
33
34    for arg in args:
35        if options.verbose:
36            result.PutCString(commands.getoutput('/bin/ls "%s"' % arg))
37        else:
38            result.PutCString(commands.getoutput('/bin/ls -lAF "%s"' % arg))
39
40if __name__ == '__main__':
41    # This script is being run from the command line, create a debugger in case we are
42    # going to use any debugger functions in our function.
43    lldb.debugger = lldb.SBDebugger.Create()
44    ls (sys.argv)
45
46def __lldb_init_module (debugger, dict):
47    # This initializer is being run from LLDB in the embedded command interpreter
48    # Add any commands contained in this module to LLDB
49    debugger.HandleCommand('command script add -f cmdtemplate.ls ls')
50    print '"ls" command installed, type "ls --help" for detailed help'
51