如何避免在调用 .so 中抛出异常的函数时崩溃

发布于 2024-11-16 09:15:38 字数 1368 浏览 4 评论 0原文

这就是我所做的,我想优雅地处理这个异常:

code_snippet:my.cpp

#include<iostream>
extern "C" void some_func()
{
    throw "(Exception Thrown by some_func!!)";
}

code_snippet:Exception.c

#include <stdio.h>
extern void some_func();
int so_main()
{
    some_func();
    return 0;
}

从上面的两个片段中,我使用以下命令创建了一个shared_object libexception.so:

g++ -c -fPIC src/my.cpp
gcc -c -ansi -fPIC src/exception.c
g++ -fPIC -shared -o libexception.so

然后我从我的main调用了函数so_main .cpp code_snippet: main.cpp

#include<iostream>
#include <dlfcn.h>
using namespace std;
extern "C" void some_func();
int main()
{
    int (*fptr)() = 0;
    void *lib = dlopen("./libexception.so", RTLD_LAZY);
    if (lib) {
        *(void **)(&fptr) = dlsym(lib, "so_main");
        try{
           if(fptr) (*fptr)();    <-- problem lies here
           //some_func();        <-- calling this directly won't crash
        }
        catch (char const* exception) {
            cout<<"Caught exception :"<<exception<<endl;
        }
    }
return 0;

}

最终命令: g++ -g -ldl -o main.exe src/main.cpp -L lib/ -lexception

执行 main.exe 崩溃。有人可以帮我检测问题出在哪里以及如何避免发生这种崩溃(我们已经有一些架构,其中一些 .SO 调用 extern c++ 函数,因此无法更改,我们只想在 main.cpp 中处理它)

Here is what I did, I want to gracefully handle this exception:

code_snippet: my.cpp

#include<iostream>
extern "C" void some_func()
{
    throw "(Exception Thrown by some_func!!)";
}

code_snippet: exception.c

#include <stdio.h>
extern void some_func();
int so_main()
{
    some_func();
    return 0;
}

From above two snippets I have created a shared_object libexception.so by using following commands:

g++ -c -fPIC src/my.cpp
gcc -c -ansi -fPIC src/exception.c
g++ -fPIC -shared -o libexception.so

Then I called the function so_main from my main.cpp
code_snippet: main.cpp

#include<iostream>
#include <dlfcn.h>
using namespace std;
extern "C" void some_func();
int main()
{
    int (*fptr)() = 0;
    void *lib = dlopen("./libexception.so", RTLD_LAZY);
    if (lib) {
        *(void **)(&fptr) = dlsym(lib, "so_main");
        try{
           if(fptr) (*fptr)();    <-- problem lies here
           //some_func();        <-- calling this directly won't crash
        }
        catch (char const* exception) {
            cout<<"Caught exception :"<<exception<<endl;
        }
    }
return 0;

}

final command: g++ -g -ldl -o main.exe src/main.cpp -L lib/ -lexception

Executing main.exe crashes. Can somebody help me detect where the problem lies and how to avoid this crash to happen (We have already some architecture where some .SO calls extern c++ function, so that can't be changed, We want to handle it in main.cpp only)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咆哮 2024-11-23 09:15:38

尝试使用 -fexception 编译 src/exception.c

-f异常
启用异常处理。生成传播异常所需的额外代码。对于某些目标,这意味着 GNU CC 将为所有函数生成帧展开信息...

但是,在编译需要与用 C++ 编写的异常处理程序正确互操作的 C 代码时,您可能需要启用此选项。如果您正在编译不使用异常处理的旧版 C++ 程序,您可能还希望禁用此选项。

Try compiling src/exception.c with -fexceptions

-fexceptions
Enable exception handling. Generates extra code needed to propagate exceptions. For some targets, this implies GNU CC will generate frame unwind information for all functions ...

However, you may need to enable this option when compiling C code that needs to interoperate properly with exception handlers written in C++. You may also wish to disable this option if you are compiling older C++ programs that don't use exception handling.

时光沙漏 2024-11-23 09:15:38

该函数可能会抛出一个不是 char* 的异常,因此您无法捕获它。

尝试使用通用捕获:

   *(void **)(&fptr) = dlsym(lib, "so_main");
    try{
       if(fptr) (*fptr)();    <-- problem lies here
       //some_func();        <-- calling this directly won't crash
    }
    catch (char const* exception) {
        cout<<"Caught exception :"<<exception<<endl;
    }
    catch (...) {
        cout<<"Caught exception : Some other exception"
    }

That function probably throws an exception that is not char* so you're not catching it.

try using a generalized catch:

   *(void **)(&fptr) = dlsym(lib, "so_main");
    try{
       if(fptr) (*fptr)();    <-- problem lies here
       //some_func();        <-- calling this directly won't crash
    }
    catch (char const* exception) {
        cout<<"Caught exception :"<<exception<<endl;
    }
    catch (...) {
        cout<<"Caught exception : Some other exception"
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文