IT_Programming/Android_NDK

[펌][디버깅] 안드로이드 NDK에서 Log 찍기

JJun ™ 2015. 1. 27. 18:24



 출처

 : http://seohs.tistory.com/284

 : https://github.com/dalinaum/writing/blob/master/gistfile1.md#jni-디버깅

 : https://android-developers.googleblog.com/2011/07/debugging-android-jni-with-checkjni.html




이 디버깅 방법은 안드로이드 개발 시 NDK를 사용하는 경우 Log.d와 같은 문자열을 찍는 방법이다.

일반적인 printf(C언어)나 Log.d(안드로이드)를 사용할 수 없기때문에

다음과 같은 방법을 통해 C++에서 문자열을 찍어 디버깅을 수행할 수 있다. 



1. 환경 준비하기


1-1. 안드로이드 MAKEFILE 수정하기

안드로이드 MAKEFILE은 "Android.mk"이다. 이 파일을 열어서 "LOCAL_LDLIBS:= -llog"을 한 줄을 추가한다.


1-2. 헤더파일 추가하기

문자열을 출력할 파일에 "#include <android/log.h>"을 추가한다.



2. 문자열 출력하기


문자열을 출력할 부분에 "__android_log_print(ANDROID_LOG_DEBUG, "TAG", "Message")"을 추가한다.

ANDROID_LOG_DEBUG를 대신할 수 있는 옵션으로는 다음과 같다.

- ANDORID_LOG_UNKNOWN

- ANDROID_LOG_DEFAULT

ANDROID_LOG_VERBOSE

ANDROID_LOG_DEBUG

ANDROID_LOG_INFO

ANDROID_LOG_WARN

ANDROID_LOG_ERROR

ANDROID_LOG_FATAL

ANDROID_LOG_SILENT


TAG Message는 Log.d("TAG", "Message")와 같은 역할을 한다.

예를 들어 __android_log_print(ANDROID_LOG_DEBUG, "CHK", "Hello World")인 경우 Log.d("CHK", "Hello World"와 같다.)

혹은 특정 변수의 값을 출력해야 한다면 다음과 같이 출력이 가능하다.

예를 들어, INT형인 VAR라는 변수의 값을 출력해야 하는 경우

__android_log_print(ANDROID_LOG_DEBUG, "CHK", "This value is %d", VAR) 의 식으로 사용할 수 있다. 




[Reference]

01. http://blog.naver.com/jbin_k?Redirect=Log&logNo=130127879251







JNI 디버깅

ART의 채택에 따라 기존에 잘 동작하던 JNI 앱의 동작에 문제가 생길 수 있다.
JNI의 동작에 문제가 있는 경우 안드로이드에 포함된 CheckJNI 툴이 도움이 된다.

$ adb shell setprop debug.checkjni 1

<리스트 17> JNI 디버깅 모드 활성화

환경이 설정된 후 JNI 코드가 포함된 앱을 수행할 때 시스템은 종종 <리스트 18>과 같이 경고나 에러 메시지를 출력한다.

W JNI WARNING: method declared to return 'Ljava/lang/String;' returned '[B'
W              failed in LJniTest;.exampleJniBug
I "main" prio=5 tid=1 RUNNABLE
I   | group="main" sCount=0 dsCount=0 obj=0x40246f60 self=0x10538
I   | sysTid=15295 nice=0 sched=0/0 cgrp=default handle=-2145061784
I   | schedstat=( 398335000 1493000 253 ) utm=25 stm=14 core=0
I   at JniTest.exampleJniBug(Native Method)
I   at JniTest.main(JniTest.java:11)
I   at dalvik.system.NativeStart.main(Native Method)
I
E VM aborting

<리스트 18> 향상된 디버깅 메시지

향상된 디버그 모드를 이용해 예상되는 JNI 문제를 해결하자.



더보기


Debugging Android JNI with CheckJNI

[This post is by Elliott Hughes, a Software Engineer on the Dalvik team — Tim Bray]


Although most Android apps run entirely on top of Dalvik, some use the Android NDK to include native code using JNI. Native code is harder to get right than Dalvik code, and when you have a bug, it’s often a lot harder to find and fix it. Using JNI is inherently tricky (there’s precious little help from the type system, for example), and JNI functions provide almost no run-time checking. Bear in mind also that the developer console’s crash reporting doesn’t include native crashes, so you don’t even necessarily know how often your native code is crashing.


