为什么这个dynamorio的指令跟踪程序输出比我的多?

Why this instruction trace program of dynamorio has more output than mine?

我正在尝试使用检测工具 DynamoRIO 进行指令跟踪。我发现他们的网站上已经有一个指令跟踪样本:instrace_x86.c. However, I don't understand why they use so many operations in instrument_instr function。我尝试用另一种简单的方式重写这个函数:

instrument_instr(void *drcontext, instrlist_t *ilist, instr_t *where)
{
    app_pc pc;
    per_thread_t *data;

    data  = drmgr_get_tls_field(drcontext, tls_index);
    pc = instr_get_app_pc(where);

    fprintf(data->logf, PIFX",%s\n",
        (ptr_uint_t)pc, decode_opcode_name(instr_get_opcode(where)));
}

我发现这个简单的方法似乎也能正常工作,只是它的输出比官方示例少。

不知道为什么我的方法log少,因为我不知道官方的示例代码为什么会做这么琐碎的操作。有人熟悉 DynamoRIO 的 API 吗? (特别是drmgr_register_bb_instrumentation_event funciton。我不明白他们为什么要那样使用回调函数)

函数instrument_instr在DynamoRIO转换一个基本块时调用,而不是在执行时调用。由于基本块通常只转换一次但执行多次,因此您的输出与示例工具的输出不同。

DynamoRIO 内部工作的 over-simplified 视图是:DynamoRIO 在执行目标应用程序之前转换它们的基本块。这使用户(您)能够执行任意更改,并使 DynamoRIO 在执行基本块后收回控制权。转换后的块被写入 so-called 代码缓存, 它们将在其中执行。 DynamoRIO 仔细重写地址,以便跳跃和偏移仍然有效。代码缓存的目的是速度:已经变换过的基本块不需要再变换,所以留在代码缓存中;从程序的其他地方跳转到原来的基本块被替换为自动跳转到代码缓存中转换后的基本块。

为了显示完整的执行轨迹,您必须更改基本块,以便它们输出它们包含的所有指令;然后,只要执行基本块,您就会自动获得输出。如果您想有效地做到这一点,这不是微不足道的。这就是示例工具包含大量代码的原因。

我建议阅读一些 tutorial slide sets, for example the DynamoRIO tutorial at CGO Feb 2017 (PDF)