android启动时自动抓取logcat

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rikeyone/article/details/84638520

创建一个脚本

 #!/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权限可以。

猜你喜欢

转载自blog.csdn.net/rikeyone/article/details/84638520