This is a creation in Article, where the information may have evolved or changed.
Hello everyone, today for everyone to bring Ubuntu under, use GDB breakpoint debugging Go program. Since there was no prior contact with GDB debugging, and the go language itself built-in there is no debugging tools, unlike ERALNG, there are debugger can use, I searched from the Internet, there is no relevant Chinese course, so this side of the study, to the novice to bring a simple debugging go program GDB The article. (There are a lot of articles about GDB.)
First, write the test code, as follows:
Package Main
Import (
"FMT"
)
Func Main () {
Fmt. Printf ("%sn", "Hello, World")
Printnumber ()
}
Func Printnumber () {
var count int;
Count = 10
Sum: = 0
For I: = 0; I < count; i++ {
sum = sum + I
Fmt. Printf ("i =%d, sum =%dn", I, sum)
}
}
Compile and run the program (Ubuntu under the sublime Text 2, if you also want to use this tool, you can see I make an article: Ubuntu under the use of sublime Text 2 to build the Go development environment):
Go Build Main.go
./main
OK, now we can start using GDB Debug, generated the main application, here in order to quickly find the application, I put the program and code into the ~/directory.
First, use GDB to load the application, open the terminal, and enter GDB main, such as:
Note the last two lines here:
Reading symbols From/home/administrator/main...done.
Loading Go Runtime Support.
Read the main program to complete and load the go runtime.
Second, enter the L command, the equivalent of list, starting from the first line to list the source code, such as:
Serious friends will find Main.go code, and there is no complete display here, what's going on?
Here we can directly hit a return, will produce the result as follows:
Or is it not complete, and then a carriage return?
Now that it's complete, what if we get a return?
At this point, we know that the code is all displayed, as prompted.
Three, set breakpoints, and display breakpoint information:
Enter break 8, which indicates that a breakpoint is set on line 8th.
Enter break Printnumber, which indicates that at the entrance of the function Printnumber function, set a breakpoint, here is not set successfully, need to enter breaks Main.printnumber. Thanks to friends Jamcode and Mikespook for their help.
Enter break 14, which indicates that a breakpoint is set on line 14th.
Enter info break, which indicates the view breakpoint information.
Four, run the program, and Debug.
Enter R, run the program, the Run command shorthand, the program will stop the 8th line at the breakpoint, such as:
Use N, single statement execution, next command shorthand.
Using C, continue to run the program (skip the current breakpoint), continue command shorthand.
Use BT to view the function stack.
Use Finish to exit the function.
Here is the result of my simple debugging: