Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

royitaqi
Copy link
Contributor

@royitaqi royitaqi commented Sep 18, 2025

A few improvements to logging when lldb-dap is started in Server Mode AND when the lldb-dap.logFolder setting is used (not lldb-dap.log-path).

Improvement #1

Avoid the prompt of restarting the server when starting each debug session.

That prompt is caused by the combination of the following facts:

  1. The log filename changes every time a new debug session is starting (see here)
  2. The log filename is passed to the server via an environment variable called "LLDBDAP_LOG" (see here)
  3. All environment variables are put into the "spawn info" variable (see here).
  4. The old and new "spawn info" are compared to decide if a prompt should show (see here).

The fix is to remove the "LLDBDAP_LOG" from the "spawn info" variable, so that the same server can be reused if the log path is the only thing that has changed.

Improvement #2

Avoid log file conflict when multiple users share a machine and start server in the same second.

The problem: If two users start lldb-dap server in the same second, they will share the same log path. The first user will create the log file. The second user will find that they cannot access the same file, so their server will fail to start.

The fix is to add a part of the VS Code session ID to the log filename.

Improvement #3

Avoid restarting the server when the order of environment variables changed.

This is done by sorting the environment variables before putting them into the "spawn info".

@llvmbot
Copy link
Member

llvmbot commented Sep 18, 2025

@llvm/pr-subscribers-lldb

Author: Roy Shi (royitaqi)

Changes

A few improvements to logging when lldb-dap is started in Server Mode AND when the lldb-dap.logFolder setting is used (not lldb-dap.log-path).

Improvement #1

Avoid the prompt of restarting the server at the start of each debug session.

That prompt is caused by the combination of the following facts:

  1. The log filename changes every time a new debug session is starting (see here)
  2. The log filename is passed to the server via an environment variable called "LLDBDAP_LOG" (see here)
  3. All environment variables are put into the "spawn info" variable (see here).
  4. The old and new "spawn info" are compared to decide if a prompt should show (see here).

The fix is to remove the "LLDBDAP_LOG" from the "spawn info" variable, so that the same server can be reused if the log path is the only thing that has changed.

Improvement #2

Avoid log file conflict when multiple users share a machine and start server in the same second.

The problem: If two users start lldb-dap server in the same second, they will share the same log path. The first user will create the log file, and the second the user will find that they cannot access the same file, so their server will fail to start.

The fix is to add username to the log path.

Limitation: In theory, one user can start two server in the same second to have the same access violation. However, to hit this issue, they will have to start the debug sessions in two separate VS Code instances (and thus two lldb-dap extensions). I'm not sure how common that is. A more correct fix would be to use some kind of session UUID. It seems we don't have that concept at least yet.

Improvement #3

Avoid restarting the server when the order of environment variables changed.

This is done by sorting the environment variables before putting them into the "spawn info".


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

2 Files Affected:

  • (modified) lldb/tools/lldb-dap/src-ts/lldb-dap-server.ts (+6-3)
  • (modified) lldb/tools/lldb-dap/src-ts/logging.ts (+2-1)
diff --git a/lldb/tools/lldb-dap/src-ts/lldb-dap-server.ts b/lldb/tools/lldb-dap/src-ts/lldb-dap-server.ts
index 774be50053a17..280a11d807f6a 100644
--- a/lldb/tools/lldb-dap/src-ts/lldb-dap-server.ts
+++ b/lldb/tools/lldb-dap/src-ts/lldb-dap-server.ts
@@ -167,9 +167,12 @@ Restarting the server will interrupt any existing debug sessions and start a new
     return [
       path,
       ...args,
-      ...Object.entries(env ?? {}).map(
-        (entry) => String(entry[0]) + "=" + String(entry[1]),
-      ),
+      ...Object.entries(env ?? {})
+        // Filter and sort to avoid restarting the server just because the
+        // order of env changed or the log path changed.
+        .filter((entry) => String(entry[0]) !== "LLDBDAP_LOG")
+        .sort()
+        .map((entry) => String(entry[0]) + "=" + String(entry[1])),
     ];
   }
 }
diff --git a/lldb/tools/lldb-dap/src-ts/logging.ts b/lldb/tools/lldb-dap/src-ts/logging.ts
index 3b1c3c37ce1ce..e3b6c56291cec 100644
--- a/lldb/tools/lldb-dap/src-ts/logging.ts
+++ b/lldb/tools/lldb-dap/src-ts/logging.ts
@@ -1,3 +1,4 @@
+import * as os from 'os';
 import * as path from "path";
 import * as vscode from "vscode";
 
