[DynamoRIO]Windows下初探drsyms
1. 环境搭建
1.1 下载
DynamoRIOVisual StudioCMakeVisual Studio Code(可选)
1.2 样例源码
见文末
1.3 构建
Client构建参考DynamoRIO tools build tutorial,Application单文件编译即可,需注意保证编译Client与Application使用同一编译器。
Client与Client CMakeLists放在同一目录下。
1.3.1 有VSCode
- 安装
CMake插件后在./vscode/setting.json内加入,或插件设置也可
"cmake.configureEnvironment": {
"DynamoRIO_DIR": "path\\to\\your\\DynamoRIO-Windows-xx.xx.xx\\cmake"
}
-
选择工具包为
Visual Studio。版本号与架构根据读者本机选择。 -
直接构建即可。
1.3.2 无VSCode
Generators等选择请参考cmake-generators。
mkdir build
cd build
cmake.exe -DDynamoRIO_DIR="path\to\your\DynamoRIO-Windows-xx.xx.xx\cmake" -G <Generators> -T host=<x64/...> -A <x64/...> ..
cmake.exe --build .
1.4 运行
cd DynamoRIO-Windows-10.0.0
.\bin64\drrun.exe -c "path\to\your\ClientBuildDir\DynamoRIO_Windows_Demo.dll" -- "path\to\your\Application.exe"
2. drsyms使用解析
DynamoRIO本体的符号查找可以使用dr_get_proc_address,但是其具有一定的局限性,- 符号应尽可能给定全限定的符号,如
A::getName,匿名名字空间等也需要指明,可通过dumpbin来查看库或可执行文件内的符号。 drsym_lookup_symbol只支持返回一个符合模式的符号,如果需要查找同名函数,可以使用drsym_search_symbols,但该函数只支持PDB符号,所以不具有平台可移植性。- 样例中的
get_pc_by_symbol取自drwrap拓展内的func_trace.cpp。
样例源码
Application
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
class A {
public:
virtual string getName() const {
return "A::getName called";
}
};
class B : public A {
public:
B(const string& name)
: name(name) {}
string getName() const override {
return "B::getName called: " + name;
}
static A* getInstance() {
static A* instance = new B("single Instance");
return instance;
}
private:
string name;
};
int testSumLength(const string& a, const string& b) {
return a.size() + b.size();
}
int main() {
A* instance = B::getInstance();
cout << instance->getName() << endl;
cout << instance->A::getName() << endl;
cout << "total length: " << testSumLength(instance->getName(), instance->A::getName()) << endl;
return 0;
}
Client
#include "dr_api.h"
#include "drmgr.h"
#include "drsyms.h"
#include "drwrap.h"
#include <cstdio>
#include <string>
#include <sstream>
#include <iostream>
#include <stack>
struct traced_function_info {
char* name;
unsigned arg_num;
app_pc pc;
};
static traced_function_info traced_function[] = { {"testSumLength", 2},
{"A::getName", 0},
{"B::getName", 0},
{"B::getInstance", 0} };
static app_pc
get_pc_by_symbol(const module_data_t* mod, const char* symbol)
{
if (mod == NULL || symbol == NULL)
return NULL;
// Try to find the symbol in the dynamic symbol table.
app_pc pc = (app_pc)dr_get_proc_address(mod->handle, symbol);
if (pc != NULL) {
// NOTIFY(2, "dr_get_proc_address found symbol %s at pc=" PFX "\n", symbol, pc);
return pc;
}
// else if (op_record_dynsym_only.get_value()) {
// NOTIFY(2, "Failed to find symbol %s in .dynsym; not recording it\n", symbol);
// return NULL;
// }
else {
// If failed to find the symbol in the dynamic symbol table, then we try to find
// it in the module loaded by reading the module file in mod->full_path.
// NOTE: mod->full_path could be invalid in the case where the original
// module file is remapped and deleted (e.g. hugepage_text).
// FIXME: find a way to find the PC of the symbol even if the original module file
// is deleted.
size_t offset;
drsym_error_t err =
drsym_lookup_symbol(mod->full_path, symbol, &offset, DRSYM_DEMANGLE);
if (err != DRSYM_SUCCESS) {
err =
drsym_lookup_symbol(mod->full_path, symbol, &offset, DRSYM_LEAVE_MANGLED);
}
if (err == DRSYM_SUCCESS) {
pc = mod->start + offset;
// NOTIFY(2, "drsym_lookup_symbol found symbol %s at pc=" PFX "\n", symbol, pc);
return pc;
}
else {
// NOTIFY(2, "Failed to find symbol %s, drsym_error_t=%d\n", symbol, err);
return NULL;
}
}
}
/*
* <function address> <function name>(<function arg address...>) =>
* (<function return val address>) <function return address>
*/
static void
func_pre_hook(void* wrapcxt, INOUT void** user_data) {
void* drcontext = drwrap_get_drcontext(wrapcxt);
size_t index = (size_t)*user_data;
std::stringstream ss;
std::string func_output_info;
ss << (void*)traced_function[index].pc << " " << traced_function[index].name << "(";
for (int i = 0; i < traced_function[index].arg_num; ++i) {
ss << std::hex << (uintptr_t)drwrap_get_arg(wrapcxt, i);
if (i != traced_function[index].arg_num - 1) {
ss << ",";
}
}
ss << ") ";
func_output_info = ss.str();
dr_fprintf(STDERR, "%s\n", func_output_info.c_str());
// std::cerr << func_output_info << std::endl;
}
static void
func_post_hook(void* wrapcxt, void* user_data) {
void* drcontext = drwrap_get_drcontext(wrapcxt);
size_t index = (size_t)user_data;
std::stringstream ss;
std::string func_output_info;
ss << "=> (" << std::hex << (uintptr_t)drwrap_get_retval(wrapcxt) << ") " << (void*)drwrap_get_retaddr(wrapcxt);
func_output_info = ss.str();
dr_fprintf(STDERR, "%s\n", func_output_info.c_str());
// std::cerr << func_output_info << std::endl;
}
static const char*
get_type(drsym_type_t* type) {
switch (type -> kind) {
case DRSYM_TYPE_INT:
return "int";
case DRSYM_TYPE_OTHER:
return "unknow";
case DRSYM_TYPE_ARRAY:
return "array";
default:
return "unknow";
}
}
static const char*
get_arg(drsym_type_t** arg, int num_args, char* buff) {
int offset = 0;
for (int i = 0; i < num_args; ++i) {
const char* arg_str = get_type(arg[i]);
strcpy(buff + offset, arg_str);
offset += strlen(arg_str);
}
return buff;
}
static bool
get_all_same_name_function(const char* name, size_t modoffs, void* data) {
const module_data_t* info = (module_data_t*)data;
char func_buff[2048];
char buff[2048];
drsym_func_type_t* func_type;
drsym_get_func_type(info->full_path, modoffs, func_buff, sizeof(func_buff), &func_type);
dr_fprintf(STDERR, "%X %s %s(%s)\n", info->start + modoffs, get_type(func_type->ret_type), name, get_arg(func_type->arg_types, func_type->num_args, buff));
return true;
}
static void
module_load(void* drcontext, const module_data_t* info, bool loaded) {
int function_number = sizeof(traced_function) / sizeof(traced_function[0]);
const char* mod_name = dr_module_preferred_name(info);
// dr_fprintf(STDERR, "%s\n", mod_name);
for (size_t i = 0; i < function_number; ++i) {
// app_pc f_pc = (app_pc)dr_get_proc_address(info->handle, traced_function[i].name);
app_pc f_pc = get_pc_by_symbol(info, traced_function[i].name);
drsym_search_symbols(info->full_path, traced_function[i].name, false, get_all_same_name_function, (void*)info);
// dr_fprintf(STDERR, "%X\n", f_pc);
if (f_pc == NULL) {
continue;
}
traced_function[i].pc = f_pc;
drwrap_wrap_ex(traced_function[i].pc, func_pre_hook, func_post_hook, (void*)(ptr_uint_t)i, 0);
}
}
DR_EXPORT void
dr_client_main(client_id_t id, int argc, const char *argv[]) {
drsym_init(0);
drmgr_init();
drwrap_init();
drmgr_register_module_load_event(module_load);
}
Client CMakeLists
cmake_minimum_required(VERSION 3.0)
project(DynamoRIO_Windows_Demo)
add_library(DynamoRIO_Windows_Demo SHARED "client-0.1.cpp")
find_package(DynamoRIO)
configure_DynamoRIO_client(DynamoRIO_Windows_Demo)
use_DynamoRIO_extension(DynamoRIO_Windows_Demo drmgr)
use_DynamoRIO_extension(DynamoRIO_Windows_Demo drx)
use_DynamoRIO_extension(DynamoRIO_Windows_Demo drsyms)
use_DynamoRIO_extension(DynamoRIO_Windows_Demo drwrap)
use_DynamoRIO_extension(DynamoRIO_Windows_Demo drreg)
use_DynamoRIO_extension(DynamoRIO_Windows_Demo droption)