C 语言中可以进行热代码交换吗?

发布于 2024-09-28 04:45:34 字数 657 浏览 8 评论 0原文

一点

en.wikipedia.org/wiki/Hot_swapping#cite_note-1

说 VS 可以在其调试器的帮助下做到这 。 gdb 是否提供类似的功能?

这是我能找到的最接近的,但似乎还没有准备好使用:

http ://www.aitdspace.gr/xmlui/handle/123456789/219

dlopen/dlsym/dlclose 也很接近,但不适用于 -lmylib 引用的库(引用计数永远不会达到 0)。

我考虑过的替代方案:

1)使用 -Wl,-wrap,foo 和 __wrap_foo() { func = dlopen();函数(); 2

) 使 libfoo.so 成为共享库,当我们需要热交换时,我们 dlopen(RTLD_GLOBAL) 加载新代码并为下一次调用 foo() 提供更新的符号;

1)效果不太好,因为它需要我枚举所有我想要热插拔的函数,这就是全部。

2) 效果不太好,因为当调用 foo() 时,会加载新代码,但 foo 永远拥有对该符号的引用。多次调用 dlopen 会使 foo 被重新评估。

this

en.wikipedia.org/wiki/Hot_swapping#cite_note-1

says that VS can do it with the help of its debugger. Does gdb provide a similar functionality ?

this is the closest i could find, but doesn't seem to be ready to be used:

http://www.aitdspace.gr/xmlui/handle/123456789/219

dlopen/dlsym/dlclose are also close, but will not work for -lmylib referenced libraries (reference count never gets to 0).

alternatives i've considered:

1) using -Wl,-wrap,foo and on __wrap_foo() { func = dlopen(); func(); }

2) making libfoo.so a shared library and when we need to hotswap we dlopen(RTLD_GLOBAL) to load the new code and provide updated symbols to the next call to foo();

1) doesn't work very well because it requires me to enumerate all the functions i want to hotswap, which are all of them.

2) doesn't work very well because when foo() is called, the new code is loaded, but foo has forever the reference to that symbol. calling dlopen multiple times make foo to be re evaluated.

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

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

发布评论

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

评论(3

隐诗 2024-10-05 04:45:34

您可能对 Ksplice 感兴趣。这是一项来自 MIT 的技术,允许在不重新启动的情况下将软件补丁应用到 Linux 内核。这与应用安全更新最相关:

http://www.ksplice.com/paper

You may be interested in Ksplice. It's a technology that came out of MIT that allows software patches to be applied to the Linux kernel without rebooting. This is most relevant for applying security updates:

http://www.ksplice.com/paper

我是男神闪亮亮 2024-10-05 04:45:34

您当然可以自行破解一个系统,在其中存储函数指针列表,并可以更改这些指针以指向您当时使用 dlopen() 的任何库。

你是对的,没有任何简单的方法可以拦截对具有固定链接的例程的调用。您始终可以通过程序集跳转到另一个例程来破坏例程的开头,但这可能很危险(而且不是 C)。

也许在您的代码中较弱但在 dlopen() 库中较强的符号会起作用?

在任何这些情况下,您都必须处理旧代码当前正在运行的情况。这也不容易,除非您的程序中有一些点您知道您想要交换的库中没有线程。

You could certainly hack yourself a system where you store a list of function pointers and can change these pointers to point to whatever library you have dlopen()'d at the time.

You're right, there isn't any easy way to intercept calls to routines with fixed linkage. You can always clobber the start of the routine with an assembly jump to another routine, but that can be dangerous (and isn't C).

Maybe a symbol which is weak in your code and strong in a dlopen()'d library would work?

In any of these cases, you have to deal with the situation where the old code is currently running. That isn't easy either, unless you have points in your program where you know no thread is in the library you want to swap.

不甘平庸 2024-10-05 04:45:34

我发现的最接近的是Oracle Developer Studio附带的solari dbx,但是dev studio在linux和solaris中都使用dbx,只有solaris版本支持“编辑并继续”或“热代码交换”

the closest i have found is solari dbx which comes with oracle developer studio,however dev studio uses dbx in both linux and solaris,only solaris version supports "edit-and-continue" or "hot code swap"

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