What CheckJNI can do


To help, there’s CheckJNI. It can catch a number of common errors, and the list is continually increasing. In Gingerbread, for example, CheckJNI can catch all of the following kinds of error:

  • Arrays: attempting to allocate a negative-sized array.

  • Bad pointers: passing a bad jarray/jclass/jobject/jstring to a JNI call, or passing a NULL pointer to a JNI call with a non-nullable argument.

  • Class names: passing anything but the “java/lang/String” style of class name to a JNI call.

  • Critical calls: making a JNI call between a GetCritical and the corresponding ReleaseCritical.

  • Direct ByteBuffers: passing bad arguments to NewDirectByteBuffer.

  • Exceptions: making a JNI call while there’s an exception pending.

  • JNIEnv*s: using a JNIEnv* from the wrong thread.

  • jfieldIDs: using a NULL jfieldID, or using a jfieldID to set a field to a value of the wrong type (trying to assign a StringBuilder to a String field, say), or using a jfieldID for a static field to set an instance field or vice versa, or using a jfieldID from one class with instances of another class.

  • jmethodIDs: using the wrong kind of jmethodID when making a Call*Method JNI call: incorrect return type, static/non-static mismatch, wrong type for ‘this’ (for non-static calls) or wrong class (for static calls).

  • References: using DeleteGlobalRef/DeleteLocalRef on the wrong kind of reference.

  • Release modes: passing a bad release mode to a release call (something other than 0, JNI_ABORT, or JNI_COMMIT).

  • Type safety: returning an incompatible type from your native method (returning a StringBuilder from a method declared to return a String, say).

  • UTF-8: passing an invalid Modified UTF-8 byte sequence to a JNI call.


If you’ve written any amount of native code without CheckJNI, you’re probably already wishing you’d known about it. There’s a performance cost to using CheckJNI (which is why it isn’t on all the time for everybody), but it shouldn’t change the behavior in any other way.


Enabling CheckJNI

If you’re using the emulator, CheckJNI is on by default. If you’re working with an Android device, use the following adb command:

adb shell setprop debug.checkjni 1

This won’t affect already-running apps, but any app launched from that point on will have CheckJNI enabled. (Changing the property to any other value or simply rebooting will disable CheckJNI again.) In this case, you’ll see something like this in your logcat output the next time each app starts:

D Late-enabling CheckJNI

If you don’t see this, your app was probably already running; you just need to force stop it and start it again.



Example


Here’s the output you get if you return a byte array from a native method declared to return a String:

W JNI WARNING: method declared to return 'Ljava/lang/String;' returned '[B'
W              failed in LJniTest;.exampleJniBug
I "main" prio=5 tid=1 RUNNABLE
I   | group="main" sCount=0 dsCount=0 obj=0x40246f60 self=0x10538
I   | sysTid=15295 nice=0 sched=0/0 cgrp=default handle=-2145061784
I   | schedstat=( 398335000 1493000 253 ) utm=25 stm=14 core=0
I   at JniTest.exampleJniBug(Native Method)
I   at JniTest.main(JniTest.java:11)
I   at dalvik.system.NativeStart.main(Native Method)
I 
E VM aborting

Without CheckJNI, you’d just die via SIGSEGV, with none of this output to help you!



New JNI documentation


We’ve also recently added a page of JNI Tips that explains some of the finer points of JNI. If you write native methods, even if CheckJNI isn’t rejecting your code, you should still read that page. It covers everything from correct usage of the JavaVM and JNIEnv types, how to work with native threads, local and global references, dealing with Java exceptions in native code, and much more, including answers to frequently-asked JNI questions.



What CheckJNI can’t do


There are still classes of error that CheckJNI can’t find. Most important amongst these are misuses of local references.
CheckJNI can spot if you stash a JNIEnv* somewhere and then reuse it on the wrong thread, but it can’t detect you
stashing a local reference (rather than a global reference) and then reusing it in a later native method call.
Doing so is invalid, but currently mostly works (at the cost of making life hard for the GC), and we’re still working on
getting CheckJNI to spot these mistakes.

We’re hoping to have more checking, including for local reference misuse, in a future release of Android.
Start using CheckJNI now, though, and you’ll be able to take advantage of our new checks as they’re added.