1""" 2Test the lldb disassemble command on lib stdc++. 3""" 4 5from __future__ import print_function 6 7 8import unittest2 9import os 10import lldb 11from lldbsuite.test.lldbtest import * 12import lldbsuite.test.lldbutil as lldbutil 13from lldbsuite.test.decorators import * 14 15class StdCXXDisassembleTestCase(TestBase): 16 17 @skipIfWindows 18 def test_stdcxx_disasm(self): 19 """Do 'disassemble' on each and every 'Code' symbol entry from the std c++ lib.""" 20 self.build() 21 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, "// Set break point at this line", lldb.SBFileSpec("main.cpp")) 22 23 # Disassemble the functions on the call stack. 24 self.runCmd("thread backtrace") 25 thread = lldbutil.get_stopped_thread( 26 process, lldb.eStopReasonBreakpoint) 27 self.assertIsNotNone(thread) 28 depth = thread.GetNumFrames() 29 for i in range(depth - 1): 30 frame = thread.GetFrameAtIndex(i) 31 function = frame.GetFunction() 32 if function.GetName(): 33 self.runCmd("disassemble -n '%s'" % function.GetName()) 34 35 lib_stdcxx = "FAILHORRIBLYHERE" 36 # Iterate through the available modules, looking for stdc++ library... 37 for i in range(target.GetNumModules()): 38 module = target.GetModuleAtIndex(i) 39 fs = module.GetFileSpec() 40 if (fs.GetFilename().startswith("libstdc++") 41 or fs.GetFilename().startswith("libc++")): 42 lib_stdcxx = str(fs) 43 break 44 45 # At this point, lib_stdcxx is the full path to the stdc++ library and 46 # module is the corresponding SBModule. 47 48 self.expect(lib_stdcxx, "Libraray StdC++ is located", exe=False, 49 substrs=["lib"]) 50 51 self.runCmd("image dump symtab '%s'" % lib_stdcxx) 52 raw_output = self.res.GetOutput() 53 # Now, look for every 'Code' symbol and feed its load address into the 54 # command: 'disassemble -s load_address -e end_address', where the 55 # end_address is taken from the next consecutive 'Code' symbol entry's 56 # load address. 57 # 58 # The load address column comes after the file address column, with both 59 # looks like '0xhhhhhhhh', i.e., 8 hexadecimal digits. 60 codeRE = re.compile(r""" 61 \ Code\ {9} # ' Code' followed by 9 SPCs, 62 0x[0-9a-f]{16} # the file address column, and 63 \ # a SPC, and 64 (0x[0-9a-f]{16}) # the load address column, and 65 .* # the rest. 66 """, re.VERBOSE) 67 # Maintain a start address variable; if we arrive at a consecutive Code 68 # entry, then the load address of the that entry is fed as the end 69 # address to the 'disassemble -s SA -e LA' command. 70 SA = None 71 for line in raw_output.split(os.linesep): 72 match = codeRE.search(line) 73 if match: 74 LA = match.group(1) 75 if self.TraceOn(): 76 print("line:", line) 77 print("load address:", LA) 78 print("SA:", SA) 79 if SA and LA: 80 if int(LA, 16) > int(SA, 16): 81 self.runCmd("disassemble -s %s -e %s" % (SA, LA)) 82 SA = LA 83 else: 84 # This entry is not a Code entry. Reset SA = None. 85 SA = None 86