Skip to content

Commit 722e4c5

Browse files
committed
feat(swo): wait for SWO connection to complete before starting debug
1 parent 9ff7e3a commit 722e4c5

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/frontend/swo/core.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,14 @@ export class SWOCore extends SWORTTCoreBase {
289289
}, (error) => {
290290
this.functionSymbols = [];
291291
});
292-
293-
if (this.source.connected) { this.connected = true; }
294-
else { this.source.on('connected', () => { this.connected = true; }); }
292+
293+
const onConnected = () => {
294+
this.connected = true;
295+
session.customRequest('swo-connected');
296+
};
297+
298+
if (this.source.connected) { onConnected(); }
299+
else { this.source.on('connected', onConnected); }
295300
this.source.on('data', this.handleData.bind(this));
296301
this.source.on('disconnected', this.handleDisconnected.bind(this));
297302

src/gdb.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import { extractBits, hexFormat } from './frontend/utils';
1515
import { Variable, VariableObject, MIError, OurDataBreakpoint, OurInstructionBreakpoint, OurSourceBreakpoint } from './backend/backend';
1616
import {
1717
TelemetryEvent, ConfigurationArguments, StoppedEvent, GDBServerController, SymbolFile,
18-
createPortName, GenericCustomEvent, quoteShellCmdLine, toStringDecHexOctBin, ADAPTER_DEBUG_MODE, defSymbolFile, CTIAction, getPathRelative
18+
createPortName, GenericCustomEvent, quoteShellCmdLine, toStringDecHexOctBin, ADAPTER_DEBUG_MODE, defSymbolFile, CTIAction, getPathRelative,
19+
SWOConfigureEvent
1920
} from './common';
2021
import { GDBServer, ServerConsoleLog } from './backend/server';
2122
import { MINode } from './backend/mi_parse';
@@ -267,6 +268,9 @@ export class GDBDebugSession extends LoggingDebugSession {
267268

268269
protected tcpPortAllocatedListner = this.tcpPortsAllocated.bind(this);
269270

271+
private swoLaunchPromise = Promise.resolve();
272+
private swoLaunched?: () => void;
273+
270274
public constructor(debuggerLinesStartAt1: boolean, public readonly isServer: boolean = false, threadID: number = 1) {
271275
super(undefined, debuggerLinesStartAt1, isServer); // Use if deriving from LogDebugSession
272276
// super(debuggerLinesStartAt1, isServer); // Use if deriving from DebugSession
@@ -660,6 +664,9 @@ export class GDBDebugSession extends LoggingDebugSession {
660664
if (showTimes) { this.handleMsg('log', 'Debug Time: objdump and nm done...\n'); }
661665
if (showTimes) { this.handleMsg('log', 'Debug Time: All pending items done, proceed to gdb connect...\n'); }
662666

667+
// if SWO launch was requested by the server controller, we wait for it to connect before starting actual debug
668+
await this.swoLaunchPromise;
669+
663670
// This is the last of the place where ports are allocated
664671
this.sendEvent(new GenericCustomEvent('post-start-server', this.args));
665672
commands.push(...this.serverController.initCommands());
@@ -1211,6 +1218,11 @@ export class GDBDebugSession extends LoggingDebugSession {
12111218
}
12121219
break;
12131220
}
1221+
case 'swo-connected': {
1222+
this.swoLaunched?.();
1223+
this.swoLaunched = undefined;
1224+
break;
1225+
}
12141226
default:
12151227
response.body = { error: 'Invalid command.' };
12161228
this.sendResponse(response);
@@ -1674,6 +1686,16 @@ export class GDBDebugSession extends LoggingDebugSession {
16741686
}
16751687

16761688
private serverControllerEvent(event: DebugProtocol.Event) {
1689+
if (event instanceof SWOConfigureEvent) {
1690+
this.swoLaunchPromise = new Promise((resolve, reject) => {
1691+
const timeout = setTimeout(() => reject(new Error('Timeout waiting for SWV connection')), 1000);
1692+
this.swoLaunched = () => {
1693+
clearTimeout(timeout);
1694+
resolve();
1695+
};
1696+
});
1697+
}
1698+
16771699
this.sendEvent(event);
16781700
}
16791701

0 commit comments

Comments
 (0)