레이블이 inner Android인 게시물을 표시합니다. 모든 게시물 표시
레이블이 inner Android인 게시물을 표시합니다. 모든 게시물 표시

2016년 9월 18일 일요일

HeapWorker 동작 방식 분석(미완)

Android 2.2 기반의 heap worker 동작방식을 분석해 보았으나, 미완성이다-_-;


시스템에 의해 특정 어플이 시작될 때, zygote 프로세스가 fork되면서 ~ 언젠가는 exec로 dalvikvm 프로세스를 실행시킬 것으로 보인다.

dalvikvm/Main.c
int main(int argc, char* const argv[])

JNI_CreateJavaVM 함수를 실행시키는데, 인자로 넘기는 p_vm, p_env 는 NULL이 할당된 채로 넘겨진다. vm_args 에는 멤버변수에 값을 채운다. 





vm/Jni.c
jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args)

이 함수에서는 ./vm/Init.c 파일에 정의된 struct DvmGlobals gDvm 글로벌 변수를 초기화한다.
또한 JavaVMExt 구조체 포인터도 malloc하는데, 하나의 프로세스 내에서 사용되는 여러 개의 JNIEnvExt구조체들의 리스트를 들고있는 구조체다. 

dvmStartup함수를 호출한다.




vm/Init.c

int dvmStartup(int argc, const char* const argv[], bool ignoreUnrecognized, JNIEnv* pEnv)
dvmStartup 함수에서는 많은 컴포넌트들을 초기화 하는데, 목록은 다음과 같다. 
dvmAllocTrackerStartup
dvmGcStartup
     --> dvnHeapStartup --> dvmInitializeHeapWorkerState();
dvmThreadStartup
     --> gDvm.pthreadKeySelf 를 만든다. 각종 쓰레드 뮤텍스, condition 변수 생성 및 초기화, 
dvmInlineNativeStartup
dvmVerificationStartup
dvmRegisterMapStartup
dvmInstanceofStartup
dvnClassStartup
dvmThreadObjStartup
dvmExceptionStartup
dvmStringInternStartup
dvmNativeStartup
dvmInternalNativeStartup
dvnJniStartup
dvnReflectStartup
dvmProfilingStartup

각 종, 클래스 로드, hashtable생성 등등의 일을 한다.

쭉 초기화 진행 후,
gDvm.zygote 변수가 true면 dvmInitZygote() 함수를 호출하고, (zygote모드)
false이면 dvmInitAfterZygote()함수를 호출한다. (non-zygote모드)


bool dvmInitAfterZygote(void)
이 함수는, 새로이 프로세스가 만들어질 때 zygote fork 이후에 호출되어진다. 
가장 먼저 dvmGcStartupAfterZygote 함수를 호출한다. 





vm/alloc/Alloc.c

bool dvmGcStartupAfterZygote(void)
이 함수 하는 일은 dvmHeapWorkerStartup()함수 호출한다.






dalvik/vm/alloc/HeapWorker.c

bool dvmHeapWorkerStartup(void)
이 함수는 각 초기화 변수들이 제대로 세팅되어 있는지 체크 후,
heapWorkerLock 뮤텍스 변수에 lock을 걸고, dvmCreateInternalThread함수로 heapWorkerThreadStart 쓰레드함수를 시작시킨다.
dvmCreateInternalThread함수는 통상적인 pthread_create 를 수행하고,
바로, thread list 락을 건 다음, create상태를 체크한다. (pthread_cond_wait()함수는 락을 풀고 시그널을 받기 전까지 현재 쓰레드를 대기시킨다. 시그널을 받으면 다시 락을 건 다음 while문의 조건 검사를 하고 조건을 만족시키면 다음으로 넘어간다.)
그러니까, 쓰레드가 제대로 생성되었는지를 기다려준다는 것이다. 제대로 생성이 되었다면 heapWorkerLock 뮤텍스 락을 푼다.




static void* heapWorkerThreadStart(void* arg)
함수는 다음과 같이 동작한다.
처음 실행 시 초기화 수행에 대한 mutex condition 조건 및 lock 들을 풀어주고.
heapWorkerLock 뮤텍스 락을 건 다음,

haltHeapWorker 변수가 false인 동안 while문을 돈다.
1. dvm상태를 THREAD_VMWAIT로 변경하고,
2. heapWorkerIdleCond 뮤텍스컨디션 변수를 풀어주며 (해당 condition으로 wait상태인 다른 쓰레드가 깨어나도록)
3.



















2014년 1월 29일 수요일

android 단말 gdbserver 크로스컴파일

android 단말 gdbserver 크로스컴파일

망할 단말에서 gdbserver를 돌려보자.

규약


  • 단말에서의 실행문은 앞에 #을, pc에서의 실행문은 앞에 $를 붙인다. 


환경: 


  • You are using Ubuntu 11.04 - the Natty Narwhal - released in April 2011 and supported until October 2012.
  • 그지같은 보안으로 인해 apt-get 을 쓸 수 없는 환경. 
  • 그리고, 안드로이드를 풀빌드 할 수 있는 환경



GDB Server 의 크로스컴파일.

참고사이트: http://checko.soup.io/post/21071057/Cross-compile-GDB-6-8-for-Android

step1을 따라한다. 

wget으로 받되, gdb의 파일명을 다음과 같이 변경해야 받을 수 있다.
ftp://ftp.gnu.org/gnu/gdb/gdb-6.8.tar.gz --> ftp://ftp.gnu.org/gnu/gdb/gdb-6.8a.tar.gz

