如何配置EXE及其同时加载的DLL?

时间:2014-12-28 20:42:55

标签: profiling mingw gprof

我在Windows上使用MinGW GCC编译器。如果我将-pg开关添加到编译器,我可以使用配置文件数据生成EXE和DLL。

生成gmon.out。但问题是,当我使用

gprof myprogram.exe gmon.out

我没有配置文件输出(除了表格标题和其他文本)。

gprof mydll.dll gmon.out

我只获取该特定DLL的输出,但不获取主exe的输出。

也许exe和dll都想生成相同的文件而dll赢了。

目标是在一个输出中获取EXE和DLL中的函数的统计信息。

gprof能做到吗?如果没有,是否有任何工具可以在Windows上执行此操作?

1 个答案:

答案 0 :(得分:0)

似乎gprof无法做到这一点。 MinGw中没有sprof。所以推出了我自己的探查器。

我使用-finstrument-functions因此,将分别在每个函数之前和之后调用两个函数__cyg_profile_func_enter__cyg_profile_func_exit

实际的分析函数被导出到DLL中并在这些函数中调用,并且DLL链接到所讨论的所有EXE和DLL。所以它可以分析它们两个

库中的代码是这样的(删除了杂乱:断言,错误检查,简化函数调用以获得清晰度)。

static void proflib_init()
{
    atexit(proflib_finalize);

    empty(callstack);
    empty(trackingData);
    proflibIntialized = 1;
}

static void proflib_finalize()
{
    /* Make a log. */
    FILE *f = fopen("proflib_log.txt", "wt");
    int i;

    sortBySelftime(trackingData);

    fprintf(f, "%10s%15s%15s%15s\n", "Func name", "Cumulative", "Self time", "Count");
    for (i = 0; i < getlength(trackingData); i++)
    {
        FunctionTimeInfo *fri = trackingData[i];

        fprintf(f, "%10p%15"PRIu64"%15"PRIu64"%20d\n", fri->addr, fri->cumulative, fri->selfTime, fri->count);
    }

    fclose(f);
}

void proflib_func_enter(void *func, void *caller)
{
    FunctionTimeInfo elem;
    long long pc;

    pc = rdtsc(); /* Read timestamp counter from CPU. */

    if (!is_prolib_initialized())
    {
        proflib_init();
    }

    /* Register self time as control moves to the child function. */
    if (!isEmpty(callstack))
    {
        FunctionTimeInfo *top = gettop(callstack);

        top->selfTime += pc - top->selfSample;
    }

    elem.addr = func; /* Address of function. */
    elem.cumulative = pc; /* Time spent in function and functions called by this. (so far store the reference point only.)*/
    elem.selfSample = pc; /* Reference point for self time counting. */
    elem.count = 1; /* Number of this the function is counted. */
    elem.selfTime = 0; /* Time spent in the function not including called functions. */

    push(callstack, elem);
}

void proflib_func_exit(void *func, void *caller)
{
    FunctionTimeInfo *fti;
    FunctionTimeInfo *storedStat;
    long long pc;

    pc = rdtsc();

    fti = gettop(callstack);

    fti->cumulative = pc - fti->cumulative; /* Finalize the time. */
    fti->selfTime += pc - fti->selfSample;
    pop(callstack);

    {
        FunctionTimeInfo *top = gettop(callstack);

        top->selfSample = pc; /* Set new self reference for the parent. */
    }

    storedStat = find(trackingData, func);

    if (storedStat)
    {
        /* Already have an entry. */
        storedStat->cumulative += fti->cumulative;
        storedStat->selfTime += fti->selfTime;
        storedStat->count++;
    }
    else
    {
        /* Add it as new entry. */
        add(trackingData, fti);
    }
}

它产生这样的日志:

 Func name     Cumulative      Self time          Count
  691C83B9  1138235861408  1138235861408             1137730
  00416396   539018507364   539018507364            16657216
  0040A0DC   259288775768   199827541522             1914832
  0041067D   876519599063   163253984165            92203200
  691C9E0E   785372027387   150744125859              190020
  004390F9  3608742795672   149177603708                   1
  0042E6A4   141485929006   116938396343               37753
  00428CB8   456357355541   112610168088              193304
  0041C2A4   340078363426    84539535634           114437798
  691CB980   402228058455    82958191728               29675
  00408A0A    79628811602    77769403982              512220
  0040D8CD    93610151071    63396331438            87773597
  0040D91A    60276409516    60276409516           175547194
  00427C36    72489783460    58130405593                   1
  691C7C3D    56702394950    56702394950             3455819
  691C949F   101350487028    47913486509             2977100
  691CBBF3   241451044787    45153581905               29771
  0043702E   920148247934    41990658926               25612
  ...

可以从MAP文件中找到函数名称。 DLL中0x691C83B9的函数确实是O(n³)复杂度的次优函数,被称为很多次,我必须重构......我完全忘记了函数甚至存在...... 0x004390F9是WinMain。< / p>