Home  >  Article  >  Backend Development  >  How do I attach to a cross-compiled go binary for debugging using the Visual Studio Code IDE?

How do I attach to a cross-compiled go binary for debugging using the Visual Studio Code IDE?

WBOY
WBOYforward
2024-02-08 20:53:53826browse

如何使用 Visual Studio Code IDE 附加到交叉编译的 go 二进制文件以进行调试?

Question content

I am using Visual Studio Code as an IDE for a golang based application. The application will run on Linux targets. I want to attach the VSC debugger to this cross-compiled binary. The binary can be compiled for arm or x86 platforms. I know a launch.json file can achieve this, but I've never used it. please help.


Correct answer


Debuggerdlv is used to debug go code. Delve or dlv has a limitation that it can only be appended to binaries for 64-bit architectures. So I compiled the code for 64 bit amd architecture and ran it on my Linux box. Next, I got the process ID of the binary and attached a drill down. I used port 2345

for delve server

dlv Attach some_process_id --listen=:2345 --headless --api-version=2 --log

You may also need to enable/change the debug level on the Linux target with sudo permissions.

cat 0 > /proc/sys/kernel/yama/ptrace_scope

Create a launch.json on the Visual Studio Code IDE with the following settings:

{
            "name": "Launch Windows",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "path/to/binary",
            "port": 2345,
            "host": "target ip address"
 }

Since I'm running Visual Studio from a Windows machine, I choose remote mode for the remote Linux target.

Now running the debugger from Visual Studio Code, the settings in launch.json will look for the dlv debugger/server on the target and attach to it. Place some breakpoints in your code, preferably somewhere that can be triggered externally. For example. Accepts calls on the server that can be triggered from client calls.

The above is the detailed content of How do I attach to a cross-compiled go binary for debugging using the Visual Studio Code IDE?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete