#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include "pin.H" #include <iostream> #include <fstream> using namespace std; INT32 Usage() { cerr << "This pin tool registers callbacks around fork().\n" "\n"; cerr << KNOB_BASE::StringKnobSummary(); cerr << endl; return -1; } pid_t parent_pid; PIN_LOCK lock; VOID BeforeFork(THREADID threadid, const CONTEXT* ctxt, VOID * arg) { PIN_GetLock(&lock, threadid+1); cerr << "TOOL: Before fork." << endl; PIN_ReleaseLock(&lock); parent_pid = PIN_GetPid(); } VOID AfterForkInParent(THREADID threadid, const CONTEXT* ctxt, VOID * arg) { PIN_GetLock(&lock, threadid+1); cerr << "TOOL: After fork in parent." << endl; PIN_ReleaseLock(&lock); if (PIN_GetPid() != parent_pid) { cerr << "PIN_GetPid() fails in parent process" << endl; exit(-1); } } VOID AfterForkInChild(THREADID threadid, const CONTEXT* ctxt, VOID * arg) { PIN_GetLock(&lock, threadid+1); cerr << "TOOL: After fork in child." << endl; PIN_ReleaseLock(&lock); if ((PIN_GetPid() == parent_pid) || (getppid() != parent_pid)) { cerr << "PIN_GetPid() fails in child process" << endl; exit(-1); } } int main(INT32 argc, CHAR **argv) { PIN_InitSymbols(); if( PIN_Init(argc,argv) ) { return Usage(); } // Initialize the pin lock PIN_InitLock(&lock); // Register a notification handler that is called when the application // forks a new process. PIN_AddForkFunction(FPOINT_BEFORE, BeforeFork, 0); PIN_AddForkFunction(FPOINT_AFTER_IN_PARENT, AfterForkInParent, 0); PIN_AddForkFunction(FPOINT_AFTER_IN_CHILD, AfterForkInChild, 0); // Never returns PIN_StartProgram(); return 0; }
fork라는 것은 유닉스 운영 체제에서 프로세스가 자기 자신을 복제하는 동작이다. 복제의 대상이 부모 프로세스, 복제의 결과가 자식 프로세스이다.
PIN_AddForkFunction은 JIT mode에서 사용되는 것이고, PIN_AddFrokFunctionProbed()는 Probed mode에서 사용되는 것이다. fork()가 실패하면 FPOINT_AFTER_IN_PARENT callback이 이루어진다.
'pintool' 카테고리의 다른 글
Managed platforms support2 (0) | 2018.10.02 |
---|---|
Managed platforms support1 (0) | 2018.10.02 |
Instrumenting Child Processes (0) | 2018.10.02 |
Replacing a Routine in Probe Mode (0) | 2018.10.02 |
Detaching Pin from the Application (0) | 2018.10.02 |