GDB Conditional Breakpoints
本文转载自GDB Conditional Breakpoints。
What if you have a large loop running and you only want to look at what’s happening as it nears the end? Do you really have to step through 99 iterations in a 100 item loop?
Of course you don’t. Step forward the conditional breakpoint.
Set a conditional breakpoint using a condition
In GDB you can specify a condition in the programming language you are debugging and apply it to any breakpoint. Let’s stop a loop at the 99th iteration (I’m debugging C/C++, so my conditions are written in C/C++):
1 | (gdb) b Message.cpp:112 if i == 99 |
That’s all there is to it.
To ensure gdb stops execution, use the first line of code inside the loop as the stopping point, not the loop itself.
You can also specify a condition on an existing breakpoint by using the breakpoint number as a reference:
1 | (gdb) cond 3 i == 99 |
And remove a condition from a breakpoint using:
1 | (gdb) cond 3 |
That’s great! What else can you do?
Pretty much anything you like! Just write the condition exactly as if you were testing for it in your code, e.g.:
1 | (gdb) cond 1 strcmp(message,"earthquake") == 0 |
Conditional breakpoints covered and your efficiency increased, all in less than 5 minutes!