Skip to content

Instantly share code, notes, and snippets.

@tetsuok
Created June 30, 2012 20:16
Show Gist options
  • Save tetsuok/3025333 to your computer and use it in GitHub Desktop.
Save tetsuok/3025333 to your computer and use it in GitHub Desktop.
Turning off optimization and inlining in Go gc compilers for debugging.
source /usr/local/go/src/pkg/runtime/runtime-gdb.py
b main.main
$ ls
README.md sum.go
$ go build sum.go
$ ~/local/bin/gdb sum
GNU gdb (GDB) 7.4.1
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin11.4.0".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /Users/tetsuo-s/github/gist-3025333/sum...done.
Loading Go Runtime support.
Breakpoint 1 at 0x202f: file /Users/tetsuo-s/github/gist-3025333/sum.go, line 13.
(gdb) l
1 package main
2
3 import "fmt"
4
5 func sum(n int) int {
6 sum := 0
7 for i := 1; i < n; i++ {
8 sum += i
9 }
10 return sum
(gdb) r
Starting program: /Users/tetsuo-s/github/gist-3025333/sum
[New Thread 0x1e03 of process 23269]
[Switching to Thread 0x1e03 of process 23269]
Breakpoint 1, main.main () at /Users/tetsuo-s/github/gist-3025333/sum.go:13
13 func main() {
(gdb) n
14 n := sum(10) // n should be 45
(gdb) n
15 fmt.Printf("sum = %d\n", n)
(gdb) p n
No symbol "n" in current context.
(gdb) c
Continuing.
sum = 45
[Inferior 1 (process 23269) exited normally]
(gdb)
$ go build -gcflags '-N -l' sum.go
$ ~/local/bin/gdb sum
GNU gdb (GDB) 7.4.1
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin11.4.0".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /Users/tetsuo-s/github/gist-3025333/sum...done.
Loading Go Runtime support.
Breakpoint 1 at 0x2055: file /Users/tetsuo-s/github/gist-3025333/sum.go, line 13.
(gdb) l
1 package main
2
3 import "fmt"
4
5 func sum(n int) int {
6 sum := 0
7 for i := 1; i < n; i++ {
8 sum += i
9 }
10 return sum
(gdb) r
Starting program: /Users/tetsuo-s/github/gist-3025333/sum
[New Thread 0x1e03 of process 23362]
[Switching to Thread 0x1e03 of process 23362]
Breakpoint 1, main.main () at /Users/tetsuo-s/github/gist-3025333/sum.go:13
13 func main() {
(gdb) n
14 n := sum(10) // n should be 45
(gdb) n
15 fmt.Printf("sum = %d\n", n)
(gdb) p n
$1 = 45
(gdb)
$ ~/local/bin/gdb sum
GNU gdb (GDB) 7.4.1
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin11.4.0".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /Users/tetsuo-s/github/gist-3025333/sum...done.
Loading Go Runtime support.
Breakpoint 1 at 0x202f: file /Users/tetsuo-s/github/gist-3025333/sum.go, line 13.
(gdb) l
1 package main
2
3 import "fmt"
4
5 func sum(n int) int {
6 sum := 0
7 for i := 1; i < n; i++ {
8 sum += i
9 }
10 return sum
(gdb) l
11 }
12
13 func main() {
14 n := sum(10) // n should be 45
15 fmt.Printf("sum = %d\n", n)
16 }
(gdb) r
Starting program: /Users/tetsuo-s/github/gist-3025333/sum
[New Thread 0x1e03 of process 23634]
[Switching to Thread 0x1e03 of process 23634]
Breakpoint 1, main.main () at /Users/tetsuo-s/github/gist-3025333/sum.go:13
13 func main() {
(gdb) s
14 n := sum(10) // n should be 45
(gdb) x/5i $rip
=> 0x2046 <main.main+23>: mov DWORD PTR [rsp],0xa
0x204d <main.main+30>: call 0x2000 <main.sum>
0x2052 <main.main+35>: mov ecx,DWORD PTR [rsp+0x8]
0x2056 <main.main+39>: lea rdi,[rsp+0x48]
0x205b <main.main+44>: xor rax,rax
(gdb) p $ecx
$1 = 1108488192
(gdb) ni
0x000000000000204d 14 n := sum(10) // n should be 45
(gdb) ni
0x0000000000002052 14 n := sum(10) // n should be 45
(gdb) x/i $rip
=> 0x2052 <main.main+35>: mov ecx,DWORD PTR [rsp+0x8]
(gdb) ni
15 fmt.Printf("sum = %d\n", n)
(gdb) p $ecx
$2 = 45
(gdb)
package main
import "fmt"
func sum(n int) int {
sum := 0
for i := 1; i < n; i++ {
sum += i
}
return sum
}
func main() {
n := sum(10) // n should be 45
fmt.Printf("sum = %d\n", n)
}
@zak905
Copy link

zak905 commented Sep 24, 2022

How about -E ?

-E debug symbol export

@jlearman
Copy link

jlearman commented Feb 2, 2024

I believe the current incantation is

-gcflags 'all=-N -l'

When I left out all= I and debugged using dlv, I got the warning that I was debugging optimized functions.

To debug using dlv I use:

dlv test --build-flags="-gcflags 'all=-N -l'"

and if I need to debug as root:

sudo -E --preserve-env=PATH bash -c "dlv test --build-flags=\"-gcflags 'all=-N -l'\""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment