私はCスタイルのコールバックを受け取るための静的関数を持っています。マルチスレッドのアプローチでいくつかの実験を行い、異なるアドレスの関数にコールバックを受け取る方法を試したいが、コード内にすべての関数を宣言したいのではなく、(エントリポイントを持つ)関数をメモリ内の別のアドレスにコピーし、そのコールバックでadrress。また、関数本体での関数のエントリポイントのアドレスを決定する必要があります。C:動的関数のコピーを実装する方法は?
Linuxでgccを使用する場合、Cを使用する可能性はありますか?
注:私の場合、コールバックはソースでユニークな引数で発生するため、コールバックの起点を区別することに問題はありません。しかし、マルチスレッド+フォーク環境では、フォークはない作品を行います:さえあり、コールバックは、いずれかのハンドルまたはプロセス/フォーク/スレッドID
を編集することによって認識されるであろう次のコードで、realme()とtestme()アドレスを共有しています。私は、その保証はありません、私はあなたがCで、確実にこれを行うことができるとは思わない程度-finstrument-functions
、backtrace()
とmemcpyの+ポインタの方法を実施するための他のいくつかのチャンス(どこかに以下のコメントを参照してください)。..
#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();
}
はのは、私が ''はsizeof(のためのmalloc'を行う考えてみましょう関数本体全体をコピーし、 'int(* ptr)()x;のように呼び出す。 x(100) ' - アイデアはhttp://core.cprogramming.com/linux-programming/102703-passing-function-pointer-using-message-queues.htmlから取られます - この方法では、私はまだ値を変更できません関数のコピー - 別のコンパイラでは、ボディを別々に変更しますが、実行時に関数の開始ポインタを受け取ることはできますか? –
を@unwindに接続すると、http://citforum.ru/book/cook/selfmod.shtmlのサンプルコードを参照してください。後でコメントします。 –