TestTargetCommand.py revision e2ac6dec08024429fcc8ef1646a435a732e5e94d
1"""
2Test some target commands: create, list, select.
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.buildDefault(dictionary=da)
25        self.addTearDownCleanup(dictionary=da)
26
27        db = {'C_SOURCES': 'b.c', 'EXE': 'b.out'}
28        self.buildDefault(dictionary=db)
29        self.addTearDownCleanup(dictionary=db)
30
31        dc = {'C_SOURCES': 'c.c', 'EXE': 'c.out'}
32        self.buildDefault(dictionary=dc)
33        self.addTearDownCleanup(dictionary=dc)
34
35        self.do_target_command()
36
37    def do_target_command(self):
38        """Exercise 'target create', 'target list', 'target select' commands."""
39        exe_a = os.path.join(os.getcwd(), "a.out")
40        exe_b = os.path.join(os.getcwd(), "b.out")
41        exe_c = os.path.join(os.getcwd(), "c.out")
42
43        self.runCmd("target list")
44        output = self.res.GetOutput()
45        if output.startswith("No targets"):
46            # We start from index 0.
47            base = 0
48        else:
49            # Find the largest index of the existing list.
50            import re
51            pattern = re.compile("target #(\d+):")
52            for line in reversed(output.split(os.linesep)):
53                match = pattern.search(line)
54                if match:
55                    # We will start from (index + 1) ....
56                    base = int(match.group(1), 10) + 1
57                    #print "base is:", base
58                    break;
59
60        self.runCmd("target create " + exe_a, CURRENT_EXECUTABLE_SET)
61        self.runCmd("run", RUN_SUCCEEDED)
62
63        self.runCmd("target create " + exe_b, CURRENT_EXECUTABLE_SET)
64        self.runCmd("breakpoint set -f %s -l %d" % ('b.c', self.line_b),
65                    BREAKPOINT_CREATED)
66        self.runCmd("run", RUN_SUCCEEDED)
67
68        self.runCmd("target create " + exe_c, CURRENT_EXECUTABLE_SET)
69        self.runCmd("breakpoint set -f %s -l %d" % ('c.c', self.line_c),
70                    BREAKPOINT_CREATED)
71        self.runCmd("run", RUN_SUCCEEDED)
72
73        self.runCmd("target list")
74
75        self.runCmd("target select %d" % base)
76        self.runCmd("thread backtrace")
77
78        self.runCmd("target select %d" % (base + 2))
79        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
80            substrs = ['c.c:%d' % self.line_c,
81                       'stop reason = breakpoint'])
82
83        self.runCmd("target select %d" % (base + 1))
84        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
85            substrs = ['b.c:%d' % self.line_b,
86                       'stop reason = breakpoint'])
87
88        self.runCmd("target list")
89
90
91if __name__ == '__main__':
92    import atexit
93    lldb.SBDebugger.Initialize()
94    atexit.register(lambda: lldb.SBDebugger.Terminate())
95    unittest2.main()
96