Go runtime debug tips


From the official account: New World grocery store


The original intention is to study the start-up process of Go program, and then go to the Internet to search for an introductory tutorial. It turned out to be a bit frustrating. Almost all the articles I found started with a passGDBDebugging, then assembly code on different platforms…

This makes me very unhappy, although C / C + + is widely used, I really have no interest in it, and I am even less interested in its related debugging tools. Although it can debug the Go program, I always feel that there is something missing in my heart, isn’t itdlvIt doesn’t smell good, so there is today’s article

DLV command line debug

I don’t have to say much about the title of DLV, so let’s get to the point

  1. Start debugging
dlv debug test.go

After executing the above command, you will enter the debug interactive command line interface and enter it at any timehWill help print information

  1. Add function breakpoint
#After entering the following command in the interactive interface, a breakpoint will be made in the main function under the main package
b main.main
  1. Specify line breakpoints
#In test.go A breakpoint is made on line 6 of the file
b test.go:6
  1. Start execution

Go runtime debug tips

As shown in the figure above, when we continue to execute, we will find that=>Will stop at the breakpoint we marked earlier. We will not introduce the other commands here too much in this article. We will try our best to stick to the theme of this article.

  1. Print call stack

Go runtime debug tips

As shown in the figure above, through the call stack of DLV, we can see that ASM was executed before calling the main function_ AMD64. S (the debug machine is MAC) proc.go The main function of.

Surprisingly, I just didn’t want to use GDB debugging to understand the start-up process of Go program, but now I have some clues. Let’s continue with the topic of this article

  1. Mark breakpoints in the runtime package and start debugging

Go runtime debug tips

To sum up: according to the above steps, we can debug runtiime through DLV, and we can also understand the startup process of Go program

Vscode graphical debug

I prefer to use vscode for code editing, so I also use vscode when I go, and the experience is very good

As for how to configure the development environment and graphical debug of go in vscode, this article will not repeat too much. The author will share his own configuration of go in vscode

"go.useLanguageServer": true,
"go.languageServerExperimentalFeatures": {
    "format": true,
    "diagnostics": true,
    "documentLink": true
"go.languageServerFlags": ["-rpc.trace"],
"go.gotoSymbol.includeGoroot": true,
"go.gotoSymbol.includeImports": true,
"go.useCodeSnippetsOnFunctionSuggestWithoutType": true,
"go.useCodeSnippetsOnFunctionSuggest": true,
"go.autocompleteUnimportedPackages": true,
  1. Break

This part is completed on the standby computer. Go version is go1.14.2

Go runtime debug tips

This time, we directly reuse the demo of the capacity of the string to byte slice in the previous article go. The three breakpoints are located in, main/ test.go , runtime/ proc.go And runtime/ string.go .

  1. function

Vscode can start debugging quickly through F5 shortcut key

Go runtime debug tips

Next, you can start the happy debug journey

Debug does not appear in call stack functions

Careful students must have found that there is no runtime in the call stack of vscode graphical debug/ string.go The shadow of. Next, we combine the theme of this article to continue to analyze

I believe you’ve seen my slice. Is it really quoted? The students of this article already have a good idea. For this kind of function, which has no call stack or clear caller, we can debug it by following two points

  1. First look at its assembly code
#Go code compilation
go tool compile -N -l -S test.go

The key assembly codes are as follows:

Go runtime debug tips

As shown above, we found outstringtoslicebyteFunction, so that we can break the point, as long as the breakpoint can be happy debugging

  1. Runtime function break point timing

After some of the rumtime functions are broken, the debug program will not be able to start. At this time, it is necessary to delay the timing (the name summarized by the author himself).

First, make a breakpoint at the entry of the main function (the breakpoints before calling the runtime function can be used) to delete the breakpoints of the runtime function.

Finally, wait for the debug program to start, and then put a breakpoint on the runtime function.

At this point, I wish you a happy debugging journey.

Note: when writing this paper, the version of go is go1.13.4;

We will continue to explore the technology of go

Original is not easy, humble to focus on the collection of two companies