diff --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/vscode.py b/lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/vscode.py --- a/lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/vscode.py +++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/vscode.py @@ -756,6 +756,16 @@ } return self.send_recv(command_dict) + def request_getCompileUnits(self, moduleId): + args_dict = {'moduleId': moduleId} + command_dict = { + 'command': 'getCompileUnits', + 'type': 'request', + 'arguments': args_dict + } + response = self.send_recv(command_dict) + return response + def request_stackTrace(self, threadId=None, startFrame=None, levels=None, dump=False): if threadId is None: diff --git a/lldb/test/API/tools/lldb-vscode/module/TestVSCode_module.py b/lldb/test/API/tools/lldb-vscode/module/TestVSCode_module.py --- a/lldb/test/API/tools/lldb-vscode/module/TestVSCode_module.py +++ b/lldb/test/API/tools/lldb-vscode/module/TestVSCode_module.py @@ -27,4 +27,24 @@ self.assertTrue('a.out' in self.vscode.get_active_modules(), 'Module: a.out is loaded') self.assertTrue('symbolFilePath' in self.vscode.get_active_modules()['a.out'], - 'Symbol exists') \ No newline at end of file + 'Symbol exists') + + def test_compile_units(self): + program= self.getBuildArtifact("a.out") + self.build_and_launch(program) + source = "main.cpp" + main_source_path = self.getSourcePath(source) + breakpoint1_line = line_number(source, '// breakpoint 1') + lines = [breakpoint1_line] + breakpoint_ids = self.set_source_breakpoints(source, lines) + self.continue_to_breakpoints(breakpoint_ids) + moduleId = self.vscode.get_active_modules()['a.out']['id'] + response = self.vscode.request_getCompileUnits(moduleId) + print(response['body']) + self.assertTrue(response['body']) + self.assertTrue(len(response['body']['compileUnits']) == 1, + 'Only one source file should exist') + self.assertTrue(response['body']['compileUnits'][0]['compileUnitPath'] == main_source_path, + 'Real path to main.cpp matches') + + diff --git a/lldb/tools/lldb-vscode/JSONUtils.h b/lldb/tools/lldb-vscode/JSONUtils.h --- a/lldb/tools/lldb-vscode/JSONUtils.h +++ b/lldb/tools/lldb-vscode/JSONUtils.h @@ -441,6 +441,8 @@ llvm::json::Value CreateVariable(lldb::SBValue v, int64_t variablesReference, int64_t varID, bool format_hex); +llvm::json::Value CreateCompileUnit(lldb::SBCompileUnit unit); + } // namespace lldb_vscode #endif diff --git a/lldb/tools/lldb-vscode/JSONUtils.cpp b/lldb/tools/lldb-vscode/JSONUtils.cpp --- a/lldb/tools/lldb-vscode/JSONUtils.cpp +++ b/lldb/tools/lldb-vscode/JSONUtils.cpp @@ -925,4 +925,12 @@ return llvm::json::Value(std::move(object)); } +llvm::json::Value CreateCompileUnit(lldb::SBCompileUnit unit) { + llvm::json::Object object; + std::string path = std::string(unit.GetFileSpec().GetDirectory()) + "/" + + std::string(unit.GetFileSpec().GetFilename()); + object.try_emplace("compileUnitPath", path); + return llvm::json::Value(std::move(object)); +} + } // namespace lldb_vscode diff --git a/lldb/tools/lldb-vscode/lldb-vscode.cpp b/lldb/tools/lldb-vscode/lldb-vscode.cpp --- a/lldb/tools/lldb-vscode/lldb-vscode.cpp +++ b/lldb/tools/lldb-vscode/lldb-vscode.cpp @@ -1171,6 +1171,34 @@ g_vsc.SendJSON(llvm::json::Value(std::move(response))); } +void request_getCompileUnits(const llvm::json::Object &request) { + llvm::json::Object response; + FillResponse(request, response); + lldb::SBProcess process = g_vsc.target.GetProcess(); + llvm::json::Object body; + llvm::json::Array units; + auto arguments = request.getObject("arguments"); + std::string module_id = std::string(GetString(arguments, "moduleId")); + int num_modules = g_vsc.target.GetNumModules(); + for (int i = 0; i < num_modules; i++) { + auto curr_module = g_vsc.target.GetModuleAtIndex(i); + if (module_id == curr_module.GetUUIDString()) { + int num_units = curr_module.GetNumCompileUnits(); + for (int j = 0; j < num_units; j++) { + auto curr_unit = curr_module.GetCompileUnitAtIndex(j); + // auto unit_file_spec = curr_unit.GetFileSpec(); + // std::string unit_path = std::string(unit_file_spec.GetDirectory()); + units.emplace_back(CreateCompileUnit(curr_unit)); + // body.try_emplace(unit_file_spec.GetFilename(), unit_path); + } + body.try_emplace("compileUnits", std::move(units)); + break; + } + } + response.try_emplace("body", std::move(body)); + g_vsc.SendJSON(llvm::json::Value(std::move(response))); +} + // "InitializeRequest": { // "allOf": [ { "$ref": "#/definitions/Request" }, { // "type": "object", @@ -2754,6 +2782,7 @@ REQUEST_CALLBACK(disconnect), REQUEST_CALLBACK(evaluate), REQUEST_CALLBACK(exceptionInfo), + REQUEST_CALLBACK(getCompileUnits), REQUEST_CALLBACK(initialize), REQUEST_CALLBACK(launch), REQUEST_CALLBACK(next),