annotate examples/inject_redir.py @ 135:b488ca519709

Make sure elements are absolute incremental for sorted arrays when injecting. - All sorted array of DEXFile must be absolute incremental. - If injected one is the same order as one already in the array, the one in array are used to replace injected one. - All references to clone and injected one refer to ones already in array instead.
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 09 Aug 2011 21:28:13 +0800
parents 044bfc415577
children
rev   line source
119
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
1 ## \file
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
2 #
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
3 # This is an example of inject a class from source to destinate DEX, and
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
4 # rewrite all references of a specified class with the injected one and its
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
5 # methods. All calls of specified class will be redirected to the
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
6 # injected class.
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
7 #
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
8 from paraspace.dexfile import DEXFile, DEXFile_linked
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
9 from paraspace.dex_deptracker import prepare_dep_decls
131
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
10 from paraspace.injection import inject_redir
119
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
11 import sys
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
12
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
13 if len(sys.argv) != 6:
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
14 print >> sys.stderr, \
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
15 'usage: %s <src dex> <inject class> ' \
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
16 '<dst dex> <redirect class> <output>' % \
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
17 (sys.argv[0])
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
18 sys.exit(1)
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
19 pass
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
20
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
21
131
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
22 if __name__ == '__main__':
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
23 src_dexname = sys.argv[1]
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
24 inj_classname = sys.argv[2]
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
25 dst_dexname = sys.argv[3]
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
26 redir_classname = sys.argv[4]
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
27 output_name = sys.argv[5]
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
28
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
29 decls = prepare_dep_decls()
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
30
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
31 src_dex = DEXFile.open(src_dexname)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
32 src_linked = DEXFile_linked.build_dependencies(src_dex, decls)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
33
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
34 dst_dex = DEXFile.open(dst_dexname)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
35 dst_linked = DEXFile_linked.build_dependencies(dst_dex, decls)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
36
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
37 inject_redir(src_linked, inj_classname,
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
38 dst_linked, redir_classname, decls)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
39
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
40 restore_raw = dst_linked.to_str()
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
41
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
42 ofile = open(output_name, 'w')
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
43 ofile.write(restore_raw)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
44 ofile.close()
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 128
diff changeset
45 pass