如何调试一个接受输入文件重定向和输出文件重定向的 Go 二进制文件?

huangapple go评论82阅读模式
英文:

How do you debug a Go binary that takes input file redirection and output file redirection?

问题

我有一个名为"runme"的Go二进制文件,可以成功运行,命令如下:

./runme encrypt --password=password < plaintext.txt > encrypted.txt

它成功读取名为"plaintext.txt"的文件,并输出一个名为"encrypted.txt"的加密文件。

现在我想使用Go的dlv调试器来调试它,命令如下:

dlv exec ./runme -- encrypt -password=password < plaintext.txt > encrypted.txt

然而,我从dlv调试器中得到以下错误信息:

Stdin is not a terminal, use '-r' to specify redirects for the target process or --allow-non-terminal-interactive=true if you really want to specify a redirect for Delve

所以我稍微改变了一下尝试:

dlv exec -r ./runme -- encrypt -password=password < plaintext.txt > encrypted.txt

但是我得到了与上面完全相同的错误信息。然后我尝试了以下命令:

dlv exec --allow-non-terminal-interactive=true ./runme -- encrypt -password=password < plaintext.txt > encrypted.txt

这次我得到了不同的错误信息:

Command failed: command not available

在调试器中似乎有一个看起来很简单的事情我做不到。我可能做错了什么?

英文:

I have a Go binary file called "runme" that successfully runs like so:

./runme encrypt --password=password &lt; plaintext.txt &gt; encrypted.txt

It successfully reads in a file called "plaintext.txt" and outputs an encrypted file called "encrypted.txt".

Now I would like to use the dlv debugger for Go to debug it like so:

dlv exec  ./runme -- encrypt -password=password &lt; plaintext.txt &gt; encrypted.txt

However I get the following error message from the dlv debugger:

Stdin is not a terminal, use &#39;-r&#39; to specify redirects for the target process or --allow-non-terminal-interactive=true if you really want to specify a redirect for Delve

So I try again slightly differently:

dlv exec -r ./runme -- encrypt -password=password &lt; plaintext.txt &gt; encrypted.txt

But I get the exact same error message shown above. Then I try the following:

dlv exec --allow-non-terminal-interactive=true  ./runme -- encrypt -password=password &lt; plaintext.txt &gt; encrypted.txt

This time I get a different error message:

Command failed: command not available

What seems like a simple thing I am not able to do in the debugger. What could I be doing wrong?

答案1

得分: 3

通过 @tkausl 和 @gopher 的帮助,我成功解决了问题。

解决方案如下:

dlv exec -r stdin:plaintext.txt  -r stdout:encrypted.txt ./runme -- encrypt -password=password
英文:

With help from @tkausl and @gopher I was able to figure it out.

Solution is:

dlv exec -r stdin:plaintext.txt  -r stdout:encrypted.txt ./runme -- encrypt -password=password

huangapple
  • 本文由 发表于 2021年10月2日 09:33:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/69413335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定