read android dmesg with code

来源:互联网 发布:算法模型电商 编辑:程序博客网 时间:2024/05/25 05:37

You can write some Android NDK code which calls the klogctl functions. Something like:

#include <sys/klog.h>#define KLOG_READ_ALL   3#define KLOG_LEN    (1 << 17)char buf[KLOG_LEN];if (klogctl(KLOG_READ_ALL, buf, KLOG_LEN) < 0){  printf("Error %s reading dmesg\n", strerror(errno));}else{  /* do something with contents of buf */}

However, in Android 4.1 Jelly Bean they have implemented a security feature which disallows access to the dmesg messages. The code above will fail with an "Operation not permitted" error. If you have root access to the device, you can turn off dmesg_restrict:

echo 0 > /proc/sys/kernel/dmesg_restrict

Also, some recent devices have SELinux enabled, in which case you will need to do

setenforce 0

If you don't have root access, you're pretty much out of luck.

原创粉丝点击