tags:

views:

787

answers:

3

Hi,

How to add a conditional breakpoint in Xcode? I try setting a breakpoint and then go to 'Edit breakpoint'

I want to break when bytes is 0.

So I add 'bytes==0' in the condition, but it never breaks.

And then I try '(bytes == 0)' as my condition, the gdb crashes:

bool SharedMemory::Map(size_t bytes) { if (mapped_file_ == -1) return false; cout
gdb stack crawl at point of internal error:
[ 0 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (align_down+0x0) [0x122300]
[ 1 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (wrap_here+0x0) [0x1225f8]
[ 2 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (prepare_threads_before_run+0x270) [0x185320]
[ 3 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (macosx_child_resume+0x263) [0x17e85d]
[ 4 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (resume+0x323) [0x6973d]
[ 5 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (keep_going+0x122) [0x69a01]
[ 6 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (handle_inferior_event+0x3338) [0x6cd4a]
[ 7 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (fetch_inferior_event+0x125) [0x6cfa8]
[ 8 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (inferior_event_handler+0xd0) [0x8216c]
[ 9 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (handle_file_event+0x159) [0x80055]
[ 10 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (process_event+0x81) [0x7fc22]
[ 11 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (gdb_do_one_event+0x46a) [0x80ae5]
LEAK: 918 RenderObject
LEAK: 1 Page
LEAK: 3 Frame
LEAK: 7 SubresourceLoader
LEAK: 95 CachedResource
LEAK: 2000 WebCoreNode
[ 12 ] /Developer/usr/libexec/gdb/gdb-i386-apple-darwin (catch_errors+0x4d) [0x7abe2]
/SourceCache/gdb/gdb-966/src/gdb/macosx/macosx-nat-infthread.c:321: internal-error: assertion failure in function "prepare_threads_before_run": tp != NULL

A problem internal to GDB has been detected,
further debugging may prove unreliable.

The Debugger has exited with status 1.The Debugger has exited with status 1.

+2  A: 

GDB using conditional breakpoints from xcode is a little bit finicky. I often find myself eschewing the conditional breakpoints feature and just adding code like:

if (bytes == 0) {
    NSLog(@"here");
}

I'd put a normal breakpoint at the NSLog statement.

Matt Bridges
Yep, that's exactly what I do.
Neil Daniels
A: 

So I add 'bytes==0' in the condition, but it never breaks.

Well, maybe that's because bytes is never 0?

Have you tried setting bytes to 0 right before your breakpoint so that your sure that bytes is 0?

I tried

int bytes = 0;
// Breakpoint here

and the breakpoint stopped when I had the condition bytes==0, but not when I had the condition bytes==1.

So that's why I have to assume that in your case bytes is never 0.

Erik B
A: 

I suggest NSAssert macro. This is widely used, and standard way for conditional break. As an example, this code aborts code execution of byte is zero.

NSAssert(bytes!=0,@"here");

Take care about preceding condition is passing condition. (not a fail condition)

If you're using default Xcode project configuration, the assertion code compiled when only in Debug build. They will be cleared in Release build. For more information about conditional compilation, dig about Xcode project setting entry Other C flags and NS_BLOCK_ASSERTIONS. (-DNS_BLOCK_ASSERTIONS) -D is compiler flag

Eonil