Skip to content

Commit 22fadbf

Browse files
authored
Avoid unnecessary ThreadInfoRequests (#416)
To query GDB's current thread ID, don't use "-thread-info", which does an expensive roundtrip to the target (several kilobytes of XML transmitted over the remote protocol connection). The current thread is known by GDB itself, and while there does not seem to be a command to query it directly, it can be read by evaluating convenience variable $_gthread. Signed-off-by: Christian Walther <walther@indel.ch>
1 parent c5ddcbe commit 22fadbf

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/gdb/GDBBackend.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
MIBreakpointInsertOptions,
1919
MIBreakpointLocation,
2020
MIShowResponse,
21+
sendDataEvaluateExpression,
2122
sendExecInterrupt,
2223
} from '../mi';
2324
import { VarManager } from '../varManager';
@@ -297,6 +298,17 @@ export class GDBBackend extends events.EventEmitter implements IGDBBackend {
297298
return isProcessActive(this.proc);
298299
}
299300

301+
public async queryCurrentThreadId(): Promise<number> {
302+
const threadIdResult = await sendDataEvaluateExpression(
303+
this,
304+
this.gdbVersionAtLeast('7.11') ? '$_gthread' : '$_thread'
305+
);
306+
return threadIdResult.value !== undefined &&
307+
threadIdResult.value !== 'void'
308+
? parseInt(threadIdResult.value, 10)
309+
: -1;
310+
}
311+
300312
protected nextToken() {
301313
return this.token++;
302314
}

src/gdb/GDBDebugSessionBase.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,7 @@ export abstract class GDBDebugSessionBase extends LoggingDebugSession {
436436
this.waitPaused = resolve;
437437
});
438438
if (this.gdb.isNonStopMode()) {
439-
const threadInfo = await mi.sendThreadInfoRequest(this.gdb, {});
440-
441-
this.waitPausedThreadId = parseInt(
442-
threadInfo['current-thread-id'],
443-
10
444-
);
439+
this.waitPausedThreadId = await this.gdb.queryCurrentThreadId();
445440
this.gdb.pause(this.waitPausedThreadId);
446441
} else {
447442
this.gdb.pause();
@@ -1934,7 +1929,8 @@ export abstract class GDBDebugSessionBase extends LoggingDebugSession {
19341929
if (
19351930
this.waitPaused &&
19361931
resultData.reason === 'signal-received' &&
1937-
this.waitPausedThreadId === id
1932+
(this.waitPausedThreadId === id ||
1933+
this.waitPausedThreadId === -1)
19381934
) {
19391935
suppressHandleGDBStopped = true;
19401936
}

src/types/gdb.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ export interface IGDBBackend extends EventEmitter {
128128
sendCommands(commands?: string[]): Promise<void>;
129129
gdbVersionAtLeast(targetVersion: string): boolean;
130130

131+
/** Ask GDB for its current thread ID.
132+
*
133+
* Can return 0 when GDB has no threads, or -1 in case of unexpected errors
134+
* (which will in some cases make things work anyway because "--thread -1"
135+
* is equivalent to omitting "--thread" - that is probably an implementation
136+
* detail though). */
137+
queryCurrentThreadId(): Promise<number>;
138+
131139
on(
132140
event: 'consoleStreamOutput',
133141
listener: (output: string, category: string) => void

0 commit comments

Comments
 (0)