Home > Backend Development > Golang > 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
Release: 2024-02-08 20:53:53
forward
891 people have browsed it

如何使用 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"
 }
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template