133 lines · python
1"""2Test lldb-dap module request3"""4 5from lldbsuite.test.decorators import *6from lldbsuite.test.lldbtest import *7import lldbdap_testcase8import re9 10# Flakey in Github CI runs, see https://github.com/llvm/llvm-project/issues/137660.11@skipIfLinux12class TestDAP_module(lldbdap_testcase.DAPTestCaseBase):13 def run_test(self, symbol_basename, expect_debug_info_size):14 program_basename = "a.out.stripped"15 program = self.getBuildArtifact(program_basename)16 self.build_and_launch(program)17 functions = ["foo"]18 19 # This breakpoint will be resolved only when the libfoo module is loaded20 breakpoint_ids = self.set_function_breakpoints(21 functions, wait_for_resolve=False22 )23 self.assertEqual(len(breakpoint_ids), len(functions), "expect one breakpoint")24 self.continue_to_breakpoints(breakpoint_ids)25 active_modules = self.dap_server.get_modules()26 program_module = active_modules[program_basename]27 self.assertIn(28 program_basename,29 active_modules,30 "%s module is in active modules" % (program_basename),31 )32 self.assertIn("name", program_module, "make sure name is in module")33 self.assertEqual(program_basename, program_module["name"])34 self.assertIn("path", program_module, "make sure path is in module")35 self.assertEqual(program, program_module["path"])36 self.assertNotIn(37 "symbolFilePath",38 program_module,39 "Make sure a.out.stripped has no debug info",40 )41 symbols_path = self.getBuildArtifact(symbol_basename)42 self.dap_server.request_evaluate(43 "`%s" % ('target symbols add -s "%s" "%s"' % (program, symbols_path)),44 context="repl",45 )46 47 def check_symbols_loaded_with_size():48 active_modules = self.dap_server.get_modules()49 program_module = active_modules[program_basename]50 self.assertIn("symbolFilePath", program_module)51 self.assertIn(symbols_path, program_module["symbolFilePath"])52 size_regex = re.compile(r"[0-9]+(\.[0-9]*)?[KMG]?B")53 return size_regex.match(program_module["debugInfoSize"])54 55 if expect_debug_info_size:56 self.assertTrue(57 self.wait_until(check_symbols_loaded_with_size),58 "expect has debug info size",59 )60 61 active_modules = self.dap_server.get_modules()62 program_module = active_modules[program_basename]63 self.assertEqual(program_basename, program_module["name"])64 self.assertEqual(program, program_module["path"])65 self.assertIn("addressRange", program_module)66 67 # Collect all the module names we saw as events.68 module_new_names = []69 module_changed_names = []70 module_event = self.dap_server.wait_for_event(["module"])71 while module_event is not None:72 reason = module_event["body"]["reason"]73 if reason == "new":74 module_new_names.append(module_event["body"]["module"]["name"])75 elif reason == "changed":76 module_changed_names.append(module_event["body"]["module"]["name"])77 78 module_event = self.dap_server.wait_for_event(["module"])79 80 # Make sure we got an event for every active module.81 self.assertNotEqual(len(module_new_names), 0)82 for module in active_modules:83 self.assertIn(module, module_new_names)84 85 # Make sure we got an update event for the program module when the86 # symbols got added.87 self.assertNotEqual(len(module_changed_names), 0)88 self.assertIn(program_module["name"], module_changed_names)89 self.continue_to_exit()90 91 @skipIfWindows92 def test_modules(self):93 """94 Mac or linux.95 96 On mac, if we load a.out as our symbol file, we will use DWARF with .o files and we will97 have debug symbols, but we won't see any debug info size because all of the DWARF98 sections are in .o files.99 100 On other platforms, we expect a.out to have debug info, so we will expect a size.101 """102 return self.run_test(103 "a.out", expect_debug_info_size=platform.system() != "Darwin"104 )105 106 @skipUnlessDarwin107 def test_modules_dsym(self):108 """109 Darwin only test with dSYM file.110 111 On mac, if we load a.out.dSYM as our symbol file, we will have debug symbols and we112 will have DWARF sections added to the module, so we will expect a size.113 """114 return self.run_test("a.out.dSYM", expect_debug_info_size=True)115 116 @skipIfWindows117 def test_compile_units(self):118 program = self.getBuildArtifact("a.out")119 self.build_and_launch(program)120 source = "main.cpp"121 main_source_path = self.getSourcePath(source)122 breakpoint1_line = line_number(source, "// breakpoint 1")123 lines = [breakpoint1_line]124 breakpoint_ids = self.set_source_breakpoints(source, lines)125 self.continue_to_breakpoints(breakpoint_ids)126 moduleId = self.dap_server.get_modules()["a.out"]["id"]127 response = self.dap_server.request_compileUnits(moduleId)128 self.assertTrue(response["body"])129 cu_paths = [cu["compileUnitPath"] for cu in response["body"]["compileUnits"]]130 self.assertIn(main_source_path, cu_paths, "Real path to main.cpp matches")131 132 self.continue_to_exit()133