Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test lldb Python event APIs. |
| 3 | """ |
| 4 | |
| 5 | import os, time |
| 6 | import re |
| 7 | import unittest2 |
| 8 | import lldb, lldbutil |
| 9 | from lldbtest import * |
| 10 | |
| 11 | class EventAPITestCase(TestBase): |
| 12 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 13 | mydir = TestBase.compute_mydir(__file__) |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 14 | |
| 15 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 16 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 17 | @dsym_test |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 18 | def test_listen_for_and_print_event_with_dsym(self): |
| 19 | """Exercise SBEvent API.""" |
| 20 | self.buildDsym() |
| 21 | self.do_listen_for_and_print_event() |
| 22 | |
Siva Chandra | aaa3d47 | 2014-12-19 22:41:43 +0000 | [diff] [blame^] | 23 | @unittest2.skipUnless((sys.platform.startswith("darwin") or |
| 24 | sys.platform.startswith("freebsd")), |
| 25 | "requires Darwin or FreeBSD") |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 26 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 27 | @dwarf_test |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 28 | def test_listen_for_and_print_event_with_dwarf(self): |
| 29 | """Exercise SBEvent API.""" |
| 30 | self.buildDwarf() |
| 31 | self.do_listen_for_and_print_event() |
| 32 | |
| 33 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 34 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 35 | @dsym_test |
Johnny Chen | 3635eae | 2010-12-21 19:52:54 +0000 | [diff] [blame] | 36 | def test_wait_for_event_with_dsym(self): |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 37 | """Exercise SBListener.WaitForEvent() API.""" |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 38 | self.buildDsym() |
Johnny Chen | 3635eae | 2010-12-21 19:52:54 +0000 | [diff] [blame] | 39 | self.do_wait_for_event() |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 40 | |
Siva Chandra | aaa3d47 | 2014-12-19 22:41:43 +0000 | [diff] [blame^] | 41 | @skipIfLinux # non-core functionality, need to reenable and fix later (DES 2014.11.07) |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 42 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 43 | @dwarf_test |
Johnny Chen | 3635eae | 2010-12-21 19:52:54 +0000 | [diff] [blame] | 44 | def test_wait_for_event_with_dwarf(self): |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 45 | """Exercise SBListener.WaitForEvent() API.""" |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 46 | self.buildDwarf() |
Johnny Chen | 3635eae | 2010-12-21 19:52:54 +0000 | [diff] [blame] | 47 | self.do_wait_for_event() |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 48 | |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 49 | @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") |
| 50 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 51 | @dsym_test |
| 52 | def test_add_listener_to_broadcaster_with_dsym(self): |
Johnny Chen | 77c4697 | 2010-12-22 00:56:47 +0000 | [diff] [blame] | 53 | """Exercise some SBBroadcaster APIs.""" |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 54 | self.buildDsym() |
Johnny Chen | 77c4697 | 2010-12-22 00:56:47 +0000 | [diff] [blame] | 55 | self.do_add_listener_to_broadcaster() |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 56 | |
Ed Maste | 2ec8e1b | 2014-10-29 20:02:54 +0000 | [diff] [blame] | 57 | @skipIfFreeBSD # llvm.org/pr21325 |
Siva Chandra | aaa3d47 | 2014-12-19 22:41:43 +0000 | [diff] [blame^] | 58 | @skipIfLinux # non-core functionality, need to reenable and fix later (DES 2014.11.07) |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 59 | @python_api_test |
Johnny Chen | 24086bc | 2012-04-06 19:54:10 +0000 | [diff] [blame] | 60 | @dwarf_test |
| 61 | def test_add_listener_to_broadcaster_with_dwarf(self): |
Johnny Chen | 77c4697 | 2010-12-22 00:56:47 +0000 | [diff] [blame] | 62 | """Exercise some SBBroadcaster APIs.""" |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 63 | self.buildDwarf() |
Johnny Chen | 77c4697 | 2010-12-22 00:56:47 +0000 | [diff] [blame] | 64 | self.do_add_listener_to_broadcaster() |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 65 | |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 66 | def setUp(self): |
| 67 | # Call super's setUp(). |
| 68 | TestBase.setUp(self) |
| 69 | # Find the line number to of function 'c'. |
| 70 | self.line = line_number('main.c', '// Find the line number of function "c" here.') |
| 71 | |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 72 | def do_listen_for_and_print_event(self): |
| 73 | """Create a listener and use SBEvent API to print the events received.""" |
| 74 | exe = os.path.join(os.getcwd(), "a.out") |
| 75 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 76 | self.dbg.SetAsync(True) |
| 77 | |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 78 | # Create a target by the debugger. |
| 79 | target = self.dbg.CreateTarget(exe) |
| 80 | self.assertTrue(target, VALID_TARGET) |
| 81 | |
| 82 | # Now create a breakpoint on main.c by name 'c'. |
| 83 | breakpoint = target.BreakpointCreateByName('c', 'a.out') |
| 84 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 85 | listener = lldb.SBListener("my listener") |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 86 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 87 | # Now launch the process, and do not stop at the entry point. |
| 88 | error = lldb.SBError() |
| 89 | process = target.Launch (listener, |
| 90 | None, # argv |
| 91 | None, # envp |
| 92 | None, # stdin_path |
| 93 | None, # stdout_path |
| 94 | None, # stderr_path |
| 95 | None, # working directory |
| 96 | 0, # launch flags |
| 97 | False, # Stop at entry |
| 98 | error) # error |
| 99 | |
| 100 | self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED) |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 101 | |
| 102 | # Create an empty event object. |
| 103 | event = lldb.SBEvent() |
| 104 | |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 105 | traceOn = self.TraceOn() |
| 106 | if traceOn: |
| 107 | lldbutil.print_stacktraces(process) |
| 108 | |
| 109 | # Create MyListeningThread class to wait for any kind of event. |
| 110 | import threading |
| 111 | class MyListeningThread(threading.Thread): |
| 112 | def run(self): |
| 113 | count = 0 |
| 114 | # Let's only try at most 4 times to retrieve any kind of event. |
| 115 | # After that, the thread exits. |
| 116 | while not count > 3: |
| 117 | if traceOn: |
| 118 | print "Try wait for event..." |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 119 | if listener.WaitForEvent(5, event): |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 120 | if traceOn: |
| 121 | desc = lldbutil.get_description(event) |
| 122 | print "Event description:", desc |
| 123 | print "Event data flavor:", event.GetDataFlavor() |
| 124 | print "Process state:", lldbutil.state_type_to_str(process.GetState()) |
| 125 | print |
| 126 | else: |
| 127 | if traceOn: |
| 128 | print "timeout occurred waiting for event..." |
| 129 | count = count + 1 |
| 130 | return |
| 131 | |
| 132 | # Let's start the listening thread to retrieve the events. |
| 133 | my_thread = MyListeningThread() |
| 134 | my_thread.start() |
| 135 | |
| 136 | # Use Python API to continue the process. The listening thread should be |
| 137 | # able to receive the state changed events. |
| 138 | process.Continue() |
| 139 | |
| 140 | # Use Python API to kill the process. The listening thread should be |
| 141 | # able to receive the state changed event, too. |
| 142 | process.Kill() |
| 143 | |
| 144 | # Wait until the 'MyListeningThread' terminates. |
| 145 | my_thread.join() |
| 146 | |
Johnny Chen | 3635eae | 2010-12-21 19:52:54 +0000 | [diff] [blame] | 147 | def do_wait_for_event(self): |
| 148 | """Get the listener associated with the debugger and exercise WaitForEvent API.""" |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 149 | exe = os.path.join(os.getcwd(), "a.out") |
| 150 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 151 | self.dbg.SetAsync(True) |
| 152 | |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 153 | # Create a target by the debugger. |
| 154 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 155 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 156 | |
| 157 | # Now create a breakpoint on main.c by name 'c'. |
| 158 | breakpoint = target.BreakpointCreateByName('c', 'a.out') |
| 159 | #print "breakpoint:", breakpoint |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 160 | self.assertTrue(breakpoint and |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 161 | breakpoint.GetNumLocations() == 1, |
| 162 | VALID_BREAKPOINT) |
| 163 | |
Johnny Chen | d762ff1 | 2011-02-03 23:15:53 +0000 | [diff] [blame] | 164 | # Get the debugger listener. |
| 165 | listener = self.dbg.GetListener() |
| 166 | |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 167 | # Now launch the process, and do not stop at entry point. |
Greg Clayton | 6f907e6 | 2011-01-23 17:46:22 +0000 | [diff] [blame] | 168 | error = lldb.SBError() |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 169 | process = target.Launch (listener, |
| 170 | None, # argv |
| 171 | None, # envp |
| 172 | None, # stdin_path |
| 173 | None, # stdout_path |
| 174 | None, # stderr_path |
| 175 | None, # working directory |
| 176 | 0, # launch flags |
| 177 | False, # Stop at entry |
| 178 | error) # error |
Johnny Chen | 2494f55 | 2011-07-20 00:14:20 +0000 | [diff] [blame] | 179 | self.assertTrue(error.Success() and process, PROCESS_IS_VALID) |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 180 | |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 181 | # Create an empty event object. |
| 182 | event = lldb.SBEvent() |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 183 | self.assertFalse(event, "Event should not be valid initially") |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 184 | |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 185 | # Create MyListeningThread to wait for any kind of event. |
| 186 | import threading |
| 187 | class MyListeningThread(threading.Thread): |
| 188 | def run(self): |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 189 | count = 0 |
| 190 | # Let's only try at most 3 times to retrieve any kind of event. |
| 191 | while not count > 3: |
| 192 | if listener.WaitForEvent(5, event): |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 193 | #print "Got a valid event:", event |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 194 | #print "Event data flavor:", event.GetDataFlavor() |
| 195 | #print "Event type:", lldbutil.state_type_to_str(event.GetType()) |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 196 | return |
| 197 | count = count + 1 |
| 198 | print "Timeout: listener.WaitForEvent" |
| 199 | |
| 200 | return |
| 201 | |
Johnny Chen | 4f8caab | 2010-12-21 05:43:37 +0000 | [diff] [blame] | 202 | # Use Python API to kill the process. The listening thread should be |
| 203 | # able to receive a state changed event. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 204 | process.Kill() |
Johnny Chen | 4f8caab | 2010-12-21 05:43:37 +0000 | [diff] [blame] | 205 | |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 206 | # Let's start the listening thread to retrieve the event. |
| 207 | my_thread = MyListeningThread() |
| 208 | my_thread.start() |
| 209 | |
Johnny Chen | 4f8caab | 2010-12-21 05:43:37 +0000 | [diff] [blame] | 210 | # Wait until the 'MyListeningThread' terminates. |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 211 | my_thread.join() |
Johnny Chen | 4f8caab | 2010-12-21 05:43:37 +0000 | [diff] [blame] | 212 | |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 213 | self.assertTrue(event, |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 214 | "My listening thread successfully received an event") |
Johnny Chen | 4f8caab | 2010-12-21 05:43:37 +0000 | [diff] [blame] | 215 | |
Johnny Chen | 77c4697 | 2010-12-22 00:56:47 +0000 | [diff] [blame] | 216 | def do_add_listener_to_broadcaster(self): |
| 217 | """Get the broadcaster associated with the process and wait for broadcaster events.""" |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 218 | exe = os.path.join(os.getcwd(), "a.out") |
| 219 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 220 | self.dbg.SetAsync(True) |
| 221 | |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 222 | # Create a target by the debugger. |
| 223 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 224 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 225 | |
| 226 | # Now create a breakpoint on main.c by name 'c'. |
| 227 | breakpoint = target.BreakpointCreateByName('c', 'a.out') |
| 228 | #print "breakpoint:", breakpoint |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 229 | self.assertTrue(breakpoint and |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 230 | breakpoint.GetNumLocations() == 1, |
| 231 | VALID_BREAKPOINT) |
| 232 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 233 | listener = lldb.SBListener("my listener") |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 234 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 235 | # Now launch the process, and do not stop at the entry point. |
| 236 | error = lldb.SBError() |
| 237 | process = target.Launch (listener, |
| 238 | None, # argv |
| 239 | None, # envp |
| 240 | None, # stdin_path |
| 241 | None, # stdout_path |
| 242 | None, # stderr_path |
| 243 | None, # working directory |
| 244 | 0, # launch flags |
| 245 | False, # Stop at entry |
| 246 | error) # error |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 247 | |
| 248 | # Create an empty event object. |
| 249 | event = lldb.SBEvent() |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 250 | self.assertFalse(event, "Event should not be valid initially") |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 251 | |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 252 | |
| 253 | # The finite state machine for our custom listening thread, with an |
| 254 | # initail state of 0, which means a "running" event is expected. |
| 255 | # It changes to 1 after "running" is received. |
| 256 | # It cahnges to 2 after "stopped" is received. |
| 257 | # 2 will be our final state and the test is complete. |
| 258 | self.state = 0 |
| 259 | |
| 260 | # Create MyListeningThread to wait for state changed events. |
| 261 | # By design, a "running" event is expected following by a "stopped" event. |
| 262 | import threading |
| 263 | class MyListeningThread(threading.Thread): |
| 264 | def run(self): |
| 265 | #print "Running MyListeningThread:", self |
| 266 | |
| 267 | # Regular expression pattern for the event description. |
| 268 | pattern = re.compile("data = {.*, state = (.*)}$") |
| 269 | |
| 270 | # Let's only try at most 6 times to retrieve our events. |
| 271 | count = 0 |
| 272 | while True: |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 273 | if listener.WaitForEvent(5, event): |
Johnny Chen | 9ae9820 | 2011-04-23 00:34:56 +0000 | [diff] [blame] | 274 | desc = lldbutil.get_description(event) |
| 275 | #print "Event description:", desc |
| 276 | match = pattern.search(desc) |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 277 | if not match: |
| 278 | break; |
| 279 | if self.context.state == 0 and match.group(1) == 'running': |
| 280 | self.context.state = 1 |
| 281 | continue |
| 282 | elif self.context.state == 1 and match.group(1) == 'stopped': |
| 283 | # Whoopee, both events have been received! |
| 284 | self.context.state = 2 |
| 285 | break |
| 286 | else: |
| 287 | break |
| 288 | print "Timeout: listener.WaitForEvent" |
| 289 | count = count + 1 |
| 290 | if count > 6: |
| 291 | break |
| 292 | |
| 293 | return |
| 294 | |
| 295 | # Use Python API to continue the process. The listening thread should be |
| 296 | # able to receive the state changed events. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 297 | process.Continue() |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 298 | |
| 299 | # Start the listening thread to receive the "running" followed by the |
| 300 | # "stopped" events. |
| 301 | my_thread = MyListeningThread() |
| 302 | # Supply the enclosing context so that our listening thread can access |
| 303 | # the 'state' variable. |
| 304 | my_thread.context = self |
| 305 | my_thread.start() |
| 306 | |
| 307 | # Wait until the 'MyListeningThread' terminates. |
| 308 | my_thread.join() |
| 309 | |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 310 | # The final judgement. :-) |
| 311 | self.assertTrue(self.state == 2, |
| 312 | "Both expected state changed events received") |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 313 | |
| 314 | |
| 315 | if __name__ == '__main__': |
| 316 | import atexit |
| 317 | lldb.SBDebugger.Initialize() |
| 318 | atexit.register(lambda: lldb.SBDebugger.Terminate()) |
| 319 | unittest2.main() |