So I'm trying to connect to a (Lua) debugger embedded in a program by redirecting the I/O. Currently I create a pair of FIFOs for the read and write streams and connect to them using cat /tmp/dbg_write & cat > /tmp/dbg_read. This is workable and pretty straightforward, but if you don't exit everything just right you have to go back and kill the background cat command. I figured it was time for a learning experience since there seems like there must be a better way. I'm totally stumped though.

Internally I'm just overriding the debugger's I/O functions to use FIFOs, since stdio is a no-go in this case. As far as I know, there is no way to do bidirectional I/O with the standard Lua APIs like sockets, and pulling in native libs is a no-go since it's embedded.

local dbg = require("debugger")

local READ_PIPE, WRITE_PIPE = "/tmp/dbg_read", "/tmp/dbg_write"
os.execute(string.format("mkfifo %s %s", READ_PIPE, WRITE_PIPE))
local input, output = io.open(READ_PIPE, "r"), io.open(WRITE_PIPE, "w")

function dbg.read(prompt)
    return input:read()

function dbg.write(str)

I also tried:

  • using popen("netcat -lU") to create a Unix domain socket, but pipes are unidirectional. Derp.
  • Using an existing pty. Seems promising except I don't know how to create one separate from a shell that wants to read from it.
  • Using screen to... somehow create a tty session I can interact with. (no idea...)
  • Finding an existing utility that can copy from a file/pipe to stdout while also copying from stdin to another.

Anything obvious that I'm missing?

  • You seem to have missed out at least 1/2 of what you did. – ctrl-alt-delor Apr 1 '20 at 21:08
  • Could you tell us what you do to have the debugger read and write to those two files? – Eduardo Trápani Apr 1 '20 at 21:29
  • Done. I guess I figured it was sort of irrelevant, but I guess more information is always better. – slembcke Apr 1 '20 at 21:49

What you can do is create two virtual terminals, back to back, with socat:

$ socat pty,link=/tmp/lua_end pty,link=/tmp/screen_end

Now, from lua, you just read to and write from a single file: /tmp/lua_end

In another shell you do:

$ screen /tmp/screen_end

And that's it, they're going to be connected, through socat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.