Xcode 相当于 ' __asm int 3 / DebugBreak() / 停止?
导致 Xcode 硬中断的指令是什么? 例如,在 Visual Studio 下我可以执行“_asm int 3”或“DebugBreak()”。 在某些 GCC 实现下,它是 asm("break 0") 或 asm("trap")。
我在 Xcode 下尝试了各种组合,但没有成功。 (内联汇编器工作正常,所以这不是语法问题)。
作为参考,这是一个断言宏。 我不想使用assert.h 中的定义,这既是为了可移植性,也是因为它们似乎在XCode 提供的版本中执行了abort() 操作。
约翰-超级,干杯。 作为参考,int 3 语法是 Intel Mac 和 iPhone 所需的语法。
Chris - 感谢您的评论,但是有很多理由避免将代码库移植到不同平台的标准 assert() 函数。 如果您不厌其烦地滚动自己的断言,通常是因为您希望保留其他功能(日志记录、堆栈展开、用户交互)。
您尝试通过“__assert”或类似的实现来替换处理程序的建议是不可移植的。标准的“assert”通常是一个宏,虽然它可能映射到Mac上的__assert,但在其他平台上则不然。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
由于 Debugger() 现在已被弃用,因此它应该可以工作。
https:// /developer.apple.com/library/mac/technotes/tn2124/_index.html#//apple_ref/doc/uid/DTS10003391-CH1-SECCONTROLLEDCRASH
Since Debugger() is depreciated now this should work instead.
https://developer.apple.com/library/mac/technotes/tn2124/_index.html#//apple_ref/doc/uid/DTS10003391-CH1-SECCONTROLLEDCRASH
对于后代:我有一些代码用于在调试器中的正确堆栈帧处生成暂停,并(可选)暂停应用程序,以便您可以及时附加调试器。 适用于模拟器和设备(如果您需要的话,也可能是桌面设备)。 详尽的帖子位于 http://iphone.m20。 nl/wp/2010/10/xcode-iphone-debugger-halt-assertions/
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/
您只需插入对 Debugger() 的调用 - 这将在调试器中停止您的应用程序(如果它正在调试器下运行),或者如果不是,则以异常停止它。
另外,不要因为“可移植性原因”而避免
assert()
- 可移植性就是它存在的原因! 它是标准 C 的一部分,无论您在哪里找到 C 编译器,都可以找到它。 您真正想做的是定义一个新的断言处理程序,它会执行调试器中断而不是调用 abort() ; 几乎所有 C 编译器都提供了一种可以实现此目的的机制。通常,这是通过简单地实现遵循此原型的函数或宏来完成的:
当断言表达式失败时调用它。 通常,执行“
printf()
后跟abort()
”(默认记录的行为)的是它,而不是assert()
本身。 。 通过自定义此函数或宏,您可以更改其行为。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 callingabort()
; 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:
It's called when an assertion expression fails. Usually it, not
assert()
itself, is what performs "theprintf()
followed byabort()
" that is the default documented behavior. By customizing this function or macro, you can change its behavior.http://developer.apple.com/documentation/DeveloperTools/Conceptual /XcodeProjectManagement/090_Running_Programs/chapter_11_section_3.html
http://developer.apple.com/documentation/DeveloperTools/Conceptual/XcodeProjectManagement/090_Running_Programs/chapter_11_section_3.html
在模拟器和设备中工作。
Works in the simulator and the device.
还有以下函数可用作跨平台直接 Halt() 替代方案:
我们在 iPhone 实现的跨平台引擎中使用它,以防致命断言。 跨任天堂 DS/Wii/XBOX 360/iOS 等平台...
There is also the following function that is available as cross platform straight Halt() alternative:
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...
我在 Apple 论坛中发现了以下内容:
I found the following in an Apple Forum: