简体   繁体   English

__builtin_trap:何时使用它?

[英]__builtin_trap: when to use it?

gcc provides additional builtin functions "for optimization". gcc提供了“用于优化”的其他内置函数。

One of them is void __builtin_trap (void) which essentially is here to abort the program by executing an illegal command. 其中一个是void __builtin_trap (void) ,它实际上是通过执行非法命令来中止程序。

From the doc: 来自doc:

__builtin_trap function causes the program to exit abnormally. __builtin_trap函数导致程序异常退出。 GCC implements this function by using a target-dependent mechanism (such as intentionally executing an illegal instruction) or by calling abort. GCC通过使用依赖于目标的机制(例如故意执行非法指令)或通过调用abort来实现此功能。 The mechanism used may vary from release to release so you should not rely on any particular implementation. 使用的机制可能因版本而异,因此您不应依赖任何特定实现。

Why would you ever use this rather than exit(1) or abort ? 你为什么要使用它而不是exit(1)abort Why did the gcc developers see this as an optimization function? 为什么gcc开发人员将此视为优化功能?

Because exit(1) causes the program to terminate normally with an error status code. 因为exit(1)导致程序正常终止并带有错误状态代码。 See the cppreference page . 请参阅cppreference页面 In contrast __builtin_trap causes the program to terminate abnormally. 相反, __builtin_trap导致程序异常终止。

The simplest way to see the differences is to look at the guarantees made by exit , if doing one of those things is something you don't want to happen, __builtin_trap would be better. 查看差异的最简单方法是查看exit所做的保证,如果其中一件事情是你不想发生的事情,那么__builtin_trap会更好。

Debugging is the most common example, as __builtin_trap could trigger a debugger to dump the process, while exit would not (since the program terminates "normally" with an error). 调试是最常见的示例,因为__builtin_trap可以触发调试器来转储进程,而exit不会(因为程序“正常”终止并出现错误)。

The __builtin functions are not necessarily for optimisation - they are for "doing things that the compiler can't do directly from source code", including support for "special instructions" and "architecture-specific operations". __builtin函数不一定用于优化 - 它们用于“执行编译器不能直接从源代码执行的操作”,包括对“特殊指令”和“特定于体系结构的操作”的支持。 One of the main purposes of __builtin functions is that the compiler will "know" what these do at a later stage. __builtin函数的主要目的之一是编译器将“知道”它们在稍后阶段的作用。 Although there are "library optimisations" in the compiler, the compiler can use __builtin functions much more freely to determine that the behaviour is something specific - for example, __builtin_trap can be relied upon to "not continue to the next instruction", so the compiler doesn't have to worry about code like: 虽然编译器中存在“库优化”,但编译器可以更自由地使用__builtin函数来确定行为是特定的 - 例如, __builtin_trap可以依赖于“不继续下一条指令”,因此编译器不必担心像这样的代码:

if (x <= 0.0) __builtin_trap();
y = ln(x);

It can then make use of the "fast inline version of ln ", since the error has been caught already. 然后它可以使用“快速内联版本的ln ”,因为错误已经被捕获。

Note also that __builtin_trap is almost guaranteed to end up as a "stop" in the debugger, where exit(1) or some such will just exit the program with a "not success" result code, which is rather annoying if you are trying to figure out where that math error message came from... 另请注意, __builtin_trap几乎可以保证在调试器中作为“停止”结束,其中exit(1)或其他一些只会以“not success”结果代码退出程序,如果你想尝试找出那个数学错误信息来自哪里......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Qt-使用内置翻译 - Qt - Use builtin translations 如何在不使用标准C库的情况下使用编译器内置函数 - How to use compiler builtin functions without Standard C library 在 macOS Mojave 上尝试 OpenCV 视频捕获时出现“Abort Trap: 6” - "Abort Trap: 6" when attempting OpenCV video capture on macOS Mojave 从OS X中的main返回时中止陷阱6,但不在Linux上 - Abort trap 6 when returning from main in OS X but NOT on linux 在 Firefox 中执行 WASM 时,什么是“调试陷阱处理” - When executing WASM in Firefox, what is "debug trap handling" 当无法尝试__try __except时如何捕获C ++中的错误 - How to trap an error in C++ when __try __except can't 显式调用模板参数类型的析构函数,即使在内置实例化时也是如此 - Explicit call to destructor of template parameter type, even when instantiated on a builtin 我可以使用有符号整数作为__builtin_popcount()的参数吗? - Can I use a signed integer as an argument to __builtin_popcount()? 调用CGPathAddArc时PyQt4 / Qt中止陷阱的原因是什么? - What is cause of PyQt4/Qt abort trap when calling CGPathAddArc? 在自底向上合并排序中合并数组时中止陷阱 6 - Abort trap 6 when merging arrays in bottom-up Merge Sort
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM