Go program using GDB breakpoint under Ubuntu

Hello everyone, today I bring you Ubuntu and use GDB breakpoints to debug Go programs. Since I have not been exposed to GDB debugging before, and there are no debugging tools built into the Go language itself, unlike Eralng, there is a Debugger that can be used. I searched from the Internet and there is no related Chinese tutorial. Bring an article to the novice about GDB simple debugging Go program. (There are still many articles about GDB)

  First, write the test code as follows:

package main

import (
    "fmt"
)

func main() {
    fmt.Printf("%s\n", "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 = %d\n", i, sum)
    }
}

  Compile and run the program (Sublime Text 2 under Ubuntu, if you also want to use this tool, you can refer to my article: Build a Go development environment using Sublime Text 2 under Ubuntu ):

  go build main.go

  ./main

  

  Ok, now we can start using GDB to debug and generate the main application. Here, in order to quickly find the application, I put the program and code in the ~ / directory.

  First, use GDB to load the application, open the terminal, enter gdb main, as shown below:

  

  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, which is equivalent to list, and the source code is listed from the first line, as shown below:

  

  Serious friends, you will find that the main.go code is not displayed here. What's going on?

  Here we directly hit another enter, and the result as shown below will be produced:

  

  Still incomplete, another carriage return?

  

  Now it's complete, what if we have another carriage return?

  

  At this time, according to the prompt, we know that the code has been displayed.

  Three, set a breakpoint, and display breakpoint information:

  Enter break 8, which means set a breakpoint on line 8.

  Enter break printNumber, which means that a breakpoint is set at the entrance of the function printNumber. Here, as shown in the figure, the break is not set successfully. You need to enter break main.printNumber. Thanks to friends JamCode and mikespook for their help.

  Enter break 14, which means set a breakpoint on line 14.

  Enter info break, which means to view the breakpoint information.

  As shown:

  

  Fourth, run the program and debug.

  Enter r, run the program, short for the run command, the program will stop at the breakpoint on line 8, as shown below:

  

  Use n to execute a single statement, abbreviated next command.

  Use c to continue running the program (skip the current breakpoint), the abbreviated continue command.

  Use bt to view the function stack.

  Use finish to exit the function.

  

  The following is the result of my simple debugging:

  

  There is a final question here, when I use p, output count, and sum value, its value is very strange, the friend who knows troubles leave a message to inform, thank you.

 

 

This article comes from: Blog Park

Thanks Author: yourihua

View original text: Go program using GDB breakpoint under Ubuntu

150 original articles published · 79 praised · 630,000 views +

Guess you like

Origin blog.csdn.net/liu0808/article/details/89473954