Skip to content

Missing support for remote GDB via intermediate remote server #192 #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 42 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@
"description": "GDB commands to run when starting to debug",
"default": []
},
"proxyConnection": {
"type": "object",
"description": "If this is set then the extension will connect to an ssh host throw a proxy server and run GDB there",
"properties": {
"host": {
"type": "string",
"description": "Remote host name/ip to connect to"
},
"username": {
"type": "string",
"description": "Username to connect as"
},
"identity": {
"type": "string",
"description": "Absolute path to private key"
}
}
},
"ssh": {
"required": [
"host",
Expand Down Expand Up @@ -276,6 +294,27 @@
"description": "GDB commands to run when starting to debug",
"default": []
},
"proxyConnection": {
"type": "object",
"description": "This is the intermediate poly connection",
"properties": {
"host": {
"type": "string",
"description": "Poly host name/ip to connect to",
"default": "localhost"
},
"username": {
"type": "string",
"description": "Username to connect as",
"default": "${4:my_user}"
},
"identity": {
"type": "string",
"description": "Absolute path to private key",
"dafault": "${4:/home/my_user/.ssh/id_rsa}"
}
}
},
"ssh": {
"required": [
"host",
Expand Down Expand Up @@ -953,9 +992,10 @@
"postinstall": "node ./node_modules/vscode/bin/install"
},
"dependencies": {
"ssh2": "^0.8.2",
"ssh2-promise": "^0.1.7",
"vscode-debugadapter": "^1.16.0",
"vscode-debugprotocol": "^1.16.0",
"ssh2": "^0.8.2"
"vscode-debugprotocol": "^1.16.0"
},
"devDependencies": {
"@types/mocha": "^5.2.6",
Expand Down
8 changes: 6 additions & 2 deletions src/backend/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ export interface SSHArguments {
x11host: string;
bootstrap: string;
}

export interface ProxySSHArguments {
host: string;
username: string;
identity: string;
}
export interface IBackend {
load(cwd: string, target: string, procArgs: string, separateConsole: string): Thenable<any>;
ssh(args: SSHArguments, cwd: string, target: string, procArgs: string, separateConsole: string, attach: boolean): Thenable<any>;
ssh(proxyConnection: ProxySSHArguments, args: SSHArguments, cwd: string, target: string, procArgs: string, separateConsole: string, attach: boolean): Thenable<any>;
attach(cwd: string, executable: string, target: string): Thenable<any>;
connect(cwd: string, executable: string, target: string): Thenable<any>;
start(): Thenable<boolean>;
Expand Down
105 changes: 68 additions & 37 deletions src/backend/mi2/mi2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Breakpoint, IBackend, Thread, Stack, SSHArguments, Variable, VariableObject, MIError } from "../backend";
import { Breakpoint, IBackend, Thread, Stack, SSHArguments, ProxySSHArguments, Variable, VariableObject, MIError } from "../backend";
import * as ChildProcess from "child_process";
import { EventEmitter } from "events";
import { parseMI, MINode } from '../mi_parse';
Expand All @@ -9,6 +9,7 @@ import { posix } from "path";
import * as nativePath from "path";
const path = posix;
import { Client } from "ssh2";
import ssh2_promise = require('ssh2-promise');

export function escape(str: string) {
return str.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
Expand Down Expand Up @@ -90,11 +91,17 @@ export class MI2 extends EventEmitter implements IBackend {
});
}

ssh(args: SSHArguments, cwd: string, target: string, procArgs: string, separateConsole: string, attach: boolean): Thenable<any> {
ssh(proxyConnection: ProxySSHArguments, args: SSHArguments, cwd: string, target: string, procArgs: string, separateConsole: string, attach: boolean): Thenable<any> {
return new Promise((resolve, reject) => {
this.isSSH = true;
this.sshReady = false;
this.sshConn = new Client();

if (proxyConnection != undefined) {
const remoteConnection = { host: args.host, username: args.user , password: args.password};
this.sshConn = new ssh2_promise([proxyConnection, remoteConnection]);
} else {
this.sshConn = new Client();
}

if (separateConsole !== undefined)
this.log("stderr", "WARNING: Output to terminal emulators are not supported over SSH");
Expand Down Expand Up @@ -134,50 +141,74 @@ export class MI2 extends EventEmitter implements IBackend {
connectionArgs.password = args.password;
}

this.sshConn.on("ready", () => {
//Promise
if (proxyConnection != undefined) {
this.sshConn.connect().then(() => {
this.log("stdout", "Running " + this.application + " over ssh...");
const execArgs: any = {};
if (args.forwardX11) {
execArgs.x11 = {
single: false,
screen: args.remotex11screen
};
}
if (args.forwardX11) { execArgs.x11 = { single: false, screen: args.remotex11screen }; }
let sshCMD = this.application + " " + this.preargs.join(" ");
if (args.bootstrap) sshCMD = args.bootstrap + " && " + sshCMD;
if (attach)
sshCMD += " -p " + target;
if (args.bootstrap) sshCMD = args.bootstrap + " && " + sshCMD;
if (attach) sshCMD += " -p " + target;
this.sshConn.exec(sshCMD, execArgs, (err, stream) => {
if (err) {
this.log("stderr", "Could not run " + this.application + " over ssh!");
this.log("stderr", err.toString());
this.emit("quit");
reject();
return;
}
if (err) { this.log("stderr", "Could not run " + this.application + " over ssh!"); this.log("stderr", err.toString()); this.emit("quit"); reject(); return; }
this.sshReady = true;
this.stream = stream;
stream.on("data", this.stdout.bind(this));
stream.stderr.on("data", this.stderr.bind(this));
stream.on("exit", (() => {
this.emit("quit");
this.sshConn.end();
}).bind(this));
stream.on("exit", (() => { this.emit("quit"); this.sshConn.end(); }).bind(this));
const promises = this.initCommands(target, cwd, true, attach);
promises.push(this.sendCommand("environment-cd \"" + escape(cwd) + "\""));
if (procArgs && procArgs.length && !attach)
promises.push(this.sendCommand("exec-arguments " + procArgs));
Promise.all(promises).then(() => {
this.emit("debug-ready");
resolve();
}, reject);
if (procArgs && procArgs.length && !attach) promises.push(this.sendCommand("exec-arguments " + procArgs));
Promise.all(promises).then(() => { this.emit("debug-ready"); resolve(); }, reject);
});
}).on("error", (err) => {
this.log("stderr", "Could not run " + this.application + " over ssh!");
this.log("stderr", err.toString());
this.emit("quit");
reject();
}).connect(connectionArgs);
}).catch(err => {this.log("stderr", "Could not run " + this.application + " over ssh!"); this.log("stderr", err.toString()); this.emit("quit"); reject(); });
} else {
this.sshConn.on("ready", () => {
this.log("stdout", "Running " + this.application + " over ssh...");
const execArgs: any = {};
if (args.forwardX11) {
execArgs.x11 = {
single: false,
screen: args.remotex11screen
};
}
let sshCMD = this.application + " " + this.preargs.join(" ");
if (args.bootstrap) sshCMD = args.bootstrap + " && " + sshCMD;
if (attach)
sshCMD += " -p " + target;
this.sshConn.exec(sshCMD, execArgs, (err, stream) => {
if (err) {
this.log("stderr", "Could not run " + this.application + " over ssh!");
this.log("stderr", err.toString());
this.emit("quit");
reject();
return;
}
this.sshReady = true;
this.stream = stream;
stream.on("data", this.stdout.bind(this));
stream.stderr.on("data", this.stderr.bind(this));
stream.on("exit", (() => {
this.emit("quit");
this.sshConn.end();
}).bind(this));
const promises = this.initCommands(target, cwd, true, attach);
promises.push(this.sendCommand("environment-cd \"" + escape(cwd) + "\""));
if (procArgs && procArgs.length && !attach)
promises.push(this.sendCommand("exec-arguments " + procArgs));
Promise.all(promises).then(() => {
this.emit("debug-ready");
resolve();
}, reject);
});
}).on("error", (err) => {
this.log("stderr", "Could not run " + this.application + " over ssh!");
this.log("stderr", err.toString());
this.emit("quit");
reject();
}).connect(connectionArgs);
}
});
}

Expand All @@ -197,7 +228,7 @@ 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"));
for (let cmd of this.extraCommands) {
for (const cmd of this.extraCommands) {
cmds.push(this.sendCommand(cmd));
}

Expand Down
10 changes: 6 additions & 4 deletions src/gdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MI2DebugSession } from './mibase';
import { DebugSession, InitializedEvent, TerminatedEvent, StoppedEvent, OutputEvent, Thread, StackFrame, Scope, Source, Handles } from 'vscode-debugadapter';
import { DebugProtocol } from 'vscode-debugprotocol';
import { MI2 } from "./backend/mi2/mi2";
import { SSHArguments, ValuesFormattingMode } from './backend/backend';
import { SSHArguments, ProxySSHArguments, ValuesFormattingMode } from './backend/backend';

export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArguments {
cwd: string;
Expand All @@ -15,6 +15,7 @@ export interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArgum
terminal: string;
autorun: string[];
ssh: SSHArguments;
proxyConnection: ProxySSHArguments;
valuesFormatting: ValuesFormattingMode;
printCalls: boolean;
showDevDebugOutput: boolean;
Expand All @@ -31,6 +32,7 @@ export interface AttachRequestArguments extends DebugProtocol.AttachRequestArgum
remote: boolean;
autorun: string[];
ssh: SSHArguments;
proxyConnection: ProxySSHArguments;
valuesFormatting: ValuesFormattingMode;
printCalls: boolean;
showDevDebugOutput: boolean;
Expand Down Expand Up @@ -77,7 +79,7 @@ class GDBDebugSession extends MI2DebugSession {
this.isSSH = true;
this.trimCWD = args.cwd.replace(/\\/g, "/");
this.switchCWD = args.ssh.cwd;
this.miDebugger.ssh(args.ssh, args.ssh.cwd, args.target, args.arguments, args.terminal, false).then(() => {
this.miDebugger.ssh(args.proxyConnection, args.ssh, args.ssh.cwd, args.target, args.arguments, args.terminal, false).then(() => {
if (args.autorun)
args.autorun.forEach(command => {
this.miDebugger.sendUserInput(command);
Expand Down Expand Up @@ -145,7 +147,7 @@ class GDBDebugSession extends MI2DebugSession {
this.isSSH = true;
this.trimCWD = args.cwd.replace(/\\/g, "/");
this.switchCWD = args.ssh.cwd;
this.miDebugger.ssh(args.ssh, args.ssh.cwd, args.target, "", undefined, true).then(() => {
this.miDebugger.ssh(args.proxyConnection, args.ssh, args.ssh.cwd, args.target, "", undefined, true).then(() => {
if (args.autorun)
args.autorun.forEach(command => {
this.miDebugger.sendUserInput(command);
Expand Down Expand Up @@ -187,7 +189,7 @@ class GDBDebugSession extends MI2DebugSession {
if (substitutions) {
Object.keys(substitutions).forEach(source => {
this.miDebugger.extraCommands.push("gdb-set substitute-path " + source + " " + substitutions[source]);
})
});
}
}
}
Expand Down
Loading