[Lldb-commits] [lldb] [lldb-dap] Implement quiet commands (PR #74808)

via lldb-commits lldb-commits at lists.llvm.org
Thu Dec 7 21:14:59 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Walter Erquinigo (walter-erquinigo)

<details>
<summary>Changes</summary>

This adds support for optionally prefixing any command with `?`, which effectively prevents the output of these commands to be printed to the console unless they fail. This comes handy when programmaticaly running commands on behalf of the user without wanting them to know unless they fail.

In a later PR I plan to implement the `!` prefix for commands that abort lldb-dap upon errors.


---
Full diff: https://github.com/llvm/llvm-project/pull/74808.diff


5 Files Affected:

- (added) lldb/test/API/tools/lldb-dap/commands/Makefile (+3) 
- (added) lldb/test/API/tools/lldb-dap/commands/TestDAP_commands.py (+24) 
- (added) lldb/test/API/tools/lldb-dap/commands/main.cpp (+1) 
- (modified) lldb/tools/lldb-dap/LLDBUtils.cpp (+25-6) 
- (modified) lldb/tools/lldb-dap/package.json (+12-12) 


``````````diff
diff --git a/lldb/test/API/tools/lldb-dap/commands/Makefile b/lldb/test/API/tools/lldb-dap/commands/Makefile
new file mode 100644
index 0000000000000..99998b20bcb05
--- /dev/null
+++ b/lldb/test/API/tools/lldb-dap/commands/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+
+include Makefile.rules
diff --git a/lldb/test/API/tools/lldb-dap/commands/TestDAP_commands.py b/lldb/test/API/tools/lldb-dap/commands/TestDAP_commands.py
new file mode 100644
index 0000000000000..f2b045b391b5c
--- /dev/null
+++ b/lldb/test/API/tools/lldb-dap/commands/TestDAP_commands.py
@@ -0,0 +1,24 @@
+import os
+
+import dap_server
+import lldbdap_testcase
+from lldbsuite.test import lldbtest, lldbutil
+from lldbsuite.test.decorators import *
+
+
+class TestDAP_commands(lldbdap_testcase.DAPTestCaseBase):
+    def test_command_directive_quiet_on_success(self):
+        program = self.getBuildArtifact("a.out")
+        command_quiet = (
+            "settings set target.show-hex-variable-values-with-leading-zeroes false"
+        )
+        command_not_quiet = (
+            "settings set target.show-hex-variable-values-with-leading-zeroes true"
+        )
+        self.build_and_launch(
+            program,
+            initCommands=["?" + command_quiet, command_not_quiet],
+        )
+        full_output = self.collect_console(duration=1.0)
+        self.assertNotIn(command_quiet, full_output)
+        self.assertIn(command_not_quiet, full_output)
diff --git a/lldb/test/API/tools/lldb-dap/commands/main.cpp b/lldb/test/API/tools/lldb-dap/commands/main.cpp
new file mode 100644
index 0000000000000..76e8197013aab
--- /dev/null
+++ b/lldb/test/API/tools/lldb-dap/commands/main.cpp
@@ -0,0 +1 @@
+int main() { return 0; }
diff --git a/lldb/tools/lldb-dap/LLDBUtils.cpp b/lldb/tools/lldb-dap/LLDBUtils.cpp
index 955c11f636895..5be36edb79df0 100644
--- a/lldb/tools/lldb-dap/LLDBUtils.cpp
+++ b/lldb/tools/lldb-dap/LLDBUtils.cpp
@@ -16,13 +16,18 @@ void RunLLDBCommands(llvm::StringRef prefix,
                      llvm::raw_ostream &strm) {
   if (commands.empty())
     return;
-  lldb::SBCommandInterpreter interp = g_dap.debugger.GetCommandInterpreter();
-  if (!prefix.empty())
-    strm << prefix << "\n";
-  for (const auto &command : commands) {
-    lldb::SBCommandReturnObject result;
+
+  bool did_print_prefix = false;
+
+  auto print_result = [&](llvm::StringRef command,
+                          lldb::SBCommandReturnObject &result) {
+    // We only print the prefix if we are effectively printing at least one
+    // command.
+    if (!did_print_prefix && !prefix.empty()) {
+      strm << prefix << "\n";
+      did_print_prefix = true;
+    }
     strm << "(lldb) " << command << "\n";
-    interp.HandleCommand(command.c_str(), result);
     auto output_len = result.GetOutputSize();
     if (output_len) {
       const char *output = result.GetOutput();
@@ -33,6 +38,20 @@ void RunLLDBCommands(llvm::StringRef prefix,
       const char *error = result.GetError();
       strm << error;
     }
+  };
+
+  lldb::SBCommandInterpreter interp = g_dap.debugger.GetCommandInterpreter();
+  for (llvm::StringRef command : commands) {
+    lldb::SBCommandReturnObject result;
+    llvm::StringRef effective_command = command;
+    bool quiet_on_success = false;
+    if (command.startswith("?")) {
+      effective_command = command.drop_front();
+      quiet_on_success = true;
+    }
+    interp.HandleCommand(effective_command.str().c_str(), result);
+    if (!(result.Succeeded() && quiet_on_success))
+      print_result(effective_command, result);
   }
 }
 
diff --git a/lldb/tools/lldb-dap/package.json b/lldb/tools/lldb-dap/package.json
index ebb1103d695e1..8734b1ade0766 100644
--- a/lldb/tools/lldb-dap/package.json
+++ b/lldb/tools/lldb-dap/package.json
@@ -204,32 +204,32 @@
 							},
 							"initCommands": {
 								"type": "array",
-								"description": "Initialization commands executed upon debugger startup.",
+								"description": "Initialization commands executed upon debugger startup. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"preRunCommands": {
 								"type": "array",
-								"description": "Commands executed just before the program is launched.",
+								"description": "Commands executed just before the program is launched. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"postRunCommands": {
 								"type": "array",
-								"description": "Commands executed just as soon as the program is successfully launched when it's in a stopped state prior to any automatic continuation.",
+								"description": "Commands executed just as soon as the program is successfully launched when it's in a stopped state prior to any automatic continuation. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"launchCommands": {
 								"type": "array",
-								"description": "Custom commands that are executed instead of launching a process. A target will be created with the launch arguments prior to executing these commands. The commands may optionally create a new target and must perform a launch. A valid process must exist after these commands complete or the \"launch\" will fail. Launch the process with \"process launch -s\" to make the process to at the entry point since lldb-dap will auto resume if necessary.",
+								"description": "Custom commands that are executed instead of launching a process. A target will be created with the launch arguments prior to executing these commands. The commands may optionally create a new target and must perform a launch. A valid process must exist after these commands complete or the \"launch\" will fail. Launch the process with \"process launch -s\" to make the process to at the entry point since lldb-dap will auto resume if necessary. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"stopCommands": {
 								"type": "array",
-								"description": "Commands executed each time the program stops.",
+								"description": "Commands executed each time the program stops. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"exitCommands": {
 								"type": "array",
-								"description": "Commands executed at the end of debugging session.",
+								"description": "Commands executed at the end of debugging session. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"runInTerminal": {
@@ -309,32 +309,32 @@
 							},
 							"attachCommands": {
 								"type": "array",
-								"description": "Custom commands that are executed instead of attaching to a process ID or to a process by name. These commands may optionally create a new target and must perform an attach. A valid process must exist after these commands complete or the \"attach\" will fail.",
+								"description": "Custom commands that are executed instead of attaching to a process ID or to a process by name. These commands may optionally create a new target and must perform an attach. A valid process must exist after these commands complete or the \"attach\" will fail. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"initCommands": {
 								"type": "array",
-								"description": "Initialization commands executed upon debugger startup.",
+								"description": "Initialization commands executed upon debugger startup. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"preRunCommands": {
 								"type": "array",
-								"description": "Commands executed just before the program is attached to.",
+								"description": "Commands executed just before the program is attached to. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"postRunCommands": {
 								"type": "array",
-								"description": "Commands executed just as soon as the program is successfully attached when it's in a stopped state prior to any automatic continuation.",
+								"description": "Commands executed just as soon as the program is successfully attached when it's in a stopped state prior to any automatic continuation. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"stopCommands": {
 								"type": "array",
-								"description": "Commands executed each time the program stops.",
+								"description": "Commands executed each time the program stops. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"exitCommands": {
 								"type": "array",
-								"description": "Commands executed at the end of debugging session.",
+								"description": "Commands executed at the end of debugging session. Individual commands prefixed with `?` won't be printed to the console unless they fail.",
 								"default": []
 							},
 							"coreFile": {

``````````

</details>


https://github.com/llvm/llvm-project/pull/74808


More information about the lldb-commits mailing list