@@ -49,7 +49,9 @@ static std::string s_targetName;
49
49
static std::map<std::string, FileCodeCoverage> s_fileCodeCoverageMap;
50
50
static std::map<std::string, std::string> s_funcFileMap;
51
51
52
- static void initializeCovorage (IMG img, void *v)
52
+ static std::map<ADDRINT, std::string> s_addrFuncNameMap;
53
+
54
+ static void ImageLoad (IMG img, void *v)
53
55
{
54
56
if (!IMG_Valid (img))
55
57
{
@@ -111,6 +113,7 @@ static void initializeCovorage(IMG img, void *v)
111
113
ifs.close ();
112
114
113
115
s_fileCodeCoverageMap[filePath] = fileCodeCoverage;
116
+
114
117
}
115
118
116
119
FuncCodeCoverage funcCodeCoverage;
@@ -121,6 +124,8 @@ static void initializeCovorage(IMG img, void *v)
121
124
addr = INS_Address (ins);
122
125
PIN_GetSourceLocation (addr, &col, &line, &filePath);
123
126
127
+ s_addrFuncNameMap[addr] = funcName;
128
+
124
129
// set executable line
125
130
// note that line number start from 1
126
131
s_fileCodeCoverageMap[filePath].Lines [line - 1 ].Executable = true ;
@@ -144,20 +149,13 @@ static void initializeCovorage(IMG img, void *v)
144
149
return ;
145
150
}
146
151
147
- VOID updateCoverage (INS ins, VOID* v )
152
+ static VOID updateCoverage (ADDRINT addr )
148
153
{
149
- ADDRINT addr = INS_Address (ins);
150
- std::string funcName = RTN_FindNameByAddress (addr);
151
- if (funcName == " " )
154
+ if (s_addrFuncNameMap.find (addr) == s_addrFuncNameMap.end ())
152
155
{
153
156
return ;
154
157
}
155
-
156
- if (s_funcFileMap.find (funcName) == s_funcFileMap.end ())
157
- {
158
- return ;
159
- }
160
-
158
+ std::string funcName = s_addrFuncNameMap[addr];
161
159
std::string filePath = s_funcFileMap[funcName];
162
160
if (s_fileCodeCoverageMap[filePath].FuncCodeCoverageMap .find (funcName) == s_fileCodeCoverageMap[filePath].FuncCodeCoverageMap .end ())
163
161
{
@@ -558,7 +556,35 @@ void generateAsmHtml(std::string asmReportFilePath, std::string filePath, const
558
556
asmHtml.close ();
559
557
}
560
558
561
- VOID generateCoverageReport (INT32 code, VOID* v)
559
+ static VOID Instruction (INS ins, VOID *v)
560
+ {
561
+ ADDRINT addr = INS_Address (ins);
562
+ std::string funcName = RTN_FindNameByAddress (addr);
563
+ if (funcName == " " )
564
+ {
565
+ return ;
566
+ }
567
+
568
+ if (s_funcFileMap.find (funcName) == s_funcFileMap.end ())
569
+ {
570
+ return ;
571
+ }
572
+
573
+ std::string filePath = s_funcFileMap[funcName];
574
+ if (s_fileCodeCoverageMap[filePath].FuncCodeCoverageMap .find (funcName) == s_fileCodeCoverageMap[filePath].FuncCodeCoverageMap .end ())
575
+ {
576
+ return ;
577
+ }
578
+
579
+ if (s_fileCodeCoverageMap[filePath].FuncCodeCoverageMap [funcName].AddrLineMap .find (addr) == s_fileCodeCoverageMap[filePath].FuncCodeCoverageMap [funcName].AddrLineMap .end ())
580
+ {
581
+ return ;
582
+ }
583
+
584
+ INS_InsertCall (ins, IPOINT_BEFORE, (AFUNPTR)updateCoverage, IARG_ADDRINT, addr, IARG_END);
585
+ }
586
+
587
+ VOID Fini (INT32 code, VOID* v)
562
588
{
563
589
std::cout << " [CodeCoverage] Program trace Finished, generating Coverage report..." << std::endl;
564
590
@@ -596,9 +622,9 @@ int main(int argc, char **argv)
596
622
std::exit (EXIT_FAILURE);
597
623
}
598
624
599
- IMG_AddInstrumentFunction (initializeCovorage, NULL );
600
- INS_AddInstrumentFunction (updateCoverage, NULL );
601
- PIN_AddFiniFunction (generateCoverageReport , 0 );
625
+ IMG_AddInstrumentFunction (ImageLoad, 0 );
626
+ INS_AddInstrumentFunction (Instruction, 0 );
627
+ PIN_AddFiniFunction (Fini , 0 );
602
628
603
629
std::cout << " [CodeCoverage] Program trace Start" << std::endl;
604
630
0 commit comments