diff options
| author | Ajax <commial@gmail.com> | 2017-02-06 17:47:42 +0100 |
|---|---|---|
| committer | Ajax <commial@gmail.com> | 2017-02-06 18:26:46 +0100 |
| commit | 4773ce90e56d561b37bd4087d560e9ee84869abe (patch) | |
| tree | 350821d91d3c58c7128762e62defaeac8665b2ed /example/jitter/trace.py | |
| parent | 8cad9d68fce39cf98073c8306236fc9de105260b (diff) | |
| download | miasm-4773ce90e56d561b37bd4087d560e9ee84869abe.tar.gz miasm-4773ce90e56d561b37bd4087d560e9ee84869abe.zip | |
Add a tracer example
Diffstat (limited to 'example/jitter/trace.py')
| -rw-r--r-- | example/jitter/trace.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/example/jitter/trace.py b/example/jitter/trace.py new file mode 100644 index 00000000..aeb4c775 --- /dev/null +++ b/example/jitter/trace.py @@ -0,0 +1,58 @@ +""" +This example demonstrates two instrumentation possibility: + - instrumentation executed at each instruction + - instrumentation on jitter behavior (here, memory tracking) + +Note: for better performance, one can also extend Codegen to produce +instrumentation at the C / LLVM level +""" +import os +import time +from pdb import pm +from miasm2.analysis.sandbox import Sandbox_Linux_arml +from miasm2.jitter.emulatedsymbexec import EmulatedSymbExec +from miasm2.jitter.jitcore_python import JitCore_Python + +# Function called at each instruction +instr_count = 0 +def instr_hook(jitter): + global instr_count + instr_count += 1 + return True + +# Extension of the Python jitter to track memory accesses +class ESETrackMemory(EmulatedSymbExec): + """Emulated symb exec with memory access tracking""" + + def _func_read(self, expr_mem): + value = super(ESETrackMemory, self)._func_read(expr_mem) + print "Read %s: %s" % (expr_mem, value) + return value + + def _func_write(self, symb_exec, dest, data): + print "Write %s: %s" % (dest, data) + return super(ESETrackMemory, self)._func_write(symb_exec, dest, data) + +# Parse arguments +parser = Sandbox_Linux_arml.parser(description="Tracer") +parser.add_argument("filename", help="ELF Filename") +options = parser.parse_args() + +# Use our memory tracker +JitCore_Python.SymbExecClass = ESETrackMemory + +# Create sandbox, forcing Python jitter +options.jitter = "python" +sb = Sandbox_Linux_arml(options.filename, options, globals()) + +# Force jit one instr per call, and register our callback +sb.jitter.jit.set_options(jit_maxline=1, max_exec_per_call=1) +sb.jitter.exec_cb = instr_hook + +# Run +start_time = time.time() +sb.run() +stop_time = time.time() + +assert sb.jitter.run is False +print "Instr speed: %02.f / sec" % (instr_count / (stop_time - start_time)) |