1 2import json 3import re 4 5import gdbremote_testcase 6from lldbsuite.test.decorators import * 7from lldbsuite.test.lldbtest import * 8from lldbsuite.test import lldbutil 9 10class TestGdbRemoteThreadsInStopReply( 11 gdbremote_testcase.GdbRemoteTestCaseBase): 12 13 mydir = TestBase.compute_mydir(__file__) 14 15 ENABLE_THREADS_IN_STOP_REPLY_ENTRIES = [ 16 "read packet: $QListThreadsInStopReply#21", 17 "send packet: $OK#00", 18 ] 19 20 def gather_stop_reply_fields(self, post_startup_log_lines, thread_count, 21 field_names): 22 # Set up the inferior args. 23 inferior_args = [] 24 for i in range(thread_count - 1): 25 inferior_args.append("thread:new") 26 inferior_args.append("sleep:10") 27 procs = self.prep_debug_monitor_and_inferior( 28 inferior_args=inferior_args) 29 30 self.add_register_info_collection_packets() 31 self.add_process_info_collection_packets() 32 33 # Assumes test_sequence has anything added needed to setup the initial state. 34 # (Like optionally enabling QThreadsInStopReply.) 35 if post_startup_log_lines: 36 self.test_sequence.add_log_lines(post_startup_log_lines, True) 37 self.test_sequence.add_log_lines([ 38 "read packet: $c#63" 39 ], True) 40 context = self.expect_gdbremote_sequence() 41 self.assertIsNotNone(context) 42 hw_info = self.parse_hw_info(context) 43 44 # Give threads time to start up, then break. 45 time.sleep(self._WAIT_TIMEOUT) 46 self.reset_test_sequence() 47 self.test_sequence.add_log_lines( 48 [ 49 "read packet: {}".format( 50 chr(3)), 51 { 52 "direction": "send", 53 "regex": r"^\$T([0-9a-fA-F]+)([^#]+)#[0-9a-fA-F]{2}$", 54 "capture": { 55 1: "stop_result", 56 2: "key_vals_text"}}, 57 ], 58 True) 59 context = self.expect_gdbremote_sequence() 60 self.assertIsNotNone(context) 61 62 # Wait until all threads have started. 63 threads = self.wait_for_thread_count(thread_count, 64 timeout_seconds=self._WAIT_TIMEOUT) 65 self.assertIsNotNone(threads) 66 self.assertEqual(len(threads), thread_count) 67 68 # Run, then stop the process, grab the stop reply content. 69 self.reset_test_sequence() 70 self.test_sequence.add_log_lines(["read packet: $c#63", 71 "read packet: {}".format(chr(3)), 72 {"direction": "send", 73 "regex": r"^\$T([0-9a-fA-F]+)([^#]+)#[0-9a-fA-F]{2}$", 74 "capture": {1: "stop_result", 75 2: "key_vals_text"}}, 76 ], 77 True) 78 context = self.expect_gdbremote_sequence() 79 self.assertIsNotNone(context) 80 81 # Parse the stop reply contents. 82 key_vals_text = context.get("key_vals_text") 83 self.assertIsNotNone(key_vals_text) 84 kv_dict = self.parse_key_val_dict(key_vals_text) 85 self.assertIsNotNone(kv_dict) 86 87 result = dict(); 88 result["pc_register"] = hw_info["pc_register"] 89 result["little_endian"] = hw_info["little_endian"] 90 for key_field in field_names: 91 result[key_field] = kv_dict.get(key_field) 92 93 return result 94 95 def gather_stop_reply_threads(self, post_startup_log_lines, thread_count): 96 # Pull out threads from stop response. 97 stop_reply_threads_text = self.gather_stop_reply_fields( 98 post_startup_log_lines, thread_count, ["threads"])["threads"] 99 if stop_reply_threads_text: 100 return [int(thread_id, 16) 101 for thread_id in stop_reply_threads_text.split(",")] 102 else: 103 return [] 104 105 def gather_stop_reply_pcs(self, post_startup_log_lines, thread_count): 106 results = self.gather_stop_reply_fields( post_startup_log_lines, 107 thread_count, ["threads", "thread-pcs"]) 108 if not results: 109 return [] 110 111 threads_text = results["threads"] 112 pcs_text = results["thread-pcs"] 113 thread_ids = threads_text.split(",") 114 pcs = pcs_text.split(",") 115 self.assertEquals(len(thread_ids), len(pcs)) 116 117 thread_pcs = dict() 118 for i in range(0, len(pcs)): 119 thread_pcs[int(thread_ids[i], 16)] = pcs[i] 120 121 result = dict() 122 result["thread_pcs"] = thread_pcs 123 result["pc_register"] = results["pc_register"] 124 result["little_endian"] = results["little_endian"] 125 return result 126 127 def switch_endian(self, egg): 128 return "".join(reversed(re.findall("..", egg))) 129 130 def parse_hw_info(self, context): 131 self.assertIsNotNone(context) 132 process_info = self.parse_process_info_response(context) 133 endian = process_info.get("endian") 134 reg_info = self.parse_register_info_packets(context) 135 (pc_lldb_reg_index, pc_reg_info) = self.find_pc_reg_info(reg_info) 136 137 hw_info = dict() 138 hw_info["pc_register"] = pc_lldb_reg_index 139 hw_info["little_endian"] = (endian == "little") 140 return hw_info 141 142 def gather_threads_info_pcs(self, pc_register, little_endian): 143 self.reset_test_sequence() 144 self.test_sequence.add_log_lines( 145 [ 146 "read packet: $jThreadsInfo#c1", 147 { 148 "direction": "send", 149 "regex": r"^\$(.*)#[0-9a-fA-F]{2}$", 150 "capture": { 151 1: "threads_info"}}, 152 ], 153 True) 154 155 context = self.expect_gdbremote_sequence() 156 self.assertIsNotNone(context) 157 threads_info = context.get("threads_info") 158 register = str(pc_register) 159 # The jThreadsInfo response is not valid JSON data, so we have to 160 # clean it up first. 161 jthreads_info = json.loads(self.decode_gdbremote_binary(threads_info)) 162 thread_pcs = dict() 163 for thread_info in jthreads_info: 164 tid = thread_info["tid"] 165 pc = thread_info["registers"][register] 166 thread_pcs[tid] = self.switch_endian(pc) if little_endian else pc 167 168 return thread_pcs 169 170 def gather_threads_info_memory(self): 171 self.reset_test_sequence() 172 self.test_sequence.add_log_lines( 173 [ 174 "read packet: $jThreadsInfo#c1", 175 { 176 "direction": "send", 177 "regex": r"^\$(.*)#[0-9a-fA-F]{2}$", 178 "capture": { 179 1: "threads_info"}}, 180 ], 181 True) 182 183 context = self.expect_gdbremote_sequence() 184 self.assertIsNotNone(context) 185 threads_info = context.get("threads_info") 186 # The jThreadsInfo response is not valid JSON data, so we have to 187 # clean it up first. 188 jthreads_info = json.loads(self.decode_gdbremote_binary(threads_info)) 189 # Collect all the memory chunks from all threads 190 memory_chunks = dict() 191 for thread_info in jthreads_info: 192 chunk_list = thread_info["memory"] 193 self.assertNotEqual(len(chunk_list), 0) 194 for chunk in chunk_list: 195 memory_chunks[chunk["address"]] = chunk["bytes"] 196 return memory_chunks 197 198 def QListThreadsInStopReply_supported(self): 199 procs = self.prep_debug_monitor_and_inferior() 200 self.test_sequence.add_log_lines( 201 self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, True) 202 203 context = self.expect_gdbremote_sequence() 204 self.assertIsNotNone(context) 205 206 @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet 207 @debugserver_test 208 def test_QListThreadsInStopReply_supported_debugserver(self): 209 self.init_debugserver_test() 210 self.build() 211 self.set_inferior_startup_launch() 212 self.QListThreadsInStopReply_supported() 213 214 @llgs_test 215 def test_QListThreadsInStopReply_supported_llgs(self): 216 self.init_llgs_test() 217 self.build() 218 self.set_inferior_startup_launch() 219 self.QListThreadsInStopReply_supported() 220 221 def stop_reply_reports_multiple_threads(self, thread_count): 222 # Gather threads from stop notification when QThreadsInStopReply is 223 # enabled. 224 stop_reply_threads = self.gather_stop_reply_threads( 225 self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, thread_count) 226 self.assertEqual(len(stop_reply_threads), thread_count) 227 228 @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet 229 @debugserver_test 230 def test_stop_reply_reports_multiple_threads_debugserver(self): 231 self.init_debugserver_test() 232 self.build() 233 self.set_inferior_startup_launch() 234 self.stop_reply_reports_multiple_threads(5) 235 236 # In current implementation of llgs on Windows, as a response to '\x03' packet, the debugger 237 # of the native process will trigger a call to DebugBreakProcess that will create a new thread 238 # to handle the exception debug event. So one more stop thread will be notified to the 239 # delegate, e.g. llgs. So tests below to assert the stop threads number will all fail. 240 @expectedFailureAll(oslist=["windows"]) 241 @skipIfNetBSD 242 @llgs_test 243 def test_stop_reply_reports_multiple_threads_llgs(self): 244 self.init_llgs_test() 245 self.build() 246 self.set_inferior_startup_launch() 247 self.stop_reply_reports_multiple_threads(5) 248 249 def no_QListThreadsInStopReply_supplies_no_threads(self, thread_count): 250 # Gather threads from stop notification when QThreadsInStopReply is not 251 # enabled. 252 stop_reply_threads = self.gather_stop_reply_threads(None, thread_count) 253 self.assertEqual(len(stop_reply_threads), 0) 254 255 @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet 256 @debugserver_test 257 def test_no_QListThreadsInStopReply_supplies_no_threads_debugserver(self): 258 self.init_debugserver_test() 259 self.build() 260 self.set_inferior_startup_launch() 261 self.no_QListThreadsInStopReply_supplies_no_threads(5) 262 263 @expectedFailureAll(oslist=["windows"]) 264 @skipIfNetBSD 265 @llgs_test 266 def test_no_QListThreadsInStopReply_supplies_no_threads_llgs(self): 267 self.init_llgs_test() 268 self.build() 269 self.set_inferior_startup_launch() 270 self.no_QListThreadsInStopReply_supplies_no_threads(5) 271 272 def stop_reply_reports_correct_threads(self, thread_count): 273 # Gather threads from stop notification when QThreadsInStopReply is 274 # enabled. 275 stop_reply_threads = self.gather_stop_reply_threads( 276 self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, thread_count) 277 self.assertEqual(len(stop_reply_threads), thread_count) 278 279 # Gather threads from q{f,s}ThreadInfo. 280 self.reset_test_sequence() 281 self.add_threadinfo_collection_packets() 282 283 context = self.expect_gdbremote_sequence() 284 self.assertIsNotNone(context) 285 286 threads = self.parse_threadinfo_packets(context) 287 self.assertIsNotNone(threads) 288 self.assertEqual(len(threads), thread_count) 289 290 # Ensure each thread in q{f,s}ThreadInfo appears in stop reply threads 291 for tid in threads: 292 self.assertTrue(tid in stop_reply_threads) 293 294 @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet 295 @debugserver_test 296 def test_stop_reply_reports_correct_threads_debugserver(self): 297 self.init_debugserver_test() 298 self.build() 299 self.set_inferior_startup_launch() 300 self.stop_reply_reports_correct_threads(5) 301 302 @expectedFailureAll(oslist=["windows"]) 303 @skipIfNetBSD 304 @llgs_test 305 def test_stop_reply_reports_correct_threads_llgs(self): 306 self.init_llgs_test() 307 self.build() 308 self.set_inferior_startup_launch() 309 self.stop_reply_reports_correct_threads(5) 310 311 def stop_reply_contains_thread_pcs(self, thread_count): 312 results = self.gather_stop_reply_pcs( 313 self.ENABLE_THREADS_IN_STOP_REPLY_ENTRIES, thread_count) 314 stop_reply_pcs = results["thread_pcs"] 315 pc_register = results["pc_register"] 316 little_endian = results["little_endian"] 317 self.assertEqual(len(stop_reply_pcs), thread_count) 318 319 threads_info_pcs = self.gather_threads_info_pcs(pc_register, 320 little_endian) 321 322 self.assertEqual(len(threads_info_pcs), thread_count) 323 for thread_id in stop_reply_pcs: 324 self.assertTrue(thread_id in threads_info_pcs) 325 self.assertTrue(int(stop_reply_pcs[thread_id], 16) 326 == int(threads_info_pcs[thread_id], 16)) 327 328 @expectedFailureAll(oslist=["windows"]) 329 @skipIfNetBSD 330 @llgs_test 331 def test_stop_reply_contains_thread_pcs_llgs(self): 332 self.init_llgs_test() 333 self.build() 334 self.set_inferior_startup_launch() 335 self.stop_reply_contains_thread_pcs(5) 336 337 @skipIfDarwinEmbedded # <rdar://problem/34539270> lldb-server tests not updated to work on ios etc yet 338 @debugserver_test 339 def test_stop_reply_contains_thread_pcs_debugserver(self): 340 self.init_debugserver_test() 341 self.build() 342 self.set_inferior_startup_launch() 343 self.stop_reply_contains_thread_pcs(5) 344 345 def read_memory_chunk(self, address, length): 346 self.test_sequence.add_log_lines( 347 ["read packet: $x{0:x},{1:x}#00".format(address, length), 348 { 349 "direction": "send", 350 "regex": r"^\$([\s\S]*)#[0-9a-fA-F]{2}$", 351 "capture": { 352 1: "contents"}}, 353 ], 354 True) 355 contents = self.expect_gdbremote_sequence()["contents"] 356 contents = self.decode_gdbremote_binary(contents) 357 hex_contents = "" 358 for c in contents: 359 hex_contents += "%02x" % ord(c) 360 return hex_contents 361 362 def check_memory_chunks_equal(self, memory_chunks): 363 self.reset_test_sequence() 364 for address in memory_chunks: 365 contents = memory_chunks[address] 366 byte_size = len(contents) // 2 367 mem = self.read_memory_chunk(address, byte_size) 368 self.assertEqual(mem, contents) 369 370 def stop_reply_thread_info_correct_memory(self, thread_count): 371 # Run and stop the program. 372 self.gather_stop_reply_fields([], thread_count, []) 373 # Read memory chunks from jThreadsInfo. 374 memory_chunks = self.gather_threads_info_memory() 375 # Check the chunks are correct. 376 self.check_memory_chunks_equal(memory_chunks) 377 378 @expectedFailureAll(oslist=["windows"]) 379 @skipIfNetBSD 380 @llgs_test 381 def test_stop_reply_thread_info_correct_memory_llgs(self): 382 self.init_llgs_test() 383 self.build() 384 self.set_inferior_startup_launch() 385 self.stop_reply_thread_info_correct_memory(5) 386