从硬件异常处理程序引发C++异常.为什么 -fnon-call-exception 没有按预期运行
Throwing C++ exceptions from a hardware exception handler. Why does -fnon-call-exceptions not behave as expected?
我昨晚有一个有趣的想法,捕获硬件异常并抛出一个C++ exception
。认为这可能对像 FPU exceptions
这样的事情有用,它们通常要么崩溃,要么静默返回NaN
然后导致意外行为。在这里,C++ exception
会更可取。
所以我整个早上都在黑客攻击,终于让它工作了。嗯,差不多。编译器仍然没有意识到算术运算现在可以抛出C++ exceptions
,并且会默默地丢弃它周围的try/catch
块。当函数中发生异常时,它确实有效。
void throw_exception()
{
throw std::runtime_error("Division by zero!");
}
__attribute__((noinline))
void try_div0()
{
cout << 1 / 0 << endl;
}
int main()
{
// this class traps a hardware exception (division by zero, in this case) and calls the supplied lambda function.
// uh, no, you probably don't want to see the assembly code behind this...
exception_wrapper div0_exc { 0, [] (exception_frame* frame, bool)
{
if (frame->address.segment != get_cs()) return false; // only handle exceptions that occured in our own code
frame->stack.offset -= 4; // sub <fault esp>, 4;
auto* stack = reinterpret_cast<std::uintptr_t *>(frame->stack.offset); // get <fault esp>
*stack = frame->address.offset; // mov [<fault esp>], <fault address>;
frame->address.offset = reinterpret_cast<std::uintptr_t>(throw_exception); // set return address to throw_exception()
return true; // exception handled!
} };
try
{
// cout << 1 / 0 << endl; // this throws, as expected, but calls std::terminate().
try_div0(); // this exception is caught.
}
catch (std::exception& e)
{
cout << "oops: " << e.what() << endl;
}
}
我意识到这是一个不寻常的问题...但是有什么办法可以做到这一点吗?有某种方法告诉gcc
异常可能发生在任何地方?
我正在使用(我相信)使用异常处理DWARF
djgpp
进行编译。
编辑:我刚刚找到了gcc
标志-fnon-call-exceptions
和-fasynchronous-unwind-tables
,这似乎是我正在寻找的。但它仍然不起作用...
编辑:现在使用前面提到的gcc
标志,当两个函数调用之间发生异常时,它确实会捕获:
inline void nop() { asm(""); }
// or { cout << flush; } or something. empty function does not work.
int main()
{
/* ... */
try
{
nop();
cout << 1 / 0 << endl;
nop();
}
/* ... */
}
edit:嵌套try/catch
块具有相同的效果,除非捕获的指令前面有函数调用,否则不会捕获异常。
inline void nop() { asm(""); }
void try_div(int i)
{
try
{
// this works, catches exception in try_div(0).
nop();
cout << 1 / i << endl;
try_div(i - 1);
// without the first nop(), calls std::terminate()
//cout << 1 / i << endl;
//try_div(i - 1);
// reverse order, also terminates.
//if (i != 0) try_div(i - 1);
//cout << 1 / i << endl;
//nop();
}
catch (std::exception& e)
{
cout << "caught in try_div(" << i << "): " << e.what() << endl;
}
}
int main()
{
/* ... */
try
{
try_div(4);
}
catch (std::exception& e)
{
cout << "caught in main(): " << e.what() << endl;
}
}
编辑:我已将其作为 gcc 中可能的错误提交,并将我的代码简化为一个简单的测试用例。
已经有一段时间了,但我终于想通了......投掷功能需要标记为具有信号帧。
[[gnu::no_caller_saved_registers]]
void throw_exception()
{
asm(".cfi_signal_frame");
throw std::runtime_error("Division by zero!");
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 调用专用模板时出错"no matching function for call to [...]"
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 运行 shell 脚本时出错:"pipe call failed"
- 从硬件异常处理程序引发C++异常.为什么 -fnon-call-exception 没有按预期运行
- Linux3/GCC46:"-fnon-call-exceptions",哪些信号是陷阱指令?