2011年10月14日 星期五

如何寫Android.mk,使得可以copy 某目錄下所有檔案到image裡

我先說明一下情境
Vender/a_folder/Android.mk
Vender/a_folder/files/1.dat
Vender/a_folder/files/2.dat
Vender/a_folder/files/99.dat
Vender/a_folder/files/a.ini
Vender/a_folder/files/b.ini
Vender/a_folder/files/z.ini

我們想把Vender/a_folder/files/目錄下的所有檔案copy out/target/product/target_product/system/data/example/目錄下該如血Android.mk?
答案是:
PRODUCT_COPY_FILES += \
        $(call find-copy-subdir-files,*,$(LOCAL_PATH)/files,system/data/example)


find-copy-subdir-files function是定義在 build/core/product_config.mk
###########################################################
## List all of the files in a subdirectory in a format
## suitable for PRODUCT_COPY_FILES and
## PRODUCT_SDK_ADDON_COPY_FILES
##
## $(1): Glob to match file name
## $(2): Source directory
## $(3): Target base directory
###########################################################

define find-copy-subdir-files
$(shell find $(2) -name "$(1)" | $(SED_EXTENDED) "s:($(2)/?(.*)):\\1\\:$(3)/\\2:" | sed "s://:/:g")
endef

2011年7月11日 星期一

當Android crash 該如何有效利用tombstone

當Android發生crash時會產生一個類似core dump的檔案在 /data/tombstones/tombstone_XX where XX is a number increased by one with each crash. 我們要如何使用該檔案呢? 
tombstones_XX 檔案內容如下, 我們可以利用addr2line將pc所指的位置的function name給找出來
pid: 153, tid: 161  >>> system_server <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 43bf3000
 r0 003dc4a0  r1 43bf3000  r2 00000400  r3 00000000
 r4 002cbc98  r5 002cbc98  r6 00000000  r7 00000000
 r8 00000001  r9 00000140  10 00000019  fp 00301e04
 ip 8090a1e0  sp 43f1eb68  lr 80c92cbb  pc afd0cdfc  cpsr 20000010
 d0  003b3a700033c9f8  d1  00301280002eb23b
 d2  000000000000003b  d3  0000000000000000
 d4  43bbc00043bb8000  d5  43d4800043bb8000
 d6  3f80000000000000  d7  000001e03f000000
 d8  0000000000000000  d9  0000000000000000
 d10 0000000000000000  d11 0000000000000000
 d12 0000000000000000  d13 0000000000000000
 d14 0000000000000000  d15 0000000000000000
 d16 3effff003effff00  d17 3f80000041808889
 d18 3f80000041c4cccd  d19 0701070100700798
 d20 0000000000000c27  d21 0000043f00890000
 d22 0000000000000000  d23 0000000000000008
 d24 3fc74721cad6b0ed  d25 3fc39a09d078c69f
 d26 0000000000000000  d27 0000000000000000
 d28 0000000000000000  d29 0000000000000000
 d30 0000000000000000  d31 0000000000000000
 scr 20000010

         #00  pc 0000cdfc  /system/lib/libc.so
         #01  pc 00092cb8  /system/lib/egl/libGLESv2_adreno200.so
         #02  pc 00093814  /system/lib/egl/libGLESv2_adreno200.so
         #03  pc 00093890  /system/lib/egl/libGLESv2_adreno200.so
         #04  pc 000938bc  /system/lib/egl/libGLESv2_adreno200.so
         #05  pc 00095cce  /system/lib/egl/libGLESv2_adreno200.so
         #06  pc 0006526a  /system/lib/egl/libGLESv2_adreno200.so
         #07  pc 000655cc  /system/lib/egl/libGLESv2_adreno200.so
         #08  pc 000188c4  /system/lib/egl/libGLESv1_CM_adreno200.so
         #09  pc 000265ca  /system/lib/libsurfaceflinger.so
         #10  pc 0001b04c  /system/lib/libsurfaceflinger.so
         #11  pc 0001bda8  /system/lib/libsurfaceflinger.so
         #12  pc 0001bf52  /system/lib/libsurfaceflinger.so
         #13  pc 00020f3c  /system/lib/libsurfaceflinger.so
         #14  pc 00023df6  /system/lib/libsurfaceflinger.so
         #15  pc 000259de  /system/lib/libsurfaceflinger.so
         #16  pc 0001c52c  /system/lib/libutils.so
         #17  pc 0001ca8a  /system/lib/libutils.so
         #18  pc 00011bc4  /system/lib/libc.so
         #19  pc 00011790  /system/lib/libc.so
