Intel PinTools安装使用简单教程
文章目录
日志:
- 2018-08-02:修正相关资源链接。现在 Intel 恢复对 Pintool 的维护了,经测试,Linux 4.15 内核可以使用 Pintool;
- 2018-01-04:发布了第一个版本;
Note:本文称 Instrumentation 为「插桩」。
安装测试 Pin
About
有关资源及资料见下述链接:
安装 Pin
依次输入如下指令完成 pin 的下载、解压:
wget http://software.intel.com/sites/landingpage/pintool/downloads/pin-2.14-71313-gcc.4.4.7-linux.tar.gz
tar -xzf pin-2.14-71313-gcc.4.4.7-linux.tar.gz
Note:你的内核版本可能不受支持。
测试 Pin
我们可以 先运行一个 demo 进行测试。在这个例子中我们将生成一个名为 insmix 的工具,这个工具是用来统计应用程序执行的 x86 指令的数量的。通过如下指令打开 insmix 工具所在目录,并生成该工具:
cd pin-2.14-71313-gcc.4.4.7-linux/source/tools/Insmix/
make
如果执行成功的话,应该可以在 source/tools/Insmix/obj-intel64/
目录下找到文件insmix.so
。此时我们返回到 pin 的根目录,然后输入如下指令将 insmix 工具附加在 ls 指令上并由 pin 运行 ls 指令:
cd ../../../
./pin.sh -t source/tools/Insmix/obj-intel64/insmix.so -- ls
运行结束后结果文件输出在 pin 根目录下,即 insmix.out
。
自定义 PinTool
仅仅使用 Pin 平台提供的已有 PinTools 肯定无法满足我们所有的需求,有时甚至对已有 PinTool 进行自定义也无法满足我们的需求,这时候我们就需要自己自定义 PinTool。
下面就将介绍一种基于镜像的函数插桩式 PinTool 的代码结构及生成、使用方式。
完成 PinTool 源码
假定我们需要一种获取程序所有内存分配、释放情况的 PinTool。
我们先查找 pin 中已经提供的 pintools,可以发现 source/tools/ManualExamples/malloctrace.cpp
这个 PinTool 完全可实现我们的需求。这个 PinTool 可以实现函数粒度的插桩并实现对 malloc 与 free 的简单分析:记录 malloc 的参数及返回值,记录 free 的参数。
该源码内容如下:
/*BEGIN_LEGAL
Intel Open Source License
Copyright (c) 2002-2015 Intel Corporation. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. Redistributions
in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution. Neither the name of
the Intel Corporation nor the names of its contributors may be used to
endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT