为共享库或 DLL 编写信号处理程序?

发布于 2024-07-10 08:57:20 字数 705 浏览 5 评论 0原文

我有一个应用程序 A(由某公司 X 提供)。 该应用程序允许我通过编写自己的函数来扩展功能。

我告诉应用程序 A 在应用程序 A 的配置文件中调用我的用户函数(这就是它知道应用程序 A 必须调用用户编写的函数的方式)。 appl A 使用函数指针,在调用用户编写的函数之前,我必须向应用程序 A 注册该指针。

如果我的用户在生产中编写的函数存在错误或故障,Appl A 将停止运行。 例如,如果我的用户编写的函数中有分段错误。

因此应用程序 A 将从共享 DLL 文件加载我的用户编写的函数。 这意味着我的用户编写的函数将在应用程序 A' 进程地址空间中运行。

我希望处理某些信号,例如分段错误、除以零和堆栈溢出,但应用程序 A 有自己为此编写的信号处理程序,

我如何编写自己的信号处理程序来捕获用户编写的函数中的异常,以便我可以优雅地清理而不影响应用程序 A 的大部分内容吗? 由于我的用户函数将在应用程序 A 的进程中调用,因此操作系统将调用应用程序 A 中编写的信号处理程序,而不是我的用户函数。

我怎样才能改变这个? 我希望操作系统调用在我的函数中编写的信号处理程序,但仅限于我的函数引发的信号,这本质上是异步的。

注意:我没有应用程序 A 的源代码,也无法对其进行任何更改,因为它由另一家公司控制。

我将使用 C 语言,并且仅在 Linux、solaris 和可能的 Windows 平台上使用 C 语言。

I have a Application A(by some company X). This application allows me to extend the functionality by allowing me to write my own functions.

I tell the Application A to call my user functions in the Applications A's configuration file (this is how it knows that Appl A must call user written Functions). The appl A uses Function pointers which I must register with Application A prior to calling my user written functions.

If there is a bug or fault in my user written functions in production, the Appl A will stop functioning. For example, if I have a segmentation fault in my User written functions.

So Application A will load my user written function from a shared DLL file. This means that my user written functions will be running in Application A' Process address space.

I wish to handle certain signals like Segmentation fault, divide by zero and stack overflow, but applications A has its own signal handlers written for this,

How can I write my own signal handlers to catch the exceptions in my user written functions, so that I can clean up gracefully w/o affecting much of Application A? Since my user functions will be called in Applications A's process, the OS will call signal handlers written in Application A and not my user functions.

How can I change this? I want OS to call signal handlers written in my functions but only for signal raised by my functions, which is asynchronous in nature.

Note: I do not have the source code of Application A and I cannot make any changes to it, because it's controlled by a different company.

I will be using C , and only C on a Linux, solaris and probably windows platforms.

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

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

发布评论

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

评论(3

£噩梦荏苒 2024-07-17 08:57:20

您没有指定您正在使用哪个平台,所以我将回答 Linux,它也应该对 Windows 有效。

当您设置信号处理程序时,您使用的系统调用将返回前一个处理程序。 这样做是为了当您不再对处理该信号感兴趣时可以返回它。

信号的 Linux 手册页
信号上的 MSDN 条目

由于您是加载到应用程序中的共享库您操作信号处理程序应该没有问题。 只需确保覆盖所需的最小值,以减少中断应用程序本身的机会(某些应用程序使用信号进行异步通知)。

You do not specify which platform you're working with, so I'll answer for Linux, and it should be valid for Windows as well.

When you set your signal handlers, the system call that you use returns the previous handler. It does it so that you can return it once you are no longer interested in handling that signal.

Linux man page for signal
MSDN entry on signal

Since you are a shared library loaded into the application you should have no problems manipulating the signals handlers. Just make sure to override the minimum you need in order to reduce the chances of disrupting the application itself (some applications use signals for async notifications).

梦里°也失望 2024-07-17 08:57:20

最简洁的方法是在一个单独的进程中运行应用程序代码,该进程通过某种 IPC 机制与嵌入式共享 DLL 代码进行通信。 您可以在进程中处理您想要的任何信号,而不会影响其他进程。 通常,您提到的条件(段错误、除以零、堆栈溢出)表明程序中存在错误,并将导致终止。 除了修复错误的根本原因之外,您无能为力“处理”这些问题。

The cleanest way to do this would be run your application code in a separate process that communicates with the embedded shared DLL code via some IPC mechanism. You could handle whatever signals you wanted in your process without affecting the other process. Typically the conditions you mention (seg fault, divide by zero, stack overflow) indicate bugs in the program and will result in termination. There isn't much you can do to "handle" these beyond fixing the root cause of the bug.

秋风の叶未落 2024-07-17 08:57:20

在 C++ 中,您可以通过将代码放入 try-catch 来捕获这些内容:

try
{
   // here goes your code
}
catch ( ... )
{
   // handle segfaults
}

in C++, you can catch these by putting your code in a try-catch:

try
{
   // here goes your code
}
catch ( ... )
{
   // handle segfaults
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文