Created
August 29, 2014 10:47
-
-
Save v0i0/6478e4f1fa39ce6f6be0 to your computer and use it in GitHub Desktop.
Trace assembly instructions via gdb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess as sp | |
import select | |
import sys | |
import time | |
executable = "a.out" | |
function = "call_the_wrapper_fn" | |
arguments = "" | |
gdb = sp.Popen(["gdb", executable], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT) | |
gdb_poll = select.poll() | |
gdb_poll.register(gdb.stdout, select.POLLIN) | |
def gdb_read(): | |
timeout = 0 | |
out = '' | |
while True: | |
res = gdb_poll.poll(0) | |
if res: | |
out += gdb.stdout.read(1) | |
timeout = 0 | |
elif timeout == 0: | |
time.sleep(0.01) | |
timeout = 1 | |
else: | |
break | |
return out | |
time.sleep(1) | |
gdb_read() | |
gdb.stdin.write('set disassembly-flavor intel\n') | |
gdb.stdin.write('break %s\n' % function) | |
gdb.stdin.write('run %s\n' % arguments) | |
time.sleep(1) | |
gdb_read() | |
gdb.stdin.write('display/i $pc\n') | |
out = gdb_read() | |
for line in out.split('\n'): | |
if line.startswith('=>'): | |
print line | |
depth = 0 | |
while depth >= 0: | |
gdb.stdin.write('stepi\n') | |
out = gdb_read() | |
for line in out.split('\n'): | |
if '=>' in line: | |
s = line.split() | |
instr = s[3] | |
if 'call' in instr: | |
depth += 1 | |
if 'ret' in instr: | |
depth -= 1 | |
print line | |
gdb.stdin.write('continue\n') | |
gdb_read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment