Static language debugging tools such as Golang are essential, both in daily development and to familiarize yourself with the underlying principles of the language are very important tools. GDB is a powerful debugging tool for Unix and UNIX-like systems that can also debug Golang applications.

You can walk through the basics of how golang code is executed, how variables are assigned, when memory is allocated, and so on.

Preparation environment:


GDB 7.1 and above

Go Development environment

A short piece of Golang code

package main
import "fmt"
func main(){
  c:=make(map[string]interface{})
  fmt.Println(c)
}Copy the code

Go build-gcflags “-n -l” test.go // generate executable code, compile, and turn off inline optimization

Ok, now that we are ready, we have the environment, tools, and running programs, let’s start debugging the GO executable using GDB.

Gdb-tui test # is particularly convenient to display code simultaneously at run time


>> b main.main // Add a breakpoint to main

>>run // Run the process

>> S //s is short for step. See below for the difference between s and N

、、、、、、、、、、

S: Executes a line of source code, entering the function if there is a function call in this line of code;

N: A line of source code is executed, along with the function call in this line of code.

S is equivalent to “Step Into” in other debuggers;


N is equivalent to “Step Over” in other debuggers.

、、、、、、、、、、、



Go ahead and type s



It can be clearly seen from the above. With the input of S, you can see the detailed execution process of the code, such as the execution process of map, memory allocation process, etc. If you just want to see the execution result of the written code line by line, you can enter N.

The above command has provided a good view of the golang code running process. The following is some specific variable information for more detailed understanding of the values.

Here are some common commands to look at,
L main.go:8 // View source code in: mode.

B main.main // Set a breakpoint in. Mode.


B main.go:17 // Set a breakpoint in: mode.



Info breakpoints // View all breakpoints.
Info Goroutines // View information about goroutines.




Goroutine 1 bt // View the goroutine call stack with the specified sequence number.


Goroutine 2 bt // This goroutine is related to GC.


C Continue
N Run the next command
Info Goroutines // The current goroutine sequence number is 1.
Goroutine 1 bt // Current Goroutine call stack.


Bt // View the current call stack, which can be compared with the current Goroutine call stack.


Info Frame // Stack frame information.


Info locals // View local variables


P s // View variables in Pretty-Print mode.

Whatis I // View the object type

C // Continue and trigger breakpoint().

Info args // From the parameter information, we can see that the name returns the value of the parameter.

X / 3xw&r // View r memory data. (Pointer 8 + length 4)

Q // Exit GDB.


Blog is highly recommended:

This article can be good at https://blog.csdn.net/liigo/article/details/582231?utm_source=copy