Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test lldb Python event APIs. |
| 3 | """ |
| 4 | |
Zachary Turner | 77db4a8 | 2015-10-22 20:06:20 +0000 | [diff] [blame^] | 5 | import lldb_shared |
| 6 | |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 7 | import os, time |
| 8 | import re |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 9 | import lldb, lldbutil |
| 10 | from lldbtest import * |
| 11 | |
| 12 | class EventAPITestCase(TestBase): |
| 13 | |
Greg Clayton | 4570d3e | 2013-12-10 23:19:29 +0000 | [diff] [blame] | 14 | mydir = TestBase.compute_mydir(__file__) |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 15 | |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 16 | def setUp(self): |
| 17 | # Call super's setUp(). |
| 18 | TestBase.setUp(self) |
| 19 | # Find the line number to of function 'c'. |
| 20 | self.line = line_number('main.c', '// Find the line number of function "c" here.') |
| 21 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 22 | @python_api_test |
| 23 | @expectedFailureLinux("llvm.org/pr23730") # Flaky, fails ~1/10 cases |
| 24 | @skipIfLinux # skip to avoid crashes |
| 25 | def test_listen_for_and_print_event(self): |
| 26 | """Exercise SBEvent API.""" |
| 27 | self.build() |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 28 | exe = os.path.join(os.getcwd(), "a.out") |
| 29 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 30 | self.dbg.SetAsync(True) |
| 31 | |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 32 | # Create a target by the debugger. |
| 33 | target = self.dbg.CreateTarget(exe) |
| 34 | self.assertTrue(target, VALID_TARGET) |
| 35 | |
| 36 | # Now create a breakpoint on main.c by name 'c'. |
| 37 | breakpoint = target.BreakpointCreateByName('c', 'a.out') |
| 38 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 39 | listener = lldb.SBListener("my listener") |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 40 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 41 | # Now launch the process, and do not stop at the entry point. |
| 42 | error = lldb.SBError() |
| 43 | process = target.Launch (listener, |
| 44 | None, # argv |
| 45 | None, # envp |
| 46 | None, # stdin_path |
| 47 | None, # stdout_path |
| 48 | None, # stderr_path |
| 49 | None, # working directory |
| 50 | 0, # launch flags |
| 51 | False, # Stop at entry |
| 52 | error) # error |
| 53 | |
| 54 | self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED) |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 55 | |
| 56 | # Create an empty event object. |
| 57 | event = lldb.SBEvent() |
| 58 | |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 59 | traceOn = self.TraceOn() |
| 60 | if traceOn: |
| 61 | lldbutil.print_stacktraces(process) |
| 62 | |
| 63 | # Create MyListeningThread class to wait for any kind of event. |
| 64 | import threading |
| 65 | class MyListeningThread(threading.Thread): |
| 66 | def run(self): |
| 67 | count = 0 |
| 68 | # Let's only try at most 4 times to retrieve any kind of event. |
| 69 | # After that, the thread exits. |
| 70 | while not count > 3: |
| 71 | if traceOn: |
| 72 | print "Try wait for event..." |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 73 | if listener.WaitForEvent(5, event): |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 74 | if traceOn: |
| 75 | desc = lldbutil.get_description(event) |
| 76 | print "Event description:", desc |
| 77 | print "Event data flavor:", event.GetDataFlavor() |
| 78 | print "Process state:", lldbutil.state_type_to_str(process.GetState()) |
| 79 | print |
| 80 | else: |
| 81 | if traceOn: |
| 82 | print "timeout occurred waiting for event..." |
| 83 | count = count + 1 |
| 84 | return |
| 85 | |
| 86 | # Let's start the listening thread to retrieve the events. |
| 87 | my_thread = MyListeningThread() |
| 88 | my_thread.start() |
| 89 | |
| 90 | # Use Python API to continue the process. The listening thread should be |
| 91 | # able to receive the state changed events. |
| 92 | process.Continue() |
| 93 | |
| 94 | # Use Python API to kill the process. The listening thread should be |
| 95 | # able to receive the state changed event, too. |
| 96 | process.Kill() |
| 97 | |
| 98 | # Wait until the 'MyListeningThread' terminates. |
| 99 | my_thread.join() |
| 100 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 101 | @python_api_test |
| 102 | def test_wait_for_event(self): |
| 103 | """Exercise SBListener.WaitForEvent() API.""" |
| 104 | self.build() |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 105 | exe = os.path.join(os.getcwd(), "a.out") |
| 106 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 107 | self.dbg.SetAsync(True) |
| 108 | |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 109 | # Create a target by the debugger. |
| 110 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 111 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 112 | |
| 113 | # Now create a breakpoint on main.c by name 'c'. |
| 114 | breakpoint = target.BreakpointCreateByName('c', 'a.out') |
| 115 | #print "breakpoint:", breakpoint |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 116 | self.assertTrue(breakpoint and |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 117 | breakpoint.GetNumLocations() == 1, |
| 118 | VALID_BREAKPOINT) |
| 119 | |
Johnny Chen | d762ff1 | 2011-02-03 23:15:53 +0000 | [diff] [blame] | 120 | # Get the debugger listener. |
| 121 | listener = self.dbg.GetListener() |
| 122 | |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 123 | # Now launch the process, and do not stop at entry point. |
Greg Clayton | 6f907e6 | 2011-01-23 17:46:22 +0000 | [diff] [blame] | 124 | error = lldb.SBError() |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 125 | process = target.Launch (listener, |
| 126 | None, # argv |
| 127 | None, # envp |
| 128 | None, # stdin_path |
| 129 | None, # stdout_path |
| 130 | None, # stderr_path |
| 131 | None, # working directory |
| 132 | 0, # launch flags |
| 133 | False, # Stop at entry |
| 134 | error) # error |
Johnny Chen | 2494f55 | 2011-07-20 00:14:20 +0000 | [diff] [blame] | 135 | self.assertTrue(error.Success() and process, PROCESS_IS_VALID) |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 136 | |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 137 | # Create an empty event object. |
| 138 | event = lldb.SBEvent() |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 139 | self.assertFalse(event, "Event should not be valid initially") |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 140 | |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 141 | # Create MyListeningThread to wait for any kind of event. |
| 142 | import threading |
| 143 | class MyListeningThread(threading.Thread): |
| 144 | def run(self): |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 145 | count = 0 |
| 146 | # Let's only try at most 3 times to retrieve any kind of event. |
| 147 | while not count > 3: |
| 148 | if listener.WaitForEvent(5, event): |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 149 | #print "Got a valid event:", event |
Johnny Chen | 3a709ac | 2011-07-08 23:02:33 +0000 | [diff] [blame] | 150 | #print "Event data flavor:", event.GetDataFlavor() |
| 151 | #print "Event type:", lldbutil.state_type_to_str(event.GetType()) |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 152 | return |
| 153 | count = count + 1 |
| 154 | print "Timeout: listener.WaitForEvent" |
| 155 | |
| 156 | return |
| 157 | |
Johnny Chen | 4f8caab | 2010-12-21 05:43:37 +0000 | [diff] [blame] | 158 | # Use Python API to kill the process. The listening thread should be |
| 159 | # able to receive a state changed event. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 160 | process.Kill() |
Johnny Chen | 4f8caab | 2010-12-21 05:43:37 +0000 | [diff] [blame] | 161 | |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 162 | # Let's start the listening thread to retrieve the event. |
| 163 | my_thread = MyListeningThread() |
| 164 | my_thread.start() |
| 165 | |
Johnny Chen | 4f8caab | 2010-12-21 05:43:37 +0000 | [diff] [blame] | 166 | # Wait until the 'MyListeningThread' terminates. |
Johnny Chen | f667ab5 | 2010-12-21 02:06:56 +0000 | [diff] [blame] | 167 | my_thread.join() |
Johnny Chen | 4f8caab | 2010-12-21 05:43:37 +0000 | [diff] [blame] | 168 | |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 169 | self.assertTrue(event, |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 170 | "My listening thread successfully received an event") |
Johnny Chen | 4f8caab | 2010-12-21 05:43:37 +0000 | [diff] [blame] | 171 | |
Tamas Berghammer | c8fd130 | 2015-09-30 10:12:40 +0000 | [diff] [blame] | 172 | @skipIfFreeBSD # llvm.org/pr21325 |
| 173 | @python_api_test |
| 174 | @expectedFlakeyLinux("llvm.org/pr23617") # Flaky, fails ~1/10 cases |
| 175 | @expectedFailureWindows("llvm.org/pr24778") |
| 176 | def test_add_listener_to_broadcaster(self): |
| 177 | """Exercise some SBBroadcaster APIs.""" |
| 178 | self.build() |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 179 | exe = os.path.join(os.getcwd(), "a.out") |
| 180 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 181 | self.dbg.SetAsync(True) |
| 182 | |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 183 | # Create a target by the debugger. |
| 184 | target = self.dbg.CreateTarget(exe) |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 185 | self.assertTrue(target, VALID_TARGET) |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 186 | |
| 187 | # Now create a breakpoint on main.c by name 'c'. |
| 188 | breakpoint = target.BreakpointCreateByName('c', 'a.out') |
| 189 | #print "breakpoint:", breakpoint |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 190 | self.assertTrue(breakpoint and |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 191 | breakpoint.GetNumLocations() == 1, |
| 192 | VALID_BREAKPOINT) |
| 193 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 194 | listener = lldb.SBListener("my listener") |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 195 | |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 196 | # Now launch the process, and do not stop at the entry point. |
| 197 | error = lldb.SBError() |
| 198 | process = target.Launch (listener, |
| 199 | None, # argv |
| 200 | None, # envp |
| 201 | None, # stdin_path |
| 202 | None, # stdout_path |
| 203 | None, # stderr_path |
| 204 | None, # working directory |
| 205 | 0, # launch flags |
| 206 | False, # Stop at entry |
| 207 | error) # error |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 208 | |
| 209 | # Create an empty event object. |
| 210 | event = lldb.SBEvent() |
Johnny Chen | 4ebd019 | 2011-05-24 18:22:45 +0000 | [diff] [blame] | 211 | self.assertFalse(event, "Event should not be valid initially") |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 212 | |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 213 | |
| 214 | # The finite state machine for our custom listening thread, with an |
Bruce Mitchener | e171da5 | 2015-07-22 00:16:02 +0000 | [diff] [blame] | 215 | # initial state of None, which means no event has been received. |
Siva Chandra | 8a1f769 | 2015-05-08 00:43:28 +0000 | [diff] [blame] | 216 | # It changes to 'connected' after 'connected' event is received (for remote platforms) |
| 217 | # It changes to 'running' after 'running' event is received (should happen only if the |
| 218 | # currentstate is either 'None' or 'connected') |
| 219 | # It changes to 'stopped' if a 'stopped' event is received (should happen only if the |
| 220 | # current state is 'running'.) |
| 221 | self.state = None |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 222 | |
| 223 | # Create MyListeningThread to wait for state changed events. |
| 224 | # By design, a "running" event is expected following by a "stopped" event. |
| 225 | import threading |
| 226 | class MyListeningThread(threading.Thread): |
| 227 | def run(self): |
| 228 | #print "Running MyListeningThread:", self |
| 229 | |
| 230 | # Regular expression pattern for the event description. |
| 231 | pattern = re.compile("data = {.*, state = (.*)}$") |
| 232 | |
| 233 | # Let's only try at most 6 times to retrieve our events. |
| 234 | count = 0 |
| 235 | while True: |
Greg Clayton | a6cffde | 2014-10-17 23:58:27 +0000 | [diff] [blame] | 236 | if listener.WaitForEvent(5, event): |
Johnny Chen | 9ae9820 | 2011-04-23 00:34:56 +0000 | [diff] [blame] | 237 | desc = lldbutil.get_description(event) |
| 238 | #print "Event description:", desc |
| 239 | match = pattern.search(desc) |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 240 | if not match: |
| 241 | break; |
Siva Chandra | 8a1f769 | 2015-05-08 00:43:28 +0000 | [diff] [blame] | 242 | if match.group(1) == 'connected': |
| 243 | # When debugging remote targets with lldb-server, we |
| 244 | # first get the 'connected' event. |
| 245 | self.context.assertTrue(self.context.state == None) |
| 246 | self.context.state = 'connected' |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 247 | continue |
Siva Chandra | 8a1f769 | 2015-05-08 00:43:28 +0000 | [diff] [blame] | 248 | elif match.group(1) == 'running': |
| 249 | self.context.assertTrue(self.context.state == None or self.context.state == 'connected') |
| 250 | self.context.state = 'running' |
| 251 | continue |
| 252 | elif match.group(1) == 'stopped': |
| 253 | self.context.assertTrue(self.context.state == 'running') |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 254 | # Whoopee, both events have been received! |
Siva Chandra | 8a1f769 | 2015-05-08 00:43:28 +0000 | [diff] [blame] | 255 | self.context.state = 'stopped' |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 256 | break |
| 257 | else: |
| 258 | break |
| 259 | print "Timeout: listener.WaitForEvent" |
| 260 | count = count + 1 |
| 261 | if count > 6: |
| 262 | break |
| 263 | |
| 264 | return |
| 265 | |
| 266 | # Use Python API to continue the process. The listening thread should be |
| 267 | # able to receive the state changed events. |
Johnny Chen | 5a0bee7 | 2011-06-15 22:14:12 +0000 | [diff] [blame] | 268 | process.Continue() |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 269 | |
| 270 | # Start the listening thread to receive the "running" followed by the |
| 271 | # "stopped" events. |
| 272 | my_thread = MyListeningThread() |
| 273 | # Supply the enclosing context so that our listening thread can access |
| 274 | # the 'state' variable. |
| 275 | my_thread.context = self |
| 276 | my_thread.start() |
| 277 | |
| 278 | # Wait until the 'MyListeningThread' terminates. |
| 279 | my_thread.join() |
| 280 | |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 281 | # The final judgement. :-) |
Siva Chandra | 8a1f769 | 2015-05-08 00:43:28 +0000 | [diff] [blame] | 282 | self.assertTrue(self.state == 'stopped', |
Johnny Chen | f2df189 | 2010-12-22 00:32:54 +0000 | [diff] [blame] | 283 | "Both expected state changed events received") |