How to stop in a breakpoint after x passes in Pycharm? - debugging

Within Visual Studio, I can determine for the debug to stop at a breakpoint only after a specific number of times the program passes through it.
However, I can not find the same inside Pycharm.
Would anyone know how to do this?

Add a breakpoint at your desired position. Use hotkey ctrl,shift,F8 to open the "View Breakpoints" window. There you can mark the checkbox "condition".
In case you want to stop the program in a loop after 5 iterations, you can set the condition to target==5, where target is part of the loop as follows:
for target in object:
<your loop body>
and object is your iterable.

Related

Visual Studio: Is there a way to skip execution of code between two breakpoints?

I know I can comment the code but would be even better if there is a shortcut to skip execution of code between two breakpoints.
VS has a feature 'set next statement' which moves the program counter to the to that location and continues execution from there (skipping anything in between). You can read more about it at: https://learn.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2017#BKMK_Set_the_next_statement_to_execute. The easiest way to use it is to either use the context menu item "Set Next Statement" (right click on where you want to set it) or hold down the ctrl key which changes the green "Run to click" editor glyphs into yellow "Set next statement" glyphs and just click on where you want to set it.
Set next statement is a great tool but it's really dangerous as a debugging tool. You're using the debugger to execute code in a way which would never happen normally. The results of which could cause crashes or other failures easily. It's real easy to do things like skipping over the initialization of a variable that's later used and will now cause an exception/crash.
It can be used in JS, .NET and native.

Is it possible to record variable changes?

I am using Unreal Engine 4 and there is a bug where the mouse is not constrained to the window.
I am trying to debug the mouse position, but it am not sure how I would do it.
Every time I set a break point at the mouse position visual studio will pause the application, it is just really hard to debug because there are so many pauses.
I am wonder if it is possible to record variable changes? For example set a special break point at a variable and visual studio will only very briefly pause the application, look at the variable and put the value into a list.
I could just log the mouse position, but I want to get away from using log all the time.
This can be done by using the "When Breakpoint is Hit" window. Set the breakpoint, right-click it and choose "When hit..."
On this window, check both the "Print a message" and the "Continue execution" checkboxes. Then update the string you want to print to include the value(s) that you want printed (variable values and expressions needs to be wrapped in {} to be interpreted as such. Setting "Continue execution" makes the debugger not break at the line, instead, it will just log and continue. The messages are printed to the Debug >> Output window at runtime.

How get a breakpoint on variable write in Visual Studio?

How I can set breakpoint on variable change (I think this is write access) in Visual Studio?
This is referred to as a Data Breakpoint in Visual Studio. To create one you'll need the address of the variable in question (just add &variableName) to the watch or immediate window. Then do the following
Debug -> New Breakpoint -> New Data Breakpoint
Enter the address in and size of the value in bytes
Note: This is only supported for C++ applications. Managed languages don't support data break points.
You need to add "Has Changed" condition to your breakpoint. To do this:
Set breakpoint on the line you want it to break when your variable is changed.
Right-click red dot icon, select "Condition".
Enter your variable name and select "Has Changed" option.
You may find more information in this MSDN how-to.
This is now supported in VS2019 for . NET Core 3.0 or higher check this out
How do I set a data breakpoint? Setting a data breakpoint is as easy as right-clicking on the property you’re
interested in watching inside the watch, autos, or locals window and
selecting “Break when value changes” in the context menu. All data
breakpoints are displayed in the Breakpoints window. They are also
represented by the standard, red breakpoint circle next to the
specified property.
If you right click on the break point you can can set Conditions... This lets you specify a if a variable value is true or if its changed.
Break point conditions
You can add a conditional breakpoint by:
Add a normal breakpoint
Right-Click on it and select "Condition"
Select "Has changed"
The breakpoint will only be hit when the condition inside the textbox has changed.
As far as I'm aware, the condition inside the textbox needs to be written in the language you are debugging. I.e. in C#: x >= 5
If you are just looking for the change of a variable, you can simply add the variable itself to the TextBox and the breakpoint will be hit when the variable changes.
HTH,
Christian

Is there any way to set or code breakpoints conditionally?

I've been wondering this for a while - is there a way to code/program breakpoints...? Conditionally? For example, can I specify something like - "when this variable becomes this value, break and open the debugger"? (Would be quite useful, especially in long loops when you want to debug loop execution of a late loop value.)
I suppose this may be IDE-specific since debugging is implemented differently in different IDEs... I'd be interested to know how to do this in any IDE, but specifically in Eclipse and Visual Studio.
This is definitely possible in Visual Studio. You can normally click in the left margin to insert the breakpoint, and then right click on that breakpoint. One of the options in the right click menu is "Condition...", and you can specify a predicate that will tell the debugger only the break on that breakpoint if the predicate is met.
In Visual Studio, you can declaratively set conditional breakpoint, which are like normal breakpoint but will only break when a certain condition is true. The condition can use local variables and whatever is accessible from where the breakpoint is set. Just right click any breakpoint (red dot) and select "Condition...".
In addition, .NET languages can call the Debugger.Break() method to programmatically break the execution. This also can be done within an if statement:
if (count > 8 && Debugger.IsAttached)
Debugger.Break();
If conditional breakpoints aren't supported by your IDE, add in an if statement and break inside of it.
if (variable == 3) {
// Stub code to attach breakpoint.
1 = 1;
}
Setting a conditional breakpoint in Eclipse (thanks for all the Visual Studio answers!):
Set a breakpoint. Right-click and choose "Breakpoint Properties...". Check "Enable Condition" and enter your condition code into the text area.
Most IDEs allow for conditional break points for this very reason. In Visual Studio, you can right click on the red dot for the breakpoint in the margin and open the condition dialog from there. You can also get at the condition dialog from the breakpoint window in Visual Studio. I am not familiar with Eclipse.

Visual Studio 2008 - Debugging tips/tricks - Continue "until next function" or "until next file"

Is there any way to tell the debugger to just continue until the next file is accessed, and/or until the next (developer written) function is accessed, without setting debug points ahead of time? I'm kind of new to VS debugging so all I use right now are f5, f10, and f11.
There is currently no way to do what you are asking. The main ways of telling VS to go until something happens are the following
Hit F5 and VS will go until the next user breakpoint or ,depending on your settings and where it occurs, the next exception is raised
Right click and select "Run to cursor"
Shift-F11 breaks out of the current method
Run to cursor doesn't require an explicit break point but it does require that you know where you want to break next.
You can right-click and select "run to cursor" if you just want to run to a specific line ahead in the execution stream.
Another one is Shift-F11 which finishes the current method and breaks again when you get back to the caller.
Actually, there is a way to set conditional break points.
Click in the left margin on the line where you want to break, as usual. (or F9)
Right-click on the red dot. In the context menu, click on "Condition...."
In the dialog, set your condition, e.g., fileName == "foo"
Hit F5 and go until the conditional break is hit.
Looks like there's not a way to do what I wanted to do

Resources