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

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Monte Hoover