forked from OSchip/llvm-project
Update cmdtemplate.py to use best pratices.
Fixes include: - fix all lint errors - add code that will automatically register and LLDB command classes by detecting the classes and any classes that have a "register_lldb_command" function - automatically fill in the correct module name when registering commands - automatically fill in the class name when registering command llvm-svn: 335401
This commit is contained in:
parent
203eaaf5ba
commit
ef115de629
|
@ -1,68 +1,90 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
# ---------------------------------------------------------------------
|
||||||
# Be sure to add the python path that points to the LLDB shared library.
|
# Be sure to add the python path that points to the LLDB shared library.
|
||||||
#
|
#
|
||||||
# # To use this in the embedded python interpreter using "lldb" just
|
# # To use this in the embedded python interpreter using "lldb" just
|
||||||
# import it with the full path using the "command script import"
|
# import it with the full path using the "command script import"
|
||||||
# command
|
# command
|
||||||
# (lldb) command script import /path/to/cmdtemplate.py
|
# (lldb) command script import /path/to/cmdtemplate.py
|
||||||
#----------------------------------------------------------------------
|
# ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
import inspect
|
||||||
import lldb
|
import lldb
|
||||||
import commands
|
|
||||||
import optparse
|
import optparse
|
||||||
import shlex
|
import shlex
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class FrameStatCommand:
|
class FrameStatCommand:
|
||||||
def create_options(self):
|
program = 'framestats'
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def register_lldb_command(cls, debugger, module_name):
|
||||||
|
parser = cls.create_options()
|
||||||
|
cls.__doc__ = parser.format_help()
|
||||||
|
# Add any commands contained in this module to LLDB
|
||||||
|
command = 'command script add -c %s.%s %s' % (module_name,
|
||||||
|
cls.__name__,
|
||||||
|
cls.program)
|
||||||
|
debugger.HandleCommand(command)
|
||||||
|
print('The "{0}" command has been installed, type "help {0}" or "{0} '
|
||||||
|
'--help" for detailed help.'.format(cls.program))
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create_options(cls):
|
||||||
|
|
||||||
usage = "usage: %prog [options]"
|
usage = "usage: %prog [options]"
|
||||||
description = '''This command is meant to be an example of how to make an LLDB command that
|
description = ('This command is meant to be an example of how to make '
|
||||||
does something useful, follows best practices, and exploits the SB API.
|
'an LLDB command that does something useful, follows '
|
||||||
Specifically, this command computes the aggregate and average size of the variables in the current frame
|
'best practices, and exploits the SB API. '
|
||||||
and allows you to tweak exactly which variables are to be accounted in the computation.
|
'Specifically, this command computes the aggregate '
|
||||||
'''
|
'and average size of the variables in the current '
|
||||||
|
'frame and allows you to tweak exactly which variables '
|
||||||
|
'are to be accounted in the computation.')
|
||||||
|
|
||||||
# Pass add_help_option = False, since this keeps the command in line with lldb commands,
|
# Pass add_help_option = False, since this keeps the command in line
|
||||||
# and we wire up "help command" to work by providing the long & short help methods below.
|
# with lldb commands, and we wire up "help command" to work by
|
||||||
self.parser = optparse.OptionParser(
|
# providing the long & short help methods below.
|
||||||
description = description,
|
parser = optparse.OptionParser(
|
||||||
prog = 'framestats',
|
description=description,
|
||||||
usage = usage,
|
prog=cls.program,
|
||||||
add_help_option = False)
|
usage=usage,
|
||||||
|
add_help_option=False)
|
||||||
|
|
||||||
self.parser.add_option(
|
parser.add_option(
|
||||||
'-i',
|
'-i',
|
||||||
'--in-scope',
|
'--in-scope',
|
||||||
action = 'store_true',
|
action='store_true',
|
||||||
dest = 'inscope',
|
dest='inscope',
|
||||||
help = 'in_scope_only = True',
|
help='in_scope_only = True',
|
||||||
default = True)
|
default=True)
|
||||||
|
|
||||||
self.parser.add_option(
|
parser.add_option(
|
||||||
'-a',
|
'-a',
|
||||||
'--arguments',
|
'--arguments',
|
||||||
action = 'store_true',
|
action='store_true',
|
||||||
dest = 'arguments',
|
dest='arguments',
|
||||||
help = 'arguments = True',
|
help='arguments = True',
|
||||||
default = True)
|
default=True)
|
||||||
|
|
||||||
self.parser.add_option(
|
parser.add_option(
|
||||||
'-l',
|
'-l',
|
||||||
'--locals',
|
'--locals',
|
||||||
action = 'store_true',
|
action='store_true',
|
||||||
dest = 'locals',
|
dest='locals',
|
||||||
help = 'locals = True',
|
help='locals = True',
|
||||||
default = True)
|
default=True)
|
||||||
|
|
||||||
self.parser.add_option(
|
parser.add_option(
|
||||||
'-s',
|
'-s',
|
||||||
'--statics',
|
'--statics',
|
||||||
action = 'store_true',
|
action='store_true',
|
||||||
dest = 'statics',
|
dest='statics',
|
||||||
help = 'statics = True',
|
help='statics = True',
|
||||||
default = True)
|
default=True)
|
||||||
|
|
||||||
|
return parser
|
||||||
|
|
||||||
def get_short_help(self):
|
def get_short_help(self):
|
||||||
return "Example command for use in debugging"
|
return "Example command for use in debugging"
|
||||||
|
@ -71,7 +93,7 @@ and allows you to tweak exactly which variables are to be accounted in the compu
|
||||||
return self.help_string
|
return self.help_string
|
||||||
|
|
||||||
def __init__(self, debugger, unused):
|
def __init__(self, debugger, unused):
|
||||||
self.create_options()
|
self.parser = self.create_options()
|
||||||
self.help_string = self.parser.format_help()
|
self.help_string = self.parser.format_help()
|
||||||
|
|
||||||
def __call__(self, debugger, command, exe_ctx, result):
|
def __call__(self, debugger, command, exe_ctx, result):
|
||||||
|
@ -82,12 +104,14 @@ and allows you to tweak exactly which variables are to be accounted in the compu
|
||||||
try:
|
try:
|
||||||
(options, args) = self.parser.parse_args(command_args)
|
(options, args) = self.parser.parse_args(command_args)
|
||||||
except:
|
except:
|
||||||
# if you don't handle exceptions, passing an incorrect argument to the OptionParser will cause LLDB to exit
|
# if you don't handle exceptions, passing an incorrect argument to
|
||||||
# (courtesy of OptParse dealing with argument errors by throwing SystemExit)
|
# the OptionParser will cause LLDB to exit (courtesy of OptParse
|
||||||
|
# dealing with argument errors by throwing SystemExit)
|
||||||
result.SetError("option parsing failed")
|
result.SetError("option parsing failed")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Always get program state from the SBExecutionContext passed in as exe_ctx
|
# Always get program state from the lldb.SBExecutionContext passed
|
||||||
|
# in as exe_ctx
|
||||||
frame = exe_ctx.GetFrame()
|
frame = exe_ctx.GetFrame()
|
||||||
if not frame.IsValid():
|
if not frame.IsValid():
|
||||||
result.SetError("invalid frame")
|
result.SetError("invalid frame")
|
||||||
|
@ -108,15 +132,16 @@ and allows you to tweak exactly which variables are to be accounted in the compu
|
||||||
variable_type = variable.GetType()
|
variable_type = variable.GetType()
|
||||||
total_size = total_size + variable_type.GetByteSize()
|
total_size = total_size + variable_type.GetByteSize()
|
||||||
average_size = float(total_size) / variables_count
|
average_size = float(total_size) / variables_count
|
||||||
print >>result, "Your frame has %d variables. Their total size is %d bytes. The average size is %f bytes" % (
|
print >>result, ("Your frame has %d variables. Their total size "
|
||||||
variables_count, total_size, average_size)
|
"is %d bytes. The average size is %f bytes") % (
|
||||||
# not returning anything is akin to returning success
|
variables_count, total_size, average_size)
|
||||||
|
# not returning anything is akin to returning success
|
||||||
|
|
||||||
|
|
||||||
def __lldb_init_module(debugger, dict):
|
def __lldb_init_module(debugger, dict):
|
||||||
# This initializer is being run from LLDB in the embedded command interpreter
|
# Register all classes that have a register_lldb_command method
|
||||||
|
for _name, cls in inspect.getmembers(sys.modules[__name__]):
|
||||||
# Add any commands contained in this module to LLDB
|
if inspect.isclass(cls) and callable(getattr(cls,
|
||||||
debugger.HandleCommand(
|
"register_lldb_command",
|
||||||
'command script add -c cmdtemplate.FrameStatCommand framestats')
|
None)):
|
||||||
print 'The "framestats" command has been installed, type "help framestats" for detailed help.'
|
cls.register_lldb_command(debugger, __name__)
|
||||||
|
|
Loading…
Reference in New Issue