TestConvenienceVariables.py revision db7e5006ee42ee569ddc4a708dae0d9f095bdf30
1"""Test convenience variables when you drop in from lldb prompt into an embedded interpreter."""
2
3import os
4import unittest2
5import lldb
6import pexpect
7from lldbtest import *
8
9class ConvenienceVariablesCase(TestBase):
10
11    mydir = os.path.join("functionalities", "embedded_interpreter")
12
13    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
14    @dsym_test
15    def test_with_dsym_and_run_command(self):
16        """Test convenience variables lldb.debugger, lldb.target, lldb.process, lldb.thread, and lldb.frame."""
17        self.buildDsym()
18        self.convenience_variables()
19
20    @dwarf_test
21    def test_with_dwarf_and_run_commands(self):
22        """Test convenience variables lldb.debugger, lldb.target, lldb.process, lldb.thread, and lldb.frame."""
23        self.buildDwarf()
24        self.convenience_variables()
25
26    def setUp(self):
27        # Call super's setUp().
28        TestBase.setUp(self)
29        # Find the line number to break on inside main.cpp.
30        self.line = line_number('main.c', 'Hello world.')
31
32    def convenience_variables(self):
33        """Test convenience variables lldb.debugger, lldb.target, lldb.process, lldb.thread, and lldb.frame."""
34        exe = os.path.join(os.getcwd(), "a.out")
35        prompt = "(lldb) "
36        python_prompt = ">>> "
37
38        # So that the child gets torn down after the test.
39        self.child = pexpect.spawn('%s %s %s' % (self.lldbHere, self.lldbOption, exe))
40        child = self.child
41        # Turn on logging for what the child sends back.
42        if self.TraceOn():
43            child.logfile_read = sys.stdout
44
45        # Set the breakpoint, run the inferior, when it breaks, issue print on
46        # the various convenience variables.
47        child.expect_exact(prompt)
48        child.sendline('breakpoint set -f main.c -l %d' % self.line)
49        child.expect_exact(prompt)
50        child.sendline('run')
51        child.expect_exact(prompt)
52        child.sendline('script')
53        child.expect_exact(python_prompt)
54
55        # Set a flag so that we know during teardown time, we need to exit the
56        # Python interpreter, then the lldb interpreter.
57        self.child_in_script_interpreter = True
58
59        child.sendline('print lldb.debugger')
60        child.expect_exact(python_prompt)
61        self.expect(child.before, exe=False,
62            patterns = ['Debugger \(instance: .*, id: \d\)'])
63
64        child.sendline('print lldb.target')
65        child.expect_exact(python_prompt)
66        self.expect(child.before, exe=False,
67            substrs = ['a.out'])
68
69        child.sendline('print lldb.process')
70        child.expect_exact(python_prompt)
71        self.expect(child.before, exe=False,
72            patterns = ['SBProcess: pid = \d+, state = stopped, threads = \d, executable = a.out'])
73
74        child.sendline('print lldb.thread')
75        child.expect_exact(python_prompt)
76        self.expect(child.before, exe=False,
77            patterns = ['SBThread: tid = 0x[0-9a-f]+'])
78
79        child.sendline('print lldb.frame')
80        child.expect_exact(python_prompt)
81        self.expect(child.before, exe=False,
82            substrs = ['frame #0', 'main.c:%d' % self.line])
83
84
85if __name__ == '__main__':
86    import atexit
87    lldb.SBDebugger.Initialize()
88    atexit.register(lambda: lldb.SBDebugger.Terminate())
89    unittest2.main()
90