By goland combination delve remote debug go program

1. Cause

Sometimes there are many projects rely on the server, some may depend, for whatever reason can only run on linux environment, or too many packages depend, in short, is not convenient locally run project, it can only be debugged remotely.


2. Install delve

This command is installed on a server running delve

go get -u github.com/go-delve/delve/cmd/dlv

3. Compile the project

Remember to add -gcflags='all=-N -l
here assume that the compiler is main.go

go build -gcflags="all=-N -l"  ./main.go

4. Start the server program debug mode

One way: intrusive debugging code

It is in the process of commissioning the
assumption that the program mainis already running in the
first ps -aux | grep mainview of the main program PIDand then start debugging invasion

dlv attach $PID --headless --api-version=2 --log --listen=:1234

Second way: Start the debugging process

dlv --listen=:1234 --headless=true --api-version=2 --accept-multiclient exec ./main

5.Goland debugging

下面这几步很重要!!!!

Current local projects designated GOPATH
Here Insert Picture Description
Here Insert Picture Description

Add a remote debugging strategies
Here Insert Picture Description

Host , is that you can Unicom's IP address or machine name of a remote server; Port , is just dlv specified when the debug start listening port
Here Insert Picture Description

Editor top right, select the name of the policy remote debugging, is named after the previous step, and then click the green bug
Here Insert Picture Description

The successful commissioning
Here Insert Picture Description

6. End Debugging

After the end of the local debugging, if the program wants to suspend the server, CTRL + C is not able to terminate the program, so it is necessary to force killout the service
ps -aux | grep dlv
ps -aux | grep main
to return
root 27089 8.0 2.1 297472 39812 pts/1 Sl 12:34 0:00 dlv --listen=:8888 --headless=true --api-version=2 --accept-multiclient exec ./main

root 3157 0.0 0.4 148296 8496 pts/1 Sl 11:59 0:00 ./main

Then kill off separately


7. Finally, note

After completion of the code to change the local remote server must recompile! ! ! Otherwise debugging time will be the case
Here Insert Picture Description
Here Insert Picture Description

Published 48 original articles · won praise 56 · views 20000 +

Guess you like

Origin blog.csdn.net/zhetmdoubeizhanyong/article/details/105171611