linuxgovisual-studio-codecross-compiling

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


I am using Visual Studio Code as the ide for a golang based application. The application will run on a linux target. I want to attach the VSC debugger to this cross compiled binary. The binary could be compiled for both arm or x86 platforms. I am aware of a launch.json file that can make this possible but I have never used it. Please help.


Solution

  • The debugger dlv is used for debugging go code. Delve or dlv has a limitation it can only attach to a binary with 64 bit architecture. Hence, I compiled my code for 64 bit amd architecture and ran it on my linux machine. Next, I fetched process id of my binary and attached delve onto it. I used a port 2345 for the delve server

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

    You might also need to enable/change debugging level on the linux target under sudo privileges.

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

    On the Visual studio code IDE create a launch.json with below settings:

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

    Since I was running Visual Studio from a windows machine I chose the mode as remote for the remote linux target.

    Now run the debugger from visual studio code and the settings from launch.json will look for the dlv debugger/server on the target and attach to it. Put some break points in the code preferably at places which can be triggered externally. eg. on a server accept call that can be triggered from a client call.