更改 C 代码以完全运行(包括 if 的所有部分)

标签 c performance testing automation dead-code

注意:我意识到我的问题不清楚。我现在已经修改了它,并为当初的错误道歉。

我有一个要在嵌入式系统上运行的大型 C 项目。我使用 ARM 编译器。 代码分布在多个子文件夹中,由 .c 和 .h 文件组成。

我想盘点一下哪个函数被调用了多少次,这样我就可以识别死代码,也可以加速最常用的函数。 ARM 编译器有几个选项可以删除无法访问的代码,当函数指针起作用时它会失败 因此,我想遍历代码的每个分支并记录对函数的调用次数。

例如(这是一个非常简单的程序来演示我正在寻找的东西,而不是原始代码):

void foo1(void)
{
    printf("Number is divisible by 5");
}
void foo2(void)
{
    printf("Number is divisible by 10");
}
void foo3(void)
{
    printf("Number is divisible by neither 10 nor 5");
}
void foo4(void)
{
    printf("foo4 called");
}


int main (void)
{
    int x;

    x = rand (11);

    if (! x%10)
    {
        foo2();
        foo1();
    }
    else if (! x%5)       foo1();
    else             foo3();

    return 0;
}

我想运行整个代码以访问 if 分支的所有部分(即 foo1、foo2 和 foo3)。这将帮助我确定哪个函数被调用了多少次。 在上面的例子中,foo1() 比 foo2() 更频繁地被调用,而 foo4() 从未被调用过。 因此,识别和删除 foo4() 并优化 foo1() 是有意义的。

运行整个代码有什么想法/工具吗?

我想到的一种方法是像这样修改主函数:

int main (void)
{
    int x ;

    x = rand (11);

    //*************************************************
    //starting modification
    int a = 1; //added by me
    if_1: //added by me
    if (a == 1)
    {
        foo1(); //original code
        foo2(); //original code

        a=2; //added by me
        goto if_1; //added by me
    }

    else if (a==2)
    {
        foo2(); //original code
        a=3; //added by me
        goto if_1: //added by me
    }
    else             foo3(); //original code
    //end of modification
    //********************************************************        

    return 0;
}

这样它就贯穿了原始代码。 知道如何进行此类修改吗?

最佳答案

int main (void)
{
    int x = 5;
#ifdef DEBUG
    int debug_i, debug_data[] = { 5, 10, 20 };
    for(debug_i = 0;debug_i<sizeof debug_data / sizeof *debug_data;++debug_i){
        x = debug_data[debug_i];
#endif
    if (x==5)        foo1();
    else if (x==10)  foo2();
    else             foo3();
#ifdef DEBUG
    }
#endif

    return 0;
}

关于更改 C 代码以完全运行(包括 if 的所有部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17588053/

相关文章:

c - 管理员权限的替代方案 - fopen 不会在 Windows 中创建 C :\temp. txt

sql - GATHER_PLAN_STATISTICS 不生成基本计划统计信息

string - 如何在 Julia 的字符串数组中查找子字符串或字符

c++ - (C++) 使用功能测试工具

php - 如何在 codeception 中检索当前的 cest 名称?

c++ - C++中静态变量的存储位置是什么时候确定的?

c - 为什么此函数无法成功确定两个任意字符串是否相同?

您可以选择哪个子进程接收来自父进程的消息吗?

java - 限制 Publisher 中的预取

javascript - Jasmine 测试超时, "Async callback was not invoked within 5000ms"altghough 在我的 Angular 项目测试中没有使用异步函数