1 / 20

Pintools .

Pintools . an AtoZ tutorial of Pin instrumentation framework October 24, 2012 GNU DSLab . Contents. Pin Instrumentation Framework Examples of Pintools Detecting Races with a Pintool. Pin Instrumentation Framework. Instrumentation Instrumentation frameworks Pin overview

olympe
Download Presentation

Pintools .

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pintools. an AtoZ tutorial of Pin instrumentation framework October 24, 2012 GNU DSLab.

  2. Contents • Pin Instrumentation Framework • Examples of Pintools • Detecting Races with a Pintool Pintools for DSLab.

  3. Pin Instrumentation Framework • Instrumentation • Instrumentation frameworks • Pin overview • What Pin handles Pintools for DSLab.

  4. Instrumentation • A technique that inserts code to collect run-time information • Program analysis : performance profiling, error detection, capture & replay • Architectural study : processor and cache simulation, trace collection • Source-Code Instrumentation • Static Binary Instrumentation • Dynamic Binary Instrumentation (Pin) • Instrument code just before it runs (Just In Time – JIT) • No need to recompile or re-link • Discover code at runtime • Attach to running processes Pintools for DSLab.

  5. Instrumentation Frameworks * incorrect in some cases < Comparison with four characteristic > Pintools for DSLab.

  6. Pin Overview • Programmable Instrumentation: • Provides rich set of APIs to write, in C/C++, assembly, your own instrumentation tools, called PinTools • APIs are designed to maximize ease of use • Multiplatform: • Supports IA-32, Intel64, IA-64 • Supports Linux, Windows, MacOS • Robust: • Can instrument real-life applications: Database, web browsers, … • Can instrument multithreaded applications • Supports signals and exceptions, self modifying code… • If you can Run it – you can Pin it • Efficient: • Applies compiler optimizations on instrumentation code Pintools for DSLab.

  7. Launch Pin $ pin-t my_pintool.so-- application instrumentation tool (write your own, or use a provided sample) instrumentation engine (provided) Pintools for DSLab.

  8. Launcher Process PIN.EXE Launcher inscount.dll PIN.LIB Code Cache Application Code and Data PINVM.DLL System Call Dispatcher Event Dispatcher Thread Dispatcher NTDLL.DLL Windows kernel pin.exe -t inscount.dll -- gzip.exe input.txt 1. Read a Trace from Application Code 2. Jit it, adding instrumentation code from inscount.dll 3. Encode the Jitted trace into the Code Cache 4. Execute it Application Process Boot Routine + Data: firstAppIp, “Inscount.dll” First app IP Encoder Decoder Pintools for DSLab.

  9. What Pin Handles • Process/Thread • Start/End, Fork • Image • Loading/Unloading • Procedure • Entry/Exit*, Parameters • SystemCall • Entry/Exit, errno • Instruction • Start/End, Basic block, Memory accesses • Thread Context • Read/Write context * incorrect in some cases Pintools for DSLab.

  10. Examples of Pintools • Instruction Handling • Function Handling • Thread Handling • Memory Access Handling Pintools for DSLab.

  11. Instruction Handling #include "pin.h" UINT64 icount = 0; • void docount() { icount++; }  Callback function • void Instruction(INS ins, void *v) • { • INS_InsertCall(ins, IPOINT_BEFORE, • (AFUNPTR)docount, IARG_END); • }  Installing Callback • intmain(intargc, char * argv[]) • { • PIN_Init(argc, argv); • INS_AddInstrumentFunction(Instruction, 0); • PIN_StartProgram(); // Never returns • return 0; • } Initializing &  Registering Instrumentation Pintools for DSLab.

  12. Function Handling • VOID BeforeCriNameStart(THREADIDtid, intomplock) { • GetLock(&lock, tid + 1); • … // DO SOMETHING HERE • ReleaseLock(&lock); • } • VOID Image(IMG img, VOID *v) { • RTN rtn = RTN_FindByName(img, "GOMP_critical_name_start"); • if (RTN_Valid(rtn)) { • RTN_Open(rtn);RTN_InsertCall(rtn, IPOINT_BEFORE, (AFUNPTR)BeforeCriNameStart, IARG_THREAD_ID, • IARG_FUNCARG_ENTRYPOINT_VALUE, 0, IARG_END); • RTN_InsertCall(rtn, IPOINT_AFTER, (AFUNPTR)AfterCriNameStart, IARG_THREAD_ID, • IARG_FUNCARG_EXITPOINT_VALUE, IARG_END); • RTN_Close(rtn); • } } • intmain(intargc, char * argv[]) { • … • IMG_AddInstrumentFunction(Image, 0); • … • PIN_StartProgram(); // Never returns • return 0; • } Pintools for DSLab.

  13. Thread Handling • VOID ThreadStart(THREADID threadid, CONTEXT *ctxt, INT32 flags, VOID *v) { • GetLock(&lock, threadid+1); • … // DO SOMETHING HERE • ReleaseLock(&lock); • } • VOID ThreadFini(THREADID threadid, const CONTEXT *ctxt, INT32 code, VOID *v) { • GetLock(&lock, threadid+1); • … // DO SOMETHING HERE • ReleaseLock(&lock); • } • intmain(intargc, char * argv[]) { • … • PIN_AddThreadStartFunction(ThreadStart, 0); • PIN_AddThreadFiniFunction(ThreadFini, 0); • … • } Pintools for DSLab.

  14. Memory Access Handling • VOID RecordMemRead(VOID * ip, VOID * addr) { • … // DO SOMETHING HERE • } • VOID Instruction(INS ins, VOID *v) { • UINT32 memOperands = INS_MemoryOperandCount(ins); • for (UINT32 memOp = 0; memOp < memOperands; memOp++) { • if (INS_MemoryOperandIsRead(ins, memOp)) { • INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead, • IARG_INST_PTR, IARG_MEMORYOP_EA, memOp, IARG_END); • } • if (INS_MemoryOperandIsWritten(ins, memOp)) { • INS_InsertPredicatedCall( ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite, • IARG_INST_PTR, IARG_MEMORYOP_EA, memOp, IARG_END); • } } } • intmain(intargc, char * argv[]) { • … • INS_AddInstrumentFunction(Instruction, 0); • … • } Pintools for DSLab.

  15. Getting Source Line Numbers INT32 line; string file; string s; PIN_LockClient(); PIN_GetSourceLocation((ADDRINT)ip, NULL, &line, &file); PIN_UnlockClient(); ReleaseLock(&lock); Instruction Address Pintools for DSLab.

  16. Instrumentation 3-Stages • Instruction Instrumentation • Routine Instrumentation • Image Instrumentation Pintools for DSLab.

  17. Instruction Instrumentation #include "pin.h" UINT64 icount = 0; • void docount() { icount++; }  Callback function • void Instruction(INS ins, void *v) • { • INS_InsertCall(ins, IPOINT_BEFORE, • (AFUNPTR)docount, IARG_END); • }  Installing Callback • intmain(intargc, char * argv[]) • { • PIN_Init(argc, argv); • INS_AddInstrumentFunction(Instruction, 0); • PIN_StartProgram(); // Never returns • return 0; • } Initializing &  Registering Instrumentation Pintools for DSLab.

  18. Routine Instrumentation #include "pin.h" UINT64 rcount= 0; • void docount() { rcount++; }  Callback function • void Routine(RTNrtn, void *v) • { • RTN_InsertCall(rtn, IPOINT_BEFORE, • (AFUNPTR)docount, IARG_END); • }  Installing Callback • intmain(intargc, char * argv[]) • { • PIN_Init(argc, argv); • RTN_AddInstrumentFunction(Routine, 0); • PIN_StartProgram(); // Never returns • return 0; • } Initializing &  Registering Instrumentation Pintools for DSLab.

  19. Image Instrumentation UINT64 rcount = 0; • void docount() { rcount++; }  Callback function • void ImageLoad(IMGimg, void *v) { • SEC sec= IMG_SecHead(img); • RTN rtn = SEC_RtnHead(sec); • RTN_InsertCall(rtn, IPOINT_AFTER, • (AFUNPTR)docount, IARG_END); • }  Installing Callback • intmain(intargc, char * argv[]) • { • PIN_Init(argc, argv); • IMG_AddInstrumentFunction(ImageLoad, 0); • PIN_StartProgram(); // Never returns • return 0; • } Initializing &  Registering Instrumentation Pintools for DSLab.

  20. References • Pin Download & Manual • http://software.intel.com/en-us/articles/pintool • Pin APIs • http://software.intel.com/sites/landingpage/pintool/docs/53271/Pin/html/group__API__REF.html • Pin Newsgroup • http://tech.groups.yahoo.com/groups/pinheads/ • Pin Tutorial Slide • http://www.cs.virginia.edu/kim/publicity/pin/tutorials/cgo10/cgo_2010_final.ppt Pintools for DSLab.

More Related