In many script language, we have a programming method like this:
First, there is a function named func:
void func()
{
}
Second, I want to log some information when client call this function, but I don’t want to
modify the function , so i can do something like:
void (*pfunc)(void) = func;
void func()
{
log("Someone call fund");
pfunc();
}
After that, anyone who call fund will call my “override” function.This is OK in many script language. Can I do the same thing in C language? And how to code it?
I want to use this method to do some work in some 3party library, so I must do something affect the link process, not just the compile process.
Maybe you are looking for a library interposer. You could define your own functions in a file and call the program you want to log calls using that functions instead of the syscalls.
For example, interposing some syscalls made by
ls:It is better suited for situations where you can’t change the program code (maybe because it is unavailable) that is calling the functions you want to log.