【DynamoRIO 入门教程】三: div.c

该例子在注释中这样写到:统计 div 指令个数, 要求是 除数必须为2。因为这种情况下我们可以将div 优化为 移位。使用实时操作数值演示基于标注的分析。

本例使用了 drmgr extension ,在编写 CMakeLists.txt 时要注意。

第一步:dr_client_main 函数

DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char *argv[])
{
    if (!drmgr_init())
        DR_ASSERT(false);
    dr_register_exit_event(exit_event);
    if (!drmgr_register_bb_instrumentation_event(NULL, event_app_instruction, NULL))
        DR_ASSERT(false);
    count_mutex = dr_mutex_create();
}

首先使用 drmgr_init() 函数初始化了 drmgr extension。
这里一共注册两个回调函数:
dr_register_exit_event(exit_event) 将 exit_event 注册为 进程结束的回调函数。

drmgr_register_bb_instrumentation_event(NULL, event_app_instruction, NULL 将event_app_instruction 注册为 basic block insert 时的回调函数。这里用了 drmgr 扩展。
dr_mutex_create() 则是用来生成互斥锁的,count_mutex 就是生产的锁。

然后看 exit_event 函数

static void
exit_event(void)
{
    char msg[512];
    int len;
    len = dr_snprintf(msg, sizeof(msg) / sizeof(msg[0]),
                      "Instrumentation results:\n"
                      "  saw %d div instructions\n"
                      "  of which %d were powers of 2\n",
                      div_count, div_p2_count);
    DR_ASSERT(len > 0);
    NULL_TERMINATE(msg);
    DISPLAY_STRING(msg);

    dr_mutex_destroy(count_mutex);
    drmgr_exit();
}

本函数的主要作用是打印结果,这里要先格式化字符串,然后再用 DISPLAY_STRING宏来打印。
这里的 NULL_TERMINATE 的作用是在字符串结尾处加上空字符。

然后就是用 dr_mutex_destroy 来销毁互斥锁,并调用 drmgr_exit();

再然后看: event_app_instruction() 函数

static dr_emit_flags_t
event_app_instruction(void *drcontext, void *tag, instrlist_t *bb, instr_t *instr,
                      bool for_trace, bool translating, void *user_data)
{
    /* if find div, insert a clean call to our instrumentation routine */
    opnd_t opnd;
    if (instr_is_div(instr, &opnd)) {
        dr_insert_clean_call(drcontext, bb, instr, (void *)callback, false /*no fp save*/,
                             2, OPND_CREATE_INTPTR(instr_get_app_pc(instr)), opnd);
    }
    return DR_EMIT_DEFAULT;
}

首先是 instr_is_div 来检测一个 instr 是不是我们要找的div 指令。
然后在 instr 之前插入了一个 clean call ,调用了 call back 函数。

下面我们就来看这两个函数:

instr_is_div(instr_t *instr, OUT opnd_t *opnd)

    static bool
    instr_is_div(instr_t *instr, OUT opnd_t *opnd)
    {
        int opc = instr_get_opcode(instr);
    #if defined(X86)
        if (opc == OP_div) {
            *opnd = instr_get_src(instr, 0); /* divisor is 1st src */
            return true;
        }
    #elif defined(AARCHXX)
        if (opc == OP_udiv) {
            *opnd = instr_get_src(instr, 1); /* divisor is 2nd src */
            return true;
        }
    #else
    #    error NYI
    #endif
        return false;
    }

instr_get_opcode 会返回 instr 指令的操作码,以 OP_constant 的形式。
instr_get_src 获取instr 对应位置的操作数。
所以该函数获取了 div 的除数并存放在 opnd 全局变量里面。

然后再看 clean call 调用的callback 函数:

callback(app_pc addr, uint divisor)

static void
callback(app_pc addr, uint divisor)
{
    /* instead of a lock could use atomic operations to
     * increment the counters */
    dr_mutex_lock(count_mutex);

    div_count++;

    /* check for power of 2 or zero */
    if ((divisor & (divisor - 1)) == 0)
        div_p2_count++;

    dr_mutex_unlock(count_mutex);
}

dr_mutex_lock 首先将之前的互斥锁 上锁。
div_count++ 表示找到了一个 div 指令,计数器加1.
然乎就要看 div 的除数是否是 2 的幂了。注意这里判断的方法,利用了二进制的一些特点。
dr_mutex_unlock() 然后解锁。

在这个例子里,我们没有对指令进行优化,只是根据 操作码,操作数来找到 目标指令,并统计其个数,并最终打印出来。

完整代码如下:

/* Counts the number of dynamic div instruction for which the
 * divisor is a power of 2 (these are cases where div could be
 * strength reduced to a simple shift).  Demonstrates callout
 * based profiling with live operand values. */

#include "dr_api.h"
#include "drmgr.h"

#ifdef WINDOWS
#    define DISPLAY_STRING(msg) dr_messagebox(msg)
#else
#    define DISPLAY_STRING(msg) dr_printf("%s\n", msg);
#endif

#define NULL_TERMINATE(buf) buf[(sizeof(buf) / sizeof(buf[0])) - 1] = '\0'

static dr_emit_flags_t
event_app_instruction(void *drcontext, void *tag, instrlist_t *bb, instr_t *instr,
                      bool for_trace, bool translating, void *user_data);
static void
exit_event(void);

static int div_count = 0, div_p2_count = 0;
static void *count_mutex; /* for multithread support */

DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char *argv[])
{
//    dr_set_client_name("DynamoRIO Sample Client 'div'", "http://dynamorio.org/issues");
    if (!drmgr_init())
        DR_ASSERT(false);
    dr_register_exit_event(exit_event);
    if (!drmgr_register_bb_instrumentation_event(NULL, event_app_instruction, NULL))
        DR_ASSERT(false);
    count_mutex = dr_mutex_create();
}

