简体   繁体   English

如何在 vscode 中调试 goroutine?

[英]how to debug goroutine in vscode?

consider the small snippet below:考虑下面的小片段:

package main

import (
    "fmt"
    "sync"
)

func main() {
    var wg sync.WaitGroup
    wg.Add(2)
    go func() {
        defer wg.Done()
        for i := 1; i < 100; i++ {
            fmt.Println("A:", i)
        }
    }()
    go func() {
        defer wg.Done()
        for i := 1; i < 100; i++ {
            fmt.Println("B:", i)
        }
    }()
    wg.Wait()
}

in delve, we can easily switch between goroutines using the commands like在 delve 中,我们可以使用以下命令轻松地在 goroutine 之间切换

goroutine
goroutine <id>
goroutine <id> <command>

and if i want run step by step in goroutine 1, just use the command如果我想在 goroutine 1 中逐步运行,只需使用命令

goroutine 1 next

In vscode, it seems the only way to deal with goroutines is the call stack, however, it seems that this is the internal threads in go runtime, not the goroutines, so how can i focus the running process in the specified goroutine?在vscode中,似乎处理goroutines的唯一方法是调用堆栈,但是,这似乎是go运行时的内部线程,而不是goroutines,那么我怎样才能将运行进程集中在指定的goroutine中?

so how can i focus the running process in the specified goroutine?那么如何将正在运行的进程集中在指定的 goroutine 中呢?

vscode-go issue 1797 should bring a possible solution: vscode-go issue 1797应该会带来一个可能的解决方案:

For example, when debugging a simple hello world program, 5 different goroutines appear in the callstack:例如,在调试一个简单的 hello world 程序时,调用堆栈中会出现 5 个不同的 goroutine:

程序在 main.go 的断点处停止:

This proposal is to (by default) only show the user goroutines in the call stack.该提议是(默认情况下)仅在调用堆栈中显示用户 goroutine。 When using this new default, the call stack for hello world will only show a single goroutine, as would be expected from a simple program with no concurrency:当使用这个新的默认值时,hello world 的调用堆栈将只显示一个 goroutine,正如一个没有并发的简单程序所期望的那样:

独特的协程

It just (Oct. 2021) has been submitted in CL 359402 (CL = Change List: a set of commits/patches)它刚刚(2021 年 10 月)已在CL 359402中提交(CL = 更改列表:一组提交/补丁)

package.json: add config to hide system goroutines in debug package.json:添加配置以在调试中隐藏系统 goroutines

This change includes the configuration for hiding the system goroutines in a debug session此更改包括在调试 session 中隐藏系统 goroutine 的配置

Jetbrains have introduced a cool new feature for debugging go-routines in its IDE. Jetbrains 在其 IDE 中引入了一个很酷的新功能,用于调试 go-routines。 Please follow this link to take a look请点击此链接查看

It uses feature of labels provided by pprof and is not exclusive to jetbrains I suppose although they might have tweaked it for usability.它使用 pprof 提供的标签功能,并且我认为它并不是 jetbrains 独有的,尽管他们可能已经对其进行了调整以提高可用性。 Here is an article on lables so that you can try to implement it in vscode also.这是一篇关于标签的文章,因此您也可以尝试在 vscode 中实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM