Home | History | Annotate | Download | only in disassembly
      1 """Test lldb's disassemblt speed.  This bench deliberately attaches to an lldb
      2 inferior and traverses the stack for thread0 to arrive at frame with function
      3 'MainLoop'.  It is important to specify an lldb executable as the inferior."""
      4 
      5 import os, sys
      6 import unittest2
      7 import lldb
      8 import pexpect
      9 from lldbbench import *
     10 
     11 class AttachThenDisassemblyBench(BenchBase):
     12 
     13     mydir = os.path.join("benchmarks", "disassembly")
     14 
     15     def setUp(self):
     16         BenchBase.setUp(self)
     17         if lldb.bmExecutable:
     18             self.exe = lldb.bmExecutable
     19         else:
     20             self.exe = self.lldbHere
     21         self.count = lldb.bmIterationCount
     22         if self.count <= 0:
     23             self.count = 10
     24 
     25     @benchmarks_test
     26     def test_attach_then_disassembly(self):
     27         """Attach to a spawned lldb process then run disassembly benchmarks."""
     28         print
     29         self.run_lldb_attach_then_disassembly(self.exe, self.count)
     30         print "lldb disassembly benchmark:", self.stopwatch
     31 
     32     def run_lldb_attach_then_disassembly(self, exe, count):
     33         target = self.dbg.CreateTarget(exe)
     34 
     35         # Spawn a new process and don't display the stdout if not in TraceOn() mode.
     36         import subprocess
     37         popen = subprocess.Popen([exe, self.lldbOption],
     38                                  stdout = open(os.devnull, 'w') if not self.TraceOn() else None)
     39         if self.TraceOn():
     40             print "pid of spawned process: %d" % popen.pid
     41 
     42         # Attach to the launched lldb process.
     43         listener = lldb.SBListener("my.attach.listener")
     44         error = lldb.SBError()
     45         process = target.AttachToProcessWithID(listener, popen.pid, error)
     46 
     47         # Set thread0 as the selected thread, followed by the 'MainLoop' frame
     48         # as the selected frame.  Then do disassembly on the function.
     49         thread0 = process.GetThreadAtIndex(0)
     50         process.SetSelectedThread(thread0)
     51         i = 0
     52         found = False
     53         for f in thread0:
     54             #print "frame#%d %s" % (i, f.GetFunctionName())
     55             if "MainLoop" in f.GetFunctionName():
     56                 found = True
     57                 thread0.SetSelectedFrame(i)
     58                 if self.TraceOn():
     59                     print "Found frame#%d for function 'MainLoop'" % i
     60                 break
     61             i += 1
     62             
     63         # Reset the stopwatch now.
     64         self.stopwatch.reset()
     65         for i in range(count):
     66             with self.stopwatch:
     67                 # Disassemble the function.
     68                 self.runCmd("disassemble -f")
     69 
     70 
     71 if __name__ == '__main__':
     72     import atexit
     73     lldb.SBDebugger.Initialize()
     74     atexit.register(lambda: lldb.SBDebugger.Terminate())
     75     unittest2.main()
     76