2013-03-23 09:44:48 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
# Be sure to add the python path that points to the LLDB shared library.
|
|
|
|
# On MacOSX csh, tcsh:
|
|
|
|
# setenv PYTHONPATH /Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python
|
|
|
|
# On MacOSX sh, bash:
|
|
|
|
# export PYTHONPATH=/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
import commands
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import platform
|
2013-03-27 05:00:29 +08:00
|
|
|
import re
|
2013-03-23 09:44:48 +08:00
|
|
|
import resource
|
|
|
|
import sys
|
|
|
|
import time
|
2013-04-04 06:59:27 +08:00
|
|
|
import types
|
2013-03-23 09:44:48 +08:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
# Code that auto imports LLDB
|
|
|
|
#----------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
try:
|
2013-03-23 09:44:48 +08:00
|
|
|
# Just try for LLDB in case PYTHONPATH is already correctly setup
|
|
|
|
import lldb
|
|
|
|
except ImportError:
|
|
|
|
lldb_python_dirs = list()
|
|
|
|
# lldb is not in the PYTHONPATH, try some defaults for the current platform
|
|
|
|
platform_system = platform.system()
|
|
|
|
if platform_system == 'Darwin':
|
|
|
|
# On Darwin, try the currently selected Xcode directory
|
|
|
|
xcode_dir = commands.getoutput("xcode-select --print-path")
|
|
|
|
if xcode_dir:
|
2016-09-07 04:57:50 +08:00
|
|
|
lldb_python_dirs.append(
|
|
|
|
os.path.realpath(
|
|
|
|
xcode_dir +
|
|
|
|
'/../SharedFrameworks/LLDB.framework/Resources/Python'))
|
|
|
|
lldb_python_dirs.append(
|
|
|
|
xcode_dir + '/Library/PrivateFrameworks/LLDB.framework/Resources/Python')
|
|
|
|
lldb_python_dirs.append(
|
|
|
|
'/System/Library/PrivateFrameworks/LLDB.framework/Resources/Python')
|
2013-03-23 09:44:48 +08:00
|
|
|
success = False
|
|
|
|
for lldb_python_dir in lldb_python_dirs:
|
|
|
|
if os.path.exists(lldb_python_dir):
|
|
|
|
if not (sys.path.__contains__(lldb_python_dir)):
|
|
|
|
sys.path.append(lldb_python_dir)
|
2016-09-07 04:57:50 +08:00
|
|
|
try:
|
2013-03-23 09:44:48 +08:00
|
|
|
import lldb
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print 'imported lldb from: "%s"' % (lldb_python_dir)
|
|
|
|
success = True
|
|
|
|
break
|
|
|
|
if not success:
|
|
|
|
print "error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
class Timer:
|
|
|
|
|
2013-03-23 09:44:48 +08:00
|
|
|
def __enter__(self):
|
|
|
|
self.start = time.clock()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
self.end = time.clock()
|
|
|
|
self.interval = self.end - self.start
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-04-04 06:59:27 +08:00
|
|
|
class Action(object):
|
|
|
|
"""Class that encapsulates actions to take when a thread stops for a reason."""
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def __init__(self, callback=None, callback_owner=None):
|
2013-04-04 06:59:27 +08:00
|
|
|
self.callback = callback
|
|
|
|
self.callback_owner = callback_owner
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def ThreadStopped(self, thread):
|
2013-04-04 06:59:27 +08:00
|
|
|
assert False, "performance.Action.ThreadStopped(self, thread) must be overridden in a subclass"
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-04-04 06:59:27 +08:00
|
|
|
class PlanCompleteAction (Action):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def __init__(self, callback=None, callback_owner=None):
|
2013-04-04 06:59:27 +08:00
|
|
|
Action.__init__(self, callback, callback_owner)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def ThreadStopped(self, thread):
|
2013-04-04 06:59:27 +08:00
|
|
|
if thread.GetStopReason() == lldb.eStopReasonPlanComplete:
|
|
|
|
if self.callback:
|
|
|
|
if self.callback_owner:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.callback(self.callback_owner, thread)
|
2013-04-04 06:59:27 +08:00
|
|
|
else:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.callback(thread)
|
2013-04-04 06:59:27 +08:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class BreakpointAction (Action):
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
callback=None,
|
|
|
|
callback_owner=None,
|
|
|
|
name=None,
|
|
|
|
module=None,
|
|
|
|
file=None,
|
|
|
|
line=None,
|
|
|
|
breakpoint=None):
|
2013-04-04 06:59:27 +08:00
|
|
|
Action.__init__(self, callback, callback_owner)
|
|
|
|
self.modules = lldb.SBFileSpecList()
|
|
|
|
self.files = lldb.SBFileSpecList()
|
|
|
|
self.breakpoints = list()
|
|
|
|
# "module" can be a list or a string
|
|
|
|
if breakpoint:
|
|
|
|
self.breakpoints.append(breakpoint)
|
|
|
|
else:
|
|
|
|
if module:
|
|
|
|
if isinstance(module, types.ListType):
|
|
|
|
for module_path in module:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.modules.Append(
|
|
|
|
lldb.SBFileSpec(module_path, False))
|
2013-04-04 06:59:27 +08:00
|
|
|
elif isinstance(module, types.StringTypes):
|
|
|
|
self.modules.Append(lldb.SBFileSpec(module, False))
|
|
|
|
if name:
|
|
|
|
# "file" can be a list or a string
|
|
|
|
if file:
|
|
|
|
if isinstance(file, types.ListType):
|
|
|
|
self.files = lldb.SBFileSpecList()
|
|
|
|
for f in file:
|
|
|
|
self.files.Append(lldb.SBFileSpec(f, False))
|
|
|
|
elif isinstance(file, types.StringTypes):
|
|
|
|
self.files.Append(lldb.SBFileSpec(file, False))
|
2016-09-07 04:57:50 +08:00
|
|
|
self.breakpoints.append(
|
|
|
|
self.target.BreakpointCreateByName(
|
|
|
|
name, self.modules, self.files))
|
2013-04-04 06:59:27 +08:00
|
|
|
elif file and line:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.breakpoints.append(
|
|
|
|
self.target.BreakpointCreateByLocation(
|
|
|
|
file, line))
|
|
|
|
|
|
|
|
def ThreadStopped(self, thread):
|
2013-04-04 06:59:27 +08:00
|
|
|
if thread.GetStopReason() == lldb.eStopReasonBreakpoint:
|
|
|
|
for bp in self.breakpoints:
|
|
|
|
if bp.GetID() == thread.GetStopReasonDataAtIndex(0):
|
|
|
|
if self.callback:
|
|
|
|
if self.callback_owner:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.callback(self.callback_owner, thread)
|
2013-04-04 06:59:27 +08:00
|
|
|
else:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.callback(thread)
|
2013-04-04 06:59:27 +08:00
|
|
|
return True
|
|
|
|
return False
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2013-03-23 09:44:48 +08:00
|
|
|
class TestCase:
|
|
|
|
"""Class that aids in running performance tests."""
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-03-23 09:44:48 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.verbose = False
|
|
|
|
self.debugger = lldb.SBDebugger.Create()
|
|
|
|
self.target = None
|
|
|
|
self.process = None
|
|
|
|
self.thread = None
|
|
|
|
self.launch_info = None
|
2013-04-04 06:59:27 +08:00
|
|
|
self.done = False
|
2013-03-23 09:44:48 +08:00
|
|
|
self.listener = self.debugger.GetListener()
|
2013-04-04 06:59:27 +08:00
|
|
|
self.user_actions = list()
|
|
|
|
self.builtin_actions = list()
|
|
|
|
self.bp_id_to_dict = dict()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-03-23 09:44:48 +08:00
|
|
|
def Setup(self, args):
|
|
|
|
self.launch_info = lldb.SBLaunchInfo(args)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def Run(self, args):
|
2013-04-04 06:59:27 +08:00
|
|
|
assert False, "performance.TestCase.Run(self, args) must be subclassed"
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-03-23 09:44:48 +08:00
|
|
|
def Launch(self):
|
|
|
|
if self.target:
|
|
|
|
error = lldb.SBError()
|
2016-09-07 04:57:50 +08:00
|
|
|
self.process = self.target.Launch(self.launch_info, error)
|
2013-03-23 09:44:48 +08:00
|
|
|
if not error.Success():
|
|
|
|
print "error: %s" % error.GetCString()
|
|
|
|
if self.process:
|
2016-09-07 04:57:50 +08:00
|
|
|
self.process.GetBroadcaster().AddListener(self.listener,
|
|
|
|
lldb.SBProcess.eBroadcastBitStateChanged | lldb.SBProcess.eBroadcastBitInterrupt)
|
2013-03-23 09:44:48 +08:00
|
|
|
return True
|
|
|
|
return False
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def WaitForNextProcessEvent(self):
|
2013-03-23 09:44:48 +08:00
|
|
|
event = None
|
|
|
|
if self.process:
|
|
|
|
while event is None:
|
|
|
|
process_event = lldb.SBEvent()
|
2016-09-07 04:57:50 +08:00
|
|
|
if self.listener.WaitForEvent(lldb.UINT32_MAX, process_event):
|
|
|
|
state = lldb.SBProcess.GetStateFromEvent(process_event)
|
2013-03-23 09:44:48 +08:00
|
|
|
if self.verbose:
|
|
|
|
print "event = %s" % (lldb.SBDebugger.StateAsCString(state))
|
|
|
|
if lldb.SBProcess.GetRestartedFromEvent(process_event):
|
|
|
|
continue
|
2016-09-07 04:57:50 +08:00
|
|
|
if state == lldb.eStateInvalid or state == lldb.eStateDetached or state == lldb.eStateCrashed or state == lldb.eStateUnloaded or state == lldb.eStateExited:
|
2013-04-04 06:59:27 +08:00
|
|
|
event = process_event
|
|
|
|
self.done = True
|
2013-03-23 09:44:48 +08:00
|
|
|
elif state == lldb.eStateConnected or state == lldb.eStateAttaching or state == lldb.eStateLaunching or state == lldb.eStateRunning or state == lldb.eStateStepping or state == lldb.eStateSuspended:
|
2013-04-04 06:59:27 +08:00
|
|
|
continue
|
2013-03-23 09:44:48 +08:00
|
|
|
elif state == lldb.eStateStopped:
|
|
|
|
event = process_event
|
|
|
|
call_test_step = True
|
|
|
|
fatal = False
|
|
|
|
selected_thread = False
|
|
|
|
for thread in self.process:
|
|
|
|
frame = thread.GetFrameAtIndex(0)
|
|
|
|
select_thread = False
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-04-04 06:59:27 +08:00
|
|
|
stop_reason = thread.GetStopReason()
|
2013-03-23 09:44:48 +08:00
|
|
|
if self.verbose:
|
2016-09-07 04:57:50 +08:00
|
|
|
print "tid = %#x pc = %#x " % (thread.GetThreadID(), frame.GetPC()),
|
2013-03-23 09:44:48 +08:00
|
|
|
if stop_reason == lldb.eStopReasonNone:
|
|
|
|
if self.verbose:
|
|
|
|
print "none"
|
|
|
|
elif stop_reason == lldb.eStopReasonTrace:
|
|
|
|
select_thread = True
|
|
|
|
if self.verbose:
|
|
|
|
print "trace"
|
|
|
|
elif stop_reason == lldb.eStopReasonPlanComplete:
|
|
|
|
select_thread = True
|
|
|
|
if self.verbose:
|
|
|
|
print "plan complete"
|
|
|
|
elif stop_reason == lldb.eStopReasonThreadExiting:
|
|
|
|
if self.verbose:
|
|
|
|
print "thread exiting"
|
|
|
|
elif stop_reason == lldb.eStopReasonExec:
|
|
|
|
if self.verbose:
|
|
|
|
print "exec"
|
|
|
|
elif stop_reason == lldb.eStopReasonInvalid:
|
|
|
|
if self.verbose:
|
|
|
|
print "invalid"
|
|
|
|
elif stop_reason == lldb.eStopReasonException:
|
|
|
|
select_thread = True
|
|
|
|
if self.verbose:
|
|
|
|
print "exception"
|
|
|
|
fatal = True
|
|
|
|
elif stop_reason == lldb.eStopReasonBreakpoint:
|
|
|
|
select_thread = True
|
2013-04-04 06:59:27 +08:00
|
|
|
bp_id = thread.GetStopReasonDataAtIndex(0)
|
|
|
|
bp_loc_id = thread.GetStopReasonDataAtIndex(1)
|
2013-03-23 09:44:48 +08:00
|
|
|
if self.verbose:
|
2013-04-04 06:59:27 +08:00
|
|
|
print "breakpoint id = %d.%d" % (bp_id, bp_loc_id)
|
2013-03-23 09:44:48 +08:00
|
|
|
elif stop_reason == lldb.eStopReasonWatchpoint:
|
|
|
|
select_thread = True
|
|
|
|
if self.verbose:
|
|
|
|
print "watchpoint id = %d" % (thread.GetStopReasonDataAtIndex(0))
|
|
|
|
elif stop_reason == lldb.eStopReasonSignal:
|
|
|
|
select_thread = True
|
|
|
|
if self.verbose:
|
|
|
|
print "signal %d" % (thread.GetStopReasonDataAtIndex(0))
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-03-23 09:44:48 +08:00
|
|
|
if select_thread and not selected_thread:
|
2013-04-04 06:59:27 +08:00
|
|
|
self.thread = thread
|
2016-09-07 04:57:50 +08:00
|
|
|
selected_thread = self.process.SetSelectedThread(
|
|
|
|
thread)
|
|
|
|
|
2013-04-04 06:59:27 +08:00
|
|
|
for action in self.user_actions:
|
2016-09-07 04:57:50 +08:00
|
|
|
action.ThreadStopped(thread)
|
2013-04-04 06:59:27 +08:00
|
|
|
|
2013-03-23 09:44:48 +08:00
|
|
|
if fatal:
|
2016-09-07 04:57:50 +08:00
|
|
|
# if self.verbose:
|
2013-04-04 06:59:27 +08:00
|
|
|
# Xcode.RunCommand(self.debugger,"bt all",true)
|
|
|
|
sys.exit(1)
|
2013-03-23 09:44:48 +08:00
|
|
|
return event
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2013-03-27 05:00:29 +08:00
|
|
|
class Measurement:
|
|
|
|
'''A class that encapsulates a measurement'''
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-04-04 06:59:27 +08:00
|
|
|
def __init__(self):
|
|
|
|
object.__init__(self)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-03-27 05:00:29 +08:00
|
|
|
def Measure(self):
|
|
|
|
assert False, "performance.Measurement.Measure() must be subclassed"
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
|
2013-03-27 05:00:29 +08:00
|
|
|
class MemoryMeasurement(Measurement):
|
|
|
|
'''A class that can measure memory statistics for a process.'''
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-03-27 05:00:29 +08:00
|
|
|
def __init__(self, pid):
|
2013-04-04 06:59:27 +08:00
|
|
|
Measurement.__init__(self)
|
2013-03-27 05:00:29 +08:00
|
|
|
self.pid = pid
|
2016-09-07 04:57:50 +08:00
|
|
|
self.stats = [
|
|
|
|
"rprvt",
|
|
|
|
"rshrd",
|
|
|
|
"rsize",
|
|
|
|
"vsize",
|
|
|
|
"vprvt",
|
|
|
|
"kprvt",
|
|
|
|
"kshrd",
|
|
|
|
"faults",
|
|
|
|
"cow",
|
|
|
|
"pageins"]
|
|
|
|
self.command = "top -l 1 -pid %u -stats %s" % (
|
|
|
|
self.pid, ",".join(self.stats))
|
2013-03-27 05:00:29 +08:00
|
|
|
self.value = dict()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-03-27 05:00:29 +08:00
|
|
|
def Measure(self):
|
|
|
|
output = commands.getoutput(self.command).split("\n")[-1]
|
|
|
|
values = re.split('[-+\s]+', output)
|
|
|
|
for (idx, stat) in enumerate(values):
|
|
|
|
multiplier = 1
|
|
|
|
if stat:
|
|
|
|
if stat[-1] == 'K':
|
2013-04-04 06:59:27 +08:00
|
|
|
multiplier = 1024
|
2013-03-27 05:00:29 +08:00
|
|
|
stat = stat[:-1]
|
|
|
|
elif stat[-1] == 'M':
|
2016-09-07 04:57:50 +08:00
|
|
|
multiplier = 1024 * 1024
|
2013-03-27 05:00:29 +08:00
|
|
|
stat = stat[:-1]
|
|
|
|
elif stat[-1] == 'G':
|
2016-09-07 04:57:50 +08:00
|
|
|
multiplier = 1024 * 1024 * 1024
|
2013-03-27 05:00:29 +08:00
|
|
|
elif stat[-1] == 'T':
|
2016-09-07 04:57:50 +08:00
|
|
|
multiplier = 1024 * 1024 * 1024 * 1024
|
2013-03-27 05:00:29 +08:00
|
|
|
stat = stat[:-1]
|
2016-09-07 04:57:50 +08:00
|
|
|
self.value[self.stats[idx]] = int(stat) * multiplier
|
2013-03-27 05:00:29 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
'''Dump the MemoryMeasurement current value'''
|
|
|
|
s = ''
|
|
|
|
for key in self.value.keys():
|
|
|
|
if s:
|
|
|
|
s += "\n"
|
|
|
|
s += "%8s = %s" % (key, self.value[key])
|
|
|
|
return s
|
|
|
|
|
2013-03-23 09:44:48 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
class TesterTestCase(TestCase):
|
|
|
|
|
2013-04-04 06:59:27 +08:00
|
|
|
def __init__(self):
|
|
|
|
TestCase.__init__(self)
|
|
|
|
self.verbose = True
|
|
|
|
self.num_steps = 5
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def BreakpointHit(self, thread):
|
2013-04-04 06:59:27 +08:00
|
|
|
bp_id = thread.GetStopReasonDataAtIndex(0)
|
|
|
|
loc_id = thread.GetStopReasonDataAtIndex(1)
|
|
|
|
print "Breakpoint %i.%i hit: %s" % (bp_id, loc_id, thread.process.target.FindBreakpointByID(bp_id))
|
|
|
|
thread.StepOver()
|
2016-09-07 04:57:50 +08:00
|
|
|
|
|
|
|
def PlanComplete(self, thread):
|
2013-04-04 06:59:27 +08:00
|
|
|
if self.num_steps > 0:
|
|
|
|
thread.StepOver()
|
|
|
|
self.num_steps = self.num_steps - 1
|
|
|
|
else:
|
|
|
|
thread.process.Kill()
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
def Run(self, args):
|
2013-03-23 09:44:48 +08:00
|
|
|
self.Setup(args)
|
2013-03-27 05:00:29 +08:00
|
|
|
with Timer() as total_time:
|
|
|
|
self.target = self.debugger.CreateTarget(args[0])
|
|
|
|
if self.target:
|
2013-03-23 09:44:48 +08:00
|
|
|
with Timer() as breakpoint_timer:
|
2013-04-04 06:59:27 +08:00
|
|
|
bp = self.target.BreakpointCreateByName("main")
|
2016-09-07 04:57:50 +08:00
|
|
|
print(
|
|
|
|
'Breakpoint time = %.03f sec.' %
|
|
|
|
breakpoint_timer.interval)
|
|
|
|
|
|
|
|
self.user_actions.append(
|
|
|
|
BreakpointAction(
|
|
|
|
breakpoint=bp,
|
|
|
|
callback=TesterTestCase.BreakpointHit,
|
|
|
|
callback_owner=self))
|
|
|
|
self.user_actions.append(
|
|
|
|
PlanCompleteAction(
|
|
|
|
callback=TesterTestCase.PlanComplete,
|
|
|
|
callback_owner=self))
|
|
|
|
|
2013-03-27 05:00:29 +08:00
|
|
|
if self.Launch():
|
2013-04-04 06:59:27 +08:00
|
|
|
while not self.done:
|
|
|
|
self.WaitForNextProcessEvent()
|
2013-03-27 05:00:29 +08:00
|
|
|
else:
|
|
|
|
print "error: failed to launch process"
|
2013-03-23 09:44:48 +08:00
|
|
|
else:
|
2013-03-27 05:00:29 +08:00
|
|
|
print "error: failed to create target with '%s'" % (args[0])
|
|
|
|
print('Total time = %.03f sec.' % total_time.interval)
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-03-23 09:44:48 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
lldb.SBDebugger.Initialize()
|
|
|
|
test = TesterTestCase()
|
2016-09-07 04:57:50 +08:00
|
|
|
test.Run(sys.argv[1:])
|
2013-03-27 05:00:29 +08:00
|
|
|
mem = MemoryMeasurement(os.getpid())
|
|
|
|
mem.Measure()
|
|
|
|
print str(mem)
|
2013-03-23 09:44:48 +08:00
|
|
|
lldb.SBDebugger.Terminate()
|
2013-03-27 05:00:29 +08:00
|
|
|
# print "sleeeping for 100 seconds"
|
|
|
|
# time.sleep(100)
|