[Lldb-commits] [lldb] [lldb-dap] Allow providing debug adapter arguments in the extension (PR #129262)

Matthew Bastien via lldb-commits lldb-commits at lists.llvm.org
Fri Feb 28 11:58:16 PST 2025


https://github.com/matthewbastien updated https://github.com/llvm/llvm-project/pull/129262

>From b40c3e7e4ebb154c5f231676451acbd17e1f39f7 Mon Sep 17 00:00:00 2001
From: Matthew Bastien <matthew_bastien at apple.com>
Date: Fri, 28 Feb 2025 11:08:25 -0500
Subject: [PATCH 1/2] allow providing debug adapter arguments

---
 lldb/tools/lldb-dap/package.json              | 23 ++++++
 .../lldb-dap/src-ts/debug-adapter-factory.ts  | 72 +++++++++++++------
 2 files changed, 72 insertions(+), 23 deletions(-)

diff --git a/lldb/tools/lldb-dap/package.json b/lldb/tools/lldb-dap/package.json
index cd450a614b3f7..aa11d8bcaa66e 100644
--- a/lldb/tools/lldb-dap/package.json
+++ b/lldb/tools/lldb-dap/package.json
@@ -75,6 +75,15 @@
           "type": "string",
           "description": "The path to the lldb-dap binary."
         },
+        "lldb-dap.arguments": {
+          "scope": "resource",
+          "type": "array",
+          "default": [],
+          "items": {
+            "type": "string"
+          },
+          "description": "The arguments provided to the lldb-dap process."
+        },
         "lldb-dap.log-path": {
           "scope": "resource",
           "type": "string",
@@ -162,6 +171,13 @@
                 "type": "string",
                 "markdownDescription": "The absolute path to the LLDB debug adapter executable to use."
               },
+              "debugAdapterArgs": {
+                "type": "array",
+                "items": {
+                  "type": "string"
+                },
+                "markdownDescription": "The list of arguments used to launch the debug adapter executable."
+              },
               "program": {
                 "type": "string",
                 "description": "Path to the program to debug."
@@ -352,6 +368,13 @@
                 "type": "string",
                 "markdownDescription": "The absolute path to the LLDB debug adapter executable to use."
               },
+              "debugAdapterArgs": {
+                "type": "array",
+                "items": {
+                  "type": "string"
+                },
+                "markdownDescription": "The list of arguments used to launch the debug adapter executable."
+              },
               "program": {
                 "type": "string",
                 "description": "Path to the program to attach to."
diff --git a/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts b/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts
index 1f76fe31b00ad..51f45f87d660a 100644
--- a/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts
+++ b/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts
@@ -25,7 +25,7 @@ async function findWithXcrun(executable: string): Promise<string | undefined> {
       if (stdout) {
         return stdout.toString().trimEnd();
       }
-    } catch (error) { }
+    } catch (error) {}
   }
   return undefined;
 }
@@ -93,13 +93,33 @@ async function getDAPExecutable(
   return undefined;
 }
 
+function getDAPArguments(session: vscode.DebugSession): string[] {
+  // Check the debug configuration for arguments first
+  const debugConfigArgs = session.configuration.debugAdapterArgs;
+  if (
+    Array.isArray(debugConfigArgs) &&
+    debugConfigArgs.findIndex((entry) => typeof entry !== "string") === -1
+  ) {
+    return debugConfigArgs;
+  }
+  // Fall back on the workspace configuration
+  return vscode.workspace
+    .getConfiguration("lldb-dap")
+    .get<string[]>("arguments", []);
+}
+
 /**
  * This class defines a factory used to find the lldb-dap binary to use
  * depending on the session configuration.
  */
 export class LLDBDapDescriptorFactory
