Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set breakpoints by the sourcefile line number in Delve?

The title pretty much says it all.

The only way I know how to set one is either during the runtime of the program or just before with breakpoint main.main

Is there a way I can do this by line number like breakpoint ./otherfile.go:200?

like image 481
benbot Avatar asked Mar 08 '16 00:03

benbot


People also ask

How do you code breakpoints?

To set a breakpoint in source code, click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.

Do breakpoints execute the line?

Breakpoints break before executing the line on which they are placed.

How does debugger set breakpoints?

They work by patching the code you are trying to execute with an instruction that triggers a debug event in some fashion. This is accomplished by injecting a breakpoint instruction or when that is not supported by inserting an instruction that causes a fault that halts the core.


2 Answers

In your Source code type

  runtime.Breakpoint()

type in the CLI

dlv test

and then

continue

The program will stop in the line of code where you set the breakpoint.

like image 96
Alejandro Serret Avatar answered Sep 28 '22 07:09

Alejandro Serret


The following is valid in delve:

(dlv) break <breakpoint_name> <filename_pattern>:<line_number>

If you have ambiguous filenames, say main.go spread all over your sources and possibly vendor directory, just make you make it look "unique" to delve (filename_pattern is not your exact file location). For example:

(dlv) break myLoopingStuff project_name/loops.go:30
(dlv) condition myLoopingStuff thing == someOther.thing
like image 42
Wari Wahab Avatar answered Sep 28 '22 07:09

Wari Wahab