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

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