2012년 6월 11일 월요일

APK 디컴파일 방법.



오늘은 Android 최종 package 형태인 apk 파일에 대해서 Decompile 하는 방법을 소개 하겠습니다.
최종 APK 파일이 나오기 까지는 아래 그림처럼 많은 단계를 거칩니다.
대충 간단하게 정리해보면 리소스 파일들과 java 컴파일된 dex 파일 그리고 signature 정보가 들어가게 됩니다.
여튼 자세한 정보는 여기를 참고 하시고… http://developer.android.com/guide/developing/building/index.html

우리가 열심히 개발한 소스는 classes.dex 라는 파일 안에 들어가게 되는데.. 이 dex 파일을 풀면 class 파일들이 나옵니다.
이 class 파일을.. decompile 하면 소스 코드가 보이는 것이죠..

이런 코드가

이렇게까지 디컴파일 됩니다.

그럼 자세한 decompile 순서를 알아보겠습니다.

  • Apk 파일을 압축을 풉니다.
알집을 사용해도 되고.. 간단하게 jar xf hello.apk 하면 apk 파일이 풀립니다.
위 그림처럼 android manifest파일과 사용한 resource들 그리고 classes.dex 파일과 META-INF의 signature 정보들이 보입니다.

  • Dex를 jar로 변환합니다.
Classes.dex 파일은 android의 dalvik 에서 사용하기 위한 일종의 jar 패키지 형태인데.. 이를 다시 jar로 변환이 가능합니다.
여기에서 간단히 쓸 수 있는 변환툴을 다운로드 할 수 있다.
실행해보면
Jarfile이 하나 생긴 것을 알 수 있다.

  • Jar를 class로 압축을 풀어줍니다.
다시 jar xf classed_dex2jar.jar 하면 압축이 풀리면서.. class 파일들이 나온다.

Class 파일은 아시다시피 중간 바이너리 형태라 디컴파일이 가능하다.

  • Class 파일을 디컴파일 하여 보는 툴을 사용하여 소스를 본다.
소스를 보기위해.. jd 라는 툴을 사용하면 된다.
http://java.decompiler.free.fr/?q=jdgui 여기서 다운 받을수 있다.

다운받아 간단히 실행하여 class 파일을 열어보면 소스가 나온다..

정말 무서운 세상이다…

NDK addr2line 사용방법

FFmpeg프로젝트 중에 계속해서 build fingerprint 라는 알수없는 로그가 발생하면서
실행중인 앱이 죽어 버리는 증상이 발생하고 있다.

뭔가 알아 볼수 없는 주소값인건 알겠는데 어찌 읽어야 하나 방법을 찾다가 발견했다.

아래와 같이 로그가 발생 했다면

06-11 13:27:17.275: I/DEBUG(1435): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-11 13:27:17.275: I/DEBUG(1435): Build fingerprint: 'generic/broadcom_cmp/cmp/:2.2/FRF91/132:jp/release'
06-11 13:27:17.275: I/DEBUG(1435): pid: 9504, tid: 9504  >>> net.jbong.FFmpegBasic <<<
06-11 13:27:17.275: I/DEBUG(1435): signal 11 (SIGSEGV), fault addr 00000008
06-11 13:27:17.275: I/DEBUG(1435):  r0 00000000  r1 bef68370  r2 00000000  r3 00000001
06-11 13:27:17.275: I/DEBUG(1435):  r4 00000000  r5 bef68370  r6 00000c60  r7 81a50e20
06-11 13:27:17.275: I/DEBUG(1435):  r8 00000680  r9 4410eac8  10 4410eab4  fp 452090f4
06-11 13:27:17.275: I/DEBUG(1435):  ip 00000003  sp bef68370  lr 810b6624  pc 810b65dc  cpsr 60000010
06-11 13:27:17.425: I/DEBUG(1435):          #00  pc 000b65dc  /data/data/net.jbong.FFmpegBasic/lib/libbasicplayer.so
이하 생략


arm-linux-androideabi-addr2line.exe -f -e libbasicplayer.so 000b65dc
위와 같이 실행하면
다음과 같은 결과를 출력한다.

av_interleave_packet
D:\workspace2\FFmpegBasic/jni/ffmpeg/libavformat/utils.c:3239

위처럼 실행할때 so파일은 libs폴더가 아니고 obj/local/armeabi 아래 있는 파일을 사용해야 한다.

이제 어디가 문제인지 찾았으니 수정을 해야겠다.

2012년 6월 1일 금요일

Android - ndk-gdb

AndroidManifest.xml

<application
        android:icon="@drawable/showplay_launcher"
        android:label="@string/app_name" android:debuggable="true">
...
debuggable 확인

하지만 현재 작업중인 ffmpeg에서는 뭔가가 때문에 저렇게 하고 ndk-build하면
에러 나면서 정상진행 되지 않는다.
뭐가 문제 일까...

Android - view call stack of crashed application on android


출처 : http://rxwen.blogspot.com/2011/01/resolve-stack-trace-of-crashed.html

view call stack of crashed application on android

On android, when a process crashes in native code, the call stack of the process will be saved to a log file in /data/tombstomes/, and written to logcat as well. The information is helpful for debugging.
Unfortunately, the call stack doesn't show in human readable format, file name, function name. Instead, it's shown as module name (e.g., libc.so) and memory address of the instruction. We can use addr2line to translate the address to corresponding file name and function name if we have the binary of the module that contains symbol information.
To make it easier to use, this function is included in agdbtool (see here for more). We can use "agdb.py -r -e module_name address" to find out the function name of specified address within the module.

