Skip to content

Commit

Permalink
Merge pull request #17 from Tyilo/patch-1
Browse files Browse the repository at this point in the history
Get args from manpages for auto-generated tracer
  • Loading branch information
oleavr committed Mar 2, 2015
2 parents ef8145c + a0bb52f commit daf1a31
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/frida/tracer.py
Expand Up @@ -5,6 +5,7 @@
import time import time
import re import re
import binascii import binascii
import subprocess


from frida.core import ModuleFunction from frida.core import ModuleFunction


Expand Down Expand Up @@ -308,6 +309,31 @@ def _notify_update(self, function, handler, source):
self._on_update_callback(function, handler, source) self._on_update_callback(function, handler, source)


def _create_stub_handler(self, function): def _create_stub_handler(self, function):
args = ""
argc = 0
varargs = False
try:
with open(os.devnull, 'w') as devnull:
output = subprocess.check_output(["man", "-P", "col -b", "2", function.name], stderr=devnull)
match = re.search(r"^SYNOPSIS(?:.|\n)*?((?:^.+$\n)* {5}" + function.name + r"\(.*\n(^.+$\n)*)(?:.|\n)*^DESCRIPTION", output.decode(), re.MULTILINE)
if match:
decl = match.group(1)
for argm in re.finditer(r"([^* ]*)\s*(,|\))", decl):
arg = argm.group(1)
if arg == '...':
args += '+ ", ..."'
varargs = True
continue

args += '%(pre)s%(arg)s=" + args[%(argc)s]' % {"arg": arg, "argc": argc, "pre": '"' if argc == 0 else '+ ", '}
argc += 1

except (subprocess.CalledProcessError, OSError): # WindowError or FileNotFoundError
pass

if args == "":
args = '""'

return """\ return """\
/* /*
* Auto-generated by Frida. Please modify to match the signature of %(name)s. * Auto-generated by Frida. Please modify to match the signature of %(name)s.
Expand All @@ -332,7 +358,7 @@ def _create_stub_handler(self, function):
* use "this" which is an object for keeping state local to an invocation. * use "this" which is an object for keeping state local to an invocation.
*/ */
onEnter: function onEnter(log, args, state) { onEnter: function onEnter(log, args, state) {
log("%(name)s()"); log("%(name)s(" + %(args)s + ")");
}, },
/** /**
Expand All @@ -348,7 +374,7 @@ def _create_stub_handler(self, function):
onLeave: function onLeave(log, retval, state) { onLeave: function onLeave(log, retval, state) {
} }
} }
""" % { 'name': function.name } """ % { "name": function.name, "args": args }


class MemoryRepository(Repository): class MemoryRepository(Repository):
def __init__(self): def __init__(self):
Expand Down

0 comments on commit daf1a31

Please sign in to comment.