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

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

问题描述

为了记录调用,我想覆盖对各种 API 的某些函数调用,但我也可能想在将数据发送到实际函数之前对其进行操作.

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 的函数数千次.有时我想临时覆盖这个函数,因为我想改变这个函数的行为以查看不同的结果.

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.

有没有更简单的方法来覆盖"一个函数而不会出现链接/编译错误/警告?理想情况下,我希望能够通过编译和链接一两个额外的文件而不是摆弄链接选项或更改程序的实际源代码来覆盖该函数.

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) myGetObjectName(x)
#endif

然后你实现如下函数(在intercept.c包含intercept.h):

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

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

然后确保您要拦截调用的每个源文件的顶部都包含以下内容:

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

#include "intercept.h"

当您使用-DINTERCEPT"进行编译时,所有文件都会调用您的函数而不是真正的函数,而您的函数仍会调用真正的函数.

When you compile with "-DINTERCEPT", all files will call your function rather than the real one, whereas your function will still call the real one.

在没有-DINTERCEPT"的情况下编译;将防止发生拦截.

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

如果您想拦截所有调用(不仅仅是来自源的调用),这有点棘手 - 这通常可以通过动态加载和解析实际函数(使用 dlload-dlsym-type calls)但我认为在您的情况下没有必要.

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天全站免登陆