blob: 2598b92e5668508c6864c062ff5a0ac076d2aa1b [file] [log] [blame]
Johnny Chenf667ab52010-12-21 02:06:56 +00001"""
2Test lldb Python event APIs.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb, lldbutil
9from lldbtest import *
10
11class EventAPITestCase(TestBase):
12
13 mydir = os.path.join("python_api", "event")
14
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16 @python_api_test
Johnny Chen3635eae2010-12-21 19:52:54 +000017 def test_wait_for_event_with_dsym(self):
18 """Exercise SBListener.WaitForEvent() APIs."""
Johnny Chenf667ab52010-12-21 02:06:56 +000019 self.buildDsym()
Johnny Chen3635eae2010-12-21 19:52:54 +000020 self.do_wait_for_event()
Johnny Chenf667ab52010-12-21 02:06:56 +000021
22 @python_api_test
Johnny Chen3635eae2010-12-21 19:52:54 +000023 def test_wait_for_event_with_dwarf(self):
24 """Exercise SBListener.WaitForEvent() APIs."""
Johnny Chenf667ab52010-12-21 02:06:56 +000025 self.buildDwarf()
Johnny Chen3635eae2010-12-21 19:52:54 +000026 self.do_wait_for_event()
Johnny Chenf667ab52010-12-21 02:06:56 +000027
28 def setUp(self):
29 # Call super's setUp().
30 TestBase.setUp(self)
31 # Find the line number to of function 'c'.
32 self.line = line_number('main.c', '// Find the line number of function "c" here.')
33
Johnny Chen3635eae2010-12-21 19:52:54 +000034 def do_wait_for_event(self):
35 """Get the listener associated with the debugger and exercise WaitForEvent API."""
Johnny Chenf667ab52010-12-21 02:06:56 +000036 exe = os.path.join(os.getcwd(), "a.out")
37
38 # Create a target by the debugger.
39 target = self.dbg.CreateTarget(exe)
40 self.assertTrue(target.IsValid(), VALID_TARGET)
41
42 # Now create a breakpoint on main.c by name 'c'.
43 breakpoint = target.BreakpointCreateByName('c', 'a.out')
44 #print "breakpoint:", breakpoint
45 self.assertTrue(breakpoint.IsValid() and
46 breakpoint.GetNumLocations() == 1,
47 VALID_BREAKPOINT)
48
49 # Now launch the process, and do not stop at entry point.
50 self.process = target.LaunchProcess([''], [''], os.ctermid(), 0, False)
51
52 self.process = target.GetProcess()
53 self.assertTrue(self.process.IsValid(), PROCESS_IS_VALID)
54
55 # Get a handle on the process's broadcaster.
56 broadcaster = self.process.GetBroadcaster()
57 self.assertTrue(broadcaster.IsValid(), "Process with valid broadcaster")
58
59 # Create an empty event object.
60 event = lldb.SBEvent()
61 self.assertFalse(event.IsValid(), "Event should not be valid initially")
62
Johnny Chen0b0c5782010-12-21 02:10:18 +000063 # Get the debugger listener.
Johnny Chenf667ab52010-12-21 02:06:56 +000064 listener = self.dbg.GetListener()
65
66 # Create MyListeningThread to wait for any kind of event.
67 import threading
68 class MyListeningThread(threading.Thread):
69 def run(self):
70 print "Running MyListeningThread:", self
71 count = 0
72 # Let's only try at most 3 times to retrieve any kind of event.
73 while not count > 3:
74 if listener.WaitForEvent(5, event):
75 print "Got a valid event:", event
76 print "Event type:", event.GetType()
77 print "Event broadcaster:", event.GetBroadcaster().GetName()
78 return
79 count = count + 1
80 print "Timeout: listener.WaitForEvent"
81
82 return
83
84 # Let's start the listening thread before we launch the inferior process.
85 my_thread = MyListeningThread()
86 my_thread.start()
87
Johnny Chen4f8caab2010-12-21 05:43:37 +000088 # Set the debugger to be in asynchronous mode since our listening thread
89 # is waiting for events to come.
90 self.dbg.SetAsync(True)
Johnny Chenf667ab52010-12-21 02:06:56 +000091
Johnny Chen4f8caab2010-12-21 05:43:37 +000092 # Use Python API to kill the process. The listening thread should be
93 # able to receive a state changed event.
94 self.process.Kill()
95
96 # Wait until the 'MyListeningThread' terminates.
Johnny Chenf667ab52010-12-21 02:06:56 +000097 my_thread.join()
Johnny Chen4f8caab2010-12-21 05:43:37 +000098
99 # Restore the original synchronous mode.
100 self.dbg.SetAsync(False)
101
Johnny Chenf667ab52010-12-21 02:06:56 +0000102 self.assertTrue(event.IsValid())
103
104
105if __name__ == '__main__':
106 import atexit
107 lldb.SBDebugger.Initialize()
108 atexit.register(lambda: lldb.SBDebugger.Terminate())
109 unittest2.main()