Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see if the GoLand debugger is running in the program?

Tags:

go

goland

delve

In C# the executing program can detect if it's running in the debugger using:

System.Diagnostics.Debugger.IsAttached

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!

I am using the GoLand debugger.

like image 259
Philip Beber Avatar asked Dec 19 '17 02:12

Philip Beber


People also ask

How do I run code in GoLand?

Open the Run/Debug Configuration dialog in one of the following ways: Select Run | Edit Configurations from the main menu. With the Navigation bar visible (View | Appearance | Navigation Bar), choose Edit Configurations from the run/debug configuration selector. Press Alt+Shift+F10 and then press 0 .

Where is run debug configuration in Intellij?

From the main menu, select Run | Edit Configurations. Alternatively, press Alt+Shift+F10 , then 0 . on the toolbar or press Alt+Insert . The list shows the run/debug configuration templates.

How do you add a breakpoint in GoLand?

Set breakpointsClick the gutter at the executable line of code where you want to set the breakpoint. Alternatively, place the caret at the line and press Ctrl+F8 .


1 Answers

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)
}

In Goland, you can enable this under 'Run/Debug Configurations', by adding the following into 'Go tool arguments:'

-tags=delve

Goland Run/Debug Configurations window


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.


Alternatively, you can use delve's set command to manually set a variable after starting the debugger.

like image 82
Martin Tournoij Avatar answered Sep 21 '22 00:09

Martin Tournoij