C:如何实现动态函数复制?
我有一个静态函数,用于接收 C 风格的回调。使用多线程方法进行一些实验,想要尝试一种在不同地址的函数中接收回调的方法,但不想在代码中声明所有这些函数,而是将函数(及其入口点)复制到内存中的另一个地址,然后注册该地址带有回调。还需要确定函数体中函数入口点的地址。
是否有可能使用 C,特别是 Linux 上的 gcc 来实现这一点?
注意:在我的例子中,回调是通过一个参数发生的,该参数对于它的源来说是唯一的 - 所以我在区分回调起源方面没有问题。但是,我觉得需要描述多线程+分叉环境的方法 - 即使在那里,回调将通过句柄或进程/分叉/线程 ID 进行识别
编辑:分叉不起作用:在以下代码中,realme() 和 testme() 共享地址;我想到了 -finstrument-functions 、 backtrace() 和其他一些实现 memcpy+pointer 方式的机会(请参阅下面的评论)。
#include <iostream>
#include <string>
// Required by for routine
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h> // Declaration for exit()
using namespace std;
int globalVariable = 2;
void realme()
{
cout << "Testme() is at: " << __builtin_return_address(1) << std::endl;
}
void testme()
{
realme();
}
main()
{
string sIdentifier;
int iStackVariable = 20;
pid_t pID = fork();
if (pID == 0) // child
{
// Code only executed by child process
sIdentifier = "Child Process: ";
globalVariable++;
iStackVariable++;
}
else if (pID < 0) // failed to fork
{
cerr << "Failed to fork" << endl;
exit(1);
// Throw exception
}
else // parent
{
// Code only executed by parent process
sIdentifier = "Parent Process:";
}
// Code executed by both parent and child.
cout << sIdentifier;
cout << " Global variable: " << globalVariable;
cout << " Stack variable: " << iStackVariable << endl;
testme();
}
I have a static function which is meant for receiving C-style callbacks. Doing some experiments with multithreading approaches, want to try a way of receiving callback into functions at different addresses, but do not want to declare all of them in code, rather copying a function (with its entrypoint) to another address in memory, and registering that adrress with a callback. Also it is required to determine address of function entrypoint at the function body..
Any possiblity to reach this with C and particularly with gcc on Linux?
Note: in my case, callback happens with an argument, which is unique for it's source - so I have no problems with distinguishing callback origins.. however, I feel the need of described approached for multithreaded+forked environment - even there, the callback will be recognized either by handle or process/fork/thread ID
Edit: forking doesn't works: in the following code, realme() and testme() are sharing addresses; I think about -finstrument-functions
, backtrace()
and some other chances to implement memcpy+pointer way (see comments somewhere below)..
#include <iostream>
#include <string>
// Required by for routine
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h> // Declaration for exit()
using namespace std;
int globalVariable = 2;
void realme()
{
cout << "Testme() is at: " << __builtin_return_address(1) << std::endl;
}
void testme()
{
realme();
}
main()
{
string sIdentifier;
int iStackVariable = 20;
pid_t pID = fork();
if (pID == 0) // child
{
// Code only executed by child process
sIdentifier = "Child Process: ";
globalVariable++;
iStackVariable++;
}
else if (pID < 0) // failed to fork
{
cerr << "Failed to fork" << endl;
exit(1);
// Throw exception
}
else // parent
{
// Code only executed by parent process
sIdentifier = "Parent Process:";
}
// Code executed by both parent and child.
cout << sIdentifier;
cout << " Global variable: " << globalVariable;
cout << " Stack variable: " << iStackVariable << endl;
testme();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为你可以在 C 中可靠地做到这一点。一旦执行,就不能保证函数内部的代码独立于它自己的位置(考虑本地分支)。 C 语言中无法获取函数的大小,因此您不知道要复制多少。
I don't think you can do this reliably, in C. There's no guarantee that the code residing inside a function is independent of its own position (think about local branches), once it's executing. There's no way to get the size of a function in C, so you wouldn't know how much to copy.
这将要求您编写自修改代码,除了它并不那么简单之外,它还会给您带来巨大的性能损失。模拟所需内容而又不会太麻烦的唯一方法是创建某种宏,每次需要传递回调函数指针时都会编写一个新函数,但必须在编译时知道它。也许宏可以在每次引用它时根据某些参数生成一个函数到 .c 文件。
PS 不是谈论
#define
而是一个将在预编译时调用的应用程序,搜索关键字并输入 .c 文件This would require you to make self modifying code, apart from the fact that it is not that simple it also gives you a huge performance penalty. The only way to simulate the needed stuff without too much trouble is to make some sort of macro that will write a new function each time you need to pass a callback function pointer, but it has to be know at compile time. Maybe the macro can generate a function, based on certain parameters, to a .c file each time you referenced it.
P.S not talking about
#define
but rather an app that will be called at pre-compile time, search for a keyword and make an entry to a .c file