blob: 1b1962f866f5d356cd51debd0a2746e77f63e5cb [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
17 def test_with_dsym(self):
18 """Exercise SBEvent APIs."""
19 self.buildDsym()
20 self.do_events()
21
22 @python_api_test
23 def test_with_dwarf(self):
24 """Exercise SBEvent APIs."""
25 self.buildDwarf()
26 self.do_events()
27
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
34 def do_events(self):
35 """Get the listener associated with the debugger and exercise some event APIs."""
36 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
63 # Get the debugger listenr.
64 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
88 # Use Python API to continue the process. The listening thread should be
89 # able to receive a state changed event.
90 self.process.Continue()
91
92 my_thread.join()
93 self.assertTrue(event.IsValid())
94
95
96if __name__ == '__main__':
97 import atexit
98 lldb.SBDebugger.Initialize()
99 atexit.register(lambda: lldb.SBDebugger.Terminate())
100 unittest2.main()