arm 크로스 툴 체인은 예제에 나온 대로 /usr/local/ 에 풀어두는 것이 나중에 스크립트를 그대로 실행하는 데 유리하다. 마찬가지로 wget으로 받는다.

그리고, 압축을 풀어놓았다면, 이제 PATH를 ~/.bashrc 에 추가해 둔다.

step2를 따라한다.

ncurses2를 설치하는데, 이는 "configure: error: no termcap library found" 를 없애기 위한 용도라고 하지만, 모든 설명대로 다 진행했음에도 불구하고 여전히 step1의 gdb를 컴파일할 때 같은 오류가 발생한다. 인터넷을 찾아보니, 우분투의 경우 libncurses5-dev를 설치하라고 나와 있다.
다음과 같은 방법으로 설치할 수 있다.

$ sudo apt-get install libncurses5-dev

그러나, gdb를 위한 라이브러리이기 때문에, gdbserver만 필요할 경우, 굳이 설치할 필요가 없다고 한다. 스킵해도 된다.

step3를 따라한다. 

위에 step2 를 따라할 때 처럼 에러 메시지가 나온다.  gdbserver만 컴파일하면 되기 때문에
'소스폴더/gdb/gdbserver'로 이동한다.
이동한 다음, step3에 적혀 있는  configure 와 make 명령을 수행해 준다.
gdbserver파일이 만들어 진다.

step4. 단말에 gdbserver를 복사

만든 파일을 다음과 같은 명령어로 복사해 둔다.

$ adb push ./gdbserver /system/bin


gdbserver의 실행


참고 사이트: http://techpedia.tistory.com/13
















2012년 6월 5일 화요일

Android onInterceptTouchEvent함수 설명의 번역


onInterceptTouchEvent (MotionEvent ev) 함수 설명 번역. 


Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.
Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:
  1. You will receive the down event here.
  2. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
  3. For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
  4. If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.
Parameters
evThe motion event being dispatched down the hierarchy.
Returns
  • Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.
모든 터치 동작 이벤트들을 가로채기 위한다면 이 함수를 구현하면된다. 이 함수를 구현하면 당신은 당신의 자식(뷰)들에게 이벤트가 보내지는 것을 지켜 볼 수 있다. 그리고 어느 시점이든 현재 제스처에 대한 소유권을 가질 수 있다. 
이 함수를 사용할 때는 onTouchEvent(MotionEvent) 함수와 상당히 복잡한 상호작용을 하기 때문에 주의해야 한다. 또한 onTouchEvent()함수를 올바른 방법으로 구현해 주어야 한다. 이벤트는 다음과 같은 순서로 전달될 것이다. 
  1. down이벤트를 여기서 받을 것이다. 
  2. down이벤트는 이 뷰 그룹의 차일드에게, 아니면 onTouchEvent()에게 처리되도록 전달된다. 이 말인즉슨, 남은 제스처를 계속 보기 위해서는 onTouchEvent()함수에서 true값을 리턴하도록 구현해야 한다는 것을 의미한다. (대신 부모 뷰가 이것을 처리하도록 할 수도 있다. ) 또한, onTouchEvent()함수에서 true를 리턴하게 함으로서, onInterceptTouchEvent() 함수에서 따라오는 모든 이벤트들을 받지 않게 할 수 있으며, 모든 터치 처리는 onTouchEvent에서 정상적으로 처리되도록 할 수 있다. 
  3. 이 함수에서 false를 리턴하는 한, 각 따라오는 이벤트들(마지막 up이벤트까지)은 제일 먼저 이 함수로 배달될 것이며, 그 다음에 타겟의 onTouchEvent()함수로 전달될 것이다. 
  4. 만약 true를 이 함수에서 리턴한다면, 따라오는 어떠한 이벤트들도 받지 못할 것이다: 타겟 뷰는 같은 이벤트를 받겠지만 ACTION_CANCEL 액션도 받을 것이다. 그리고 그 이후의 이벤트들은 onTouchEvent()에만 전달되고 더이상 이 함수에 나타나지 않을 것이다. 
Parameters
ev계층적으로 전달되는 모션 이벤트. 
Returns
  • true를 리턴하면 자식들로부터의 이벤트를 훔치고 이 viewGroup으로 onTouchEvent함수를 통해 이벤트들이 전달되도록 한다. 현재 타겟은 ACTION_CANCEL 이벤트를 받게 될 것이고 더이상 어떠한 메시지들도 이 함수에 전달되지 않을 것이다. 
  • 역주) false를 리턴하면 계속 이 함수로 이벤트를 받아서 검사하겠다는 의미. 


ViewPager.java 에서의 onInterceptTouchEvent (MotionEvent ev)
이 함수는 단지 모션을 가로챌 것인지 말 것인지를 결정한다. 만약 true를 리턴하면 onMotionEvent함수가 불리게 될 것이며 실질적인 스크롤링을 그곳에서 할 것이다. 
이 함수가 하는 짓은 다음과 같다. 

  1. 전달된 Action이 ACTION_CANCEL 또는 ACTION_UP 과 같다면, 드래그가 끝난 것이므로 멤버변수들을 초기화하고 false를 리턴한다. 
  2. 만약 전달된 이벤트가 ACTION_DOWN이 아니라면, 
    1. 만약 지금 드래깅 중이면 true를 리턴
    2. 만약 드래그를 할 수 없는 상태라면 false를 리턴
  3. 케이스 ACTION_MOVE: