Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read android dmesg with code

Tags:

android

How can I read dmesg output in my program?

Thanks..

like image 876
Ran Avatar asked Dec 13 '25 18:12

Ran


1 Answers

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.

like image 102
ndyer Avatar answered Dec 15 '25 08:12

ndyer