Skip to content

Commit d61715e

Browse files
committed
use capabilities event to determine SupportsModuleSymbolsRequest instead of lldb version
1 parent 6fccbaa commit d61715e

File tree

10 files changed

+68
-29
lines changed

10 files changed

+68
-29
lines changed

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,27 @@ protocol::Capabilities DAP::GetCapabilities() {
12581258
return capabilities;
12591259
}
12601260

1261+
protocol::Capabilities DAP::GetCustomCapabilities() {
1262+
protocol::Capabilities capabilities;
1263+
1264+
// Add all custom capabilities here.
1265+
const llvm::DenseSet<AdapterFeature> all_custom_features = {
1266+
protocol::eAdapterFeatureSupportsModuleSymbolsRequest,
1267+
};
1268+
1269+
for (auto &kv : request_handlers) {
1270+
llvm::SmallDenseSet<AdapterFeature, 1> features =
1271+
kv.second->GetSupportedFeatures();
1272+
1273+
for (auto &feature : features) {
1274+
if (all_custom_features.contains(feature))
1275+
capabilities.supportedFeatures.insert(feature);
1276+
}
1277+
}
1278+
1279+
return capabilities;
1280+
}
1281+
12611282
void DAP::StartEventThread() {
12621283
event_thread = std::thread(&DAP::EventThread, this);
12631284
}

lldb/tools/lldb-dap/DAP.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ struct DAP final : private DAPTransport::MessageHandler {
367367
/// The set of capabilities supported by this adapter.
368368
protocol::Capabilities GetCapabilities();
369369

370+
/// The set of custom capabilities supported by this adapter.
371+
protocol::Capabilities GetCustomCapabilities();
372+
370373
/// Debuggee will continue from stopped state.
371374
void WillContinue() { variables.Clear(); }
372375

lldb/tools/lldb-dap/EventHelper.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ void SendTargetBasedCapabilities(DAP &dap) {
5959
dap.Send(protocol::Event{"capabilities", body});
6060
}
6161

62+
void SendCustomCapabilities(DAP &dap) {
63+
protocol::CapabilitiesEventBody body;
64+
body.capabilities = dap.GetCustomCapabilities();
65+
66+
// Notify the client about the custom capabilities.
67+
dap.Send(protocol::Event{"capabilities", body});
68+
}
69+
6270
// "ProcessEvent": {
6371
// "allOf": [
6472
// { "$ref": "#/definitions/Event" },

lldb/tools/lldb-dap/EventHelper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ enum LaunchMethod { Launch, Attach, AttachForSuspendedLaunch };
2020
/// Update capabilities based on the configured target.
2121
void SendTargetBasedCapabilities(DAP &dap);
2222

23+
/// Send lldb-dap custom capabilities.
24+
void SendCustomCapabilities(DAP &dap);
25+
2326
void SendProcessEvent(DAP &dap, LaunchMethod launch_method);
2427

2528
llvm::Error SendThreadStoppedEvent(DAP &dap, bool on_entry = false);

lldb/tools/lldb-dap/Handler/ConfigurationDoneRequestHandler.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ ConfigurationDoneRequestHandler::Run(const ConfigurationDoneArguments &) const {
4646
// may have different capabilities than the final target.
4747
SendTargetBasedCapabilities(dap);
4848

49+
/// Send custom capabilities to the client.
50+
/// This is consumed by the lldb-dap specific editor extension.
51+
SendCustomCapabilities(dap);
52+
4953
// Clients can request a baseline of currently existing threads after
5054
// we acknowledge the configurationDone request.
5155
// Client requests the baseline of currently existing threads after

lldb/tools/lldb-dap/Handler/RequestHandler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,9 @@ class ModuleSymbolsRequestHandler
601601
public:
602602
using RequestHandler::RequestHandler;
603603
static llvm::StringLiteral GetCommand() { return "moduleSymbols"; }
604+
FeatureSet GetSupportedFeatures() const override {
605+
return {protocol::eAdapterFeatureSupportsModuleSymbolsRequest};
606+
}
604607
llvm::Expected<protocol::ModuleSymbolsResponseBody>
605608
Run(const protocol::ModuleSymbolsArguments &args) const override;
606609
};

lldb/tools/lldb-dap/Protocol/ProtocolTypes.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ static llvm::StringLiteral ToString(AdapterFeature feature) {
335335
return "supportsWriteMemoryRequest";
336336
case eAdapterFeatureTerminateDebuggee:
337337
return "supportTerminateDebuggee";
338+
case eAdapterFeatureSupportsModuleSymbolsRequest:
339+
return "supportsModuleSymbolsRequest";
338340
}
339341
llvm_unreachable("unhandled adapter feature.");
340342
}
@@ -406,6 +408,8 @@ bool fromJSON(const llvm::json::Value &Params, AdapterFeature &feature,
406408
eAdapterFeatureValueFormattingOptions)
407409
.Case("supportsWriteMemoryRequest", eAdapterFeatureWriteMemoryRequest)
408410
.Case("supportTerminateDebuggee", eAdapterFeatureTerminateDebuggee)
411+
.Case("supportsModuleSymbolsRequest",
412+
eAdapterFeatureSupportsModuleSymbolsRequest)
409413
.Default(std::nullopt);
410414

