覆盖在C函数调用 [英] Override a function call in C

查看:80
本文介绍了覆盖在C函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重写某些函数调用各种AP​​I来记录电话的缘故,但我也可能想,然后发送到实际功能来处理数据。

I want to override certain function calls to various APIs for the sake of logging the calls, but I also might want to manipulate data before it is sent to the actual function.

举例来说,说我使用一个名为 getObjectName 数千次功能我的源$ C ​​$ C。我想是因为我想改变这个函数的行为,看到不同的结果有时会暂时覆盖此功能。

For example, say I use a function called getObjectName thousands of times in my source code. I want to temporarily override this function sometimes because I want to change the behaviour of this function to see the different result.

我创建这样一个新的源文件:

I create a new source file like this:

#include <apiheader.h>    

const char *getObjectName (object *anObject)
{
    if (anObject == NULL)
        return "(null)";
    else
        return "name should be here";
}

我编译我的所有其他来源,我通常会,但我先用的API库链接之前链接它针对此功能。但我显然不能叫我重写函数内部的真正功能这工作得很好。

I compile all my other source as I normally would, but I link it against this function first before linking with the API's library. This works fine except I can obviously not call the real function inside my overriding function.

有没有覆盖没有得到联/编译错误/警告功能更简单的方法?理想的情况是我希望能够通过只编译和链接一个额外的文件或两个覆盖功能,而不是摆弄链接选项或改变我的计划的实际来源$ C ​​$ C。

Is there an easier way to "override" a function without getting linking/compiling errors/warnings? Ideally I want to be able to override the function by just compiling and linking an extra file or two rather than fiddle around with linking options or altering the actual source code of my program.

推荐答案

如果它是只为你想捕捉/修改呼叫源,最简单的解决方法是把一个头文件( intercept.h )其中:

If it's only for your source that you want to capture/modify the calls, the simplest solution is to put together a header file (intercept.h) with:

#ifdef INTERCEPT
    #define getObjectName(x) myGetObectName(x)
#endif

和实现功能如下(以 intercept.c 其中的的包括 intercept.h

and implement the function as follows (in intercept.c which doesn't include intercept.h):

const char *myGetObjectName (object *anObject) {
    if (anObject == NULL)
        return "(null)";
    else
        return getObjectName(anObject);
}

然后确保每一个源文件要截取呼叫有:

Then make sure each source file where you want to intercept the call has:

#include "intercept.h"

在顶部。

然后,当你用 -DINTERCEPT ​​编译,所有文件都将调用你的函数,而不是真正的和你的函数仍然可以调用的真正之一。

Then, when you compile with "-DINTERCEPT", all files will call your function rather than the real one and your function can still call the real one.

编译没有 -DINTERCEPT ​​将prevent拦截的发生。

Compiling without the "-DINTERCEPT" will prevent interception from occurring.

如果你想拦截所有呼叫(而不只是从您的源)这是一个有点棘手 - 这一般可以用动态加载和实际功能的分辨率进行(以 dlload - 则dlsym - 键入电话),但我不认为这是必要的,你的情况

It's a bit trickier if you want to intercept all calls (not just those from your source) - this can generally be done with dynamic loading and resolution of the real function (with dlload- and dlsym-type calls) but I don't think it's necessary in your case.

这篇关于覆盖在C函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