Index: lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/vscode.py =================================================================== --- lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/vscode.py +++ lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/vscode.py @@ -369,7 +369,13 @@ def wait_for_exited(self): event_dict = self.wait_for_event('exited') if event_dict is None: - raise ValueError("didn't get stopped event") + raise ValueError("didn't get exited event") + return event_dict + + def wait_for_terminated(self): + event_dict = self.wait_for_event('terminated') + if event_dict is None: + raise ValueError("didn't get terminated event") return event_dict def get_initialize_value(self, key): Index: lldb/test/API/tools/lldb-vscode/terminated-event/Makefile =================================================================== --- /dev/null +++ lldb/test/API/tools/lldb-vscode/terminated-event/Makefile @@ -0,0 +1,17 @@ +DYLIB_NAME := foo +DYLIB_CXX_SOURCES := foo.cpp +CXX_SOURCES := main.cpp + +LD_EXTRAS := -Wl,-rpath "-Wl,$(shell pwd)" +USE_LIBDL :=1 + +include Makefile.rules + +all: a.out.stripped + +a.out.stripped: + strip -o a.out.stripped a.out + +ifneq "$(CODESIGN)" "" + $(CODESIGN) -fs - a.out.stripped +endif \ No newline at end of file Index: lldb/test/API/tools/lldb-vscode/terminated-event/TestVSCode_terminatedEvent.py =================================================================== --- /dev/null +++ lldb/test/API/tools/lldb-vscode/terminated-event/TestVSCode_terminatedEvent.py @@ -0,0 +1,59 @@ +""" +Test lldb-vscode terminated event +""" + +import vscode +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil +import lldbvscode_testcase +import re +import json + +class TestVSCode_terminatedEvent(lldbvscode_testcase.VSCodeTestCaseBase): + + @skipIfWindows + @skipIfRemote + def test_terminated_event(self): + ''' + Terminated Event + Now contains the statistics of a debug session: + metatdata: + totalDebugInfoByteSize > 0 + totalDebugInfoEnabled > 0 + totalModuleCountHasDebugInfo > 0 + ... + targetInfo: + totalBreakpointResolveTime > 0 + breakpoints: + recognize function breakpoint + recognize source line breakpoint + It should contains the breakpoints info: function bp & source line bp + ''' + + program_basename = "a.out.stripped" + program = self.getBuildArtifact(program_basename) + self.build_and_launch(program) + # Set breakpoints + functions = ['foo'] + breakpoint_ids = self.set_function_breakpoints(functions) + self.assertEquals(len(breakpoint_ids), len(functions), 'expect one breakpoint') + main_bp_line = line_number('main.cpp', '// main breakpoint 1') + breakpoint_ids.append(self.set_source_breakpoints('main.cpp', [main_bp_line])) + + self.continue_to_breakpoints(breakpoint_ids) + self.continue_to_exit() + + statistics = self.vscode.wait_for_terminated()['statistics'] + self.assertTrue(statistics['metadata']['totalDebugInfoByteSize'] > 0) + self.assertTrue(statistics['metadata']['totalDebugInfoEnabled'] > 0) + self.assertTrue(statistics['metadata']['totalModuleCountHasDebugInfo'] > 0) + + self.assertTrue(statistics['targetInfo']['totalBreakpointResolveTime'] > 0) + + breakpoints = json.loads(statistics['breakpoints']) + self.assertIn('foo', + breakpoints[0]['details']['Breakpoint']['BKPTResolver']['Options']['SymbolNames'], + 'foo is a symbol breakpoint') + self.assertTrue(breakpoints[1]['details']['Breakpoint']['BKPTResolver']['Options']['FileName'].endswith('main.cpp'), + 'target has source line breakpoint in main.cpp') Index: lldb/test/API/tools/lldb-vscode/terminated-event/foo.h =================================================================== --- /dev/null +++ lldb/test/API/tools/lldb-vscode/terminated-event/foo.h @@ -0,0 +1 @@ +int foo(); Index: lldb/test/API/tools/lldb-vscode/terminated-event/foo.cpp =================================================================== --- /dev/null +++ lldb/test/API/tools/lldb-vscode/terminated-event/foo.cpp @@ -0,0 +1,3 @@ +int foo() { + return 12; +} Index: lldb/test/API/tools/lldb-vscode/terminated-event/main.cpp =================================================================== --- /dev/null +++ lldb/test/API/tools/lldb-vscode/terminated-event/main.cpp @@ -0,0 +1,8 @@ +#include +#include "foo.h" + +int main(int argc, char const *argv[]) { + std::cout << "Hello World!" << std::endl; // main breakpoint 1 + foo(); + return 0; +} Index: lldb/tools/lldb-vscode/JSONUtils.h =================================================================== --- lldb/tools/lldb-vscode/JSONUtils.h +++ lldb/tools/lldb-vscode/JSONUtils.h @@ -485,6 +485,12 @@ llvm::StringRef debug_adaptor_path, llvm::StringRef comm_file); +/// Create a "Terminated" JSON object that contains statistics +/// +/// \return +/// A body JSON object with debug info and breakpoint info +llvm::json::Object CreateTerminatedEventObject(); + /// Convert a given JSON object to a string. std::string JSONToString(const llvm::json::Value &json); Index: lldb/tools/lldb-vscode/JSONUtils.cpp =================================================================== --- lldb/tools/lldb-vscode/JSONUtils.cpp +++ lldb/tools/lldb-vscode/JSONUtils.cpp @@ -1139,6 +1139,86 @@ return reverse_request; } +void GetValuesForKeys(const lldb::SBStructuredData data, const char *keys[], + size_t size, llvm::json::Object &out) { + for (size_t i = 0; i < size; i++) { + lldb::SBStructuredData value = data.GetValueForKey(keys[i]); + if (value.GetType() == lldb::eStructuredDataTypeFloat) { + out.try_emplace(keys[i], value.GetFloatValue()); + } else if (value.GetType() == lldb::eStructuredDataTypeInteger) { + out.try_emplace(keys[i], value.GetIntegerValue()); + } else { + llvm_unreachable("unimplemented"); + } + } +} + +llvm::json::Object CreateTerminatedEventObject() { + llvm::json::Object event(CreateEventObject("terminated")); + lldb::SBStructuredData statistics = g_vsc.target.GetStatistics(); + bool is_dictionary = + statistics.GetType() == lldb::eStructuredDataTypeDictionary; + if (!is_dictionary) { + return event; + } + + // Get statistics and return as body + llvm::json::Object stats_body; + + // Get metadata of the debug session + { + const char *metadata_keys[] = { + "totalDebugInfoByteSize", + "totalModuleCount", + "totalModuleCountHasDebugInfo", + "totalDebugInfoEnabled", + "totalDebugInfoIndexLoadedFromCache", + "totalDebugInfoIndexSavedToCache", + "totalSymbolTableStripped", + "totalSymbolTablesLoadedFromCache", + "totalSymbolTablesSavedToCache", + "totalDebugInfoIndexTime", + "totalDebugInfoParseTime", + "totalSymbolTableIndexTime", + "totalSymbolTableParseTime", + }; + llvm::json::Object metadata; + GetValuesForKeys(statistics, metadata_keys, + sizeof(metadata_keys) / sizeof(metadata_keys[0]), + metadata); + stats_body.try_emplace("metadata", std::move(metadata)); + } + + // Get targets statistics + { + const char *targets_keys[] = { + "firstStopTime", + "launchOrAttachTime", + "totalBreakpointResolveTime", + }; + // For lldb-vscode, only one target per session + const lldb::SBStructuredData stats_targets = + statistics.GetValueForKey("targets").GetItemAtIndex(0); + llvm::json::Object target_info; + GetValuesForKeys(stats_targets, targets_keys, + sizeof(targets_keys) / sizeof(targets_keys[0]), + target_info); + stats_body.try_emplace("targetInfo", std::move(target_info)); + + // Get breakpoints statistics as a json string + lldb::SBStream breakpoints; + // Notice "breakpoints" is not a guaranteed field in targets key + if (stats_targets.GetValueForKey("breakpoints").IsValid()) { + stats_targets.GetValueForKey("breakpoints").GetAsJSON(breakpoints); + stats_body.try_emplace("breakpoints", + llvm::json::fixUTF8(breakpoints.GetData())); + } + } + + event.try_emplace("statistics", std::move(stats_body)); + return event; +} + std::string JSONToString(const llvm::json::Value &json) { std::string data; llvm::raw_string_ostream os(data); Index: lldb/tools/lldb-vscode/VSCode.h =================================================================== --- lldb/tools/lldb-vscode/VSCode.h +++ lldb/tools/lldb-vscode/VSCode.h @@ -43,6 +43,7 @@ #include "lldb/API/SBProcess.h" #include "lldb/API/SBStream.h" #include "lldb/API/SBStringList.h" +#include "lldb/API/SBStructuredData.h" #include "lldb/API/SBTarget.h" #include "lldb/API/SBThread.h" Index: lldb/tools/lldb-vscode/lldb-vscode.cpp =================================================================== --- lldb/tools/lldb-vscode/lldb-vscode.cpp +++ lldb/tools/lldb-vscode/lldb-vscode.cpp @@ -204,7 +204,7 @@ g_vsc.sent_terminated_event = true; g_vsc.RunTerminateCommands(); // Send a "terminated" event - llvm::json::Object event(CreateEventObject("terminated")); + llvm::json::Object event(CreateTerminatedEventObject()); g_vsc.SendJSON(llvm::json::Value(std::move(event))); } }