-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.cpp
113 lines (95 loc) · 2.22 KB
/
part1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<stdio.h>
#include "pin.H"
#include<string.h>
FILE *trace;
PIN_LOCK pinLock;
VOID RecordMemRead(VOID *ip, VOID *addr_temp, UINT32 op_size, THREADID tid)
{
PIN_GetLock(&pinLock, tid+1);
unsigned long long addr = (unsigned long long)addr_temp;
unsigned long long block_capacity=64 - addr%64;
while(op_size>0)
{
while(block_capacity >=8 && op_size >=8)
{
fprintf(trace,"%d %lld\n", tid, addr);
addr = addr+8;
op_size = op_size-8;
block_capacity -= 8;
}
while(block_capacity >=4 && op_size >=4)
{
fprintf(trace,"%d %lld\n", tid, addr);
addr = addr+4;
op_size = op_size-4;
block_capacity -= 4;
}
while(block_capacity >=2 && op_size >=2)
{
fprintf(trace,"%d %lld\n", tid, addr);
addr = addr+2;
op_size = op_size-2;
block_capacity -= 2;
}
while(block_capacity >=1 && op_size >=1)
{
fprintf(trace,"%d %lld\n", tid, addr);
addr = addr+1;
op_size = op_size-1;
block_capacity -= 1;
}
if(op_size>0)
{
block_capacity+=64;
}
}
fflush(trace);
PIN_ReleaseLock(&pinLock);
}
VOID Instruction(INS ins, VOID *v)
{
UINT32 num_operands = INS_MemoryOperandCount(ins);
for(UINT32 operand=0; operand<num_operands; operand++)
{
UINT32 op_size = INS_MemoryOperandSize(ins, operand);
if(INS_MemoryOperandIsRead(ins, operand))
{
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead,
IARG_INST_PTR,
IARG_MEMORYOP_EA, operand,
IARG_UINT32, op_size,
IARG_THREAD_ID,
IARG_END);
}
if(INS_MemoryOperandIsWritten(ins, operand))
{
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead,
IARG_INST_PTR,
IARG_MEMORYOP_EA, operand,
IARG_UINT32, op_size,
IARG_THREAD_ID,
IARG_END);
}
}
}
VOID Fini(INT32 code, VOID *v)
{
fclose(trace);
}
INT32 Usage()
{
PIN_ERROR("This tool collects the thread wise memory access trace\n"
+ KNOB_BASE::StringKnobSummary() + "\n");
return -1;
}
int main(int argc, char *argv[])
{
trace=fopen("trace.out","w");
if (PIN_Init(argc, argv)) return Usage();
INS_AddInstrumentFunction(Instruction,0);
PIN_AddFiniFunction(Fini, 0);
PIN_StartProgram();
return 0;
}