Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode equivalent of ' __asm int 3 / DebugBreak() / Halt?

Tags:

What's the instruction to cause a hard-break in Xcode? For example under Visual Studio I could do '_asm int 3' or 'DebugBreak()'. Under some GCC implementations it's asm("break 0") or asm("trap").

I've tried various combos under Xcode without any luck. (inline assembler works fine so it's not a syntax issue).

For reference this is for an assert macro. I don't want to use the definitions in assert.h both for portability, and because they appear to do an abort() in the version XCode provides.


John - Super, cheers. For reference the int 3 syntax is the one required for Intel Macs and iPhone.


Chris - Thanks for your comment but there are many reasons to avoid the standard assert() function for codebases ported to different platforms. If you've gone to the trouble of rolling your own assert it's usually because you have additional functionality (logging, stack unwinding, user-interaction) that you wish to retain.

Your suggestion of attempting to replace the hander via an implementation of '__assert" or similar is not going to be portable. The standard 'assert' is usually a macro and while it may map to __assert on the Mac it doesn't on other platforms.

like image 381
Andrew Grant Avatar asked Sep 01 '08 00:09

Andrew Grant


2 Answers

http://developer.apple.com/documentation/DeveloperTools/Conceptual/XcodeProjectManagement/090_Running_Programs/chapter_11_section_3.html

asm {trap}            ; Halts a program running on PPC32 or PPC64.  __asm {int 3}         ; Halts a program running on IA-32. 
like image 61
John Millikin Avatar answered Oct 02 '22 04:10

John Millikin


You can just insert a call to Debugger() — that will stop your app in the debugger (if it's being run under the debugger), or halt it with an exception if it's not.

Also, do not avoid assert() for "portability reasons" — portability is why it exists! It's part of Standard C, and you'll find it wherever you find a C compiler. What you really want to do is define a new assertion handler that does a debugger break instead of calling abort(); virtually all C compilers offer a mechanism by which you can do this.

Typically this is done by simply implementing a function or macro that follows this prototype:

void __assert(const char *expression, const char *file, int line); 

It's called when an assertion expression fails. Usually it, not assert() itself, is what performs "the printf() followed by abort()" that is the default documented behavior. By customizing this function or macro, you can change its behavior.

like image 21
Chris Hanson Avatar answered Oct 02 '22 05:10

Chris Hanson