[Lldb-commits] [lldb] Report exit status message in lldb-dap, same as lldb cli (PR #89405)

Miro Bucko via lldb-commits lldb-commits at lists.llvm.org
Fri Apr 19 13:46:23 PDT 2024


https://github.com/mbucko updated https://github.com/llvm/llvm-project/pull/89405

>From 31d688e70beec442cf731986ff8e4c8e9b5a9f0c Mon Sep 17 00:00:00 2001
From: Miro Bucko <mbucko at meta.com>
Date: Fri, 19 Apr 2024 08:08:02 -0700
Subject: [PATCH] Report exit status message in lldb-dap, same as lldb cli

Summary:
When the target inferior process that is being debugged exits in lldb command line, it emits following message:
Process 4049526 exited with status = -1 (0xffffffff) debugserver died with signal SIGTERM
lldb-dap on the other hand does not emit a similar message.
This PR adds the same status message to lldb-dap.

Test Plan:
In VSCode debug any target and hit stop mode, kill lldb-server and observe an exit status message similar to the following:
Process 2167677 exited with status = -1 (0xffffffff) debugserver died with signal SIGTERM

Reviewers:

Subscribers:

Tasks:
lldb-dap

Tags:
---
 .../tools/lldb-dap/console/TestDAP_console.py | 47 +++++++++++++++++++
 lldb/tools/lldb-dap/lldb-dap.cpp              |  8 ++++
 2 files changed, 55 insertions(+)

diff --git a/lldb/test/API/tools/lldb-dap/console/TestDAP_console.py b/lldb/test/API/tools/lldb-dap/console/TestDAP_console.py
index ffa0dc943e0693..4be05b8607c3bf 100644
--- a/lldb/test/API/tools/lldb-dap/console/TestDAP_console.py
+++ b/lldb/test/API/tools/lldb-dap/console/TestDAP_console.py
@@ -4,10 +4,33 @@
 
 import dap_server
 import lldbdap_testcase
+import psutil
 from lldbsuite.test import lldbutil
 from lldbsuite.test.decorators import *
 from lldbsuite.test.lldbtest import *
 
+def get_latest_pid(process_name):
+    processes = []
+    for proc in psutil.process_iter():
+        try:
+            if proc.name() == process_name:
+                processes += [proc]
+        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
+            pass
+    if len(processes) == 0:
+        print("No lldb-server process found", flush=True, file=sys.stderr)
+        return None
+
+    processes = sorted(processes, key=lambda x:x.create_time())
+    return processes[-1].pid
+
+def killProcess(pid, process_name):
+    process = psutil.Process(pid)
+    process.terminate()
+    try:
+        process.wait(timeout=5)
+    except psutil.TimeoutExpired:
+        self.assertTrue(False, process_name + " process should have exited by now")
 
 class TestDAP_console(lldbdap_testcase.DAPTestCaseBase):
     def check_lldb_command(
@@ -104,3 +127,27 @@ def test_empty_escape_prefix(self):
             "Help can be invoked",
             command_escape_prefix="",
         )
+
+    @skipIfWindows
+    @skipIfRemote
+    def test_exit_status_message(self):
+        source = "main.cpp"
+        program = self.getBuildArtifact("a.out")
+        self.build_and_launch(program, commandEscapePrefix="")
+        breakpoint1_line = line_number(source, "// breakpoint 1")
+        breakpoint_ids = self.set_source_breakpoints(source, [breakpoint1_line])
+        self.continue_to_breakpoints(breakpoint_ids)
+
+        # Kill lldb-server process.
+        process_name = "lldb-server"
+        pid = get_latest_pid(process_name)
+        killProcess(pid, process_name)
+        # Get the console output
+        console_output = self.collect_console(1.0)
+
+        # Verify the exit status message is printed.
+        self.assertIn(
+            "exited with status = -1 (0xffffffff) debugserver died with signal SIGTERM",
+            console_output,
+            "Exit status does not contain message 'exited with status'"
+        )
diff --git a/lldb/tools/lldb-dap/lldb-dap.cpp b/lldb/tools/lldb-dap/lldb-dap.cpp
index 25c5ad56e3d6fe..124e5a087618a7 100644
--- a/lldb/tools/lldb-dap/lldb-dap.cpp
+++ b/lldb/tools/lldb-dap/lldb-dap.cpp
@@ -503,6 +503,14 @@ void EventThreadFunction() {
             SendContinuedEvent();
             break;
           case lldb::eStateExited:
+            const int exit_status = process.GetExitStatus();
+            const char *const exit_description = process.GetExitDescription();
+            g_dap.SendFormattedOutput(
+                OutputType::Console,
+                "Process %" PRIu64 " exited with status = %i (0x%8.8x) %s\n",
+                process.GetProcessID(), exit_status, exit_status,
+                exit_description ? exit_description : "");
+
             // When restarting, we can get an "exited" event for the process we
             // just killed with the old PID, or even with no PID. In that case
             // we don't have to terminate the session.



More information about the lldb-commits mailing list