简体   繁体   中英

How to override a function (system call) in C language?

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.

You would code it with a macro, in your simple case

#define func()                \
do {                          \
    log("Someone called func"); \
    func();                   \
} while(0)

inside the expansion of a macro the macro is not expanded recursively.

For more complicated cases you'd implement a function and a macro. In a ".h" file you would put:

void func_annotated(void);
#ifdef DEBUG
# define func() func_annotated()
// or if the function takes parameters
// # define func(...) func_annotated(__VA_ARGS__)
#endif

in a ".c" file you'd then put the implementation

void func_annotated(void)
{
    log("Someone called func");
    (func)();
}

Here the extra () arround func ensures that this always uses the real function.

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 :

$ LD_PRELOAD=/your/own/functions/file.so ls -l

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.

You don't need to use function pointers in the case. You can just have the function func take parameters for whatever you want to log. And Same thing with the override, that doesn't need to be a function pointer (unless you plan on changing what the override function will be later). Also, in this case it will create an infinite loop because the function pointer is pointing to the func function. Also I'm not even sure if you're allowed to use func as a function pointer before you declare the function.

I would try something like this, this would log an integer, but you can use it to log whatever you want.

void func(int value)
{
    printf("%d\n", value);
    anotherFunc(value);
}

where anotherFunc() is a different function entirely. Like I said earlier, if you want to change the function that's called by anotherFunc() then it would make sense to use a function pointer, otherwise one is not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM