来自共享对象的Linux异常(.so) [英] Exceptions on Linux from a shared object (.so)

查看:199
本文介绍了来自共享对象的Linux异常(.so)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为ftest的测试程序。它加载包含测试的.so文件,并运行它在其中找到的测试。其中一个测试加载并运行一个.so文件,其中包含一个用于O / RM的Postgres数据库驱动程序。



当Postgres驱动程序抛出一个在其中定义的异常时。所以这个文件(或者它链接到的一个,但是ftest没有链接到)并且被测试框架捕获到,异常析构函数会触发一个段错误。



编译的异常处于已被动态加载(使用dload)的.so中。



这种方式在Windows中具有相同的体系结构时可以正常工作。我们并不想限制自己只使用来自核心库的异常 - 插件应该可以自由创建它们自己的异常类并正常处理。



异常是std :: exception的子类。有时例外可能会在库中定义(例如libpqxx),这意味着异常有时也不受我们的控制。



使用类似的方法抛出异常:

  throw exception_class(exception_arguments); 

并且正在使用:

  catch(std :: exception& e){
//处理程序代码
}

是否需要一些特殊的编译器选项才能使其工作?我们是否需要通过抛出异常抛出异常(args)(我们不是真的想这样做)?



添加-Wl,-E解决方案



VC ++使用字符串比较匹配typeinfo,结果较慢的dynamic_cast等,但较小的二进制文件。 g ++使用指针比较。

在尝试使用运行时加载的.so中实现的纯虚拟接口类时,我遇到了同样的问题。



还有一些关于这个问题的文章也浮在网上。

希望有帮助,
Hayman。

I have a test program called ftest. It loads .so files that contain tests and runs the tests that it finds in there. One of these tests loads and runs a .so that contains a Postgres database driver for our O/RM.

When the Postgres driver throws an exception which is defined in that .so file (or one that it links to, but ftest does not link to) and is caught by the test framework the exception destructor triggers a segfault.

This segfault happens whenever the compiled exception is in a .so that has been dynamically loaded (using dload).

This sort of thing works fine in Windows which has the same architecture. We don't really want to restrict ourselves to only use exceptions that are from the core libraries -- add-ins should be free to create their own exception classes and have them handled normally.

The exceptions are sub-classes of std::exception. Sometimes the exceptions may be defined in libraries (such as libpqxx) which means that exceptions are sometimes out of our control too.

Exceptions are thrown using something like:

throw exception_class( exception_arguments );

And are caught using:

catch ( std::exception &e ) {
    // handler code
}

Is there some special compiler option needed to get this working? Do we need to switch to throw exceptions via throw new exception_class( args ) (we don't really want to do this)?

解决方案

Assuming your using gcc -

Append -Wl,-E when you build the executable calling dlload(). This exports all type info symbols from the executable, which should allow the RTTI (when catching the exception) to work properly.

VC++ uses string compares to match typeinfo, results in slower dynamic_cast<> etc but smaller binaries. g++ uses pointer compares.

I encountered the same problem when attempting to use pure virtual interfaces classes implemented in a run-time loaded .so.

There are a few articles relating to the subject floating around on the net as well.

hope that helps, Hayman.

这篇关于来自共享对象的Linux异常(.so)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