1. Android 產生出來的還沒進行strip的執行檔或shared libraries 是放在$android_root/out/target/product/YOUR_PRODUCT_NAME/symbols/system/bin 與 $android_root/out/target/product/YOUR_PRODUCT_NAME/symbols/system/lib目錄下
2. Android所使用的toolchain是放在$android_root/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/ 下面
3. 假設我們要看 #09  pc 000265ca  /system/lib/libsurfaceflinger.so 是 call 到哪一個function
 (假設tombstone_XX 是在 $android_root 目錄下)
    $ ./prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-addr2line -f -e ./out/target/product/YOUR_PRODUCT_NAME/symbols/system/lib/libsurfaceflinger.so  0x000265ca  
  它會show出_ZN7android14TextureManager12initEglImageEPNS_5ImageEPvRKNS_2spINS_13GraphicBufferEEE
  $android_root/frameworks/base/services/surfaceflinger/TextureManager.cpp:164 這樣的訊息

2011年6月30日 星期四

How to use gdb to debug Android native c /c++ code.

如何使用GDB debug Android native c/c++ code
假設你的target device(Android手機或平板電腦) 跟你的Linux電腦主機之間已經用usb連線,而且target device 已經將"USB debugging" 打開
1.(host) $android_root/out/host/linux-x86/bin/adb forward tcp:1234 tcp:1234
  (使他們之間用port 1234溝通)

2. (host)$android_root/out/host/linux-x86/bin/adb shell
    (接下來就連到target端)

3.(target)#gdbserver :1234 --attach YOUR_PID
   (你可以使用 cat /proc/YOUR_PID/maps | grep YOUR_LIBRARY 去查看你要debug的library是不是在該PID裡面)
   (如果成功attach上,你可以看到
     Attached; pid = xxx
      Listening on port 1234
    的訊息)

4. 在host端要使用 $android_root/prebuilt/linux-x86/toolchain/arm-linux-androideabi-4.4.x/bin/arm-linux-androideabi-gdb , 不要使用NDK所附的arm-linux-androideabi-gdb.

5.在host端再開一個console,因為前一個console已經連到target端無法再使用囉
(host) $ ./arm-linux-androideabi-gdb  (假設你已經在$android_root/prebuilt/linux-x86/toolchain/arm-linux-androideabi-4.4.x/bin目錄下)

6. android 沒有strip過的 libraries 是放在   
  $android_root/out/target/product/YOUR_PRODUCT_NAME/symbols/system/lib 下
  而執行檔是放在$android_root/out/target/product/YOUR_PRODUCT_NAME/symbols/system/bin 下

7.(host) (gdb) target remote  :1234 
    (這是告訴gdb要跟remote端 的1234 port做連接, 這時候在target端的gdbserver會出現 Remote debugging from host 127.0.0.1 這就表示gdb與gdbserver已經連上線囉)

8.(host)(gdb) set solib-search-path $android_root/out/target/product/YOUR_PRODUCT_NAME/symbols/system/lib/:
$android_root/out/target/product/YOUR_PRODUCT_NAME/symbols/system/bin/
     (這是告訴gdb要在哪找相對應的debug symbol,這時候你應該可以看到gdb在load debug symbol )

9.如果你要debug InputDispatcher::notifyMotion  in InputDispatcher.cpp 你可以使用下述方法
  (host)(gdb)b android::InputDispatcher::notifyMotion 
  (你也可以使用Tab鍵 幫你省去打字的困擾, 當然啦,也可以利用Tab鍵得知gdb是否已正確找到你要設break point的點的debug symbol. 因為InputDispatcher.cpp在檔案開頭有加上namespace andeoid{}去包code,所以在找function時要把namespace加上去 , namespace::class_name::function_name)

10.(host)(gdb) c
    (讓被attached的process繼續跑下去)

11.這時候你去觸碰screen,就會觸動這break point

12.(host)(gdb)detach
     (釋放被attached的process)

13.(host)(gdb)quit
     (離開gdb)