@@ -28,60 +28,60 @@ using namespace std;
2828 ******************************************************************************/
2929// Name of the function to profile
3030KNOB<string> knobFunctionName (KNOB_MODE_WRITEONCE, " pintool" , " func" , " main" ,
31- " Function to be profiled." );
31+ " Function to be profiled." );
3232
3333// Max log items per thread
3434KNOB<UINT64> knobMaxLog (KNOB_MODE_WRITEONCE, " pintool" , " lim" , " 1000000" ,
35- " Max number of read/writes to log per thread." );
35+ " Max number of read/writes to log per thread." );
3636
3737// Output file name
3838KNOB<string> knobOutFile (KNOB_MODE_WRITEONCE, " pintool" , " out" , " mem_trace.csv" ,
39- " Output file name." );
39+ " Output file name." );
4040
4141// Max threads
4242KNOB<UINT64> knobMaxThreads (KNOB_MODE_WRITEONCE, " pintool" , " threads" , " 10000" ,
43- " Upper limit of the number of threads that can be used by the program \
44- being profiled." );
43+ " Upper limit of the number of threads that can be used by the program \
44+ being profiled." );
4545
4646// Stack based access logging (1: enable, 0: disable)
4747KNOB<bool > knobStack (KNOB_MODE_WRITEONCE, " pintool" , " stack" , " 0" , " Stack based access logging \
48- [1: enable, 0: disable (default)]." );
48+ [1: enable, 0: disable (default)]." );
4949
5050// Instruction pointer relative access logging (1: enable, 0: disable)
5151KNOB<bool > knobIP (KNOB_MODE_WRITEONCE, " pintool" , " ip" , " 1" , " IP relative access logging \
52- [1: enable (default), 0: disable]." );
52+ [1: enable (default), 0: disable]." );
5353
5454// Read logging (1: enable, 0: disable)
5555KNOB<bool > knobRead (KNOB_MODE_WRITEONCE, " pintool" , " read" , " 1" , " Read logging \
56- [1: enable (default), 0: disable]." );
56+ [1: enable (default), 0: disable]." );
5757
5858// Write logging (1: enable, 0: disable)
5959KNOB<bool > knobWrite (KNOB_MODE_WRITEONCE, " pintool" , " write" , " 1" , " Write \
60- logging [1: enable (default), 0: disable]." );
60+ logging [1: enable (default), 0: disable]." );
6161
6262
6363
6464/* ******************************************************************************
6565 * Structs
6666 ******************************************************************************/
67- #define LINE_SIZE 64
67+ #define LINE_SIZE 64
6868
6969// Structure for keeping thread specific data. Padded to LINE_SIZE for avoiding
7070// false sharing.
7171typedef struct {
72- // Tracks if the thread is inside the requested functions. Greater than 0
73- // means it is.
72+ // Tracks if the thread is inside the requested functions. Greater than 0
73+ // means it is.
7474 UINT64 rtnEntryCnt __attribute ((aligned(LINE_SIZE)));
7575} ThreadData;
7676
7777
7878// Keeps information for each memory access. Padded to LINE_SIZE for avoiding
7979// false sharing.
8080typedef struct {
81- // Effective virtual address
81+ // Effective virtual address
8282 ADDRINT ea __attribute ((aligned(LINE_SIZE)));
8383
84- // Type of access. 'R' for read and 'W' for write.
84+ // Type of access. 'R' for read and 'W' for write.
8585 UINT8 type;
8686} MemInfo;
8787
@@ -133,23 +133,23 @@ INT32 Usage() {
133133 * Records a read or write access to 'info' array.
134134 */
135135inline VOID record (THREADID tid, ADDRINT ea, UINT8 type){
136- // First check if thread is inside the function beign profiled.
136+ // First check if thread is inside the function beign profiled.
137137 UINT64 entCnt = tdata[tid].rtnEntryCnt ;
138138 if (entCnt > 0 ){
139- // Inside the function. Atomically update the array index.
139+ // Inside the function. Atomically update the array index.
140140 UINT64 idx;
141141 PIN_GetLock (&lock, tid + 1 );
142142 idx = buf_log_cnt++;
143143 PIN_ReleaseLock (&lock);
144144
145- // Check if log limit reached.
145+ // Check if log limit reached.
146146 if (idx < buf_log_lim){
147- // Record entry.
147+ // Record entry.
148148 info[idx].ea = ea;
149149 info[idx].type = type;
150150 } else {
151- // Log limit reached. Exit.
152- // Intenally calls FINI function before quitting.
151+ // Log limit reached. Exit.
152+ // Intenally calls FINI function before quitting.
153153 PIN_ExitApplication (0 );
154154 }
155155 }
@@ -176,33 +176,33 @@ VOID RecordMemWrite(THREADID tid, ADDRINT ea) {
176176 * Instruments instructions having read or write accesses.
177177 */
178178VOID Instruction (INS ins, VOID *v){
179- if (!knobStack.Value ()){
180- if (INS_IsStackRead (ins) || INS_IsStackWrite (ins)){
181- return ;
182- }
183- }
184-
185- if (!knobIP.Value ()){
186- if (INS_IsIpRelRead (ins) || INS_IsIpRelWrite (ins)){
187- return ;
188- }
189- }
190-
191- // Get the memory operand count of the current instruction.
192- UINT32 memOperands = INS_MemoryOperandCount (ins);
193-
194- // Iterate over each memory operand of the instruction.
195- for (UINT32 memOp = 0 ; memOp < memOperands; memOp++) {
196- if (INS_MemoryOperandIsRead (ins, memOp) && read_log_en) {
197- // Operand is read by this instruction.
198- INS_InsertPredicatedCall (ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead, IARG_THREAD_ID, IARG_MEMORYOP_EA, memOp, IARG_END);
199- }
200-
201- else if (INS_MemoryOperandIsWritten (ins, memOp) && write_log_en) {
202- // Operand is written by this instruction.
203- INS_InsertPredicatedCall (ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite, IARG_THREAD_ID, IARG_MEMORYOP_EA, memOp, IARG_END);
204- }
205- }
179+ if (!knobStack.Value ()){
180+ if (INS_IsStackRead (ins) || INS_IsStackWrite (ins)){
181+ return ;
182+ }
183+ }
184+
185+ if (!knobIP.Value ()){
186+ if (INS_IsIpRelRead (ins) || INS_IsIpRelWrite (ins)){
187+ return ;
188+ }
189+ }
190+
191+ // Get the memory operand count of the current instruction.
192+ UINT32 memOperands = INS_MemoryOperandCount (ins);
193+
194+ // Iterate over each memory operand of the instruction.
195+ for (UINT32 memOp = 0 ; memOp < memOperands; memOp++) {
196+ if (INS_MemoryOperandIsRead (ins, memOp) && read_log_en) {
197+ // Operand is read by this instruction.
198+ INS_InsertPredicatedCall (ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead, IARG_THREAD_ID, IARG_MEMORYOP_EA, memOp, IARG_END);
199+ }
200+
201+ else if (INS_MemoryOperandIsWritten (ins, memOp) && write_log_en) {
202+ // Operand is written by this instruction.
203+ INS_InsertPredicatedCall (ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite, IARG_THREAD_ID, IARG_MEMORYOP_EA, memOp, IARG_END);
204+ }
205+ }
206206}
207207
208208
@@ -229,98 +229,98 @@ VOID ImgCallback(IMG img, VOID* arg){
229229 if (!IMG_IsMainExecutable (img))
230230 return ;
231231
232- int match_count = 0 ;
233- cout << " Tagging functions with \" " << rtn_name << " \" ..." << endl;
232+ int match_count = 0 ;
233+ cout << " Tagging functions with \" " << rtn_name << " \" ..." << endl;
234234
235- // First try for exact match of function
236- for ( SEC sec= IMG_SecHead (img); SEC_Valid (sec); sec = SEC_Next (sec)){
235+ // First try for exact match of function
236+ for ( SEC sec= IMG_SecHead (img); SEC_Valid (sec); sec = SEC_Next (sec)){
237237 if (SEC_Name (sec) == " .text" ){
238238 for (RTN rtn= SEC_RtnHead (sec); RTN_Valid (rtn); rtn = RTN_Next (rtn)){
239239 string name = PIN_UndecorateSymbolName (RTN_Name (rtn), UNDECORATION_NAME_ONLY);
240- // add suffix for openmp functions
241- string rtn_name_omp = rtn_name + " ._omp_fn." ;
240+ // add suffix for openmp functions
241+ string rtn_name_omp = rtn_name + " ._omp_fn." ;
242242
243- // Try exact name match
243+ // Try exact name match
244244 if ((name == rtn_name) || (name.find (rtn_name_omp) != string::npos)){
245- // Match found!
246- match_count++;
247- cout << " Tagged function \" " << name << " \" " << endl;
245+ // Match found!
246+ match_count++;
247+ cout << " Tagged function \" " << name << " \" " << endl;
248248
249- // Instrument function entry and exit
250- RTN_Open (rtn);
249+ // Instrument function entry and exit
250+ RTN_Open (rtn);
251251 RTN_InsertCall (rtn, IPOINT_BEFORE, (AFUNPTR)RtnEntry, IARG_THREAD_ID, IARG_END);
252252 RTN_InsertCall (rtn, IPOINT_AFTER, (AFUNPTR)RtnLeave, IARG_THREAD_ID, IARG_END);
253- RTN_Close (rtn);
254- }
253+ RTN_Close (rtn);
254+ }
255255 }
256256 }
257257 }
258- if (match_count) return ;
259-
260- // Exact match not found. Try to find a function containing the given function name.
261- cout << " Exact match not found! Tagging all functions containing \" " << rtn_name << " \" ..." << endl;
262- for ( SEC sec= IMG_SecHead (img); SEC_Valid (sec); sec = SEC_Next (sec)){
258+ if (match_count) return ;
259+
260+ // Exact match not found. Try to find a function containing the given function name.
261+ cout << " Exact match not found! Tagging all functions containing \" " << rtn_name << " \" ..." << endl;
262+ for ( SEC sec= IMG_SecHead (img); SEC_Valid (sec); sec = SEC_Next (sec)){
263263 if (SEC_Name (sec) == " .text" ){
264264 for (RTN rtn= SEC_RtnHead (sec); RTN_Valid (rtn); rtn = RTN_Next (rtn)){
265265 string name = PIN_UndecorateSymbolName (RTN_Name (rtn), UNDECORATION_NAME_ONLY);
266266
267- // Check if the current routine contains the requested routine name
267+ // Check if the current routine contains the requested routine name
268268 if (name.find (rtn_name) != string::npos){
269- // Match found!
270- match_count++;
271- cout << " Tagged function \" " << name << " \" " << endl;
269+ // Match found!
270+ match_count++;
271+ cout << " Tagged function \" " << name << " \" " << endl;
272272
273- // Instrument function entry and exit
274- RTN_Open (rtn);
273+ // Instrument function entry and exit
274+ RTN_Open (rtn);
275275 RTN_InsertCall (rtn, IPOINT_BEFORE, (AFUNPTR)RtnEntry, IARG_THREAD_ID, IARG_END);
276276 RTN_InsertCall (rtn, IPOINT_AFTER, (AFUNPTR)RtnLeave, IARG_THREAD_ID, IARG_END);
277- RTN_Close (rtn);
278- }
277+ RTN_Close (rtn);
278+ }
279279 }
280280 }
281281 }
282282
283- // Not found
284- if (!match_count){
285- cout << " Unable to find any function containing \" " << rtn_name << " \" ... Quitting..." << endl;
286- PIN_ExitProcess (11 );
287- }
283+ // Not found
284+ if (!match_count){
285+ cout << " Unable to find any function containing \" " << rtn_name << " \" ... Quitting..." << endl;
286+ PIN_ExitProcess (11 );
287+ }
288288}
289289
290290
291291/* *
292292 * This function is called when exiting Pin. Writes the entries into a log file.
293293 */
294294VOID Fini (INT32 code, VOID *v) {
295- // Open log file.
295+ // Open log file.
296296 string out_file = knobOutFile.Value ();
297297 ofstream log (out_file.c_str ());
298298 if (!log.is_open ()) {
299299 cerr << " Cannot open log file:" << out_file << endl;
300- PIN_ExitProcess (11 );
300+ PIN_ExitProcess (11 );
301+ }
302+ else {
303+ cout << " Writing trace to " << out_file << " ... " ;
301304 }
302- else {
303- cout << " Writing trace to " << out_file << " ... " ;
304- }
305305
306306 // Write headers for csv files
307307 log << " R0_W1,Addr\n " ;
308308
309- // write log
309+ // write log
310310 if (buf_log_cnt > buf_log_lim){
311311 buf_log_cnt = buf_log_lim;
312312 }
313313
314314 for (UINT64 i=0 ; i < buf_log_cnt; i++){
315- log << info[i].type << " ," << info[i].ea << " \n " ;
315+ log << info[i].type << " ," << info[i].ea << " \n " ;
316316 }
317317
318- // cleanup tasks
318+ // cleanup tasks
319319 log.close ();
320320 delete [] info;
321- delete [] tdata;
321+ delete [] tdata;
322322
323- cout << " Done" << endl;
323+ cout << " Done" << endl;
324324}
325325
326326
@@ -332,20 +332,20 @@ int main(int argc, char *argv[]) {
332332 return Usage ();
333333 }
334334
335- // Initializations
335+ // Initializations
336336 PIN_InitLock (&lock);
337337 PIN_InitSymbolsAlt (IFUNC_SYMBOLS);
338338
339339 buf_log_lim = knobMaxLog.Value ();
340340 info = new MemInfo[buf_log_lim];
341- UINT64 max_threads = knobMaxThreads.Value ();
342- tdata = new ThreadData[max_threads];
343- for (UINT64 i = 0 ; i < max_threads; ++i){
344- tdata[i].rtnEntryCnt = 0 ;
345- }
341+ UINT64 max_threads = knobMaxThreads.Value ();
342+ tdata = new ThreadData[max_threads];
343+ for (UINT64 i = 0 ; i < max_threads; ++i){
344+ tdata[i].rtnEntryCnt = 0 ;
345+ }
346346 rtn_name = knobFunctionName.Value ();
347- read_log_en = knobRead.Value ();
348- write_log_en = knobWrite.Value ();
347+ read_log_en = knobRead.Value ();
348+ write_log_en = knobWrite.Value ();
349349
350350 IMG_AddInstrumentFunction (ImgCallback, NULL );
351351 INS_AddInstrumentFunction (Instruction, NULL );
0 commit comments