]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/lang/cpp/virtual/TestVirtual.py
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / lang / cpp / virtual / TestVirtual.py
1 """
2 Test C++ virtual function and virtual inheritance.
3 """
4
5 from __future__ import print_function
6
7 import os
8 import time
9 import re
10 import lldb
11 from lldbsuite.test.decorators import *
12 from lldbsuite.test.lldbtest import *
13 from lldbsuite.test import lldbutil
14
15
16 def Msg(expr, val):
17     return "'expression %s' matches the output (from compiled code): %s" % (
18         expr, val)
19
20
21 class CppVirtualMadness(TestBase):
22
23     mydir = TestBase.compute_mydir(__file__)
24
25     # This is the pattern by design to match the "my_expr = 'value'" output from
26     # printf() stmts (see main.cpp).
27     pattern = re.compile("^([^=]*) = '([^=]*)'$")
28
29     def setUp(self):
30         # Call super's setUp().
31         TestBase.setUp(self)
32         # Find the line number to break for main.cpp.
33         self.source = 'main.cpp'
34         self.line = line_number(self.source, '// Set first breakpoint here.')
35
36     @expectedFailureAll(
37         compiler="icc",
38         bugnumber="llvm.org/pr16808 lldb does not call the correct virtual function with icc.")
39     def test_virtual_madness(self):
40         """Test that expression works correctly with virtual inheritance as well as virtual function."""
41         self.build()
42
43         # Bring the program to the point where we can issue a series of
44         # 'expression' command to compare against the golden output.
45         self.dbg.SetAsync(False)
46
47         # Create a target by the debugger.
48         target = self.dbg.CreateTarget("a.out")
49         self.assertTrue(target, VALID_TARGET)
50
51         # Create the breakpoint inside function 'main'.
52         breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
53         self.assertTrue(breakpoint, VALID_BREAKPOINT)
54
55         # Now launch the process, and do not stop at entry point.
56         process = target.LaunchSimple(
57             None, None, self.get_process_working_directory())
58         self.assertTrue(process, PROCESS_IS_VALID)
59
60         self.assertTrue(process.GetState() == lldb.eStateStopped)
61         thread = lldbutil.get_stopped_thread(
62             process, lldb.eStopReasonBreakpoint)
63         self.assertTrue(
64             thread.IsValid(),
65             "There should be a thread stopped due to breakpoint condition")
66
67         # First, capture the golden output from the program itself.
68         golden = thread.GetFrameAtIndex(0).FindVariable("golden")
69         self.assertTrue(
70             golden.IsValid(),
71             "Encountered an error reading the process's golden variable")
72         error = lldb.SBError()
73         golden_str = process.ReadCStringFromMemory(
74             golden.AddressOf().GetValueAsUnsigned(), 4096, error)
75         self.assertTrue(error.Success())
76         self.assertTrue("c_as_C" in golden_str)
77
78         # This golden list contains a list of "my_expr = 'value' pairs extracted
79         # from the golden output.
80         gl = []
81
82         # Scan the golden output line by line, looking for the pattern:
83         #
84         #     my_expr = 'value'
85         #
86         for line in golden_str.split(os.linesep):
87             match = self.pattern.search(line)
88             if match:
89                 my_expr, val = match.group(1), match.group(2)
90                 gl.append((my_expr, val))
91         #print("golden list:", gl)
92
93         # Now iterate through the golden list, comparing against the output from
94         # 'expression var'.
95         for my_expr, val in gl:
96
97             self.runCmd("expression %s" % my_expr)
98             output = self.res.GetOutput()
99
100             # The expression output must match the oracle.
101             self.expect(output, Msg(my_expr, val), exe=False,
102                         substrs=[val])