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