293 views
# cs6332: pintool tutorial ## inscount.so example 1. Download [inscount][inscounts.tar.gz] examples ```shell $ mkdir inscounts $ cd inscounts inscounts $ wget https://cs6332.syssec.org/l/week07/res/inscounts.tar.gz # or $ mkdir inscounts $ cd inscounts inscount $ cp /home/labs/inscounts.tar.gz . ... ``` 2. Decompess ``` inscounts $ tar xvf ./inscounts.tar.gz inscount0.cpp inscount1.cpp inscount2.cpp Makefile nullpin.cpp obj-intel64/ opcodemix.cpp ``` 3. PINTOOL Build ``` inscounts $ make -e obj-intel64/inscount0.so ... ``` 4. Run ``` inscounts $ pin -t obj-intel64/inscount0.so -- ls ``` ## inscount0.cpp ```cpp= /* * Copyright (C) 2004-2021 Intel Corporation. * SPDX-License-Identifier: MIT */ #include <iostream> #include <fstream> #include "pin.H" using std::cerr; using std::endl; using std::ios; using std::ofstream; using std::string; ofstream OutFile; // The running count of instructions is kept here // make it static to help the compiler optimize docount static UINT64 icount = 0; // This function is called before every instruction is executed VOID docount() { icount++; } // Pin calls this function every time a new instruction is encountered VOID Instruction(INS ins, VOID* v) { // Insert a call to docount before every instruction, no arguments are passed INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_END); } KNOB< string > KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "inscount.out", "specify output file name"); // This function is called when the application exits VOID Fini(INT32 code, VOID* v) { // Write to a file since cout and cerr maybe closed by the application OutFile.setf(ios::showbase); OutFile << "Count " << icount << endl; OutFile.close(); } /* ===================================================================== */ /* Print Help Message */ /* ===================================================================== */ INT32 Usage() { cerr << "This tool counts the number of dynamic instructions executed" << endl; cerr << endl << KNOB_BASE::StringKnobSummary() << endl; return -1; } /* ===================================================================== */ /* Main */ /* ===================================================================== */ /* argc, argv are the entire command line: pin -t <toolname> -- ... */ /* ===================================================================== */ int main(int argc, char* argv[]) { // Initialize pin if (PIN_Init(argc, argv)) return Usage(); OutFile.open(KnobOutputFile.Value().c_str()); // Register Instruction to be called to instrument instructions INS_AddInstrumentFunction(Instruction, 0); // Register Fini to be called when the application exits PIN_AddFiniFunction(Fini, 0); // Start the program, never returns PIN_StartProgram(); return 0; } ``` ## Resources * [PIN User Guide] * [PIN API] * [PIN Tutorial] ###### tags: `pintools`,`inscount`,`pintool` [inscounts.tar.gz]:https://cs6332.syssec.org/l/week07/res/inscounts.tar.gz [PIN API]:https://cs6332.syssec.org/l/week07/res/inscounts.tar.gz [PIN User Guide]:https://software.intel.com/sites/landingpage/pintool/docs/98650/Pin/doc/html/index.html [PIN API]:https://software.intel.com/sites/landingpage/pintool/docs/98650/Pin/doc/html/group__API__OVERVIEW.html [PIN Tutorial]:https://software.intel.com/sites/landingpage/pintool/docs/98650/Pin/doc/html/group__API__OVERVIEW.html