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")
14a3ed7d834b0e0c6924ac95629e740682bbcd15baJohnny Chen    @dsym_test
153a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    def test_with_dsym_and_run_command(self):
163a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        """Test convenience variables lldb.debugger, lldb.target, lldb.process, lldb.thread, and lldb.frame."""
173a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.buildDsym()
183a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.convenience_variables()
193a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
20a3ed7d834b0e0c6924ac95629e740682bbcd15baJohnny Chen    @dwarf_test
216bc4dcdfbcdfa455299d2123011b82a77351ee72Daniel Malea    @skipIfLinux # llvm.org/pr14637: this test case fails sometimes because the input prompt "(lldb)" is missing
2204427e19a34679967694e02cc1f44731faf8a909Johnny Chen    def test_with_dwarf_and_run_commands(self):
233a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        """Test convenience variables lldb.debugger, lldb.target, lldb.process, lldb.thread, and lldb.frame."""
243a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.buildDwarf()
253a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.convenience_variables()
263a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
273a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    def setUp(self):
283a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Call super's setUp().
293a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        TestBase.setUp(self)
303a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Find the line number to break on inside main.cpp.
313a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.line = line_number('main.c', 'Hello world.')
323a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
333a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    def convenience_variables(self):
343a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        """Test convenience variables lldb.debugger, lldb.target, lldb.process, lldb.thread, and lldb.frame."""
353a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        exe = os.path.join(os.getcwd(), "a.out")
363a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        prompt = "(lldb) "
373a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        python_prompt = ">>> "
383a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
393a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # So that the child gets torn down after the test.
407d7f447af7fba3eae442ab99c18f5ae532886099Johnny Chen        self.child = pexpect.spawn('%s %s %s' % (self.lldbHere, self.lldbOption, exe))
413a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child = self.child
423a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Turn on logging for what the child sends back.
433a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        if self.TraceOn():
443a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            child.logfile_read = sys.stdout
453a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
463a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Set the breakpoint, run the inferior, when it breaks, issue print on
473a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # the various convenience variables.
483a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(prompt)
493a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('breakpoint set -f main.c -l %d' % self.line)
503a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(prompt)
513a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('run')
523a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(prompt)
533a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('script')
543a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
553a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
563a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Set a flag so that we know during teardown time, we need to exit the
573a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        # Python interpreter, then the lldb interpreter.
583a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.child_in_script_interpreter = True
593a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
603a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('print lldb.debugger')
613a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
623a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.expect(child.before, exe=False,
633a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            patterns = ['Debugger \(instance: .*, id: \d\)'])
643a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
653a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('print lldb.target')
663a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
673a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.expect(child.before, exe=False,
683a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            substrs = ['a.out'])
693a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
703a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('print lldb.process')
713a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
723a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.expect(child.before, exe=False,
733a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            patterns = ['SBProcess: pid = \d+, state = stopped, threads = \d, executable = a.out'])
743a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
753a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('print lldb.thread')
763a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
773a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.expect(child.before, exe=False,
783a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            patterns = ['SBThread: tid = 0x[0-9a-f]+'])
793a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
803a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.sendline('print lldb.frame')
813a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        child.expect_exact(python_prompt)
823a4622ecd670637002852690a54bcaa9339909d8Johnny Chen        self.expect(child.before, exe=False,
833a4622ecd670637002852690a54bcaa9339909d8Johnny Chen            substrs = ['frame #0', 'main.c:%d' % self.line])
843a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
853a4622ecd670637002852690a54bcaa9339909d8Johnny Chen
863a4622ecd670637002852690a54bcaa9339909d8Johnny Chenif __name__ == '__main__':
873a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    import atexit
883a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    lldb.SBDebugger.Initialize()
893a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    atexit.register(lambda: lldb.SBDebugger.Terminate())
903a4622ecd670637002852690a54bcaa9339909d8Johnny Chen    unittest2.main()
91