From d2b60baeee5aebd9db0b9d9415e5c2a56a62fae1 Mon Sep 17 00:00:00 2001 From: Sam Brown Date: Tue, 5 May 2015 19:36:44 +0100 Subject: [PATCH 1/3] fixed some bugs in the pintool --- pintool/pintool.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/pintool/pintool.cpp b/pintool/pintool.cpp index b671a68..749fc0d 100644 --- a/pintool/pintool.cpp +++ b/pintool/pintool.cpp @@ -18,6 +18,7 @@ #define FREE "free" #define REALLOC "realloc" #endif +#define MAIN "main" using namespace std; @@ -27,6 +28,7 @@ using namespace std; class Args; +bool Record = false; ofstream TraceFile; Args* args = NULL; @@ -64,16 +66,19 @@ Args::~Args() VOID BeforeMalloc(ADDRINT size) { + if(!Record) return; args->size = size; } VOID AfterMalloc(ADDRINT ret) { + if(!Record) return; TraceFile << "malloc(" << args->size << ") = " << ADDRINTToHexString(ret) << endl; } VOID Free(ADDRINT addr) { + if(!Record) return; string formatted_addr = ""; if(addr == 0){ formatted_addr = "0"; @@ -85,24 +90,35 @@ VOID Free(ADDRINT addr) VOID BeforeCalloc(ADDRINT num, ADDRINT size) { + if(!Record) return; args->num = num; args->size = size; } VOID AfterCalloc(ADDRINT ret) { - TraceFile << "calloc(" << args->num << "," << ADDRINTToHexString(args->size) +") = " + ADDRINTToHexString(ret) << endl; + if(!Record) return; + TraceFile << "calloc(" << args->num << ", " << ADDRINTToHexString(args->size) +") = " + ADDRINTToHexString(ret) << endl; } VOID BeforeRealloc(ADDRINT addr, ADDRINT size) { + if(!Record) return; args->addr = addr; args->size = size; } VOID AfterRealloc(ADDRINT ret) { - TraceFile << "realloc(" << ADDRINTToHexString(args->addr) << "," << args->size << ") = " << ADDRINTToHexString(ret) << endl; + if(!Record) return; + TraceFile << "realloc(" << ADDRINTToHexString(args->addr) << ", " << args->size << ") = " << ADDRINTToHexString(ret) << endl; +} + +VOID RecordMainBegin() { + Record = true; +} +VOID RecordMainEnd() { + Record = false; } /* ===================================================================== */ @@ -175,6 +191,16 @@ VOID Image(IMG img, VOID *v) RTN_Close(reallocRtn); } + + RTN mainRtn = RTN_FindByName(img, MAIN); + if (mainRtn.is_valid()) { + RTN_Open(mainRtn); + RTN_InsertCall(mainRtn, IPOINT_BEFORE, (AFUNPTR)RecordMainBegin, + IARG_END); + RTN_InsertCall(mainRtn, IPOINT_AFTER, (AFUNPTR)RecordMainEnd, + IARG_END); + RTN_Close(mainRtn); + } } /* ===================================================================== */ From 905fd4d0bc7f04157a39f417c34d036e6f34b356 Mon Sep 17 00:00:00 2001 From: Sam Brown Date: Wed, 6 May 2015 19:57:44 +0100 Subject: [PATCH 2/3] some tidy up, support for stripped binaries and support for OSX _main --- pintool/pintool.cpp | 106 +++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/pintool/pintool.cpp b/pintool/pintool.cpp index 749fc0d..a083aa6 100644 --- a/pintool/pintool.cpp +++ b/pintool/pintool.cpp @@ -8,17 +8,18 @@ /* Names of malloc and free */ /* ===================================================================== */ #if defined(TARGET_MAC) +#define MAIN "_main" #define CALLOC "_calloc" #define MALLOC "_malloc" #define FREE "_free" #define REALLOC "_realloc" #else +#define MAIN "main" #define CALLOC "calloc" #define MALLOC "malloc" #define FREE "free" #define REALLOC "realloc" #endif -#define MAIN "main" using namespace std; @@ -28,8 +29,8 @@ using namespace std; class Args; -bool Record = false; -ofstream TraceFile; +bool record = false; +ofstream trace_file; Args* args = NULL; @@ -66,59 +67,59 @@ Args::~Args() VOID BeforeMalloc(ADDRINT size) { - if(!Record) return; + if(!record) return; args->size = size; } VOID AfterMalloc(ADDRINT ret) { - if(!Record) return; - TraceFile << "malloc(" << args->size << ") = " << ADDRINTToHexString(ret) << endl; + if(!record) return; + trace_file << "malloc(" << args->size << ") = " << ADDRINTToHexString(ret) << endl; } VOID Free(ADDRINT addr) { - if(!Record) return; + if(!record) return; string formatted_addr = ""; if(addr == 0){ formatted_addr = "0"; } else { formatted_addr = ADDRINTToHexString(addr); } - TraceFile << "free(" + formatted_addr +") = " << endl; + trace_file << "free(" + formatted_addr +") = " << endl; } VOID BeforeCalloc(ADDRINT num, ADDRINT size) { - if(!Record) return; + if(!record) return; args->num = num; args->size = size; } VOID AfterCalloc(ADDRINT ret) { - if(!Record) return; - TraceFile << "calloc(" << args->num << ", " << ADDRINTToHexString(args->size) +") = " + ADDRINTToHexString(ret) << endl; + if(!record) return; + trace_file << "calloc(" << args->num << ", " << ADDRINTToHexString(args->size) +") = " + ADDRINTToHexString(ret) << endl; } VOID BeforeRealloc(ADDRINT addr, ADDRINT size) { - if(!Record) return; + if(!record) return; args->addr = addr; args->size = size; } VOID AfterRealloc(ADDRINT ret) { - if(!Record) return; - TraceFile << "realloc(" << ADDRINTToHexString(args->addr) << ", " << args->size << ") = " << ADDRINTToHexString(ret) << endl; + if(!record) return; + trace_file << "realloc(" << ADDRINTToHexString(args->addr) << ", " << args->size << ") = " << ADDRINTToHexString(ret) << endl; } VOID RecordMainBegin() { - Record = true; + record = true; } VOID RecordMainEnd() { - Record = false; + record = false; } /* ===================================================================== */ @@ -131,75 +132,78 @@ VOID Image(IMG img, VOID *v) // of each malloc() or free(), and the return value of malloc(). // // Find the malloc() function. - RTN mallocRtn = RTN_FindByName(img, MALLOC); - if (RTN_Valid(mallocRtn)) + RTN malloc_rtn = RTN_FindByName(img, MALLOC); + if (RTN_Valid(malloc_rtn)) { - RTN_Open(mallocRtn); + RTN_Open(malloc_rtn); // Instrument malloc() to print the input argument value and the return value. - RTN_InsertCall(mallocRtn, IPOINT_BEFORE, (AFUNPTR)BeforeMalloc, + RTN_InsertCall(malloc_rtn, IPOINT_BEFORE, (AFUNPTR)BeforeMalloc, IARG_FUNCARG_ENTRYPOINT_VALUE, 0, IARG_END); - RTN_InsertCall(mallocRtn, IPOINT_AFTER, (AFUNPTR)AfterMalloc, + RTN_InsertCall(malloc_rtn, IPOINT_AFTER, (AFUNPTR)AfterMalloc, IARG_FUNCRET_EXITPOINT_VALUE, IARG_END); - RTN_Close(mallocRtn); + RTN_Close(malloc_rtn); } // Find the free() function. - RTN freeRtn = RTN_FindByName(img, FREE); - if (RTN_Valid(freeRtn)) + RTN free_rtn = RTN_FindByName(img, FREE); + if (RTN_Valid(free_rtn)) { - RTN_Open(freeRtn); + RTN_Open(free_rtn); // Instrument free() to print the input argument value. - RTN_InsertCall(freeRtn, IPOINT_BEFORE, (AFUNPTR)Free, + RTN_InsertCall(free_rtn, IPOINT_BEFORE, (AFUNPTR)Free, IARG_FUNCARG_ENTRYPOINT_VALUE, 0, IARG_END); - RTN_Close(freeRtn); + RTN_Close(free_rtn); } //Find the calloc() function - RTN callocRtn = RTN_FindByName(img, CALLOC); - if (RTN_Valid(callocRtn)) + RTN calloc_rtn = RTN_FindByName(img, CALLOC); + if (RTN_Valid(calloc_rtn)) { - RTN_Open(callocRtn); + RTN_Open(calloc_rtn); - // Instrument callocRtn to print the input argument value and the return value. - RTN_InsertCall(callocRtn, IPOINT_BEFORE, (AFUNPTR)BeforeCalloc, + // Instrument calloc_rtn to print the input argument value and the return value. + RTN_InsertCall(calloc_rtn, IPOINT_BEFORE, (AFUNPTR)BeforeCalloc, IARG_FUNCARG_ENTRYPOINT_VALUE, 0, IARG_FUNCARG_ENTRYPOINT_VALUE, 1, IARG_END); - RTN_InsertCall(callocRtn, IPOINT_AFTER, (AFUNPTR)AfterCalloc, + RTN_InsertCall(calloc_rtn, IPOINT_AFTER, (AFUNPTR)AfterCalloc, IARG_FUNCRET_EXITPOINT_VALUE, IARG_END); - RTN_Close(callocRtn); + RTN_Close(calloc_rtn); } //Find the realloc() function - RTN reallocRtn = RTN_FindByName(img, REALLOC); - if (RTN_Valid(reallocRtn)) + RTN realloc_rtn = RTN_FindByName(img, REALLOC); + if (RTN_Valid(realloc_rtn)) { - RTN_Open(reallocRtn); + RTN_Open(realloc_rtn); // Instrument malloc() to print the input argument value and the return value. - RTN_InsertCall(reallocRtn, IPOINT_BEFORE, (AFUNPTR)BeforeRealloc, + RTN_InsertCall(realloc_rtn, IPOINT_BEFORE, (AFUNPTR)BeforeRealloc, IARG_FUNCARG_ENTRYPOINT_VALUE, 0, IARG_FUNCARG_ENTRYPOINT_VALUE, 1, IARG_END); - RTN_InsertCall(reallocRtn, IPOINT_AFTER, (AFUNPTR)AfterRealloc, + RTN_InsertCall(realloc_rtn, IPOINT_AFTER, (AFUNPTR)AfterRealloc, IARG_FUNCRET_EXITPOINT_VALUE, IARG_END); - RTN_Close(reallocRtn); + RTN_Close(realloc_rtn); } - RTN mainRtn = RTN_FindByName(img, MAIN); - if (mainRtn.is_valid()) { - RTN_Open(mainRtn); - RTN_InsertCall(mainRtn, IPOINT_BEFORE, (AFUNPTR)RecordMainBegin, - IARG_END); - RTN_InsertCall(mainRtn, IPOINT_AFTER, (AFUNPTR)RecordMainEnd, - IARG_END); - RTN_Close(mainRtn); + RTN main_rtn = RTN_FindByName(img, MAIN); + if (main_rtn.is_valid()) { + RTN_Open(main_rtn); + RTN_InsertCall(main_rtn, IPOINT_BEFORE, (AFUNPTR)RecordMainBegin, + IARG_END); + RTN_InsertCall(main_rtn, IPOINT_AFTER, (AFUNPTR)RecordMainEnd, + IARG_END); + RTN_Close(main_rtn); + } else { + //if the binary stripped then record everything + record = true; } } @@ -207,7 +211,7 @@ VOID Image(IMG img, VOID *v) VOID Fini(INT32 code, VOID *v) { - TraceFile.close(); + trace_file.close(); } /* ===================================================================== */ @@ -241,8 +245,8 @@ int main(int argc, char *argv[]) { return Usage(); } - TraceFile.open(KnobOutputFile.Value().c_str()); - // Write to a file since TraceFile and cerr maybe closed by the application + trace_file.open(KnobOutputFile.Value().c_str()); + // Write to a file since trace_file and cerr maybe closed by the application Args* initial = new Args(); args = initial; // Register Image to be called to instrument functions. From 8c0f8311e98317da411c64739c85ce552671d1ce Mon Sep 17 00:00:00 2001 From: Sam Brown Date: Thu, 17 Mar 2016 23:12:48 +0000 Subject: [PATCH 3/3] performance improvements --- villoc.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/villoc.py b/villoc.py index 67629a8..6d99a3e 100755 --- a/villoc.py +++ b/villoc.py @@ -220,18 +220,23 @@ def parse_ltrace(ltrace): for line in ltrace: + # if the trace file contains PID (for ltrace -f) + head, _, tail = line.partition(" ") + if head.isdigit(): + line = tail + if not any(line.startswith(f) for f in operations): continue try: func, args, ret = re.findall(match_call, line)[0] - except: + except Exception: try: - # maybe this stoped the program + # maybe this stopped the program func, args = re.findall(match_err, line)[0] ret = None - except: + except Exception: print("ignoring line: %s" % line, file=sys.stderr) continue @@ -284,12 +289,12 @@ def print_state(out, boundaries, state): known_stops = set() - todo = state + todo = {x.start():x for x in state} while todo: out.write('
\n') - done = [] + done = set() current = None last = 0 @@ -305,15 +310,13 @@ def print_state(out, boundaries, state): if current: # stops here. known_stops.add(i) current.gen_html(out, i - last) - done.append(current) + done.add(current) last = i current = None - for block in todo: - if block.start() == b: - current = block - break - else: + try: + current = todo[b] + except: continue if last != i: @@ -344,7 +347,7 @@ def print_state(out, boundaries, state): out.write('
\n') - todo = [x for x in todo if x not in done] + todo = {x.start():x for x in todo.values() if x not in done} out.write('
')