TestTargetCommand.py revision 192865315e77294605d83a985663ab467270fed0
1"""
2Test some target commands: create, list, select, variable.
3"""
4
5import unittest2
6import lldb
7import sys
8from lldbtest import *
9
10class targetCommandTestCase(TestBase):
11
12    mydir = os.path.join("functionalities", "target_command")
13
14    def setUp(self):
15        # Call super's setUp().
16        TestBase.setUp(self)
17        # Find the line numbers for our breakpoints.
18        self.line_b = line_number('b.c', '// Set break point at this line.')
19        self.line_c = line_number('c.c', '// Set break point at this line.')
20
21    def test_target_command_with_dwarf(self):
22        """Test some target commands: create, list, select."""
23        da = {'C_SOURCES': 'a.c', 'EXE': 'a.out'}
24        self.buildDwarf(dictionary=da)
25        self.addTearDownCleanup(dictionary=da)
26
27        db = {'C_SOURCES': 'b.c', 'EXE': 'b.out'}
28        self.buildDwarf(dictionary=db)
29        self.addTearDownCleanup(dictionary=db)
30
31        dc = {'C_SOURCES': 'c.c', 'EXE': 'c.out'}
32        self.buildDwarf(dictionary=dc)
33        self.addTearDownCleanup(dictionary=dc)
34
35        self.do_target_command()
36
37    # rdar://problem/9763907
38    # 'target variable' command fails if the target program has been run
39    #@unittest2.expectedFailure
40    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
41    def test_target_variable_command_with_dsym(self):
42        """Test 'target variable' command before and after starting the inferior."""
43        d = {'C_SOURCES': 'globals.c', 'EXE': 'globals'}
44        self.buildDsym(dictionary=d)
45        self.addTearDownCleanup(dictionary=d)
46
47        self.do_target_variable_command('globals')
48
49    def do_target_command(self):
50        """Exercise 'target create', 'target list', 'target select' commands."""
51        exe_a = os.path.join(os.getcwd(), "a.out")
52        exe_b = os.path.join(os.getcwd(), "b.out")
53        exe_c = os.path.join(os.getcwd(), "c.out")
54
55        self.runCmd("target list")
56        output = self.res.GetOutput()
57        if output.startswith("No targets"):
58            # We start from index 0.
59            base = 0
60        else:
61            # Find the largest index of the existing list.
62            import re
63            pattern = re.compile("target #(\d+):")
64            for line in reversed(output.split(os.linesep)):
65                match = pattern.search(line)
66                if match:
67                    # We will start from (index + 1) ....
68                    base = int(match.group(1), 10) + 1
69                    #print "base is:", base
70                    break;
71
72        self.runCmd("target create " + exe_a, CURRENT_EXECUTABLE_SET)
73        self.runCmd("run", RUN_SUCCEEDED)
74
75        self.runCmd("target create " + exe_b, CURRENT_EXECUTABLE_SET)
76        self.runCmd("breakpoint set -f %s -l %d" % ('b.c', self.line_b),
77                    BREAKPOINT_CREATED)
78        self.runCmd("run", RUN_SUCCEEDED)
79
80        self.runCmd("target create " + exe_c, CURRENT_EXECUTABLE_SET)
81        self.runCmd("breakpoint set -f %s -l %d" % ('c.c', self.line_c),
82                    BREAKPOINT_CREATED)
83        self.runCmd("run", RUN_SUCCEEDED)
84
85        self.runCmd("target list")
86
87        self.runCmd("target select %d" % base)
88        self.runCmd("thread backtrace")
89
90        self.runCmd("target select %d" % (base + 2))
91        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
92            substrs = ['c.c:%d' % self.line_c,
93                       'stop reason = breakpoint'])
94
95        self.runCmd("target select %d" % (base + 1))
96        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
97            substrs = ['b.c:%d' % self.line_b,
98                       'stop reason = breakpoint'])
99
100        self.runCmd("target list")
101
102    def do_target_variable_command(self, exe_name):
103        """Exercise 'target variable' command before and after starting the inferior."""
104        self.runCmd("file " + exe_name, CURRENT_EXECUTABLE_SET)
105
106        self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY,
107            substrs = ["my_global_char", "'X'"])
108        self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
109            substrs = ['my_global_str', '"abc"'])
110
111        self.runCmd("run")
112
113        # rdar://problem/9763907
114        # 'target variable' command fails if the target program has been run
115        self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY,
116            substrs = ["my_global_char", "'X'"])
117        self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
118            substrs = ['my_global_str', '"abc"'])
119
120
121if __name__ == '__main__':
122    import atexit
123    lldb.SBDebugger.Initialize()
124    atexit.register(lambda: lldb.SBDebugger.Terminate())
125    unittest2.main()
126