Skip to content

Commit

Permalink
Accumulate gdbserver output before checking for target port
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 eclipse-cdt-cloud#299
  • Loading branch information
jonahgraham committed Sep 22, 2023
1 parent 815600e commit 347f7cb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
18 changes: 14 additions & 4 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 @@ -243,6 +244,9 @@ export class GDBTargetDebugSession extends GDBDebugSession {
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 347f7cb

Please sign in to comment.