Skip to content

Commit

Permalink
On mac and linux we use regular file to comminicate because fifo pipe…
Browse files Browse the repository at this point in the history
…s have no support for non blocking read or seek
  • Loading branch information
A-Kovalev-Playrix committed Nov 14, 2022
1 parent 0970405 commit abdb816
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
11 changes: 8 additions & 3 deletions debugger/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ export namespace Debugger {
}
pullFile = file as LuaFile;
pullFile.setvbuf("no");
lastPullSeek = pullFile.seek("end")[0] as number;
const [fileSize, errorSeek] = pullFile.seek("end");
if (!fileSize) {
luaError(`Failed to read pull file "${pullFilePath}": ${errorSeek}\n`);
} else {
lastPullSeek = fileSize;
}
} else {
pullFile = null;
}
Expand Down Expand Up @@ -868,7 +873,7 @@ export namespace Debugger {
if (isDebugHookDisabled) {
return;
}

//Stepping
if (breakAtDepth >= 0) {
const activeThread = getActiveThread();
Expand Down Expand Up @@ -1167,7 +1172,7 @@ export namespace Debugger {
isDebugHookDisabled = breakAtDepth < 0 && Breakpoint.getCount() === 0;
// Do not disable debugging in luajit environment with pull breakpoints support enabled
// or functions will be jitted and will lose debug info of lines and files
if (isDebugHookDisabled && (_G['jit'] === null || pullFile == null)) {
if (isDebugHookDisabled && (_G["jit"] === null || pullFile === null)) {
debug.sethook();

for (const [thread] of pairs(threadIds)) {
Expand Down
2 changes: 1 addition & 1 deletion debugger/lldebugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function stop(): void {
}

//Pull breakpoints change
export function pullBreakpoints():void {
export function pullBreakpoints(): void {
Debugger.pullBreakpoints();
}

Expand Down
55 changes: 27 additions & 28 deletions extension/debugPipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as crypto from "crypto";
import * as net from "net";
import * as childProcess from "child_process";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";

export interface DebugPipe {
open: (onData: (data: unknown) => void, onError: (err: unknown) => void) => void;
Expand All @@ -11,7 +13,7 @@ export interface DebugPipe {
requestPull: () => void;
getOutputPipePath: () => string;
getInputPipePath: () => string;
getPullPipePath: () => string,
getPullPipePath: () => string;
}

export function createNamedPipe(): DebugPipe {
Expand Down Expand Up @@ -43,16 +45,15 @@ export function createNamedPipe(): DebugPipe {
);
inputPipe.listen(inputPipePath);
},
openPull: (onError: (err: unknown) => void) =>
{
openPull: (onError: (err: unknown) => void) => {
if (!onErrorCallback) {
onErrorCallback = onError;
}

pullPipe = net.createServer(
stream => {
stream.on("error", err =>{
onError(`error on pull pipe: ${err}`)
stream.on("error", err => {
onError(`error on pull pipe: ${err}`);
});
pullStream = stream;
}
Expand Down Expand Up @@ -87,7 +88,16 @@ export function createFifoPipe(): DebugPipe {
const pipeId = crypto.randomBytes(16).toString("hex");
const outputPipePath = `/tmp/lldbg_out_${pipeId}`;
const inputPipePath = `/tmp/lldbg_in_${pipeId}`;
const pullPipePath = `/tmp/lldbg_pull_${pipeId}`;
let pullPipePath = "";

const appPrefix = 'lldebugger';
try {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), appPrefix));
pullPipePath = path.join(tmpDir, "pull.txt");
} catch {
// handle error
}

let outputFd: number | null;
let inputFd: number | null;
let pullFd: number | null;
Expand Down Expand Up @@ -148,33 +158,22 @@ export function createFifoPipe(): DebugPipe {
);
},

openPull: (onError: (err: unknown) => void) =>
{
openPull: (onError: (err: unknown) => void) => {
if (!onErrorCallback) {
onErrorCallback = onError;
}

childProcess.exec(
`mkfifo ${pullPipePath}`,
fifoErr => {
if (fifoErr) {
onError(`error executing mkfifo for input pipe: ${fifoErr}`);

fs.open(
pullPipePath,
fs.constants.O_WRONLY | fs.constants.O_CREAT,
(fdErr, fd) => {
if (fdErr) {
onError(`error opening input pipe: ${fdErr}`);
return;
}

fs.open(
pullPipePath,
fs.constants.O_WRONLY,
(fdErr, fd) => {
if (fdErr) {
onError(`error opening fifo for pull pipe: ${fdErr}`);
return;
}

pullFd = fd;
pullStream = fs.createWriteStream(null as unknown as fs.PathLike, {fd});
}
);
pullFd = fd;
pullStream = fs.createWriteStream(null as unknown as fs.PathLike, {fd});
}
);
},
Expand Down Expand Up @@ -211,7 +210,7 @@ export function createFifoPipe(): DebugPipe {
pullPipePath,
err => {
if (err) {
onErrorCallback?.(`error removing fifo for pull pipe: ${err}`);
onErrorCallback?.(`error removing pull file '${pullPipePath}': ${err}`);
}
}
);
Expand Down
10 changes: 5 additions & 5 deletions extension/luaDebugSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export class LuaDebugSession extends LoggingDebugSession {
processOptions.env[stepUnmappedLinesEnv] = this.config.stepUnmappedLines ? "1" : "0";
}

this.usePipeCommutication = this.config.program.communication === "pipe"
this.usePipeCommutication = this.config.program.communication === "pipe";

//Open pipes
if (this.usePipeCommutication || this.pullBreakpointsSupport) {
Expand All @@ -262,14 +262,14 @@ export class LuaDebugSession extends LoggingDebugSession {
data => { void this.onDebuggerOutput(data); },
err => { this.showOutput(`${err}`, OutputCategory.Error); }
);

processOptions.env[outputFileEnv] = this.debugPipe.getOutputPipePath();
processOptions.env[inputFileEnv] = this.debugPipe.getInputPipePath();
}
}

if (this.pullBreakpointsSupport) {
this.debugPipe?.openPull(err => { this.showOutput(`${err}`, OutputCategory.Error); })
this.debugPipe?.openPull(err => { this.showOutput(`${err}`, OutputCategory.Error); });
processOptions.env[pullFileEnv] = this.debugPipe?.getPullPipePath();
}

Expand Down Expand Up @@ -346,7 +346,7 @@ export class LuaDebugSession extends LoggingDebugSession {
this.autoContinueNext = true;
this.debugPipe?.requestPull();
}

const oldBreakpoints = this.fileBreakpoints[filePath];
if (typeof oldBreakpoints !== "undefined") {
for (const breakpoint of oldBreakpoints) {
Expand Down Expand Up @@ -852,7 +852,7 @@ export class LuaDebugSession extends LoggingDebugSession {

private async onDebuggerStop(msg: LuaDebug.DebugBreak) {
this.isRunning = false;
let prevInDebugger = this.inDebuggerBreakpoint;
const prevInDebugger = this.inDebuggerBreakpoint;
this.inDebuggerBreakpoint = true;

if (this.pendingScripts) {
Expand Down

0 comments on commit abdb816

Please sign in to comment.