1. smali调试日志
在 smali 中调试日志。例如,在方法 test() 中,您要打印“Inside Test()”调试日志。在 smali 方法的开头添加以下说明:
sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;
const-string v1, "Inside Test()"
invoke-virtual {v0, v1}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V
注意 -在此处使用寄存器 v0、v1 时需要小心。在代码执行流程中,您必须检查您没有使用稍后在流程中使用的寄存器之一。或者你可能会得到异常。
2.堆栈跟踪
这是 smali 打印方法堆栈跟踪的代码
Java 代码
public static void printStackTraces() {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
for (StackTraceElement element : stackTraceElements) {
System.out.println("Class name :: " + element.getClassName() + " || method name :: " + element.getMethodName());
}
}
等效的smali代码是
.method public static printStackTraces()V
.locals 7
.prologue
.line 74
invoke-static {}, Ljava/lang/Thread;->currentThread()Ljava/lang/Thread;
move-result-object v2
invoke-virtual {v2}, Ljava/lang/Thread;->getStackTrace()[Ljava/lang/StackTraceElement;
move-result-object v1
.line 75
.local v1, stackTraceElements:[Ljava/lang/StackTraceElement;
array-length v3, v1
const/4 v2, 0x0
:goto_0
if-lt v2, v3, :cond_0
.line 78
return-void
.line 75
:cond_0
aget-object v0, v1, v2
.line 76
.local v0, element:Ljava/lang/StackTraceElement;
sget-object v4, Ljava/lang/System;->out:Ljava/io/PrintStream;
new-instance v5, Ljava/lang/StringBuilder;
const-string v6, "Class name :: "
invoke-direct {v5, v6}, Ljava/lang/StringBuilder;-><init>(Ljava/lang/String;)V
invoke-virtual {v0}, Ljava/lang/StackTraceElement;->getClassName()Ljava/lang/String;
move-result-object v6
invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
move-result-object v5
const-string v6, " || method name :: "
invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
move-result-object v5
invoke-virtual {v0}, Ljava/lang/StackTraceElement;->getMethodName()Ljava/lang/String;
move-result-object v6
invoke-virtual {v5, v6}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;
move-result-object v5
invoke-virtual {v5}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;
move-result-object v5
invoke-virtual {v4, v5}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V
.line 75
add-int/lit8 v2, v2, 0x1
goto :goto_0
.end method
将此方法添加到任何 smali 文件中。并称为
(假设您将上述 smali 代码添加到 com.example.pakagename.ClassName 中)
invoke-static {}, Lcom/example/packagename/ClassName;->printStackTraces()V
希望这可以帮助 .....