我正在尝试调试一个简单的 golang 程序,但不知道如何在delve调试器中的特定 goroutine 上设置断点。
这是一个例子:
package main
import "fmt"
func adder(a int, b int, res_chan chan int) {
c := a + b
res_chan <- c
}
func main() {
chan1 := make(chan int)
chan2 := make(chan int)
go adder(0, 1, chan1)
go adder(0, 2, chan2)
res1 := <-chan1
res2 := <-chan2
fmt.Printf("Channel 1: %d\nChannel 2: %d\n", res1, res2)
}
我这样做dlv debug
:
Type 'help' for list of commands.
在 goroutine 上设置断点,在这里我直观地假设调试器将停止两次(在每个调用的 goroutine 上):
(dlv) b main.adder
Breakpoint 1 set at 0x49dd1f for main.adder() ./adder.go:5
(dlv) c
> main.adder() ./adder.go:5 (hits goroutine(18):1 total:2) (PC: 0x49dd1f)
> main.adder() ./adder.go:5 (hits goroutine(19):1 total:2) (PC: 0x49dd1f)
1: package main
2:
3: import "fmt"
4:
=> 5: func adder(a int, b int, res_chan chan int) {
6: c := a + b
7: res_chan <- c
8: }
9:
10: func main() {
(dlv) s
> main.adder() ./adder.go:6 (PC: 0x49dd2d)
1: package main
2:
3: import "fmt"
4:
5: func adder(a int, b int, res_chan chan int) {
=> 6: c := a + b
7: res_chan <- c
8: }
9:
10: func main() {
11: chan1 := make(chan int)
(dlv)
> main.adder() ./adder.go:7 (PC: 0x49dd3c)
2:
3: import "fmt"
4:
5: func adder(a int, b int, res_chan chan int) {
6: c := a + b
=> 7: res_chan <- c
8: }
9:
10: func main() {
11: chan1 := make(chan int)
12: chan2 := make(chan int)
检查c
变量的值:
(dlv) p c
2
继续执行:
(dlv) c
Channel 1: 1
Channel 2: 2
Process 8112 has exited with status 0
退出调试器:
(dlv) q
Process 8112 has exited with status 0
因此,带有结果的 goroutine1
没有被调试器击中。如何在 goroutine 结果中调试代码执行1
?