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.
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 is equivalent to “Step Over” in other debuggers.
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.
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