跳到正文

个人记录

[Intel Pin]Windows下初探Intel Pin插桩

ZPC2048约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月25日
篇幅
约 9 分钟

版本

  1. v1当前

1. 环境搭建

1.1 下载

1.2 样例源码

见文末,MyPinTool源码拷入\source\tools\MyPinTool\MyPinTool.cpp。

1.3 构建

参考WINDOWS_TOOLS,注意选择合适的架构。

1.4 运行

  • 工具-外部工具-添加
    • 标题:自定义。
    • 命令:\path\to\your\pin.exe
    • 参数:-t MyPinTool.dll -f A::getName -f B::getName -f B::getInstance -f testSumLength -- \path\to\your\Application.exe
    • 初始目录:根据生成的MyPinTool.dll位置选择。
    • 不勾选退出时关闭。
  • 选择自己添加的外部工具运行。

2. 样例简析

通过Intel Pin的API文档我们可以发现,该工具对于被插桩程序的层次划分很明确,自顶向下依次为IMG,SEC,RTN,TRACE,BBL,INS。其中IMG一般表示共享库或可执行文件等,RTN表示函数,INS表示底层的汇编代码。

样例代码通过IMG_AddInstrumentFunction注册了IMG加载的回调函数,在回调中依次获取该IMG中的函数,向符合条件的函数进行插桩。

样例源码

MyPinTool

C++
/*
 * Copyright (C) 2007-2021 Intel Corporation.
 * SPDX-License-Identifier: MIT
 */

/*! @file
 *  This is an example of the PIN tool that demonstrates some basic PIN APIs
 *  and could serve as the starting point for developing your first PIN tool
 */

#include "pin.H"
#include <iostream>
#include <string>
#include <vector>
#include <set>

using std::string;
using std::cerr;
using std::endl;
using std::vector;
using std::set;

KNOB<string> KnobTracedFunction(KNOB_MODE_APPEND, "pintool", "f", "", "traced function name");

set<string> isFunctionTraced;

/*
* [<function address??>] <function name>(<function arg address>) <function return address>
* => <function return val address>
*/

VOID function_pre_hook(ADDRINT function_address, char* name, ADDRINT arg0, ADDRINT ret_address) {
    cerr << function_address << " " << name << "(" << arg0 << ")" << ret_address << endl;
}

VOID function_post_hook(char* name, ADDRINT ret) {
    cerr << "=> " << std::hex << ret << endl;
}

VOID img_callback(IMG img, VOID* v) {
    for (SEC sec = IMG_SecHead(img); SEC_Valid(sec); sec = SEC_Next(sec)) {
        for (RTN rtn = SEC_RtnHead(sec); RTN_Valid(rtn); rtn = RTN_Next(rtn)) {
            //cerr << IMG_Name(img) << ": " << RTN_Name(rtn) << endl;
            if (isFunctionTraced.find(PIN_UndecorateSymbolName(RTN_Name(rtn), UNDECORATION_NAME_ONLY)) != isFunctionTraced.end()) {
                RTN_Open(rtn);
                RTN_InsertCall(rtn, IPOINT_BEFORE, (AFUNPTR)function_pre_hook, IARG_ADDRINT, RTN_Address(rtn), IARG_ADDRINT, RTN_Name(rtn).c_str(), IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
                    IARG_RETURN_IP, IARG_END);
                RTN_InsertCall(rtn, IPOINT_BEFORE, (AFUNPTR)function_post_hook, IARG_ADDRINT, RTN_Name(rtn).c_str(), IARG_FUNCRET_EXITPOINT_VALUE, IARG_END);
                RTN_Close(rtn);
            }
        }
    }
}

int main(int argc, char* argv[]) {
    // Initialize PIN library. Print help message if -h(elp) is specified
    // in the command line or the command line is invalid
    PIN_InitSymbols();
    if (PIN_Init(argc, argv))
    {
        return -1;
    }

    for (int i = 0; i < KnobTracedFunction.NumberOfValues(); ++i) {
        cerr << "traced function " << i << ": " << KnobTracedFunction.Value(i) << endl;
        isFunctionTraced.insert(KnobTracedFunction.Value(i));
    }

    IMG_AddInstrumentFunction(img_callback, NULL);

    PIN_StartProgram();

    return 0;
}

Application

C++
#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;
}
原文