Skip to content

Instantly share code, notes, and snippets.

@mitake
Created March 6, 2013 14:09
Show Gist options
  • Save mitake/5099529 to your computer and use it in GitHub Desktop.
Save mitake/5099529 to your computer and use it in GitHub Desktop.
sample of DynamoRIO client
cmake_minimum_required(VERSION 2.8)
SET(DynamoRIO_DIR "/home/mitake/dynamorio/exports/cmake")
find_package(DynamoRIO)
add_library(sample SHARED sample.c)
configure_DynamoRIO_client(sample)
#define LINUX
#define X86_64
#include "dr_api.h"
typedef struct bb_counts {
uint64 blocks;
uint64 total_size;
} bb_counts;
static bb_counts counts_as_built;
void *as_built_lock;
static bb_counts counts_dynamic;
void *count_lock;
static void event_exit(void)
{
char msg[512];
int len;
len = snprintf(msg, sizeof(msg) / sizeof(msg[0]),
"Number of basic blocks built: %"UINT64_FORMAT_CODE"\n"
" Average size : %5.2lf instructions\n",
"Number of basic blocks executed: %"UINT64_FORMAT_CODE"\n"
" Average weighted size : %5.2lf instructions\n",
counts_as_built.blocks,
counts_as_built.total_size / (double)counts_as_built.blocks,
counts_dynamic.blocks,
counts_dynamic.total_size / (double)counts_dynamic.blocks);
DR_ASSERT(0 < len);
msg[sizeof(msg) / sizeof(msg[0]) - 1] = '\0';
dr_printf("%s\n", msg);
dr_mutex_destroy(as_built_lock);
dr_mutex_destroy(count_lock);
}
static void clean_call(uint instruction_count)
{
dr_mutex_lock(count_lock);
counts_dynamic.blocks++;
counts_dynamic.total_size += instruction_count;
dr_mutex_unlock(count_lock);
}
static dr_emit_flags_t event_basic_block(void *drcontext, void *tag, instrlist_t *bb, bool for_trace, bool translating)
{
uint num_instructions = 0;
instr_t *instr;
for (instr = instrlist_first(bb); instr != NULL; instr = instr_get_next(instr))
num_instructions++;
dr_mutex_lock(as_built_lock);
counts_as_built.blocks++;
counts_as_built.total_size += num_instructions;
dr_mutex_unlock(as_built_lock);
dr_insert_clean_call(drcontext, bb, instrlist_first(bb), clean_call, false, 1, OPND_CREATE_INT32(num_instructions));
return DR_EMIT_DEFAULT;
}
DR_EXPORT void dr_init(client_id_t id)
{
dr_register_exit_event(event_exit);
dr_register_bb_event(event_basic_block);
as_built_lock = dr_mutex_create();
count_lock = dr_mutex_create();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment