Skip to content

Commit

Permalink
Merge pull request #15 from rogalmic/feature/experimentalCheckPipeOpt…
Browse files Browse the repository at this point in the history
…ions

Feature/experimental check pipe options
  • Loading branch information
rogalmic authored Nov 6, 2016
2 parents 1fdbe38 + a93c454 commit 99cf0ad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
"dependencies": {
"vscode-debugprotocol": "^1.14.0",
"vscode-debugadapter": "^1.14.0",
"child-process": "^1.0.2",
"tree-kill": "^1.1.0"
"child-process": "^1.0.2"
},
"devDependencies": {
"@types/es6-collections": "^0.5.29",
Expand Down
58 changes: 32 additions & 26 deletions src/bashDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class BashDebugSession extends DebugSession {
private static THREAD_ID = 42;
private static END_MARKER = "############################################################";

protected _debuggerProcess: ChildProcess.ChildProcess;
private _debuggerProcess: ChildProcess.ChildProcess;
private _pipeReadProcess: ChildProcess.ChildProcess;

private _currentBreakpointIds = new Map<string, Array<number>>();

Expand All @@ -36,7 +37,7 @@ class BashDebugSession extends DebugSession {

private _responsivityFactor = 5;

private _fifoPath = "/tmp/vscode-bash-debug.fifo";
private _debuggerProcessParentId = -1;

public constructor() {
super();
Expand All @@ -56,14 +57,13 @@ class BashDebugSession extends DebugSession {

protected disconnectRequest(response: DebugProtocol.DisconnectResponse, args: DebugProtocol.DisconnectArguments): void {
this._debuggerExecutableBusy = false;
var kill = require('tree-kill');

this._debuggerProcess.on("exit", ()=> {
this._debuggerExecutableClosing = true;
this.sendResponse(response)
});

kill(this._debuggerProcess.pid, 'SIGTERM', (err)=> this._debuggerProcess.stdin.write(`quit\n`));
ChildProcess.spawn("bash", ["-c", `pkill -9 -P ${this._debuggerProcessParentId}`]);
}

protected launchRequest(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): void {
Expand All @@ -75,16 +75,29 @@ class BashDebugSession extends DebugSession {
// use fifo, because --tty '&1' does not work properly for subshell (when bashdb spawns - $() )
// when this is fixed in bashdb, use &1
this._debuggerProcess = ChildProcess.spawn("bash", ["-c", `
mkfifo "${this._fifoPath}"
trap 'echo "vscode-bash-debugger: SIGTERM" 1>&2' TERM
trap 'echo "vscode-bash-debugger: SIGINT" 1>&2' INT
trap 'rm "${this._fifoPath}"; echo "vscode-bash-debugger: EXIT ($?)" 1>&2; exit;' EXIT
${args.bashDbPath} --quiet --tty "${this._fifoPath}" -- "${args.scriptPath}" ${args.commandLineArguments}`
]);
# http://tldp.org/LDP/abs/html/io-redirection.html
fifo_path=$(mktemp --dry-run /tmp/vscode-bash-debug-fifo.XXXXXX)
function cleanup()
{
exit_code=$?
exec 4>&-
rm "$fifo_path";
exit $exit_code;
}
trap 'cleanup' ERR SIGINT SIGTERM
mkfifo "$fifo_path"
cat "$fifo_path" >&3 & # Open for reading in background.
exec 4>"$fifo_path" # Keep open for writing, bashdb seems close after every write.
${args.bashDbPath} --quiet --tty "$fifo_path" -- "${args.scriptPath}" ${args.commandLineArguments}
cleanup`
], {stdio: ["pipe", "pipe", "pipe", "pipe"]});

this.processDebugTerminalOutput(args.showDebugOutput == true);

this._debuggerProcess.stdin.write(`print '${BashDebugSession.END_MARKER}'\n`);
this._debuggerProcess.stdin.write(`print "$PPID"\nprint "${BashDebugSession.END_MARKER}"\n`);

this._debuggerProcess.stdout.on("data", (data) => {
this.sendEvent(new OutputEvent(`${data}`, 'stdout'));
Expand All @@ -94,6 +107,12 @@ class BashDebugSession extends DebugSession {
this.sendEvent(new OutputEvent(`${data}`, 'stderr'));
});

this._debuggerProcess.stdio[3].on("data", (data) => {
if (args.showDebugOutput) {
this.sendEvent(new OutputEvent(`${data}`, 'console'));
}
});

this.scheduleExecution(() => this.launchRequestFinalize(response, args));
}

Expand All @@ -102,6 +121,7 @@ class BashDebugSession extends DebugSession {
for (var i = 0; i < this._fullDebugOutput.length; i++) {
if (this._fullDebugOutput[i] == BashDebugSession.END_MARKER) {

this._debuggerProcessParentId = parseInt(this._fullDebugOutput[i-1]);
this.sendResponse(response);
this.sendEvent(new OutputEvent(`Sending InitializedEvent`, 'telemetry'));
this.sendEvent(new InitializedEvent());
Expand Down Expand Up @@ -453,27 +473,13 @@ class BashDebugSession extends DebugSession {
}

private processDebugTerminalOutput(sendOutput: boolean): void {
if (!existsSync(this._fifoPath)) {
this.scheduleExecution(() => this.processDebugTerminalOutput(sendOutput));
return;
}

var readStream = createReadStream(this._fifoPath, { flags: "r", mode: 0x124 })

readStream.on('data', (data) => {
if (sendOutput) {
this.sendEvent(new OutputEvent(`${data}`, 'console'));
}

this._debuggerProcess.stdio[3].on('data', (data) => {
var list = data.toString().split("\n", -1);
var fullLine = `${this._fullDebugOutput.pop()}${list.shift()}`;
this._fullDebugOutput.push(this.removePrompt(fullLine));
list.forEach(l => this._fullDebugOutput.push(this.removePrompt(l)));
})

readStream.on('end', (data) => {
this.scheduleExecution(() => this.processDebugTerminalOutput(sendOutput));
})
}

private scheduleExecution(callback: (...args: any[]) => void) : void {
Expand Down

0 comments on commit 99cf0ad

Please sign in to comment.