1. 程式人生 > >android啟動時自動抓取logcat

android啟動時自動抓取logcat

建立一個指令碼

 #!/bin/sh
 
 log_path=/cache/boot_logcat.log
 kernel_log=/cache/boot_kernel.log
 
 while true 
 do
     df | grep cache
     cache_check=$?
     echo ${cache_check}
     if [ ${cache_check} = 1 ]; then
         echo "wait for cache!"
         sleep 2
     else
         echo "cache mounted!"
         break
     fi
 done
 
 if [ -e ${log_path}".1" ]; then
     mv /cache/boot_kernel.log.1 /cache/boot_kernel.log.2
     mv /cache/boot_logcat.log.1 /cache/boot_logcat.log.2
 fi
 
 if [ -e ${log_path} ]; then
     mv /cache/boot_logcat.log /cache/boot_logcat.log.1
     mv /cache/boot_kernel.log /cache/boot_kernel.log.1
 fi
 
 logcat -v time -b kernel > /cache/boot_kernel.log &
 logcat -v time > /cache/boot_logcat.log                                                                          

以我的情況為例,我想定位我的機器為什麼userdata掛載失敗,所以這個指令碼會抓logcat到cache分割槽中。

在initrc中建立service

service logcat  /system/bin/sh /vendor/etc/logcat.sh
    class main
    user root
    group shell log readproc
    oneshot
    seclabel u:r:qti-testscripts:s0

service的主體是sh,logcat.sh只是作為一個引數傳遞給shell進行執行。

裝置配置

install.sh

#!/bin/bash
#adb wait-for-device
adb root
adb shell mount -o remount,rw /vendor
adb push logcat.sh /vendor/etc/
adb push init.target.rc /vendor/etc/init/hw/
adb reboot

這個install指令碼是在PC上執行的,需要手機連線PC,並且開啟開發者許可權,我們可以使用adb push我們的測試指令碼到機器對應的目錄。

思考

1.為什麼service中不能直接把主體設定為我們的logcat.sh指令碼,而只把logcat.sh作為引數來傳遞。
原因也簡單,因為我們shell是已經有對應的selinux配置了,不必再去修改selinux相關的東西。而如果主體是我們的指令碼,那麼我們就需要專門為他建立selinux domain等一堆東西。

2.有沒有其他方式
除了指令碼的方式以外,還可以直接把service設定為logcat,因為指令碼本身也是呼叫的/system/bin/logcat這個可執行檔案來抓logcat的。只是這樣做,和前面一樣,必須要配置相關的selinux許可權可以。