smali插入log,打印变量

一:Log打印变量:

Log打印字符串:

#liyanzhong debug
const-string v1, "TAG"
const-string v2, "xunbu7@163.com"
#v2 为要输出的调试信息字符串
invoke-static {v1,v2} ,Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I

Toast显示字符串:

const-string v0, "Must ensure vx is Context class, then Toast.makeText(vx, xxx, 1).show();" # CharSequence对象类型
const/4 v1, 0x1 # I int类型
invoke-static {p0, v0, v1}, Landroid/widget/Toast;->makeText(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast; # p0 是一个Context
move-result-object v0
invoke-virtual {v0}, Landroid/widget/Toast;->show()V

.locals 4
。。。。。
const-string v3, "zhangmin"
invoke-static {v3, v1}, Landroid/util/Log;->v(Ljava/lang/String;Ljava/lang/String;)I

注意:在函数的开头要至少增加一个寄存器(.locals 的值增加一个)。

假如Log.d()函数的第二个参数为null,那么运行时会报错,可以用下面的方法解决:

invoke-static {v1}, Landroid/text/TextUtils;->isEmpty(Ljava/lang/CharSequence;)Z
move-result v2
if-nez v2, :errornull_1
const-string v3, "zhangmin"
invoke-static {v3, v1}, Landroid/util/Log;->v(Ljava/lang/String;Ljava/lang/String;)I
:errornull_1

如果查看int值,首先要将变量转为String,再来查看,需要添加2个寄存器:

.locals 5
#......
const/high16 v1, 0x7f030000
const-string v3, "zhangmin" invoke-static {v1}, Ljava/lang/Integer;->toString(I)Ljava/lang/String; move-result-object v4 invoke-static {v3, v4}, Landroid/util/Log;->v(Ljava/lang/String;Ljava/lang/String;)I

查看boolean值的打印类似int,先转换成String:

.locals 5
#......
const-string v3, "zhangmin"
invoke-static {v1}, Ljava/lang/Boolean;->toString(Z)Ljava/lang/String;
move-result-object v4
invoke-static {v3, v4}, Landroid/util/Log;->v(Ljava/lang/String;Ljava/lang/String;)I

 

二:栈跟踪法:

在目标位置处直接插入下面代码,查看堆栈:

invoke-static {}, Ljava/lang/Thread;->dumpStack()V

它的标签为:System.err,效果如图:

 

posted on 2014-11-26 19:19  寻步  阅读(3689)  评论(0编辑  收藏  举报