When we have a long call stack, instead of running the command above for each line in the call stack manually, we can feed the whole call stack to agdb through pipe and get the full resolved call stack. For example, use  "adb logcat | agdb.py -r"command for adb logcat output with below contents:

22 F/ASessionDescription(   33): frameworks/base/media/libstagefright/rtsp/ASessionDescription.cpp:264 CHECK_GT( end,s) failed:  vs.
23 I/DEBUG   (   30): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
24 I/DEBUG   (   30): Build fingerprint: 'generic/generic/generic:2.3.1/GINGERBREAD/eng.raymond.20101222.130550:eng/test-keys'
25 I/DEBUG   (   30): pid: 33, tid: 450  >>> /system/bin/mediaserver <<<
26 I/DEBUG   (   30): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadbaad
27 I/DEBUG   (   30):  r0 deadbaad  r1 0000000c  r2 00000027  r3 00000000
28 I/DEBUG   (   30):  r4 00000080  r5 afd46668  r6 40806c10  r7 00000000
29 I/DEBUG   (   30):  r8 8031db1d  r9 0000fae0  10 00100000  fp 00000001
30 I/DEBUG   (   30):  ip ffffffff  sp 40806778  lr afd19375  pc afd15ef0  cpsr 00000030
31 I/DEBUG   (   30):          #00  pc 00015ef0  /system/lib/libc.so
32 I/DEBUG   (   30):          #01  pc 00001440  /system/lib/liblog.so
33 I/DEBUG   (   30):
34 I/DEBUG   (   30): code around pc:
35 I/DEBUG   (   30): afd15ed0 68241c23 d1fb2c00 68dae027 d0042a00
36 I/DEBUG   (   30): afd15ee0 20014d18 6028447d 48174790 24802227
37 I/DEBUG   (   30): afd15ef0 f7f57002 2106eb56 ec92f7f6 0563aa01
38 I/DEBUG   (   30): afd15f00 60932100 91016051 1c112006 e818f7f6
39 I/DEBUG   (   30): afd15f10 2200a905 f7f62002 f7f5e824 2106eb42
40 I/DEBUG   (   30):
41 I/DEBUG   (   30): code around lr:
42 I/DEBUG   (   30): afd19354 b0834a0d 589c447b 26009001 686768a5
43 I/DEBUG   (   30): afd19364 220ce008 2b005eab 1c28d003 47889901
44 I/DEBUG   (   30): afd19374 35544306 d5f43f01 2c006824 b003d1ee
45 I/DEBUG   (   30): afd19384 bdf01c30 000281a8 ffffff88 1c0fb5f0
46 I/DEBUG   (   30): afd19394 43551c3d a904b087 1c16ac01 604d9004
47 I/DEBUG   (   30):
48 I/DEBUG   (   30): stack:
49 ........................
92 I/DEBUG   (   30):     408067e4  6f697470
93 I/BootReceiver(   75): Copying /data/tombstones/tombstone_09 to DropBox (SYSTEM_TOMBSTONE)

we get:


22 F/ASessionDescription(   33): frameworks/base/media/libstagefright/rtsp/ASessionDescription.cpp:264 CHECK_GT( end,s) failed:  vs.
23 I/DEBUG   (   30): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
24 I/DEBUG   (   30): Build fingerprint: 'generic/generic/generic:2.3.1/GINGERBREAD/eng.raymond.20101222.130550:eng/test-keys'
25 I/DEBUG   (   30): pid: 33, tid: 450  >>> /system/bin/mediaserver <<<
26 I/DEBUG   (   30): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadbaad
27 I/DEBUG   (   30):  r0 deadbaad  r1 0000000c  r2 00000027  r3 00000000
28 I/DEBUG   (   30):  r4 00000080  r5 afd46668  r6 40806c10  r7 00000000
29 I/DEBUG   (   30):  r8 8031db1d  r9 0000fae0  10 00100000  fp 00000001
30 I/DEBUG   (   30):  ip ffffffff  sp 40806778  lr afd19375  pc afd15ef0  cpsr 00000030
31 I/DEBUG   (   30):          #00  pc 00015ef0  /system/lib/libc.so
32 I/DEBUG   (   30):          #00  __libc_android_abort: abort.c:82
33 I/DEBUG   (   30):          #01  pc 00001440  /system/lib/liblog.so
34 I/DEBUG   (   30):          #01  __android_log_assert: logd_write.c:235
35 I/DEBUG   (   30):
36 I/DEBUG   (   30): code around pc:
37 I/DEBUG   (   30): afd15ed0 68241c23 d1fb2c00 68dae027 d0042a00
38 I/DEBUG   (   30): afd15ee0 20014d18 6028447d 48174790 24802227
39 I/DEBUG   (   30): afd15ef0 f7f57002 2106eb56 ec92f7f6 0563aa01

Similarly, we can use "echo tombstone_01.txt | agdb.py -r" command to resolve call stack addresses in a tombstone log file.

2024년 첫번째 도서 리뷰 GPT-4를 활용한 인공지능 앱 개발

 "한빛미디어 <나는 리뷰어다> 활동을 위해서 책을 제공받아 작성된 서평입니다." 24년 첫 도서 리뷰이다. 작년까지? 한참 핫 했던 인공지능 서비스 Chat GPT에 관한 책이다. 핑계지만 어쩌다보니 GPT에 대한 접근이...