GO language remote debugging

If you want to debug directly with Goland, skip to Section 2. Remote debugging is very common, just like JDWP in Java, remote debugging, some bugs are not convenient to double line, directly on the test server debugging is easy to determine the problem.

1. Use Delve

Delve is a debugger for the GO language. Objective To provide go language with a full function of the simplified debugging tool, Delve tool github address

Install Delve

  1. Download and install it from the Github repository
$ git clone https://github.com/go-delve/delve
$ cd delve
$ go install github.com/go-delve/delve/cmd/dlv
Copy the code
  1. Go version >1.16 is simplified as follows (see my other blog post about upgrading golang Version)
$ go install github.com/go-delve/delve/cmd/dlv@latest
Copy the code

If all else fails, then you need to consider whether your Internet is scientific. There are a lot of reasons for going away.

  1. Try the DLV command, which is usually installed in the $GOPATH directory. If command is not found, you need to put the directory /bin in your environment variable.

Delve is used for debugging

For example, my project directory structure is like this: there is a main package in the project path, and the main function is below the main package. Then, the DEBUGGING command of DLV can start the mian function to start debugging. Note that the following parameters are the ones I need for main, not for DLV.

dlv debug ./main/main.go -- -env=debug -service=agent,debug,game,login,admin,msgq
Copy the code

Then, (DLV) is similar to the command prompt. Break main.mian is the break point for the main function of the main package

(dlv) break main.main
Breakpoint 1 set at 0x20e06f3 for main.main() ./main/main.go:68
Copy the code

The continue command is then used to let the program continue until it stops when it hits a breakpoint. Such as PIC.

Step debugging is then performed using next and step commands, and args and display commands are used to display debugging values. As shown in figure:

Debugging from the command line is so anti-human, use the IDE instead. Let’s use GOLang for remote debugging.

2 Use GoLand and Delve to perform remote debugging

We’ve already seen how debugging from the command line is so anti-human. Now we use Delve supported client to debug, here we use GOLang. There are many more text editors or plug-ins that support remote debugging of DEL, see the list

Remote debugging using Golang

1. Install Delve

This is installed on a remote machine as described in 1.1 above. Note that it is installed on remote, that is, the machine you want to debug remotely.

2. Start the remote code

Note that here, use the DLV exec command to launch your remote code, where the./ascension following exec below is my program, and the following are the parameters that my own program needs. DLV parameters, listening on port 2345, no command prompt header (DLV) required, etc.

dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./ascension -- -env=test -service=login,msgq,game,fusion,agent,debug
Copy the code

If the application process is built remotely, you need to disable compilation optimization as follows:

go build -gcflags \"all=-N -l\" -o ./build/ascension ./main/main.go
Copy the code

The startup screen is shown below, and the application blocks the connection waiting for Goland:

3.GoLand just need to be configured

First open the Debug Config interface, click “+” and click “Go Remote”, as shown in the picture:

Then fill in the IP address and port of the app target server just started. Here is 5432 port. First make sure that the server port is available.

Select Remote and then launch the debud Remote.

4. Remove DLV blocking

  1. I don’t want the remote program to block and wait for me to debug, but to execute directly, so I can add the –continue parameter when starting the remote program.
  2. I don’t want to restart the remote server every time, so I’ll just write to the startup script and start with DLV and test the environment.
  3. The debug command does not contain CTRL + C or q to exit. You need to kill the process directly

Here’s the code, if you can understand the second line.

function start() {
  dlv --listen=:5432 --headless=true --api-version=2 --continue --accept-multiclient exec ./ascension -- -env=test -service=login,msgq,game,fusion,agent,debug >./logs/stdout 2>./logs/stderr &
  pid=$!
  echo "$pid" > ./boot.pid
  echo "go boot ${jarfile} Process Id:$pid begin running!"
}
Copy the code

3. Follow-up questions