This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(remote): initial support for remote debugging
Adding support for remote debugging ```bash # Start remote debugger in vs code on port 5000 then: $ PERLDB_OPTS="RemotePort=localhost:5000" perl -d test.pl ```
- Loading branch information
Morten Henriksen
committed
Feb 9, 2018
1 parent
a826b16
commit 6b1819e
Showing
8 changed files
with
207 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {spawn} from 'child_process'; | ||
import { Readable, Writable } from 'stream'; | ||
import { DebugSession, LaunchOptions } from './session'; | ||
|
||
export class LocalSession implements DebugSession { | ||
public stdin: Writable; | ||
public stdout: Readable; | ||
public stderr: Readable; | ||
public on: Function; | ||
public kill: Function; | ||
public title: Function; | ||
public dump: Function; | ||
|
||
constructor(filename: string, cwd: string, args: string[] = [], options: LaunchOptions = {}) { | ||
const perlCommand = options.exec || 'perl'; | ||
const programArguments = options.args || []; | ||
|
||
const commandArgs = [].concat(args, [ '-d', filename /*, '-emacs'*/], programArguments); | ||
|
||
const spawnOptions = { | ||
detached: true, | ||
cwd: cwd || undefined, | ||
env: { | ||
COLUMNS: 80, | ||
LINES: 25, | ||
TERM: 'dumb', | ||
...options.env, | ||
}, | ||
}; | ||
|
||
const session = spawn(perlCommand, commandArgs, spawnOptions); | ||
this.stdin = session.stdin; | ||
this.stdout = session.stdout; | ||
this.stderr = session.stderr; | ||
this.on = (type, callback) => session.on(type, callback); | ||
this.kill = () => session.kill(); | ||
this.title = () => `${perlCommand} ${commandArgs.join(' ')}`; | ||
this.dump = () => `spawn(${perlCommand}, ${JSON.stringify(commandArgs)}, ${JSON.stringify(spawnOptions)});`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as net from 'net'; | ||
import {spawn} from 'child_process'; | ||
import { Readable, Writable } from 'stream'; | ||
import { EventEmitter } from 'events'; | ||
import { DebugSession, LaunchOptions } from './session'; | ||
|
||
export class RemoteSession implements DebugSession { | ||
public stdin: Writable; | ||
public stdout: Readable; | ||
public stderr: Readable; | ||
public on: Function; | ||
public kill: Function; | ||
public title: Function; | ||
public dump: Function; | ||
private event = new EventEmitter(); | ||
|
||
constructor(port: number) { | ||
// Keep track of the chat clients | ||
let client; | ||
|
||
this.stdin = new Writable({ | ||
write(chunk, encoding, callback) { | ||
if (client) { | ||
client.write(chunk); | ||
callback(); | ||
} | ||
}, | ||
}); | ||
|
||
this.stdout = new Readable({ | ||
read() {}, | ||
}); | ||
this.stderr = new Readable({ | ||
read() {}, | ||
}); | ||
|
||
const server = net.createServer((socket) => { | ||
const name = `${socket.remoteAddress}:${socket.remotePort}`; | ||
|
||
if (!client) { | ||
client = socket; | ||
this.stdout.push(`> Remote debugger at "${name}" connected at port ${port}.`); | ||
} else { | ||
// Already have a client connected, lets close and notify user | ||
this.stderr.push(`> Warning: Additional remote client tried to connect "${name}".`); | ||
socket.end('Remote debugger already connected!'); | ||
} | ||
|
||
socket.on('data', data => this.stderr.push(data)); | ||
|
||
socket.on('end', data => { | ||
client = null; | ||
this.event.emit('close', data); | ||
}); | ||
|
||
socket.on('error', data => this.event.emit('error', data)); | ||
}); | ||
|
||
server.listen(port, '0.0.0.0'); // Listen to port make it remotely available | ||
server.on('error', data => this.event.emit('error', data)); | ||
|
||
this.on = (type, callback) => this.event.on(type, callback); | ||
this.kill = () => { | ||
server.close(); | ||
this.event.removeAllListeners(); | ||
this.stdin.removeAllListeners(); | ||
this.stdout.removeAllListeners(); | ||
this.stderr.removeAllListeners(); | ||
}; | ||
this.title = () => `Running debug server for remote session to connect on port "${port}"`; | ||
this.dump = () => `debug server port ${port}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Readable, Writable } from 'stream'; | ||
|
||
export interface DebugSession { | ||
kill: Function, | ||
stderr: Readable, | ||
stdout: Readable, | ||
stdin: Writable, | ||
on: Function, // support "close", "error" | ||
title: Function, | ||
dump: Function, // Dump debug information | ||
} | ||
|
||
export interface LaunchOptions { | ||
exec?: string; | ||
args?: string[]; | ||
env?: {}, | ||
port?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters