CCTLib
Calling-context and data-object attribution library for Intel Pin
cct_data_centric_client.cpp
Go to the documentation of this file.
1 // @COPYRIGHT@
2 // Licensed under MIT license.
3 // See LICENSE.TXT file in the project root for more information.
4 // ==============================================================
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <iostream>
10 #include <unistd.h>
11 #include <assert.h>
12 #include <string.h>
13 #include <sstream>
14 #include "pin.H"
15 // Enable data-centric
16 //
17 // default ... #define USE_SHADOW_FOR_DATA_CENTRIC
18 #include "cctlib.H"
19 using namespace std;
20 using namespace PinCCTLib;
21 
22 INT32 Usage2() {
23  PIN_ERROR("Pin tool to gather calling context on each instruction and associate each memory access to its data object (shadow memory technique).\n" + KNOB_BASE::StringKnobSummary() + "\n");
24  return -1;
25 }
26 
27 FILE* gTraceFile;
28 
29 
30 // Initialized the needed data structures before launching the target program
31 void ClientInit(int argc, char* argv[]) {
32  // Create output file
33  char name[MAX_FILE_PATH] = "client.out.";
34  char* envPath = getenv("CCTLIB_CLIENT_OUTPUT_FILE");
35 
36  if (envPath) {
37  // assumes max of MAX_FILE_PATH
38  snprintf(name, sizeof(name), "%s", envPath);
39  }
40 
41  gethostname(name + strlen(name), MAX_FILE_PATH - strlen(name));
42  pid_t pid = getpid();
43  sprintf(name + strlen(name), "%d", pid);
44  cerr << "\n Creating log file at:" << name << "\n";
45  gTraceFile = fopen(name, "w");
46  // print the arguments passed
47  fprintf(gTraceFile, "\n");
48 
49  for (int i = 0; i < argc; i++) {
50  fprintf(gTraceFile, "%s ", argv[i]);
51  }
52 
53  fprintf(gTraceFile, "\n");
54 }
55 
56 VOID SimpleCCTQuery(THREADID id, const uint32_t slot) {
57  GetContextHandle(id, slot);
58 }
59 
60 
61 VOID MemAnalysisRoutine(void* addr, THREADID threadId) {
62  GetDataObjectHandle(addr, threadId);
63 }
64 
65 VOID InstrumentInsCallback(INS ins, VOID* v, const uint32_t slot) {
66  INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)SimpleCCTQuery, IARG_THREAD_ID, IARG_UINT32, slot, IARG_END);
67 
68  // Data centric for mem inst
69  // Skip call, ret and JMP instructions
70  if (INS_IsControlFlow(ins) || INS_IsRet(ins)) {
71  return;
72  }
73 
74  // skip stack ... actually our code handles it
75  if (INS_IsStackRead(ins) || INS_IsStackWrite(ins))
76  return;
77 
78  if (INS_IsMemoryRead(ins) || INS_IsMemoryWrite(ins)) {
79  // How may memory operations?
80  UINT32 memOperands = INS_MemoryOperandCount(ins);
81 
82  // Iterate over each memory operand of the instruction and add Analysis routine
83  for (UINT32 memOp = 0; memOp < memOperands; memOp++) {
84  INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)MemAnalysisRoutine, IARG_MEMORYOP_EA, memOp, IARG_THREAD_ID, IARG_END);
85  }
86  }
87 }
88 
89 int main(int argc, char* argv[]) {
90  // Initialize PIN
91  if (PIN_Init(argc, argv))
92  return Usage2();
93 
94  // Initialize Symbols, we need them to report functions and lines
95  PIN_InitSymbols();
96  // Init Client
97  ClientInit(argc, argv);
98  // Intialize CCTLib
99  PinCCTLibInit(INTERESTING_INS_ALL, gTraceFile, InstrumentInsCallback, nullptr, /*doDataCentric=*/true);
100  // Launch program now
101  PIN_StartProgram();
102  return 0;
103 }
int main(int argc, char *argv[])
FILE * gTraceFile
void ClientInit(int argc, char *argv[])
VOID MemAnalysisRoutine(void *addr, THREADID threadId)
INT32 Usage2()
VOID SimpleCCTQuery(THREADID id, const uint32_t slot)
VOID InstrumentInsCallback(INS ins, VOID *v, const uint32_t slot)
#define MAX_FILE_PATH
Definition: cctlib.H:18
#define INTERESTING_INS_ALL
Definition: cctlib.H:85
DataHandle_t GetDataObjectHandle(VOID *address, const THREADID threadId)
Definition: cctlib.cpp:2495
int PinCCTLibInit(IsInterestingInsFptr isInterestingIns, FILE *logFile, CCTLibInstrumentInsCallback userCallback, VOID *userCallbackArg, BOOL doDataCentric=false)
Definition: cctlib.cpp:3132
ContextHandle_t GetContextHandle(const THREADID id, const uint32_t slot)
Definition: cctlib.cpp:1356