Author: GeorgeHuyubo
Date: 2026-02-04T09:10:07-08:00
New Revision: 23bf55eda43a7d2f9ffffce3f371fcf28c8d2df7

URL: 
https://github.com/llvm/llvm-project/commit/23bf55eda43a7d2f9ffffce3f371fcf28c8d2df7
DIFF: 
https://github.com/llvm/llvm-project/commit/23bf55eda43a7d2f9ffffce3f371fcf28c8d2df7.diff

LOG: [lldb]Send statistics in initialized event (#178978)

Re-attemp landing of old commit:
https://github.com/llvm/llvm-project/commit/7fe3586cda5b683766ec6b6d5ca2d98c2baaf162

Co-authored-by: George Hu <[email protected]>

Added: 
    lldb/test/API/tools/lldb-dap/eventStatistic/Makefile
    lldb/test/API/tools/lldb-dap/eventStatistic/TestVSCode_eventStatistic.py
    lldb/test/API/tools/lldb-dap/eventStatistic/foo.cpp
    lldb/test/API/tools/lldb-dap/eventStatistic/foo.h
    lldb/test/API/tools/lldb-dap/eventStatistic/main.cpp

Modified: 
    lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py
    lldb/tools/lldb-dap/Handler/RequestHandler.h
    lldb/tools/lldb-dap/JSONUtils.cpp
    lldb/tools/lldb-dap/JSONUtils.h

Removed: 
    


################################################################################
diff  --git a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py 
b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py
index 32e37c502e358..2747fc0c7573b 100644
--- a/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py
+++ b/lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py
@@ -310,6 +310,7 @@ def __init__(
 
         # trigger enqueue thread
         self._recv_thread.start()
+        self.initialized_event = None
 
     @classmethod
     def encode_content(cls, s: str) -> bytes:
@@ -516,6 +517,7 @@ def _handle_event(self, packet: Event) -> None:
                 self.output[category] = output
         elif event == "initialized":
             self.initialized = True
+            self.initialized_event = packet
         elif event == "process":
             # When a new process is attached or launched, remember the
             # details that are available in the body of the event

diff  --git a/lldb/test/API/tools/lldb-dap/eventStatistic/Makefile 
b/lldb/test/API/tools/lldb-dap/eventStatistic/Makefile
new file mode 100644
index 0000000000000..b30baf48b972e
--- /dev/null
+++ b/lldb/test/API/tools/lldb-dap/eventStatistic/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

diff  --git 
a/lldb/test/API/tools/lldb-dap/eventStatistic/TestVSCode_eventStatistic.py 
b/lldb/test/API/tools/lldb-dap/eventStatistic/TestVSCode_eventStatistic.py
new file mode 100644
index 0000000000000..59a0bbcb5370e
--- /dev/null
+++ b/lldb/test/API/tools/lldb-dap/eventStatistic/TestVSCode_eventStatistic.py
@@ -0,0 +1,76 @@
+"""
+Test lldb-dap terminated event
+"""
+
+import dap_server
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+import json
+import re
+
+import lldbdap_testcase
+from lldbsuite.test import lldbutil
+
+
+class TestDAP_eventStatistic(lldbdap_testcase.DAPTestCaseBase):
+    """
+
+    Test case that captures both initialized and terminated events.
+
+    META-ONLY: Intended to succeed TestDAP_terminatedEvent.py, but upstream 
keeps updating that file, so both that and this file will probably exist for a 
while.
+
+    """
+
+    def check_statistics_summary(self, statistics):
+        self.assertTrue(statistics["totalDebugInfoByteSize"] > 0)
+        self.assertTrue(statistics["totalDebugInfoEnabled"] > 0)
+        self.assertTrue(statistics["totalModuleCountHasDebugInfo"] > 0)
+
+        self.assertNotIn("modules", statistics.keys())
+
+    def check_target_summary(self, statistics):
+        # lldb-dap debugs one target at a time
+        target = json.loads(statistics["targets"])[0]
+        self.assertIn("totalSharedLibraryEventHitCount", target)
+
+    @skipIfWindows
+    @skipIfRemote
+    def test_terminated_event(self):
+        """
+        Terminated Event
+        Now contains the statistics of a debug session:
+        metatdata:
+            totalDebugInfoByteSize > 0
+            totalDebugInfoEnabled > 0
+            totalModuleCountHasDebugInfo > 0
+            ...
+        """
+
+        program_basename = "a.out.stripped"
+        program = self.getBuildArtifact(program_basename)
+        self.build_and_launch(program)
+        self.continue_to_exit()
+
+        statistics = 
self.dap_server.wait_for_terminated()["body"]["$__lldb_statistics"]
+        self.check_statistics_summary(statistics)
+        self.check_target_summary(statistics)
+
+    @skipIfWindows
+    @skipIfRemote
+    def test_initialized_event(self):
+        """
+        Initialized Event
+        Now contains the statistics of a debug session:
+            totalDebugInfoByteSize > 0
+            totalDebugInfoEnabled > 0
+            totalModuleCountHasDebugInfo > 0
+            ...
+        """
+
+        program_basename = "a.out"
+        program = self.getBuildArtifact(program_basename)
+        self.build_and_launch(program)
+        self.dap_server.wait_for_event("initialized")
+        statistics = 
self.dap_server.initialized_event["body"]["$__lldb_statistics"]
+        self.check_statistics_summary(statistics)
+        self.continue_to_exit()

diff  --git a/lldb/test/API/tools/lldb-dap/eventStatistic/foo.cpp 
b/lldb/test/API/tools/lldb-dap/eventStatistic/foo.cpp
new file mode 100644
index 0000000000000..b6f33b8e070a4
--- /dev/null
+++ b/lldb/test/API/tools/lldb-dap/eventStatistic/foo.cpp
@@ -0,0 +1 @@
+int foo() { return 12; }

diff  --git a/lldb/test/API/tools/lldb-dap/eventStatistic/foo.h 
b/lldb/test/API/tools/lldb-dap/eventStatistic/foo.h
new file mode 100644
index 0000000000000..5d5f8f0c9e786
--- /dev/null
+++ b/lldb/test/API/tools/lldb-dap/eventStatistic/foo.h
@@ -0,0 +1 @@
+int foo();

diff  --git a/lldb/test/API/tools/lldb-dap/eventStatistic/main.cpp 
b/lldb/test/API/tools/lldb-dap/eventStatistic/main.cpp
new file mode 100644
index 0000000000000..86044f561d257
--- /dev/null
+++ b/lldb/test/API/tools/lldb-dap/eventStatistic/main.cpp
@@ -0,0 +1,8 @@
+#include "foo.h"
+#include <iostream>
+
+int main(int argc, char const *argv[]) {
+  std::cout << "Hello World!" << std::endl;
+  foo();
+  return 0;
+}

diff  --git a/lldb/tools/lldb-dap/Handler/RequestHandler.h 
b/lldb/tools/lldb-dap/Handler/RequestHandler.h
index f435257d4dcce..9feb636fd5c28 100644
--- a/lldb/tools/lldb-dap/Handler/RequestHandler.h
+++ b/lldb/tools/lldb-dap/Handler/RequestHandler.h
@@ -11,6 +11,7 @@
 
 #include "DAP.h"
 #include "DAPError.h"
+#include "JSONUtils.h"
 #include "Protocol/ProtocolBase.h"
 #include "Protocol/ProtocolRequests.h"
 #include "Protocol/ProtocolTypes.h"
@@ -197,8 +198,7 @@ class DelayedResponseRequestHandler : public 
BaseRequestHandler {
     // The 'configurationDone' request is not sent until after 'initialized'
     // triggers the breakpoints being sent and 'configurationDone' is the last
     // message in the chain.
-    protocol::Event initialized{"initialized"};
-    dap.Send(initialized);
+    dap.SendJSON(CreateInitializedEventObject(dap.target));
   };
 
 protected:

diff  --git a/lldb/tools/lldb-dap/JSONUtils.cpp 
b/lldb/tools/lldb-dap/JSONUtils.cpp
index 79925b4bb37d3..5bcc2f9c71c2d 100644
--- a/lldb/tools/lldb-dap/JSONUtils.cpp
+++ b/lldb/tools/lldb-dap/JSONUtils.cpp
@@ -590,6 +590,12 @@ llvm::json::Object 
CreateTerminatedEventObject(lldb::SBTarget &target) {
   return event;
 }
 
+llvm::json::Object CreateInitializedEventObject(lldb::SBTarget &target) {
+  llvm::json::Object event(CreateEventObject("initialized"));
+  addStatistic(target, event);
+  return event;
+}
+
 std::string JSONToString(const llvm::json::Value &json) {
   std::string data;
   llvm::raw_string_ostream os(data);

diff  --git a/lldb/tools/lldb-dap/JSONUtils.h b/lldb/tools/lldb-dap/JSONUtils.h
index c2ffa11eceb95..232b1810a3cf4 100644
--- a/lldb/tools/lldb-dap/JSONUtils.h
+++ b/lldb/tools/lldb-dap/JSONUtils.h
@@ -333,6 +333,12 @@ llvm::json::Object CreateRunInTerminalReverseRequest(
 ///     A body JSON object with debug info and breakpoint info
 llvm::json::Object CreateTerminatedEventObject(lldb::SBTarget &target);
 
+/// Create a "Initialized" JSON object that contains statistics
+///
+/// \return
+///     A body JSON object with debug info
+llvm::json::Object CreateInitializedEventObject(lldb::SBTarget &target);
+
 /// Convert a given JSON object to a string.
 std::string JSONToString(const llvm::json::Value &json);
 


        
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to