Watch/Identify the hit count of a breakpoint in VS Code debugger

Viewed 372

Often times I add a breakpoint at a line in a loop and keep playing the to the next loop until I find the desired execution loop. And I might have to do this repeatedly to find the next possible error in the loop. I either count the number of times I hit the play button (F8) or identify a variable to add conditional breakpoint.

Is there a way to identify the number of time a breakpoint is hit? can I add a "watch" expression to get this?

This would help me if I have to debug the same line in multiple debugging sessions after changing something.

2 Answers

One of the options under Conditional Breakpoint is "Log Message". This outputs a message in the debug console with a number next to it showing how many times the log point was hit.

(This is dependent on the debugger extension, but it works in like this in the Python and JavaScript debuggers.)

There is an option for specifying "Hit Count" when you right click in the margin and select "Add Conditional Breakpoint." A text box appears with a drop down box beside it where you select "Hit Count" and add the number of hits desired in the text box:

Conditional breakpoint gif

The exact syntax depends on the debugger extension you are using, but for the Node.js debugger the syntax is as follows (from the documentation):

The syntax is either an integer or one of the operators <, <=, ==, >, >=, % followed by an integer.

Some examples:

>10 break always after 10 hits
<3 break on the first two hits only
10 same as >=10
%2 break on every other hit

Python syntax appears to be the same. It looks like hit counts are not currently working in the C++ extension though: https://github.com/Microsoft/vscode-cpptools/issues/714#issuecomment-663528553

See also https://code.visualstudio.com/Docs/editor/debugging#_conditional-breakpoints.

Related