TestConvenienceVariables.py revision 607914d2274f35335911d14ad4dcbce98f7e0511
13a4622ecd670637002852690a54bcaa9339909d8Johnny Chen"""Test convenience variables when you drop in from lldb prompt into an embedded interpreter."""
23a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
33a4622ecd670637002852690a54bcaa9339909d8Johnny Chenimport os
43a4622ecd670637002852690a54bcaa9339909d8Johnny Chenimport unittest2
53a4622ecd670637002852690a54bcaa9339909d8Johnny Chenimport lldb
63a4622ecd670637002852690a54bcaa9339909d8Johnny Chenimport pexpect
73a4622ecd670637002852690a54bcaa9339909d8Johnny Chenfrom lldbtest import *
83a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
93a4622ecd670637002852690a54bcaa9339909d8Johnny Chenclass ConvenienceVariablesCase(TestBase):
103a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
11607914d2274f35335911d14ad4dcbce98f7e0511Johnny Chen    mydir = os.path.join("functionalities", "embedded_interpreter")
123a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
133a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
143a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    def test_with_dsym_and_run_command(self):
153a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        """Test convenience variables lldb.debugger, lldb.target, lldb.process, lldb.thread, and lldb.frame."""
163a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.buildDsym()
173a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.convenience_variables()
183a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
1904427e19a34679967694e02cc1f44731faf8a909Johnny Chen    def test_with_dwarf_and_run_commands(self):
203a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        """Test convenience variables lldb.debugger, lldb.target, lldb.process, lldb.thread, and lldb.frame."""
213a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.buildDwarf()
223a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.convenience_variables()
233a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
243a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    def setUp(self):
253a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Call super's setUp().
263a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        TestBase.setUp(self)
273a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Find the line number to break on inside main.cpp.
283a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.line = line_number('main.c', 'Hello world.')
293a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
303a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    def convenience_variables(self):
313a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        """Test convenience variables lldb.debugger, lldb.target, lldb.process, lldb.thread, and lldb.frame."""
323a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        exe = os.path.join(os.getcwd(), "a.out")
333a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        prompt = "(lldb) "
343a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        python_prompt = ">>> "
353a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
363a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # So that the child gets torn down after the test.
373a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.child = pexpect.spawn('%s %s' % (self.lldbExec, exe))
383a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child = self.child
393a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Turn on logging for what the child sends back.
403a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        if self.TraceOn():
413a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            child.logfile_read = sys.stdout
423a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
433a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Set the breakpoint, run the inferior, when it breaks, issue print on
443a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # the various convenience variables.
453a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(prompt)
463a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('breakpoint set -f main.c -l %d' % self.line)
473a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(prompt)
483a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('run')
493a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(prompt)
503a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('script')
513a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
523a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
533a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Set a flag so that we know during teardown time, we need to exit the
543a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Python interpreter, then the lldb interpreter.
553a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.child_in_script_interpreter = True
563a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
573a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('print lldb.debugger')
583a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
593a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.expect(child.before, exe=False,
603a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            patterns = ['Debugger \(instance: .*, id: \d\)'])
613a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
623a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('print lldb.target')
633a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
643a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.expect(child.before, exe=False,
653a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            substrs = ['a.out'])
663a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
673a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('print lldb.process')
683a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
693a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.expect(child.before, exe=False,
703a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            patterns = ['SBProcess: pid = \d+, state = stopped, threads = \d, executable = a.out'])
713a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
723a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('print lldb.thread')
733a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
743a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.expect(child.before, exe=False,
753a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            patterns = ['SBThread: tid = 0x[0-9a-f]+'])
763a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
773a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('print lldb.frame')
783a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
793a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.expect(child.before, exe=False,
803a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            substrs = ['frame #0', 'main.c:%d' % self.line])
813a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
823a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
833a4622ecd670637002852690a54bcaa9339909d8Johnny Chenif __name__ == '__main__':
843a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    import atexit
853a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    lldb.SBDebugger.Initialize()
863a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    atexit.register(lambda: lldb.SBDebugger.Terminate())
873a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    unittest2.main()
88