1"""
2Test some lldb command abbreviations.
3"""
4import commands
5import lldb
6import os
7import time
8import unittest2
9from lldbtest import *
10import lldbutil
11
12def execute_command (command):
13    #print '%% %s' % (command)
14    (exit_status, output) = commands.getstatusoutput (command)
15    #if output:
16    #    print output
17    #print 'status = %u' % (exit_status)
18    return exit_status
19
20class ExecTestCase(TestBase):
21
22    mydir = os.path.join("functionalities", "exec")
23
24
25    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
26    @dsym_test
27    def test_with_dsym (self):
28        if self.getArchitecture() == 'x86_64':
29            source = os.path.join (os.getcwd(), "main.cpp")
30            o_file = os.path.join (os.getcwd(), "main.o")
31            execute_command ("'%s' -g -O0 -arch i386 -arch x86_64 '%s' -c -o '%s'" % (os.environ["CC"], source, o_file))
32            execute_command ("'%s' -g -O0 -arch i386 -arch x86_64 '%s'" % (os.environ["CC"], o_file))
33        else:
34            self.buildDsym()
35        self.do_test ()
36
37
38    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
39    @dwarf_test
40    def test_with_dwarf (self):
41        if self.getArchitecture() == 'x86_64':
42            source = os.path.join (os.getcwd(), "main.cpp")
43            o_file = os.path.join (os.getcwd(), "main.o")
44            dsym_path = os.path.join (os.getcwd(), "a.out.dSYM")
45            execute_command ("'%s' -g -O0 -arch i386 -arch x86_64 '%s' -c -o '%s'" % (os.environ["CC"], source, o_file))
46            execute_command ("'%s' -g -O0 -arch i386 -arch x86_64 '%s'" % (os.environ["CC"], o_file))
47            execute_command ("rm -rf '%s'" % (dsym_path))
48        else:
49            self.buildDwarf()
50        self.do_test ()
51
52    def do_test (self):
53        exe = os.path.join (os.getcwd(), "a.out")
54
55        # Create the target
56        target = self.dbg.CreateTarget(exe)
57
58        # Create any breakpoints we need
59        breakpoint = target.BreakpointCreateBySourceRegex ('Set breakpoint 1 here', lldb.SBFileSpec ("main.cpp", False))
60        self.assertTrue(breakpoint, VALID_BREAKPOINT)
61
62        # Launch the process
63        process = target.LaunchSimple(None, None, os.getcwd())
64        self.assertTrue(process, PROCESS_IS_VALID)
65
66        for i in range(6):
67            # The stop reason of the thread should be breakpoint.
68            self.assertTrue(process.GetState() == lldb.eStateStopped,
69                            STOPPED_DUE_TO_BREAKPOINT)
70
71            thread = process.GetThreadAtIndex (0)
72
73            self.assertTrue (thread.IsValid(),
74                             "Process stopped at 'main' should have a valid thread");
75
76            stop_reason = thread.GetStopReason()
77
78            self.assertTrue (stop_reason == lldb.eStopReasonBreakpoint,
79                             "Thread in process stopped in 'main' should have a stop reason of eStopReasonBreakpoint");
80
81            # Run and we should stop due to exec
82            process.Continue()
83
84            self.assertTrue(process.GetState() == lldb.eStateStopped,
85                            "Process should be stopped at __dyld_start")
86
87            thread = process.GetThreadAtIndex (0)
88
89            self.assertTrue (thread.IsValid(),
90                             "Process stopped at exec should have a valid thread");
91
92            stop_reason = thread.GetStopReason()
93
94            self.assertTrue (stop_reason == lldb.eStopReasonExec,
95                             "Thread in process stopped on exec should have a stop reason of eStopReasonExec");
96
97             # Run and we should stop at breakpoint in main after exec
98            process.Continue()
99
100if __name__ == '__main__':
101    import atexit
102    lldb.SBDebugger.Initialize()
103    atexit.register(lambda: lldb.SBDebugger.Terminate())
104    unittest2.main()
105
106