Breakpoint with Condition - c++

Is there a way how to set up more complex condition for breakpoints in QtCreator? (dbg) At least comparing the QStrings, but other complex type would be nice too. Integer comparing like in tutorials works fine.

Right click the breakpoint (red ball in the left hand side of the text editor) and select something like "Edit breakpoint". A dialog then let you put conditions on the breakpoint.

ctrl+click or right click a breakpoint and choose Edit Breakpoint.
A window comes up where you can set "Condition" and how many times it should be ignored before the debugger stops the execution.
The condition can be whatever you would write in a conditional c++ clause.
There are other options as well such as which thread are you targeting.

Related

How to view contents of an array while debugging in Code Blocks?

This is probably a silly question and i am posting this Post-Googling,
The question is,
How do i view the contents of an array while debugging, Provided the Breakpoint is in function not Main?
I am using code::blocks 13.12 version and i set a break point in my sample simple c++ code,
and i want to observe how the contents of the array change with the code but all i see is the the address of the array under watch window,
It is however displaying the values of other integer variables correctly,
a is an array
I forgot to mention that i am tracking the array in a function and not in main,and i am aware that main passes the address of the array to the function being called(by reference i meant)
I am relatively new to codeblocks, Am i missing some setting or something?
Right click on the cell, then select "Properties", there you can tell it to "watch as array", and tell it the index range to watch.
i am using 12.11. I select the array variable suppose a, choose "watch a" in the right button down menu, and I can see the values in the array a.
Another thing that I use is watching under cursor.
If you go Settings -> Debugger... -> GDB/CDB debugger -> Default and you enable selection "Evaluate expressions under cursor", every time you leave mouse pointer above a variable in the code, it will be watched.
It's not the same with having it in Watches toolbar, but strangely it will show array's fields.
Debug -> Debugging windows -> Watches
you may watch the variables in this way
in convenience, you can find button "Debugging Windows" near to the debug toolbar, choose "watch", hope you can find it!

VS 2012: Debugger: "Break all in 5 seconds"

My program is using too much CPU power when I select text. And I don't know why. Normally I'd just press "Break all" to see what the program is currently doing. But in this case I'm busy selecting text with the mouse.
Is there any trick to delay the "Break all" command for a few seconds?
you can start a separate thread with Sleep(5000); DebugBreak();
Press CTRL-Break while selecting. This should bring the debugger up immediately.
You can sprinkle all your suspicious code with special kind of breakpoints that do not stop execution, but print a message in Output window.
Add a breakpoint, go to Breakpoints window, find the breakpoint that was just added, right-click, select "When Hit...", in new window select "Print a message". Make sure check box "Continue execution" is checked (it should be by default). Breakpoint icon will change from circle to diamond. Repeat this for several breakpoints.
You can adjust the text that breakpoint will display to whatever you need.

Is there a way to make the compiler automatically hit a statement(breakpoint) once in catch clause

Currently I have to set breakpoints in my catch clauses. I wanted to know if there was a way that once in a catch clause the compiler automatically highlights a certain statement. Right now i have to set a break point in my exception handler so that my compiler lets me know that it had entered an exception handler.I am using VS2010
You can put __debugbreak(); (comes from <intrin.h>) inside your catch clause and the IDE will stop whenever __debugbreak(); is reached.
I don't know of such a functionality.
However, if you go to Debug > Exceptions in the menu, you can toggle exception types for which the debugger will break when they're thrown. I know that's not the same as when they're caught, but you should be able to reach a catch statement by a few "Step into/over" steps from there.
You could add an ASSERT(FALSE), which will cause the debugger to stop on that line for a debug build.
Just to be clear: at least as a I understand the situation, you want a breakpoint on a particular line, but only when/if a particular catch clause was executed. If that's what you want, there are ways to do things, though (to my knowledge) you'll probably have to modify your code a little to make it work.
I, at least, would do this by:
creating a global variable, something like: bool exception_caught;
In the relevant catch clause, adding exception_caught = true;
Making the breakpoint conditional on exception_caught
Offhand, I don't remember exactly how you created a conditional break-point in VS 2010. In 2012, you can set the break-point as usual (e.g., with F9) then right-click the red dot that shows up, and select condition... from the pop-up menu. In old versions, you had to bring up the "breakpoints" window separately (but as I recall, the menu entry for it was somewhat difficult to find).

Xcode 4: why "Continue to current line" is grayed out?

When I am debugging my program, I often want to make it run from where it is paused to a specific line.
In Xcode 3, I was able to do that by clicking on the button appearing next to the line number when hovering it.
In Xcode 4, it seems to be possible to do that either by right clicking on the line number and select Continue to here, either by using the Debug menu and choose Continue To Current Line item.
However these options are always grayed out when I want to use them.
Note, I don't know if this is relevant but I am using c++
Is there something I am doing wrong?
Thanks in advance,
Kevin
It seems that this is because you are using the LLDB debugger. As the same thing happens to me, whilst using the LLDB debugger and debugging C++ code, as you can see here:
I tested this with Objective-C code, and this feature works perfectly fine whilst debugging Objective-C.
The green button you mentioned, for Xcode 3, also pops-up with Objective-C code (using Xcode 4). As you can see here:
Perhaps it's a bug in Xcode, or Apple just wants you to program in Objective-C. It seems to be only an issue whilst using LLDB, and debugging C++ code (I am unsure of C code). On the other hand, debugging with GDB works fine with both: C++ and Obj-C code.
The only real options you have at the current time is:
Option 1:
Use the GDB debugger instead, as the GDB debugger with Xcode supports this.
To change the debugger:
Step 1:
Click your project name under the Scheme Menu, in the top left.
Step 2:
Click "Edit Scheme..."
Step 3:
Once the Edit Scheme menu comes up, click the pop-up menu next to the "Debugger" option.
Step 4:
Click the option "GDB" to use the GDB debugger.
NOTE:
Once you click GDB, the "Debug Process As" option will be greyed out.
Step 5:
Click OK.
NOTE:
I'm not sure about the limitations whilst using the GDB debugger, over LLDB (I think Xcode does not support any recent versions of GDB, as they have switched to LLDB and clang).
Option 2:
Create a breakpoint to where you wish to continue, and then continue to it. It really isn't that hard compared to right clicking and pressing "Continue Here" or the green button. This also provides the same functionality. With the use of keyboard shortcuts it could be just as fast (see below).
For example:
Say you want to continue until you hit line 39.
Set a breakpoint at line 39.
Click "Continue program execution" button, which is the button that likes some-what like a media-play button. Alternatively you can continue using the keyboard shortcut: Command + Ctrl + Y, or use the Menu (Product->Debug->Continue)
NOTE:
You need the Debug area shown (Shift-Command-Y), in order to see the "Continue program execution" button.

How to skip assembly code when debugging?

Sometimes when I use the debugger to step through my code, it goes into some assembly code (I guess I've stepped into some system library code).
The question is, how can I skip over it and jump to the nearest c++ code of my project?
Use the "Step-out" button or Shift+F11, this will step back up the call stack.
Alternatively display the call stack (Alt+7), then double click on the function level you want to return to; this will indicate in the source window where the call was made. Then in the source window right-click the statement following the call, and select "Run to cursor". Of course if you already know where the call came from, you could just use "Run to cursor" in any case.
You can close that assembly window by clicking X on right side in the code window. Not the one at top most right(which closes the solution)
Jump Out of the current function? (Shift-F11 in the C++ settings?)