From abdb8165a2e5baa97aa26c577e04a1a1b36a3480 Mon Sep 17 00:00:00 2001 From: Aleksandr Kovalev Date: Mon, 14 Nov 2022 15:39:23 +0000 Subject: [PATCH] On mac and linux we use regular file to comminicate because fifo pipes have no support for non blocking read or seek --- debugger/debugger.ts | 11 ++++++-- debugger/lldebugger.ts | 2 +- extension/debugPipe.ts | 55 ++++++++++++++++++------------------ extension/luaDebugSession.ts | 10 +++---- 4 files changed, 41 insertions(+), 37 deletions(-) diff --git a/debugger/debugger.ts b/debugger/debugger.ts index b65f5cd..927d98d 100644 --- a/debugger/debugger.ts +++ b/debugger/debugger.ts @@ -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; } @@ -868,7 +873,7 @@ export namespace Debugger { if (isDebugHookDisabled) { return; } - + //Stepping if (breakAtDepth >= 0) { const activeThread = getActiveThread(); @@ -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)) { diff --git a/debugger/lldebugger.ts b/debugger/lldebugger.ts index 2b3e6bf..e63ea66 100644 --- a/debugger/lldebugger.ts +++ b/debugger/lldebugger.ts @@ -56,7 +56,7 @@ export function stop(): void { } //Pull breakpoints change -export function pullBreakpoints():void { +export function pullBreakpoints(): void { Debugger.pullBreakpoints(); } diff --git a/extension/debugPipe.ts b/extension/debugPipe.ts index bfa662c..500c71f 100644 --- a/extension/debugPipe.ts +++ b/extension/debugPipe.ts @@ -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; @@ -11,7 +13,7 @@ export interface DebugPipe { requestPull: () => void; getOutputPipePath: () => string; getInputPipePath: () => string; - getPullPipePath: () => string, + getPullPipePath: () => string; } export function createNamedPipe(): DebugPipe { @@ -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; } @@ -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; @@ -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}); } ); }, @@ -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}`); } } ); diff --git a/extension/luaDebugSession.ts b/extension/luaDebugSession.ts index aad099a..ba6edd3 100644 --- a/extension/luaDebugSession.ts +++ b/extension/luaDebugSession.ts @@ -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) { @@ -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(); } @@ -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) { @@ -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) {