CCTLib
Calling-context and data-object attribution library for Intel Pin
cct_client_mem_only.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 #include "cctlib.H"
16 using namespace std;
17 using namespace PinCCTLib;
18 
19 INT32 Usage2() {
20  PIN_ERROR("Pin tool to gather calling context on each load and store.\n" + KNOB_BASE::StringKnobSummary() + "\n");
21  return -1;
22 }
23 
24 // Main for DeadSpy, initialize the tool, register instrumentation functions and call the target program.
25 FILE* gTraceFile;
26 
27 // Initialized the needed data structures before launching the target program
28 void ClientInit(int argc, char* argv[]) {
29  // Create output file
30  char name[MAX_FILE_PATH] = "client.out.";
31  char* envPath = getenv("CCTLIB_CLIENT_OUTPUT_FILE");
32 
33  if (envPath) {
34  // assumes max of MAX_FILE_PATH
35  snprintf(name, sizeof(name), "%s", envPath);
36  }
37 
38  gethostname(name + strlen(name), MAX_FILE_PATH - strlen(name));
39  pid_t pid = getpid();
40  sprintf(name + strlen(name), "%d", pid);
41  cerr << "\n Creating log file at:" << name << "\n";
42  gTraceFile = fopen(name, "w");
43  // print the arguments passed
44  fprintf(gTraceFile, "\n");
45 
46  for (int i = 0; i < argc; i++) {
47  fprintf(gTraceFile, "%s ", argv[i]);
48  }
49 
50  fprintf(gTraceFile, "\n");
51 }
52 
53 VOID SimpleCCTQuery(THREADID id, const uint32_t slot) {
54  GetContextHandle(id, slot);
55 }
56 
57 VOID InstrumentIns(INS ins, VOID* v) {
58  if (INS_IsMemoryRead(ins) || INS_IsMemoryWrite(ins))
59  INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)SimpleCCTQuery, IARG_THREAD_ID, IARG_END);
60 }
61 
62 
63 VOID InstrumentInsCallback(INS ins, VOID* v, const uint32_t slot) {
64  if (INS_IsMemoryRead(ins) || INS_IsMemoryWrite(ins))
65  INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)SimpleCCTQuery, IARG_THREAD_ID, IARG_UINT32, slot, IARG_END);
66 }
67 
68 int main(int argc, char* argv[]) {
69  // Initialize PIN
70  if (PIN_Init(argc, argv))
71  return Usage2();
72 
73  // Initialize Symbols, we need them to report functions and lines
74  PIN_InitSymbols();
75  // Init Client
76  ClientInit(argc, argv);
77  // Intialize CCTLib
79  // Launch program now
80  PIN_StartProgram();
81  return 0;
82 }
int main(int argc, char *argv[])
FILE * gTraceFile
void ClientInit(int argc, char *argv[])
VOID InstrumentIns(INS ins, VOID *v)
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_MEMORY_ACCESS
Definition: cctlib.H:87
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