Skip to content

Commit

Permalink
Add multi-process support
Browse files Browse the repository at this point in the history
  • Loading branch information
LeszekSwirski committed Feb 21, 2018
1 parent bcbbda1 commit 4436f0a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 13 deletions.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
"description": "Prints all GDB calls to the console",
"default": false
},
"multiProcess": {
"type": "boolean",
"description": "Allow multiple process debugging",
"default": false
},
"showDevDebugOutput": {
"type": "boolean",
"description": "Prints all GDB responses to the console",
Expand Down Expand Up @@ -219,6 +224,11 @@
"description": "Prints all GDB calls to the console",
"default": false
},
"multiProcess": {
"type": "boolean",
"description": "Allow multiple process debugging",
"default": false
},
"showDevDebugOutput": {
"type": "boolean",
"description": "Prints all GDB responses to the console",
Expand Down
2 changes: 1 addition & 1 deletion src/backend/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface IBackend {
start(): Thenable<boolean>;
stop();
detach();
interrupt(): Thenable<boolean>;
interrupt(all: boolean): Thenable<boolean>;
continue(): Thenable<boolean>;
next(): Thenable<boolean>;
step(): Thenable<boolean>;
Expand Down
25 changes: 20 additions & 5 deletions src/backend/mi2/mi2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ export class MI2 extends EventEmitter implements IBackend {
cmds.push(this.sendCommand("file-exec-and-symbols \"" + escape(target) + "\""));
if (this.prettyPrint)
cmds.push(this.sendCommand("enable-pretty-printing"));
if (this.multiProcess) {
cmds.push(
this.sendCommand("gdb-set follow-fork-mode parent"),
this.sendCommand("gdb-set detach-on-fork off"),
this.sendCommand("gdb-set non-stop on"),
this.sendCommand("gdb-set schedule-multiple on"),

this.sendCommand("interpreter-exec console \"handle SIGSYS nostop noprint\"")
);
}

return cmds;
}
Expand Down Expand Up @@ -356,7 +366,7 @@ export class MI2 extends EventEmitter implements IBackend {
else if (reason == "exited-normally")
this.emit("exited-normally", parsed);
else if (reason == "exited") { // exit with error code != 0
this.log("stderr", "Program exited with code " + parsed.record("exit-code"));
this.log("stderr", "Inferior exited with code " + parsed.record("exit-code"));
this.emit("exited-normally", parsed);
}
else {
Expand All @@ -370,6 +380,10 @@ export class MI2 extends EventEmitter implements IBackend {
this.emit("thread-created", parsed);
} else if (record.asyncClass == "thread-exited") {
this.emit("thread-exited", parsed);
} else if (record.asyncClass == "thread-group-started") {
this.emit("thread-group-started", parsed);
} else if (record.asyncClass == "thread-group-exited") {
this.emit("thread-group-exited", parsed);
}
}
}
Expand Down Expand Up @@ -432,21 +446,21 @@ export class MI2 extends EventEmitter implements IBackend {
this.sendRaw("-target-detach");
}

interrupt(): Thenable<boolean> {
interrupt(all: boolean = true): Thenable<boolean> {
if (trace)
this.log("stderr", "interrupt");
return new Promise((resolve, reject) => {
this.sendCommand("exec-interrupt").then((info) => {
this.sendCommand("exec-interrupt" + (all ? " --all" : "")).then((info) => {
resolve(info.resultRecords.resultClass == "done");
}, reject);
});
}

continue(reverse: boolean = false): Thenable<boolean> {
continue(reverse: boolean = false, all: boolean = true): Thenable<boolean> {
if (trace)
this.log("stderr", "continue");
return new Promise((resolve, reject) => {
this.sendCommand("exec-continue" + (reverse ? " --reverse" : "")).then((info) => {
this.sendCommand("exec-continue" + (reverse ? " --reverse" : "") + (all ? " --all" : "")).then((info) => {
resolve(info.resultRecords.resultClass == "running");
}, reject);
});
Expand Down Expand Up @@ -785,6 +799,7 @@ export class MI2 extends EventEmitter implements IBackend {
}

prettyPrint: boolean = true;
multiProcess: boolean = false;
printCalls: boolean;
debugOutput: boolean;
public procEnv: any;
Expand Down
4 changes: 4 additions & 0 deletions src/gdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArgum
ssh: SSHArguments;
valuesFormatting: ValuesFormattingMode;
printCalls: boolean;
multiProcess: boolean;
showDevDebugOutput: boolean;
}

Expand All @@ -31,6 +32,7 @@ export interface AttachRequestArguments extends DebugProtocol.AttachRequestArgum
ssh: SSHArguments;
valuesFormatting: ValuesFormattingMode;
printCalls: boolean;
multiProcess: boolean;
showDevDebugOutput: boolean;
}

Expand Down Expand Up @@ -58,6 +60,7 @@ class GDBDebugSession extends MI2DebugSession {
this.debugReady = false;
this.setValuesFormattingMode(args.valuesFormatting);
this.miDebugger.printCalls = !!args.printCalls;
this.miDebugger.multiProcess = !!args.multiProcess;
this.miDebugger.debugOutput = !!args.showDevDebugOutput;
if (args.ssh !== undefined) {
if (args.ssh.forwardX11 === undefined)
Expand Down Expand Up @@ -126,6 +129,7 @@ class GDBDebugSession extends MI2DebugSession {
this.debugReady = false;
this.setValuesFormattingMode(args.valuesFormatting);
this.miDebugger.printCalls = !!args.printCalls;
this.miDebugger.multiProcess = !!args.multiProcess;
this.miDebugger.debugOutput = !!args.showDevDebugOutput;
if (args.ssh !== undefined) {
if (args.ssh.forwardX11 === undefined)
Expand Down
45 changes: 38 additions & 7 deletions src/mibase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class MI2DebugSession extends DebugSession {
protected debugReady: boolean;
protected miDebugger: MI2;
protected commandServer: net.Server;
protected threadGroupPids = new Map<string, string>();
protected threadToPid = new Map<number, string>();

public constructor(debuggerLinesStartAt1: boolean, isServer: boolean = false) {
super(debuggerLinesStartAt1, isServer);
Expand All @@ -54,6 +56,8 @@ export class MI2DebugSession extends DebugSession {
this.miDebugger.on("signal-stop", this.handlePause.bind(this));
this.miDebugger.on("thread-created", this.threadCreatedEvent.bind(this));
this.miDebugger.on("thread-exited", this.threadExitedEvent.bind(this));
this.miDebugger.on("thread-group-started", this.threadGroupStartedEvent.bind(this));
this.miDebugger.on("thread-group-exited", this.threadGroupExitedEvent.bind(this));
this.sendEvent(new InitializedEvent());
try {
this.commandServer = net.createServer(c => {
Expand Down Expand Up @@ -138,16 +142,35 @@ export class MI2DebugSession extends DebugSession {
}

protected threadCreatedEvent(info: MINode) {
this.sendEvent(new ThreadEvent("started", info.record("id")));
let threadId = parseInt(info.record("id"), 10);

let threadPid = this.threadGroupPids.get(info.record("group-id"));
this.threadToPid.set(threadId, threadPid);

this.sendEvent(new ThreadEvent("started", threadId));
}

protected threadExitedEvent(info: MINode) {
this.sendEvent(new ThreadEvent("exited", info.record("id")));
let threadId = parseInt(info.record("id"), 10);

this.threadToPid.delete(info.record("group-id"));

this.sendEvent(new ThreadEvent("exited", threadId));
}

protected threadGroupStartedEvent(info: MINode) {
this.threadGroupPids.set(info.record("id"), info.record("pid"));
}

protected quitEvent() {
this.quit = true;
this.sendEvent(new TerminatedEvent());
protected threadGroupExitedEvent(info: MINode) {
this.threadGroupPids.delete(info.record("id"));
}

protected quitEvent(info?: MINode) {
if (this.threadGroupPids.size == 0) {
this.quit = true;
this.sendEvent(new TerminatedEvent());
}
}

protected launchError(err: any) {
Expand Down Expand Up @@ -276,7 +299,13 @@ export class MI2DebugSession extends DebugSession {
if (threadName === undefined) {
threadName = "<unnamed>";
}
response.body.threads.push(new Thread(thread.id, thread.id + ":" + threadName));
if (this.threadGroupPids.size > 1) {
let pid = this.threadToPid.get(thread.id);
threadName = `(${pid}) ${thread.id}:${threadName}`;
} else {
threadName = `${thread.id}:${threadName}`;
}
response.body.threads.push(new Thread(thread.id, threadName));
}
this.sendResponse(response);
});
Expand Down Expand Up @@ -584,7 +613,7 @@ export class MI2DebugSession extends DebugSession {
}
}

protected pauseRequest(response: DebugProtocol.ContinueResponse, args: DebugProtocol.ContinueArguments): void {
protected pauseRequest(response: DebugProtocol.PauseResponse, args: DebugProtocol.PauseArguments): void {
this.miDebugger.interrupt().then(done => {
this.sendResponse(response);
}, msg => {
Expand All @@ -594,6 +623,7 @@ export class MI2DebugSession extends DebugSession {

protected reverseContinueRequest(response: DebugProtocol.ReverseContinueResponse, args: DebugProtocol.ReverseContinueArguments): void {
this.miDebugger.continue(true).then(done => {
response.body.allThreadsContinued = true;
this.sendResponse(response);
}, msg => {
this.sendErrorResponse(response, 2, `Could not continue: ${msg}`);
Expand All @@ -602,6 +632,7 @@ export class MI2DebugSession extends DebugSession {

protected continueRequest(response: DebugProtocol.ContinueResponse, args: DebugProtocol.ContinueArguments): void {
this.miDebugger.continue().then(done => {
response.body.allThreadsContinued = true;
this.sendResponse(response);
}, msg => {
this.sendErrorResponse(response, 2, `Could not continue: ${msg}`);
Expand Down

0 comments on commit 4436f0a

Please sign in to comment.