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