summaryrefslogtreecommitdiffstats
path: root/lldb/test/functionalities/target_command/TestTargetCommand.py
blob: 600b19eca265bd6a7beff90aa46c73a167554f7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
Test some target commands: create, list, select, variable.
"""

import unittest2
import lldb
import sys
from lldbtest import *

class targetCommandTestCase(TestBase):

    mydir = os.path.join("functionalities", "target_command")

    def setUp(self):
        # Call super's setUp().
        TestBase.setUp(self)
        # Find the line numbers for our breakpoints.
        self.line_b = line_number('b.c', '// Set break point at this line.')
        self.line_c = line_number('c.c', '// Set break point at this line.')

    def test_target_command_with_dwarf(self):
        """Test some target commands: create, list, select."""
        da = {'C_SOURCES': 'a.c', 'EXE': 'a.out'}
        self.buildDwarf(dictionary=da)
        self.addTearDownCleanup(dictionary=da)

        db = {'C_SOURCES': 'b.c', 'EXE': 'b.out'}
        self.buildDwarf(dictionary=db)
        self.addTearDownCleanup(dictionary=db)

        dc = {'C_SOURCES': 'c.c', 'EXE': 'c.out'}
        self.buildDwarf(dictionary=dc)
        self.addTearDownCleanup(dictionary=dc)

        self.do_target_command()

    # rdar://problem/9763907
    # 'target variable' command fails if the target program has been run
    @unittest2.expectedFailure
    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
    def test_target_variable_command_with_dsym(self):
        """Test 'target variable' command before and after starting the inferior."""
        d = {'C_SOURCES': 'globals.c', 'EXE': 'globals'}
        self.buildDsym(dictionary=d)
        self.addTearDownCleanup(dictionary=d)

        self.do_target_variable_command('globals')

    def do_target_command(self):
        """Exercise 'target create', 'target list', 'target select' commands."""
        exe_a = os.path.join(os.getcwd(), "a.out")
        exe_b = os.path.join(os.getcwd(), "b.out")
        exe_c = os.path.join(os.getcwd(), "c.out")

        self.runCmd("target list")
        output = self.res.GetOutput()
        if output.startswith("No targets"):
            # We start from index 0.
            base = 0
        else:
            # Find the largest index of the existing list.
            import re
            pattern = re.compile("target #(\d+):")
            for line in reversed(output.split(os.linesep)):
                match = pattern.search(line)
                if match:
                    # We will start from (index + 1) ....
                    base = int(match.group(1), 10) + 1
                    #print "base is:", base
                    break;

        self.runCmd("target create " + exe_a, CURRENT_EXECUTABLE_SET)
        self.runCmd("run", RUN_SUCCEEDED)

        self.runCmd("target create " + exe_b, CURRENT_EXECUTABLE_SET)
        self.runCmd("breakpoint set -f %s -l %d" % ('b.c', self.line_b),
                    BREAKPOINT_CREATED)
        self.runCmd("run", RUN_SUCCEEDED)

        self.runCmd("target create " + exe_c, CURRENT_EXECUTABLE_SET)
        self.runCmd("breakpoint set -f %s -l %d" % ('c.c', self.line_c),
                    BREAKPOINT_CREATED)
        self.runCmd("run", RUN_SUCCEEDED)

        self.runCmd("target list")

        self.runCmd("target select %d" % base)
        self.runCmd("thread backtrace")

        self.runCmd("target select %d" % (base + 2))
        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
            substrs = ['c.c:%d' % self.line_c,
                       'stop reason = breakpoint'])

        self.runCmd("target select %d" % (base + 1))
        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
            substrs = ['b.c:%d' % self.line_b,
                       'stop reason = breakpoint'])

        self.runCmd("target list")

    def do_target_variable_command(self, exe_name):
        """Exercise 'target variable' command before and after starting the inferior."""
        self.runCmd("file " + exe_name, CURRENT_EXECUTABLE_SET)

        self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY,
            substrs = ["my_global_char", "'X'"])
        self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
            substrs = ['my_global_str', '"abc"'])
        self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY,
            substrs = ['my_static_int', '228'])

        self.runCmd("run")

        # rdar://problem/9763907
        # 'target variable' command fails if the target program has been run
        self.expect("target variable my_global_char", VARIABLES_DISPLAYED_CORRECTLY,
            substrs = ["my_global_char", "'X'"])
        self.expect("target variable my_global_str", VARIABLES_DISPLAYED_CORRECTLY,
            substrs = ['my_global_str', '"abc"'])
        self.expect("target variable my_static_int", VARIABLES_DISPLAYED_CORRECTLY,
            substrs = ['my_static_int', '228'])


if __name__ == '__main__':
    import atexit
    lldb.SBDebugger.Initialize()
    atexit.register(lambda: lldb.SBDebugger.Terminate())
    unittest2.main()
OpenPOWER on IntegriCloud