static void
exit_event(void)
{
    char msg[512];
    int len;
    len = dr_snprintf(msg, sizeof(msg) / sizeof(msg[0]),
                      "Instrumentation results:\n"
                      "  saw %d div instructions\n"
                      "  of which %d were powers of 2\n",
                      div_count, div_p2_count);
    DR_ASSERT(len > 0);
    NULL_TERMINATE(msg);
    DISPLAY_STRING(msg);

    dr_mutex_destroy(count_mutex);
    drmgr_exit();
}

static void
callback(app_pc addr, uint divisor)
{
    /* instead of a lock could use atomic operations to
     * increment the counters */
    dr_mutex_lock(count_mutex);

    div_count++;

    /* check for power of 2 or zero */
    if ((divisor & (divisor - 1)) == 0)
        div_p2_count++;

    dr_mutex_unlock(count_mutex);
}

/* If instr is unsigned division, return true and set *opnd to divisor. */
static bool
instr_is_div(instr_t *instr, OUT opnd_t *opnd)
{
    int opc = instr_get_opcode(instr);
#if defined(X86)
    if (opc == OP_div) {
        *opnd = instr_get_src(instr, 0); /* divisor is 1st src */
        return true;
    }
#elif defined(AARCHXX)
    if (opc == OP_udiv) {
        *opnd = instr_get_src(instr, 1); /* divisor is 2nd src */
        return true;
    }
#else
#    error NYI
#endif
    return false;
}

static dr_emit_flags_t
event_app_instruction(void *drcontext, void *tag, instrlist_t *bb, instr_t *instr,
                      bool for_trace, bool translating, void *user_data)
{
    /* if find div, insert a clean call to our instrumentation routine */
    opnd_t opnd;
    if (instr_is_div(instr, &opnd)) {
        dr_insert_clean_call(drcontext, bb, instr, (void *)callback, false /*no fp save*/,
                             2, OPND_CREATE_INTPTR(instr_get_app_pc(instr)), opnd);
    }
    return DR_EMIT_DEFAULT;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章