apk파일을 디컴파일하여 나온 smali code를 분석하다 보면 변수값이나 코드 흐름등을 파악하기 위해 로그를 넣고 싶은 경우가 있습니다.
검색을 해 보니 이런경우에 사용할 수 있는 좋은 내용인듯 하여 스크립 해 놓습니다.
1. Debug log in smali
Debug log in smali. Say for example inside method test() you want to print "Inside Test()" debug log. At the start of method in smali add following instructions :
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
Note - You need to be careful while using registers v0,v1 here. In code execution flow, you have to check that you are not using one of the register which is used later in the flow. Or you may get Exception.
2. StackTrace
Here is the code of smali to print stacktrace of a method
Java code
public static void printStackTraces() {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
for (StackTraceElement element : stackTraceElements) {
System.out.println("Class name :: " + element.getClassName() + " || method name :: " + element.getMethodName());
}
}
And equivalent smali code is
.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
Add this method into any smali file. And call as
(Assuming you added above smali code into com.example.pakagename.ClassName)
invoke-static {}, Lcom/example/packagename/ClassName;->printStackTraces()V
Hope this helps .....
'Software > Android' 카테고리의 다른 글
Ubuntu에서 멀티파티션을 가진 디스크 이미지 읽고 쓰기 (0) | 2021.08.24 |
---|---|
[Android Studio] USB Accessory 사용하기 (0) | 2021.01.06 |
[Android Studio] ConstraintLayout 사용하기 (0) | 2021.01.05 |
[Android Studio] 키 생성 오류(Key was created with errors) (0) | 2021.01.04 |
[안드로이드] 디컴파일을 통한 crackme0.apk 크랙 - 방법1 (0) | 2020.12.21 |
댓글