-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[vscode-lldb] Improve logging in Server Mode #159672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[vscode-lldb] Improve logging in Server Mode #159672
Conversation
@llvm/pr-subscribers-lldb Author: Roy Shi (royitaqi) ChangesA few improvements to logging when lldb-dap is started in Server Mode AND when the Improvement #1Avoid the prompt of restarting the server at the start of each debug session. That prompt is caused by the combination of the following facts:
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 #2Avoid 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 #3Avoid 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:
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;
}
}
|
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. |
@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.
If it helps, I can just keep the first segment of the UUID, so that it will look like:
Will wait for your input before merging. Kindly LMK. |
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 Ideally, the filename would look like this:
|
@JDevlieghere Here you go.
--
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. --
I'm happy to merge the current version - can always update later if/when needed. |
There was a problem hiding this 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!
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".
A few improvements to logging when lldb-dap is started in Server Mode AND when the
lldb-dap.logFolder
setting is used (notlldb-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:
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".