Go Code Debugging Using The GDB

Mayur Wadekar
4 min readJun 29, 2020

Many times your code is so clean and clear but the code contains a snippet that might cause an unexpected behaviour of code. Some examples where sudden panic occurs or logical error might happen or even sometimes considering the worse code breaks down the whole system. So at this point of time we have to check that what kind of things / code we written wrong or where exactly things / code is wrong? in that case the debugging comes into the picture.

Image from ELPROCCUS

Debugging is nothing but the identify and remove number of bugs in a program. Debugging is always manual it can’t be automated.

The important technique to find and remove the number of errors or bugs or defects in a program is called Debugging. It is a multistep process in software development. It involves identifying the bug, finding the source of the bug and correcting the problem to make the program error-free. In software development, the developer can locate the code error in the program and remove it using this process. Hence, it plays a vital role in the entire software development lifecycle.

What Is GDB?

The GNU Debugger (GDB) is a portable debugger that runs on many Unix-like systems and works for many programming languages, including Ada, C, C++, Objective-C, Free Pascal, Fortran, Go, and partially others.

When to use debugger?

  1. If changing the code is not a option.
  2. When working with many goroutines where the ability to pause and inspect program state would be beneficial

Note: Here I’m using Linux AMD64 bit machine to explain the things.

Common Operations

Common Operations
  1. list: list will simply displays the code. So you can identify where you have to set the breakpoints.
  2. list line: This simply tells that the line number to display.
  3. list filename.go:line: same as list line option.
  4. break line and break filename.go:line: This option is used to set break-point to the file(instead of whole word you can use only b to set breakpoints).
  5. disas : Disassembles a specified function or a function fragment.

Operations:

So here I have written simple code named file main.go.

package mainimport
(
"fmt"
)
func main() {
var a int = 10
for i:=1; i <= a; i++ {
fmt.Println("value of a : ", i)
}
}

After you have to make build

$ go build -gcflags=all=”-N -l” main.go

To start GDB use command like below. (Here “main” is a binary file)

$ gdb main
Starting the Debugger

After starting debugger you are ready to do the debugging. Now we have to set the breakpoints where it is actually working or pause the execution of code. So we have to put the break-point using the code as shown below. We put break-point on line number 11. After we set line number 11 then we are getting output like

Break-point settled on line 11

Or else you can simple set break-point using

$ (gdb) b main.go:11

Now it’s time to execute the code and simply check the output. But for this two commands of GDB are important.

  1. (gdb) run: This will executes your go file which you’ve given input to the debugger.
  2. (gdb) continue: This will continues the execution after breakpoints arrival.
RUN and CONTINUE

After run command if we have hit the break-point then it will shows that we hit the break-point. It also shows the line where exactly we set the break-point. And continue will continues the execution until next break-point arrives.

We can also clear and delete breakpoints using clear and delete breakpoint NUMBER respectively. By dynamically creating and toggling breakpoints we can efficiently traverse application flow.

Deletion of Breakpoints

In above image the breakpoint number were given not the line number. If breakpoints not present then it will show like No breakpoint at number.

At the last after whole code is being executed it shows like you’ve exited with your execution normally.

[Inferior 1 (process 15647) exited normally]

This is just simple explanation about the how to debug the code and identify the bugs from a simple code.

Versions:1. GDB: GNU gdb 8.22. OS: Ubuntu 18.04.1 LTS

References:

  1. https://golang.org/doc/gdb
  2. https://sourceware.org/gdb/current/onlinedocs/gdb/

--

--