android - Smali best place to inject code -
i making python script add trace each method able runtime method call in logcat.
my application crashes time, don't ask me copy error log because not point of question. try inject code right after register declaration: .locals
the first time used .registers
directives got errors because of aliasing of local , parameters registers.
i thought use .locals
directive instead it's same.
the different test made following ones:
- if difference between local , parameters registers greater 2 use
v0
,v1
. - else incremented
.locals
directive 2 , usedv0
,v1
.
but keep getting vfy errors.
why .locals
equals 0 there parameters p0
example. p0
should aliased v0
.locals
0, why if change .locals
2 , use v0
, v1
still vfy?
i thinking add code before return directive @ least not matter if change local variable long not return variable
edit: @jesusfreke thank comments.
i trying improve python script suggestion. created customclass copy in root folder fact loop throughout methods in root folder, class , method name store variables change value of parameters of function , invoke inside each method.
but fact cannot work because value of parameters of static function change each time enter new method , @ end keep value of last method entered in.
in case need generate many static functions have methods in smali folder around 40.000...
this part of code:
def edit_custom_class(custom_class_path, my_tag, my_message): open(custom_class_path, "r+") file: line in file: if ('const-string p0' in line): file.write('\tconst-string p0, "{0}" \n' .format(my_tag)) elif ('const-string p1' in line): file.write('\tconst-string p1, "{0}" \n' .format(my_message)) else: file.write(line + '\n') def process_file(file_path, custom_class_path, my_tag, file_metadata): is_inside = false valid_registers = [] open(file_path, "r+") file: line in file: # data concerning method , mark treated method if (('.method' in line) , (helper.is_valid_class_or_method_directive(line)) , (is_inside == false)): is_inside = true method_data = get_method_data(helper.get_class_or_method_name(line), file_metadata) my_message= (method_data[0] + '->' + method_data[1]) file.write(line + '\n') elif (('return' in line) , (is_inside == true) , (method_data[4] == false)): edit_custom_class(custom_class_path, my_tag, my_message) file.write('\t# has been edited smali-rmc-interceptor on {0} \n' .format(time.strftime("%y-%m-%d %h:%m:%s", time.gmtime()))) file.write('\t# start editing \n') file.write('\tinvoke-static, {0};->e(ljava/lang/string;ljava/lang/string;)i \n' .format(custom_class_path)) file.write('\t# end editing \n') file.write(line + '\n') elif (('.end method' in line) , (is_inside == true) , (method_data[4] == false)): is_inside = false method_data = [] file.write(line + '\n') else: file.write(line + '\n')
and customclass content:
.class public lcustomclass; .source "customclass.java" .method public static add_trace()v .locals 0 .parameter "tag" .parameter "message" .prologue .line 10 const-string p0, "my_tag" const-string p1, "my_message" .line 15 invoke-static {p0, p1}, landroid/util/log;->d(ljava/lang/string;ljava/lang/string;)i .line 18 return-void .end method
in general, it's easiest avoid having allocate new registers in existing method. introduces whole slew of problems due register limitations of many instructions.
your best bet create separate static helper method accepts values , prints them out or whatever you're wanting do, , inject static method call in method want instrument, without allocating new registers.
Comments
Post a Comment