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,61 @@ +""" +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 = json.loads(self.vscode.wait_for_terminated()['statistics']) + self.assertTrue(statistics['totalDebugInfoByteSize'] > 0) + self.assertTrue(statistics['totalDebugInfoEnabled'] > 0) + self.assertTrue(statistics['totalModuleCountHasDebugInfo'] > 0) + + # lldb-vscode debugs one target at a time + # terminated event will return exact one + self.assertTrue(statistics['target']['totalBreakpointResolveTime'] > 0) + + breakpoints = statistics['target']['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.cpp =================================================================== --- lldb/tools/lldb-vscode/JSONUtils.cpp +++ lldb/tools/lldb-vscode/JSONUtils.cpp @@ -19,6 +19,8 @@ #include "lldb/API/SBBreakpoint.h" #include "lldb/API/SBBreakpointLocation.h" #include "lldb/API/SBDeclaration.h" +#include "lldb/API/SBStringList.h" +#include "lldb/API/SBStructuredData.h" #include "lldb/API/SBValue.h" #include "lldb/Host/PosixApi.h" @@ -1140,6 +1142,65 @@ return reverse_request; } +// NOTE: This function will ignore large contents except "targets" +void FilterAndGetValueForKey(const lldb::SBStructuredData data, const char *key, + llvm::json::Object &out) { + lldb::SBStructuredData value = data.GetValueForKey(key); + std::string key_utf8 = llvm::json::fixUTF8(key); + switch (value.GetType()) { + case lldb::eStructuredDataTypeFloat: + out.try_emplace(key_utf8, value.GetFloatValue()); + break; + case lldb::eStructuredDataTypeInteger: + out.try_emplace(key_utf8, value.GetIntegerValue()); + break; + case lldb::eStructuredDataTypeArray: { + if (strcmp(key, "targets") != 0) { + return; + } + lldb::SBStream contents; + // lldb-vscode debug one target at a time + value.GetItemAtIndex(0).GetAsJSON(contents); + EmplaceSafeString(out, "target", + llvm::json::fixUTF8(contents.GetData())); + } break; + case lldb::eStructuredDataTypeInvalid: + llvm_unreachable("unimplemented"); + case lldb::eStructuredDataTypeBoolean: + case lldb::eStructuredDataTypeString: + case lldb::eStructuredDataTypeDictionary: + case lldb::eStructuredDataTypeNull: + case lldb::eStructuredDataTypeGeneric: + break; + } +} + +void addStatistic(llvm::json::Object &event) { + lldb::SBStructuredData statistics = g_vsc.target.GetStatistics(); + bool is_dictionary = + statistics.GetType() == lldb::eStructuredDataTypeDictionary; + if (!is_dictionary) { + return; + } + llvm::json::Object stats_body; + + lldb::SBStringList keys; + if (!statistics.GetKeys(keys)) { + return; + } + for (size_t i = 0; i < keys.GetSize(); i++) { + const char *key = keys.GetStringAtIndex(i); + FilterAndGetValueForKey(statistics, key, stats_body); + } + event.try_emplace("statistics", std::move(stats_body)); +} + +llvm::json::Object CreateTerminatedEventObject() { + llvm::json::Object event(CreateEventObject("terminated")); + addStatistic(event); + return event; +} + std::string JSONToString(const llvm::json::Value &json) { std::string data; llvm::raw_string_ostream os(data);