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