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