如何查看GoLand调试器是否在程序中运行? [英] How can I see if the GoLand debugger is running in the program?
问题描述
在C#中,执行程序可以使用以下命令检测其是否在调试器中运行:
In C# the executing program can detect if it's running in the debugger using:
System.Diagnostics.Debugger.IsAttached
Go中是否有等效项?我有一些超时,希望在我逐步执行代码时被禁用.谢谢!
Is there an equivalent in Go? I have some timeouts which I would like to be disabled while I am stepping through the code. Thanks!
我正在使用GoLand调试器.
I am using the GoLand debugger.
推荐答案
据我所知,没有内置的方法可以按照您所描述的方式来执行此操作.但是,您可以使用构建标记或多或少执行相同的操作,以指示delve调试器正在运行.您可以使用--build-flags
参数将构建标记传递给dlv
.基本上与我在中所述的技术相同,该如何检查运行时是否启用了竞争检测器?
As far as I know, there is no built-in way to do this in the manner you described. But you can do more or less the same using build tags to indicate that the delve debugger is running. You can pass build tags to dlv
with the --build-flags
argument. This is basically the same technique as I described in How can I check if the race detector is enabled at runtime?
isdelve/delve.go
// +build delve
package isdelve
const Enabled = true
isdelve/nodelve.go
:
// +build !delve
package isdelve
const Enabled = false
a.go
:
package main
import (
"isdelve"
"fmt"
)
func main() {
fmt.Println("delve", isdelve.Enabled)
}
在Goland中,您可以在运行/调试配置"下启用此功能,方法是将以下内容添加到"Go工具参数"中:
In Goland, you can enable this under 'Run/Debug Configurations', by adding the following into 'Go tool arguments:'
-tags=delve
如果您不在Goland之外,则运行go run a.go
会报告delve false
,如果要单独运行dlv,请使用
dlv debug --build-flags='-tags=delve' a.go
;这将报告delve true
.
If you are outside of Goland, running go run a.go
will report delve false
, if you want to run dlv on its own, use
dlv debug --build-flags='-tags=delve' a.go
; this will report delve true
.
或者,您可以在启动调试器后使用delve的set
命令手动设置变量.
Alternatively, you can use delve's set
command to manually set a variable after starting the debugger.
这篇关于如何查看GoLand调试器是否在程序中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!