Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed some bugs in the pintool #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 62 additions & 32 deletions pintool/pintool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
/* 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"
Expand All @@ -27,7 +29,8 @@ using namespace std;

class Args;

ofstream TraceFile;
bool record = false;
ofstream trace_file;

Args* args = NULL;

Expand Down Expand Up @@ -64,45 +67,59 @@ Args::~Args()

VOID BeforeMalloc(ADDRINT size)
{
if(!record) return;
args->size = size;
}

VOID AfterMalloc(ADDRINT ret)
{
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;
string formatted_addr = "";
if(addr == 0){
formatted_addr = "0";
} else {
formatted_addr = ADDRINTToHexString(addr);
}
TraceFile << "free(" + formatted_addr +") = <void>" << endl;
trace_file << "free(" + formatted_addr +") = <void>" << endl;
}

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;
trace_file << "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;
trace_file << "realloc(" << ADDRINTToHexString(args->addr) << ", " << args->size << ") = " << ADDRINTToHexString(ret) << endl;
}

VOID RecordMainBegin() {
record = true;
}
VOID RecordMainEnd() {
record = false;
}

/* ===================================================================== */
Expand All @@ -115,73 +132,86 @@ 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 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;
}
}

/* ===================================================================== */

VOID Fini(INT32 code, VOID *v)
{
TraceFile.close();
trace_file.close();
}

/* ===================================================================== */
Expand Down Expand Up @@ -215,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.
Expand Down
27 changes: 15 additions & 12 deletions villoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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('<div class="line" style="">\n')

done = []
done = set()

current = None
last = 0
Expand All @@ -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:
Expand Down Expand Up @@ -344,7 +347,7 @@ def print_state(out, boundaries, state):

out.write('</div>\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('<div class="log">')

Expand Down