Skip to content

Commit a37864e

Browse files
debugPort for debug adapter server in debugger settings (#1587)
* add debugPort for debug adapter server in debugger settings, add in debug doc * dispose current debug adapter server if port is changed
1 parent 8910390 commit a37864e

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

docs_espressif/en/debugproject.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ You can modify the configuration to suit your needs. Let's describe the configur
144144

145145
Some additional arguments you might use are:
146146

147+
- ``debugPort``: (Default: 43476) The port to launch the Eclipse CDT GDB Debug Adapter server. If not specified, it will use the default value of 43476.
147148
- ``runOpenOCD``: (Default: true). Run extension OpenOCD Server.
148149
- ``verifyAppBinBeforeDebug``: (Default: false) Verify that current ESP-IDF project binary is the same as binary in chip.
149150
- ``logFile``: Absolute path to the file to log interaction with gdb. Example: ${workspaceFolder}/gdb.log.

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,11 @@
18781878
"type": "string",
18791879
"description": "Working directory (cwd) to use when launching gdb. Defaults to the directory of the 'program'"
18801880
},
1881+
"debugPort": {
1882+
"type": "number",
1883+
"description": "Port for Debug Adapter server. Default: 43476",
1884+
"default": 43476
1885+
},
18811886
"runOpenOCD": {
18821887
"type": "boolean",
18831888
"description": "Run OpenOCD Server",
@@ -2147,6 +2152,11 @@
21472152
"type": "string",
21482153
"description": "Working directory (cwd) to use when launching gdb. Defaults to the directory of the 'program'"
21492154
},
2155+
"debugPort": {
2156+
"type": "number",
2157+
"description": "Port for Debug Adapter server. Default: 43476",
2158+
"default": 43476
2159+
},
21502160
"runOpenOCD": {
21512161
"type": "boolean",
21522162
"description": "Run OpenOCD Server",

src/cdtDebugAdapter/server.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export class CDTDebugAdapterDescriptorFactory
3737
executable: DebugAdapterExecutable | undefined
3838
): ProviderResult<DebugAdapterDescriptor> {
3939
if (!this.server) {
40-
// start listening on a random port
4140
const portToUse = session.configuration.debugPort || DEBUG_DEFAULT_PORT;
4241
this.server = createServer((socket) => {
4342
const gdbTargetDebugSession = new GDBTargetDebugSession();
@@ -46,13 +45,30 @@ export class CDTDebugAdapterDescriptorFactory
4645
}).listen(portToUse);
4746
}
4847

49-
// make VS Code connect to debug server
50-
return new DebugAdapterServer((<AddressInfo>this.server.address()).port);
48+
const address = this.server.address();
49+
if (address && typeof address === "object" && address.port) {
50+
return new DebugAdapterServer((<AddressInfo>address).port);
51+
} else {
52+
this.dispose();
53+
throw new Error("Failed to get CDT Debug Adapter server address or port.");
54+
}
55+
}
56+
57+
checkCurrentPort(port: number): boolean {
58+
if (!this.server) {
59+
return false;
60+
}
61+
const address = this.server.address();
62+
if (address && typeof address === "object" && address.port) {
63+
return (<AddressInfo>address).port === port;
64+
}
65+
return false;
5166
}
5267

5368
dispose() {
5469
if (this.server) {
5570
this.server.close();
71+
this.server = undefined;
5672
}
5773
}
5874
}

src/extension.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ const minIdfVersionCheck = async function (
265265

266266
let projectConfigManager: ProjectConfigurationManager | undefined;
267267

268+
let cdtDebugAdapterFactory: CDTDebugAdapterDescriptorFactory | undefined;
269+
268270
export async function activate(context: vscode.ExtensionContext) {
269271
// Always load Logger first
270272
Logger.init(context);
@@ -1201,6 +1203,21 @@ export async function activate(context: vscode.ExtensionContext) {
12011203

12021204
vscode.workspace.onDidChangeConfiguration(async (e) => {
12031205
const winFlag = process.platform === "win32" ? "Win" : "";
1206+
// Launch.json changes
1207+
if (e.affectsConfiguration("launch.configurations")) {
1208+
const config = vscode.workspace.getConfiguration("launch", workspaceRoot);
1209+
const configurations =
1210+
config.get<vscode.DebugConfiguration[]>("configurations") || [];
1211+
for (const conf of configurations) {
1212+
if (
1213+
conf.type === "gdbtarget" &&
1214+
conf.debugPort &&
1215+
!cdtDebugAdapterFactory.checkCurrentPort(conf.debugPort)
1216+
) {
1217+
cdtDebugAdapterFactory.dispose();
1218+
}
1219+
}
1220+
}
12041221
if (e.affectsConfiguration("idf.enableStatusBar")) {
12051222
const enableStatusBar = idfConf.readParameter(
12061223
"idf.enableStatusBar",
@@ -1270,10 +1287,11 @@ export async function activate(context: vscode.ExtensionContext) {
12701287
)
12711288
);
12721289

1290+
cdtDebugAdapterFactory = new CDTDebugAdapterDescriptorFactory();
12731291
context.subscriptions.push(
12741292
vscode.debug.registerDebugAdapterDescriptorFactory(
12751293
"gdbtarget",
1276-
new CDTDebugAdapterDescriptorFactory()
1294+
cdtDebugAdapterFactory
12771295
)
12781296
);
12791297

0 commit comments

Comments
 (0)