CCTLib
Calling-context and data-object attribution library for Intel Pin
cct_data_centric_client_tree_based.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 /// links with a different file #define USE_TREE_BASED_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 (balanced-binary-tree technique).\n" + KNOB_BASE::StringKnobSummary() + "\n");
24  return -1;
25 }
26 
27 
28 // Main for DeadSpy, initialize the tool, register instrumentation functions and call the target program.
29 FILE* gTraceFile;
30 
31 
32 // Initialized the needed data structures before launching the target program
33 void ClientInit(int argc, char* argv[]) {
34  // Create output file
35  char name[MAX_FILE_PATH] = "client.out.";
36  char* envPath = getenv("CCTLIB_CLIENT_OUTPUT_FILE");
37 
38  if (envPath) {
39  // assumes max of MAX_FILE_PATH
40  snprintf(name, sizeof(name), "%s", envPath);
41  }
42 
43  gethostname(name + strlen(name), MAX_FILE_PATH - strlen(name));
44  pid_t pid = getpid();
45  sprintf(name + strlen(name), "%d", pid);
46  cerr << "\n Creating log file at:" << name << "\n";
47  gTraceFile = fopen(name, "w");
48  // print the arguments passed
49  fprintf(gTraceFile, "\n");
50 
51  for (int i = 0; i < argc; i++) {
52  fprintf(gTraceFile, "%s ", argv[i]);
53  }
54 
55  fprintf(gTraceFile, "\n");
56 }
57 
58 VOID SimpleCCTQuery(THREADID id, const uint32_t slot) {
59  GetContextHandle(id, slot);
60 }
61 
62 VOID MemAnalysisRoutine(void* addr, THREADID threadId) {
63  GetDataObjectHandle(addr, threadId);
64 }
65 
66 VOID InstrumentInsCallback(INS ins, VOID* v, const uint32_t slot) {
67  //if (INS_IsMemoryRead (ins) || INS_IsMemoryWrite(ins))
68  INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)SimpleCCTQuery, IARG_THREAD_ID, IARG_UINT32, slot, IARG_END);
69 
70  // Data centric for mem inst
71  // Skip call, ret and JMP instructions
72  if (INS_IsControlFlow(ins) || INS_IsRet(ins)) {
73  return;
74  }
75 
76  // skip stack ... actually our code handles it
77  if (INS_IsStackRead(ins) || INS_IsStackWrite(ins))
78  return;
79 
80  if (INS_IsMemoryRead(ins) || INS_IsMemoryWrite(ins)) {
81  // How may memory operations?
82  UINT32 memOperands = INS_MemoryOperandCount(ins);
83 
84  // Iterate over each memory operand of the instruction and add Analysis routine
85  for (UINT32 memOp = 0; memOp < memOperands; memOp++) {
86  INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)MemAnalysisRoutine, IARG_MEMORYOP_EA, memOp, IARG_THREAD_ID, IARG_END);
87  }
88  }
89 }
90 
91 int main(int argc, char* argv[]) {
92  // Initialize PIN
93  if (PIN_Init(argc, argv))
94  return Usage2();
95 
96  // Initialize Symbols, we need them to report functions and lines
97  PIN_InitSymbols();
98  // Init Client
99  ClientInit(argc, argv);
100  // Intialize CCTLib
101  PinCCTLibInit(INTERESTING_INS_ALL, gTraceFile, InstrumentInsCallback, nullptr, /*doDataCentric=*/true);
102  // Launch program now
103  PIN_StartProgram();
104  return 0;
105 }
int main(int argc, char *argv[])
void ClientInit(int argc, char *argv[])
VOID MemAnalysisRoutine(void *addr, THREADID threadId)
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