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    @skipIfLinux # llvm.org/pr14637: this test case fails sometimes because the input prompt "(lldb)" is missing
22    def test_with_dwarf_and_run_commands(self):
23        """Test convenience variables lldb.debugger, lldb.target, lldb.process, lldb.thread, and lldb.frame."""
24        self.buildDwarf()
25        self.convenience_variables()
26
27    def setUp(self):
28        # Call super's setUp().
29        TestBase.setUp(self)
30        # Find the line number to break on inside main.cpp.
31        self.line = line_number('main.c', 'Hello world.')
32
33    def convenience_variables(self):
34        """Test convenience variables lldb.debugger, lldb.target, lldb.process, lldb.thread, and lldb.frame."""
35        exe = os.path.join(os.getcwd(), "a.out")
36        prompt = "(lldb) "
37        python_prompt = ">>> "
38
39        # So that the child gets torn down after the test.
40        self.child = pexpect.spawn('%s %s %s' % (self.lldbHere, self.lldbOption, exe))
41        child = self.child
42        # Turn on logging for what the child sends back.
43        if self.TraceOn():
44            child.logfile_read = sys.stdout
45
46        # Set the breakpoint, run the inferior, when it breaks, issue print on
47        # the various convenience variables.
48        child.expect_exact(prompt)
49        child.sendline('breakpoint set -f main.c -l %d' % self.line)
50        child.expect_exact(prompt)
51        child.sendline('run')
52        child.expect_exact(prompt)
53        child.sendline('script')
54        child.expect_exact(python_prompt)
55
56        # Set a flag so that we know during teardown time, we need to exit the
57        # Python interpreter, then the lldb interpreter.
58        self.child_in_script_interpreter = True
59
60        child.sendline('print lldb.debugger')
61        child.expect_exact(python_prompt)
62        self.expect(child.before, exe=False,
63            patterns = ['Debugger \(instance: .*, id: \d\)'])
64
65        child.sendline('print lldb.target')
66        child.expect_exact(python_prompt)
67        self.expect(child.before, exe=False,
68            substrs = ['a.out'])
69
70        child.sendline('print lldb.process')
71        child.expect_exact(python_prompt)
72        self.expect(child.before, exe=False,
73            patterns = ['SBProcess: pid = \d+, state = stopped, threads = \d, executable = a.out'])
74
75        child.sendline('print lldb.thread')
76        child.expect_exact(python_prompt)
77        self.expect(child.before, exe=False,
78            patterns = ['SBThread: tid = 0x[0-9a-f]+'])
79
80        child.sendline('print lldb.frame')
81        child.expect_exact(python_prompt)
82        self.expect(child.before, exe=False,
83            substrs = ['frame #0', 'main.c:%d' % self.line])
84
85
86if __name__ == '__main__':
87    import atexit
88    lldb.SBDebugger.Initialize()
89    atexit.register(lambda: lldb.SBDebugger.Terminate())
90    unittest2.main()
91