@@ -44,7 +45,7 @@ export class LogFilePathProvider {
     const logFolder = this.logFolder || this.context.logUri.fsPath;
     switch(type) {
     case LogType.DEBUG_SESSION:
-        return path.join(logFolder, `lldb-dap-session-${formatDate(new Date())}.log`);
+        return path.join(logFolder, `lldb-dap-session-${os.userInfo().username}-${formatDate(new Date())}.log`);
         break;
     }
   }

@JDevlieghere
Copy link
Member

Instead of using the username, can we use something more anonymous like the VSCode PID?

@royitaqi
Copy link
Contributor Author

royitaqi commented Sep 19, 2025

Instead of using the username, can we use something more anonymous like the VSCode PID?

I like the VSCode PID idea. It's more anonymous AND it avoids conflict when one user start two debug sessions from two VSCode instances in the same second. FWIW, the downside is that it's more difficult to find oneself's logs.

I will update the patch.

@royitaqi
Copy link
Contributor Author

royitaqi commented Sep 19, 2025

@JDevlieghere VSCode PID isn't readily accessible (see ref and ref), so I used the VSCode session ID instead, which is also anonymous and unique.

However, it does make the filename very long. Hope that's okay.

lldb-dap-session-20250919T133507-vscode-54067173-4e17-4947-91b7-6316a298ab321758314104490.log

If it helps, I can just keep the first segment of the UUID, so that it will look like:

lldb-dap-session-20250919T133507-vscode-54067173.log

Will wait for your input before merging. Kindly LMK.

@JDevlieghere
Copy link
Member

Using the session ID sounds fine. I suppose the first segment of the UUID is unique enough, so if it's easy, I'd lean towards that. I also think we can drop the -session and -vscode from the filename (unless forks like cursor would report something else and folks think that useful).

Ideally, the filename would look like this:

lldb-dap-20250919T133507-54067173.log

@royitaqi
Copy link
Contributor Author

royitaqi commented Sep 19, 2025

@JDevlieghere Here you go.

lldb-dap-20250919T135246-90ee62ea.log

--

drop the -session and -vscode

Originally I added "-vscode" as a visual separator between the timestamp and the UUID (which contains multiple "-"). Now it's shorter I think it's good to remove it.

--

unless forks like cursor would report something else and folks think that useful

I'm happy to merge the current version - can always update later if/when needed.

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you!

@royitaqi royitaqi merged commit 1250095 into llvm:main Sep 19, 2025
9 checks passed
SeongjaeP pushed a commit to SeongjaeP/llvm-project that referenced this pull request Sep 23, 2025
A few improvements to logging when lldb-dap is started in **Server
Mode** AND when the **`lldb-dap.logFolder`** setting is used (not
`lldb-dap.log-path`).

### Improvement llvm#1
**Avoid the prompt of restarting the server when starting each debug
session.**

That prompt is caused by the combination of the following facts:
1. The log filename changes every time a new debug session is starting
(see
[here](https://github.com/llvm/llvm-project/blob/9d6062c490548a5e6fea103e010ab3c9bc73a86d/lldb/tools/lldb-dap/src-ts/logging.ts#L47))
2. The log filename is passed to the server via an environment variable
called "LLDBDAP_LOG" (see
[here](https://github.com/llvm/llvm-project/blob/9d6062c490548a5e6fea103e010ab3c9bc73a86d/lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts#L263-L269))
3. All environment variables are put into the "spawn info" variable (see
[here](https://github.com/llvm/llvm-project/blob/9d6062c490548a5e6fea103e010ab3c9bc73a86d/lldb/tools/lldb-dap/src-ts/lldb-dap-server.ts#L170-L172)).
4. The old and new "spawn info" are compared to decide if a prompt
should show (see
[here](https://github.com/llvm/llvm-project/blob/9d6062c490548a5e6fea103e010ab3c9bc73a86d/lldb/tools/lldb-dap/src-ts/lldb-dap-server.ts#L107-L110)).

The fix is to remove the "LLDBDAP_LOG" from the "spawn info" variable,
so that the same server can be reused if the log path is the only thing
that has changed.

### Improvement llvm#2
**Avoid log file conflict when multiple users share a machine and start
server in the same second.**

The problem: If two users start lldb-dap server in the same second, they
will share the same log path. The first user will create the log file.
The second user will find that they cannot access the same file, so
their server will fail to start.

The fix is to add a part of the VS Code session ID to the log filename.

### Improvement llvm#3
**Avoid restarting the server when the order of environment variables
changed.**

This is done by sorting the environment variables before putting them
into the "spawn info".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants