Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go: print variables with gdb

Tags:

go

gdb

In this program, how can break execution with the debugger and print the value of i?

package main

import "fmt"

func main() {
        x := "abc"
        i := 3
        fmt.Println(i)
        fmt.Println(x)
}

I can't print i. However I can print x:

go build test.go
gdb test  
[...]Reading symbols from /home/ned/test...done.
(gdb) br 9
(gdb) run
(gdb) p i
No symbol "i" in current context.
(gdb) p x
$1 = "abc"
like image 623
Santiago Corredoira Avatar asked Oct 07 '22 18:10

Santiago Corredoira


1 Answers

It sounds like the variable i probably got optimized out of existence by the compiler. Did you try a debug build?

You can use go build -gcflags '-N'.

like image 142
sblom Avatar answered Oct 12 '22 10:10

sblom