Skip to content

Commit 60ef6e9

Browse files
committed
Find a free port for PyOCD telnet semihosting
1 parent bd81233 commit 60ef6e9

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/abstract-server.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ export abstract class AbstractServer extends EventEmitter {
4848

4949
try {
5050
this.timer = setTimeout(() => this.onSpawnError(new Error('Timeout waiting for gdb server to start')), TIMEOUT);
51+
5152
const command = args.gdbServer || 'gdb-server';
52-
this.process = spawn(command, args.gdbServerArguments, {
53+
const serverArguments = await this.resolveServerArguments(args.gdbServerArguments);
54+
this.process = spawn(command, serverArguments, {
5355
cwd: dirname(command),
5456
});
5557

@@ -78,6 +80,10 @@ export abstract class AbstractServer extends EventEmitter {
7880
}
7981
}
8082

83+
protected async resolveServerArguments(serverArguments?: string[]): Promise<string[]> {
84+
return serverArguments || [];
85+
}
86+
8187
protected onExit(code: number, signal: string) {
8288
this.emit('exit', code, signal);
8389

src/pyocd-server.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,35 @@
2424
*/
2525

2626
import { AbstractServer } from './abstract-server';
27+
import { PortScanner } from './port-scanner';
2728

2829
const LAUNCH_REGEX = /GDB server started/;
2930
const ERROR_REGEX = /:ERROR:gdbserver:/;
3031
const PERCENT_MULTIPLIER = 100 / 40; // pyOCD outputs 40 markers for progress
3132

3233
export class PyocdServer extends AbstractServer {
34+
35+
protected portScanner = new PortScanner();
3336
protected progress = 0;
3437

38+
protected async resolveServerArguments(serverArguments?: string[]): Promise<string[]> {
39+
if (!serverArguments) {
40+
serverArguments = [];
41+
}
42+
43+
const telnetPort = await this.portScanner.findFreePort(4444);
44+
45+
if (!telnetPort) {
46+
return serverArguments;
47+
}
48+
49+
return [
50+
...serverArguments,
51+
'--telnet-port',
52+
telnetPort.toString()
53+
];
54+
}
55+
3556
protected onStdout(chunk: string | Buffer) {
3657
super.onStdout(chunk);
3758
const buffer = typeof chunk === 'string' ? chunk : chunk.toString('utf8');

0 commit comments

Comments
 (0)