-  implements vscode.DebugAdapterDescriptorFactory, vscode.Disposable {
-  private server?: Promise<{ process: child_process.ChildProcess, host: string, port: number }>;
+  implements vscode.DebugAdapterDescriptorFactory, vscode.Disposable
+{
+  private server?: Promise<{
+    process: child_process.ChildProcess;
+    host: string;
+    port: number;
+  }>;
 
   dispose() {
     this.server?.then(({ process }) => {
@@ -109,7 +129,7 @@ export class LLDBDapDescriptorFactory
 
   async createDebugAdapterDescriptor(
     session: vscode.DebugSession,
-    executable: vscode.DebugAdapterExecutable | undefined,
+    _executable: vscode.DebugAdapterExecutable | undefined,
   ): Promise<vscode.DebugAdapterDescriptor | undefined> {
     const config = vscode.workspace.getConfiguration(
       "lldb-dap",
@@ -123,7 +143,7 @@ export class LLDBDapDescriptorFactory
     }
     const configEnvironment =
       config.get<{ [key: string]: string }>("environment") || {};
-    const dapPath = (await getDAPExecutable(session)) ?? executable?.command;
+    const dapPath = await getDAPExecutable(session);
 
     if (!dapPath) {
       LLDBDapDescriptorFactory.showLLDBDapNotFoundMessage();
@@ -137,32 +157,38 @@ export class LLDBDapDescriptorFactory
 
     const dbgOptions = {
       env: {
-        ...executable?.options?.env,
         ...configEnvironment,
         ...env,
       },
     };
-    const dbgArgs = executable?.args ?? [];
+    const dbgArgs = getDAPArguments(session);
 
-    const serverMode = config.get<boolean>('serverMode', false);
+    const serverMode = config.get<boolean>("serverMode", false);
     if (serverMode) {
-      const { host, port } = await this.startServer(dapPath, dbgArgs, dbgOptions);
+      const { host, port } = await this.startServer(
+        dapPath,
+        dbgArgs,
+        dbgOptions,
+      );
       return new vscode.DebugAdapterServer(port, host);
     }
 
     return new vscode.DebugAdapterExecutable(dapPath, dbgArgs, dbgOptions);
   }
 
-  startServer(dapPath: string, args: string[], options: child_process.CommonSpawnOptions): Promise<{ host: string, port: number }> {
-    if (this.server) return this.server;
+  startServer(
+    dapPath: string,
+    args: string[],
+    options: child_process.CommonSpawnOptions,
+  ): Promise<{ host: string; port: number }> {
+    if (this.server) {
+      return this.server;
+    }
 
-    this.server = new Promise(resolve => {
-      args.push(
-        '--connection',
-        'connect://localhost:0'
-      );
+    this.server = new Promise((resolve) => {
+      args.push("--connection", "connect://localhost:0");
       const server = child_process.spawn(dapPath, args, options);
-      server.stdout!.setEncoding('utf8').once('data', (data: string) => {
+      server.stdout!.setEncoding("utf8").once("data", (data: string) => {
         const connection = /connection:\/\/\[([^\]]+)\]:(\d+)/.exec(data);
         if (connection) {
           const host = connection[1];
@@ -170,9 +196,9 @@ export class LLDBDapDescriptorFactory
           resolve({ process: server, host, port });
         }
       });
-      server.on('exit', () => {
+      server.on("exit", () => {
         this.server = undefined;
-      })
+      });
     });
     return this.server;
   }
@@ -180,11 +206,11 @@ export class LLDBDapDescriptorFactory
   /**
    * Shows a message box when the debug adapter's path is not found
    */
-  static async showLLDBDapNotFoundMessage(path?: string) {
+  static async showLLDBDapNotFoundMessage(path?: string | undefined) {
     const message =
-      path
-        ? `Debug adapter path: ${path} is not a valid file.`
-        : "Unable to find the path to the LLDB debug adapter executable.";
+      path !== undefined
+        ? `Debug adapter path: ${path} is not a valid file`
+        : "Unable to find the LLDB debug adapter executable.";
     const openSettingsAction = "Open Settings";
     const callbackValue = await vscode.window.showErrorMessage(
       message,

>From 057ff4c9eed5c2344f5377e9199814c55f6748b1 Mon Sep 17 00:00:00 2001
From: Matthew Bastien <matthew_bastien at apple.com>
Date: Fri, 28 Feb 2025 11:22:41 -0500
Subject: [PATCH 2/2] update wording

---
 lldb/tools/lldb-dap/package.json                    | 6 +++---
 lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lldb/tools/lldb-dap/package.json b/lldb/tools/lldb-dap/package.json
index aa11d8bcaa66e..75d52786b01e8 100644
--- a/lldb/tools/lldb-dap/package.json
+++ b/lldb/tools/lldb-dap/package.json
@@ -82,7 +82,7 @@
           "items": {
             "type": "string"
           },
-          "description": "The arguments provided to the lldb-dap process."
+          "description": "The list of additional arguments used to launch the debug adapter executable."
         },
         "lldb-dap.log-path": {
           "scope": "resource",
@@ -176,7 +176,7 @@
                 "items": {
                   "type": "string"
                 },
-                "markdownDescription": "The list of arguments used to launch the debug adapter executable."
+                "markdownDescription": "The list of additional arguments used to launch the debug adapter executable."
               },
               "program": {
                 "type": "string",
@@ -373,7 +373,7 @@
                 "items": {
                   "type": "string"
                 },
-                "markdownDescription": "The list of arguments used to launch the debug adapter executable."
+                "markdownDescription": "The list of additional arguments used to launch the debug adapter executable."
               },
               "program": {
                 "type": "string",
diff --git a/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts b/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts
index 51f45f87d660a..8f1e8ad6b019a 100644
--- a/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts
+++ b/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts
@@ -210,7 +210,7 @@ export class LLDBDapDescriptorFactory
     const message =
       path !== undefined
         ? `Debug adapter path: ${path} is not a valid file`
-        : "Unable to find the LLDB debug adapter executable.";
+        : "Unable to find the path to the LLDB debug adapter executable.";
     const openSettingsAction = "Open Settings";
     const callbackValue = await vscode.window.showErrorMessage(
       message,



More information about the lldb-commits mailing list