Android8.0 user版本adb 的log输出到串口中

2023-05-16

我们在调试adb时不能使用logcat,这个时候我们需要把adb的log输出到串口,init就能实现,我们仿造init的代码,在adbd的main函数中调用了如下函数InitKernelLogging(nullptr);这个函数在init进程中也有调用。这个函数的意思把标准输入输出,标准错误全部写到/sys/fs/selinux/null,也就没有了。然后定了一InitLogging为kernelLogger,就是写在ksmg中。

void InitKernelLogging(char* argv[]) {
    // Make stdin/stdout/stderr all point to /dev/null.
    int fd = open("/sys/fs/selinux/null", O_RDWR);
    if (fd == -1) {
        int saved_errno = errno;
        android::base::InitLogging(argv, &android::base::KernelLogger);
        errno = saved_errno;
        PLOG(FATAL) << "Couldn't open /sys/fs/selinux/null";
    }
    dup2(fd, 0);
    dup2(fd, 1);
    dup2(fd, 2);
    if (fd > 2) close(fd);

    android::base::InitLogging(argv, &android::base::KernelLogger);
}

剩下的打印我们需要将log级别提高到ERROR

LOG(ERROR) << "InitKernelLogging start";

当然在adbd中调用InitKernelLogging函数还需selinux的相关权限,这个和kernel log相关。这个class是kmsg_device,而kmsg_device又属于dev_type,因此我们把init.te中dev_type相关的权限和kmsg_device都移植过来到adb.te中。

allow adbd kmsg_device:chr_file rw_file_perms;
allow adbd dev_type:blk_file r_file_perms;
allow adbd dev_type:dir create_dir_perms;
allow adbd dev_type:lnk_file create;
allow adbd {
  dev_type
  -kmem_device
  -port_device
  -device
  -vndbinder_device
  }:chr_file { read open };
auditallow adbd {
  dev_type
  -alarm_device
  -ashmem_device
  -binder_device
  -console_device
  -device
  -devpts
  -dm_device
  -hwbinder_device
  -hw_random_device
  -keychord_device
  -kmem_device
  -kmsg_device
  -null_device
  -owntty_device
  -port_device
  -ptmx_device
  -random_device
  -zero_device
}:chr_file { read open };
allow adbd { dev_type -kmem_device -port_device }:chr_file setattr;

这样我们就能把adbd的log输出到串口上了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android8.0 user版本adb 的log输出到串口中 的相关文章

随机推荐