I found a golang debugging bug?

I found a bug when I try to debug the golang code below:

package main

import "log"

type SA struct {
	dataint int
	datastr string
	datasi  []int
}

type SB struct {
}

func (sb *SB) sbfun(sa SA) {
}

func main() {
	log.Println(`start`)
	var sb *SB = nil //always nil
	if sb != nil { // always false
		log.Println(`flag 1`)
		sb.sbfun(SA{}) // I set breakpoint here
	}
	log.Println(`end`)
}

I set the breakpoint and debug(using gdb or Delve), but it always interrupted there,
and no “flag 1” appeared on console, can anyone detail why such bug with this code? can anyone try to debug this code ?
many thanks.

chinese:

我发现了一个golang在debug时的BUG,对不起,我英文不好,请看以下代码:

package main

import "log"

type SA struct {
	dataint int
	datastr string
	datasi  []int
}

type SB struct {
}

func (sb *SB) sbfun(sa SA) {
}

func main() {
	log.Println(`start`)
	var sb *SB = nil //总是为nil
	if sb != nil { // 总是为false
		log.Println(`flag 1`)
		sb.sbfun(SA{}) // 我在这里设置断点
	}
	log.Println(`end`)
}

当我在那个地方设置断点,然后开始debug这个程序(使用gdb或Delve),就会发现,
程序总是中断在那个地方,并且中断的时候在控制台看不到“flag 1”输出标记,
为什么会这样?大家可以用这段代码亲自试一试。十分感谢!

1 Like

I wouldn’t say that you’ve found a bug, but more a confusing behavior.

The block where you set the breakpoint was optimized away, because it cannot be reached. The breakpoint was probably set on the nearest bit of code that still exists, which would be log.Println(“end”). You could reasonably ask the Delve maintainers why it is doing that, it might be reasonable to hope that a debugger would say, “warning: line X does not exist, moving breakpoint to line X+2”.

-jeff

2 Likes

if i change code to this:


log.Println(start)
if rand.Intn(5)>10 { // always false
log.Println(flag 1)
sb.sbfun(SA{}) // I set breakpoint here
}

debugger still interrupted there , and I can’t see “start” flag ,why? why i can’t see “start” when debugger interrupted there

please try this code and debug it , use gdb

The “bug” couldn’t be reproduced in my env.

(gdb) break 22
Breakpoint 1 at 0x4011e9: file /data/songtianyi/golang/own/src/playground/sb.go, line 22.
(gdb) info  break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004011e9 in main.main at /data/songtianyi/golang/own/src/playground/sb.go:22
(gdb) r
Starting program: /data/songtianyi/golang/own/src/playground/sb
[New LWP 21215]
[New LWP 21216]
[New LWP 21217]
[New LWP 21218]
2017/03/03 18:38:32 start
2017/03/03 18:38:32 end
[LWP 21215 exited]
[LWP 21218 exited]
[LWP 21216 exited]
[LWP 21211 exited]
[Inferior 1 (process 21211) exited normally]

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.