411415
if (!parsedFeature) {

lldb/tools/lldb-dap/Protocol/ProtocolTypes.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,11 @@ enum AdapterFeature : unsigned {
242242
/// The debug adapter supports the `terminateDebuggee` attribute on the
243243
/// `disconnect` request.
244244
eAdapterFeatureTerminateDebuggee,
245+
/// The debug adapter supports the `supportsModuleSymbols` request.
246+
/// This request is a custom request of lldb-dap.
247+
eAdapterFeatureSupportsModuleSymbolsRequest,
245248
eAdapterFeatureFirst = eAdapterFeatureANSIStyling,
246-
eAdapterFeatureLast = eAdapterFeatureTerminateDebuggee,
249+
eAdapterFeatureLast = eAdapterFeatureSupportsModuleSymbolsRequest,
247250
};
248251
bool fromJSON(const llvm::json::Value &, AdapterFeature &, llvm::json::Path);
249252
llvm::json::Value toJSON(const AdapterFeature &);

lldb/tools/lldb-dap/src-ts/debug-session-tracker.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { DebugProtocol } from "@vscode/debugprotocol";
22
import * as vscode from "vscode";
33

4+
export interface LLDBDapCapabilities extends DebugProtocol.Capabilities {
5+
/** The debug adapter supports the `moduleSymbols` request. */
6+
supportsModuleSymbolsRequest?: boolean;
7+
}
8+
49
/** A helper type for mapping event types to their corresponding data type. */
510
// prettier-ignore
611
interface EventMap {
712
"module": DebugProtocol.ModuleEvent;
813
"exited": DebugProtocol.ExitedEvent;
9-
"initialized": DebugProtocol.InitializedEvent;
14+
"capabilities": DebugProtocol.CapabilitiesEvent;
1015
}
1116

1217
/** A type assertion to check if a ProtocolMessage is an event or if it is a specific event. */
@@ -40,7 +45,8 @@ export class DebugSessionTracker
4045
private modulesChanged = new vscode.EventEmitter<
4146
vscode.DebugSession | undefined
4247
>();
43-
private sessionInitialized = new vscode.EventEmitter<vscode.DebugSession>();
48+
private sessionGotCapabilities =
49+
new vscode.EventEmitter<[ vscode.DebugSession, LLDBDapCapabilities ]>();
4450
private sessionExited = new vscode.EventEmitter<vscode.DebugSession>();
4551

4652
/**
@@ -52,8 +58,9 @@ export class DebugSessionTracker
5258
this.modulesChanged.event;
5359

5460
/** Fired when a debug session is initialized. */
55-
onDidInitializeSession: vscode.Event<vscode.DebugSession> =
56-
this.sessionInitialized.event;
61+
onDidGetSessionCapabilities:
62+
vscode.Event<[ vscode.DebugSession, LLDBDapCapabilities ]> =
63+
this.sessionGotCapabilities.event;
5764

5865
/** Fired when a debug session is exiting. */
5966
onDidExitSession: vscode.Event<vscode.DebugSession> =
@@ -159,8 +166,8 @@ export class DebugSessionTracker
159166
);
160167

161168
this.sessionExited.fire(session);
162-
} else if (isEvent(message, "initialized")) {
163-
this.sessionInitialized.fire(session);
169+
} else if (isEvent(message, "capabilities")) {
170+
this.sessionGotCapabilities.fire([ session, message.body.capabilities ]);
164171
}
165172
}
166173
}

lldb/tools/lldb-dap/src-ts/ui/symbols-provider.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,18 @@ export class SymbolsProvider extends DisposableContext {
3535
},
3636
));
3737

38-
this.tracker.onDidInitializeSession((session) => {
39-
this.GetLLDBServerVersion(session).then((version) => {
40-
if (version !== undefined) {
41-
if (version[0] >= 22) {
42-
vscode.commands.executeCommand("setContext", "lldb-dap.supportsModuleSymbolsRequest", true);
43-
}
44-
}
45-
});
38+
this.tracker.onDidGetSessionCapabilities(([ _session, capabilities ]) => {
39+
if (capabilities.supportsModuleSymbolsRequest) {
40+
vscode.commands.executeCommand(
41+
"setContext", "lldb-dap.supportsModuleSymbolsRequest", true);
42+
}
4643
});
4744

4845
this.tracker.onDidExitSession((_session) => {
4946
vscode.commands.executeCommand("setContext", "lldb-dap.supportsModuleSymbolsRequest", false);
5047
});
5148
}
5249

53-
private async GetLLDBServerVersion(session: vscode.DebugSession): Promise<[number, number, number] | undefined> {
54-
const commandEscapePrefix = session.configuration.commandEscapePrefix || getDefaultConfigKey("commandEscapePrefix");
55-
const response = await session.customRequest("evaluate", { expression: commandEscapePrefix + "version", context: "repl" });
56-
57-
const versionLine = response.result?.split("\n")[0];
58-
if (!versionLine) return undefined;
59-
60-
const versionMatch = versionLine.match(/(\d+)\.(\d+)\.(\d+)/);
61-
if (!versionMatch) return undefined;
62-
63-
const [major, minor, patch] = versionMatch.slice(1, 4).map(Number);
64-
return [major, minor, patch];
65-
}
66-
6750
private async SelectModuleAndShowSymbols(session: vscode.DebugSession) {
6851
const modules = this.tracker.debugSessionModules(session);
6952
if (!modules || modules.length === 0) {

0 commit comments

Comments
 (0)