Watch/Identify the hit count of a breakpoint in VS Code debugger - vscode-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.

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:
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

Create a visual studio breakpoint that pauses for one second and then continues

The topic says it all. Sometimes i just want to take a quick look and then continue, especially when a code line get hit several times. Can i automate the press-manually-f5-to-continue-after-a-second?
Right click on a breakpoint an select "When Hit...":
You can run a macro like this:
Sub Sleep1s()
System.Threading.Thread.Sleep(1000)
End Sub
Or you can call 11 times {System.Threading.Thread.Sleep(90)} in the message. (Because VS debugger doesn't allow for expression to run for more than 100 ms).
But sleeping this way will block the main IDE UI thread. I don't know is it acceptable for you.

Is it possible to set breakpoint condition as "break when called by another thread"?

I'm investigating an issue that might be caused by multithread. The data is constantly being read by a thread, I'd like to see if it is being read by another thread.
I can set breakpoint and keep pressing F5 and check if it is breaking in the other thread, but it's a little annoying. Is it possible to set breakpoint condition as "break when called by another thread"?
You can set up a Filter (see Using Breakpoints). The filter can be set on ThreadId, for example. To set a filter, right-click a breakpoint, select Conditions..., and enter
ThreadId!=<thread ID you aren't interested in>
as the filter expression. Make sure there aren't any whitespace characters in the expression. Otherwise you will be greeted with a not-so-helpful error message.

R, debug line number

When debugging a function (which has been marked debug using debug("f"), the debugger gives you the Browser prompt which also tells you at what line number in the program you are. If run a couple of test statements at the prompt (to check variables, etc.) the screen scrolls and I no longer know what line number I am at (using SecureCRT so it scrolls past the buffer). The command where only tells you what function you are in. Does anyone know how to get the actual line number and next statement to be executed?
Thanks
When I use the regular browser(), I set max.lines to print to a low number:
options(deparse.max.lines=100)
so that if the output during debugging is long, I don't have to scroll too far up.

How can I see when the value of a variable change?

I have this really nasty bug in my program, which somehow changes the value of my variable. I have tried setting break points but can't find the line of code that's the culprit.
Is there any way to pause program execution when there's a line of code that's changing the value of a variable in Xcode 4?
One way would be to find the value changing operations or in general any bug using bisection. Ie. you choose two breakpoints that have the problematic operation in between and move one of them to middle and check if the value still changes. If it does repeat in this halt, if not repeat in the other half.

How can i stop the debugger in a particular variable value?

how can i set de debugger to stop when some particular variable has a defined value?. For example i have a code that it crash that loops 10000 time to make some postprocessing. I know that the error could be produced from the 7000 iteration up, so i want to stop from there on, avoiding manual loop from the first 7000.
Im using visual studio 2008 and 2010 with c#, i think that the solution will be the same for both.
What you're looking for is a conditional break point. Here's how to set it up assuming the variables name is i.
Set a normal breakpoint on the line after the value is set
Right click on the red dot portion of the breakpoint and select "Condition"
Enter the condition which you want to check for. Example: i == 10000
Hit OK
Now run your scenario again and the breakpoint will be hit only when the value of i equals 10000.
A word of warning. You can put pretty much any legal C# expression into a conditional break point but it will be evaluated every single time the break point is hit. That can lead to very slow debugging if use a complex conditional
Open the breakpoint window and create a new data breakpoint from its menu.
The easy way to do that is:
if (nameVariable = X ) {
BreakPoint: nameVariable;
}

Resources