DynamoRIO 文件操作

DynamoRIO 进行文件的读写操作:

1. 打开文件

    file_t   dr_open_file(   const char *fname,  
    				uint  mode_flags
    			 )  

该函数返回打开的文件的句柄。
以 fname 为路径打开一个文件,如果这个文件不存在,则进行创建。注意,尽量使用绝对路径。如果使用相对路径,则会利用进程初始化时设置的当前目录 以尽力而为的方式 转换为绝对路径。
文件打开的权限模式由 mode_flags 设定。mode_flags 的值是 DR_FILE_* 。比如下面的:

DR_FILE_READ
DR_FILE_WRITE_APPEND
DR_FILE_WRITE_ONLY
DR_FILE_WRITE_REQUIRE_NEW
...

具体内容可以在DynamoRIo 文档搜索 dr_open_file

2. 关闭文件

void dr_close_file( file_t )	

注意别忘了关闭文件,exit 事件的回调函数里关闭。

3. 向文件里写入字符串

ssize_t dr_fprintf ( file_t f,
					 const char *  fmt,
 					... 
					)	

向文件f 里写入格式化字符串。

4. 向文件里写入指令

void instr_disassemble ( void * drcontext,
						 instr_t * instr,
						 file_t  outfile 
					   )	

向 outfile 文件里写入 instr 指令。

下面是我根据dynamorio 文档里的 average_bb_size 例子改编 的一个打印 basic block 的客户端。

#include "dr_api.h"
#include <stdio.h>

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

typedef struct bb_counts {
	uint64 blocks;
	uint64 total_size;
} bb_counts;

file_t f;

static bb_counts counts_as_built;
void *as_built_lock;

static void
event_exit(void);
static dr_emit_flags_t
event_basic_block(void *drcontext, void *tag, instrlist_t *bb,
	bool for_trace, bool translating);

DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char *argv[])
{
	/* register events */
	dr_register_exit_event(event_exit);
	dr_register_bb_event(event_basic_block);
	/* initialize lock */
	as_built_lock = dr_mutex_create();

	f = dr_open_file("C:\\Users\\Mr.wang\\Desktop\\DynamoRIO-Windows-7.0.17873-0\\work\\log.txt", DR_FILE_WRITE_APPEND );
//	DR_ASSERT(f != INVALID_FILE);
}
static void
event_exit(void)
{
	 /* Display results - we must first snpritnf the string as on windows
	  * dr_printf(), dr_messagebox() and dr_fprintf() can't print floats. */
	char msg[512];
	int len;
	len = snprintf(msg, sizeof(msg) / sizeof(msg[0]),
		"Number of basic blocks built : %llu \n"
		"     Average size            : %.5f instructions\n",
		counts_as_built.blocks,
		counts_as_built.total_size / (double)counts_as_built.blocks);
		//counts_as_built.total_size);
	DR_ASSERT(len > 0);
	msg[sizeof(msg) / sizeof(msg[0]) - 1] = '\0'; /* NUll terminate */
	DISPLAY_STRING(msg);
	
	      /* free mutex */
	dr_close_file(f);
	dr_mutex_destroy(as_built_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;


	     /* count the number of instructions in this block */
	for (instr = instrlist_first(bb); instr != NULL; instr = instr_get_next(instr)) {
	num_instructions++;
	instr_disassemble(drcontext, instr, f);
	dr_fprintf(f, "\n");
	}

	dr_fprintf(f, "\n");

	
	     /* update the as-built counts */
	dr_mutex_lock(as_built_lock);
	counts_as_built.blocks++;
	counts_as_built.total_size += num_instructions;
	dr_mutex_unlock(as_built_lock);
	return DR_EMIT_DEFAULT;
}

原例子地址: http://dynamorio.org/docs/API_tutorial_bbdynsize1.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章