Xcode 相当于 ' __asm int 3 / DebugBreak() / Halt?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode equivalent of ' __asm int 3 / DebugBreak() / Halt?
提问by Andrew Grant
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").
在 Xcode 中导致硬中断的指令是什么?例如,在 Visual Studio 下,我可以执行“_asm int 3”或“DebugBreak()”。在某些 GCC 实现中,它是 asm("break 0") 或 asm("trap")。
I've tried various combos under Xcode without any luck. (inline assembler works fine so it's not a syntax issue).
我在 Xcode 下尝试了各种组合,但没有任何运气。(内联汇编器工作正常,所以它不是语法问题)。
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.
作为参考,这是一个断言宏。我不想将 assert.h 中的定义用于可移植性,因为它们似乎在 XCode 提供的版本中执行 abort() 。
John - Super, cheers. For reference the int 3 syntax is the one required for Intel Macs and iPhone.
约翰 - 超级,干杯。作为参考,int 3 语法是 Intel Mac 和 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.
克里斯 - 感谢您的评论,但有很多原因可以避免将代码库移植到不同平台的标准 assert() 函数。如果您遇到了滚动自己的断言的麻烦,那通常是因为您希望保留其他功能(日志记录、堆栈展开、用户交互)。
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.
您尝试通过“__assert”或类似的实现来替换处理程序的建议不会是可移植的。标准的“assert”通常是一个宏,虽然它可能在 Mac 上映射到 __assert,但在其他平台上却没有.
回答by John Millikin
asm {trap} ; Halts a program running on PPC32 or PPC64.
__asm {int 3} ; Halts a program running on IA-32.
回答by Chris Hanson
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.
您可以插入一个调用Debugger()
- 这将在调试器中停止您的应用程序(如果它正在调试器下运行),或者如果不是,则以异常方式停止它。
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 handlerthat does a debugger break instead of calling abort()
; virtually all C compilers offer a mechanism by which you can do this.
另外,不要assert()
因为“便携性原因”而避免——便携性就是它存在的原因!它是标准 C 的一部分,您可以在任何找到 C 编译器的地方找到它。您真正想要做的是定义一个新的断言处理程序,它执行调试器中断而不是调用abort()
; 几乎所有 C 编译器都提供了一种机制,您可以通过它来执行此操作。
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.
当断言表达式失败时调用它。通常它,而不是assert()
它本身,是执行“printf()
后跟abort()
”的,这是默认的记录行为。通过自定义此函数或宏,您可以更改其行为。
回答by Tod
__builtin_trap();
Since Debugger() is depreciated now this should work instead.
由于 Debugger() 现在已折旧,因此应该可以使用。
回答by Steven Kramer
For posterity: I have some code for generating halts at the correct stack frame in the debugger and (optionally) pausing the app so you can attach the debugger just-in-time. Works for simulator and device (and possibly desktop, if you should ever need it). Exhaustively detailed post at http://iphone.m20.nl/wp/2010/10/xcode-iphone-debugger-halt-assertions/
对于后代:我有一些代码用于在调试器中的正确堆栈帧处生成暂停和(可选)暂停应用程序,以便您可以及时附加调试器。适用于模拟器和设备(可能还有桌面,如果您需要的话)。http://iphone.m20.nl/wp/2010/10/xcode-iphone-debugger-halt-assertions/ 上详尽的帖子
回答by Sanjit Saluja
kill(getpid(), SIGINT);
Works in the simulator and the device.
在模拟器和设备中工作。
回答by GateKiller
I found the following in an Apple Forum:
我在Apple 论坛中发现了以下内容:
Xcode doesn't come with any symbolic breaks built in - but they're quick to add. Go to the breakpoints window and add:
-[NSException raise]
Xcode 没有内置任何符号中断 - 但它们可以快速添加。转到断点窗口并添加:
-[NSException 引发]
回答by GateKiller
There is also the following function that is available as cross platform straight Halt() alternative:
还有以下功能可用作跨平台直接 Halt() 替代方案:
#include <stdlib.h>
void abort(void);
We use it in our cross platform engine for the iPhone implementation in case of fatal asserts. Cross platform across Nintendo DS/Wii/XBOX 360/iOS etc...
我们在 iPhone 实现的跨平台引擎中使用它,以防出现致命断言。跨平台 Nintendo DS/Wii/XBOX 360/iOS 等...