Skip to content

Commit

Permalink
Accumulate gdbserver output before checking for target port (#300)
Browse files Browse the repository at this point in the history
Because the needed output from gdbserver may not arrive as a single
'data' event on stdout we need to accumulate the output before
passing it to the regex checker.

Fixes #299
  • Loading branch information
jonahgraham authored Oct 10, 2023
1 parent 33b2f35 commit 86c1e77
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/GDBTargetDebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export class GDBTargetDebugSession extends GDBDebugSession {
await new Promise<void>((resolve, reject) => {
this.gdbserver = spawn(serverExe, serverParams, { cwd: serverCwd });
let gdbserverStartupResolved = false;
let accumulatedStdout = '';
let accumulatedStderr = '';
let checkTargetPort = (_data: any) => {
// do nothing by default
Expand All @@ -238,11 +239,14 @@ export class GDBTargetDebugSession extends GDBDebugSession {
const regex = new RegExp(
target.serverPortRegExp
? target.serverPortRegExp
: 'Listening on port ([0-9]+)'
: 'Listening on port ([0-9]+)\r?\n'
);
const m = regex.exec(data);
if (m !== null) {
target.port = m[1];
checkTargetPort = (_data: any) => {
// do nothing now that we have our port
};
setTimeout(
() => {
gdbserverStartupResolved = true;
Expand All @@ -257,8 +261,12 @@ export class GDBTargetDebugSession extends GDBDebugSession {
}
if (this.gdbserver.stdout) {
this.gdbserver.stdout.on('data', (data) => {
this.sendEvent(new OutputEvent(data.toString(), 'server'));
checkTargetPort(data);
const out = data.toString();
if (!gdbserverStartupResolved) {
accumulatedStdout += out;
}
this.sendEvent(new OutputEvent(out, 'server'));
checkTargetPort(accumulatedStdout);
});
} else {
throw new Error('Missing stdout in spawned gdbserver');
Expand All @@ -267,9 +275,11 @@ export class GDBTargetDebugSession extends GDBDebugSession {
if (this.gdbserver.stderr) {
this.gdbserver.stderr.on('data', (data) => {
const err = data.toString();
accumulatedStderr += err;
if (!gdbserverStartupResolved) {
accumulatedStderr += err;
}
this.sendEvent(new OutputEvent(err, 'server'));
checkTargetPort(data);
checkTargetPort(accumulatedStderr);
});
} else {
throw new Error('Missing stderr in spawned gdbserver');
Expand Down
6 changes: 4 additions & 2 deletions src/integration-tests/attachRemote.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ describe('attach remote', function () {
}
);
port = await new Promise<number>((resolve, reject) => {
let accumulatedStderr = '';
if (gdbserver.stderr) {
gdbserver.stderr.on('data', (data) => {
const line = String(data);
accumulatedStderr += line;
const LISTENING_ON_PORT = 'Listening on port ';
const index = line.indexOf(LISTENING_ON_PORT);
const index = accumulatedStderr.indexOf(LISTENING_ON_PORT);
if (index >= 0) {
const portStr = line
const portStr = accumulatedStderr
.substr(index + LISTENING_ON_PORT.length, 6)
.trim();
resolve(parseInt(portStr, 10));
Expand Down

0 comments on commit 86c1e77

Please sign in to comment.