Android中的Logcat方法查看内核的日志

步骤如下: 1.在Android的源码中(目标路径为:system/core/logcat/logcat.cpp),将其此logcat.cpp文件中的static void readLogLines(int logfd)函数作出如下修改:

static void readLogLines(int logfd) {

char buffer[256] = {0} ;

while (1) {

unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1] attribute ((aligned(4)));

struct logger_entry *entry = (struct logger_entry *) buf;

int ret;

ret = read(logfd, entry, LOGGER_ENTRY_MAX_LEN);

if (ret < 0) {

if (errno == EINTR)

continue;

if (errno == EAGAIN)

break;

perror("logcat read"); exit(EXIT_FAILURE);

} else if (!ret) {

fprintf(stderr, "read: Unexpected EOF!/n"); exit(EXIT_FAILURE);

}

/* NOTE: driver guarantees we read exactly one full entry */

entry->msg[entry->len] = '/0';

if (g_printBinary) {

printBinary(entry);

} else {

(void) processBuffer(entry);

}

/*读入内核调试信息*/ if((ret = klogctl(9, buffer, sizeof(buffer))) > 0) {

if((ret = klogctl(2, buffer, sizeof(buffer))) > 0) {

entry->tid = 0;

entry->pid = getpid();

/*priority*/

entry->msg[0] = ANDROID_LOG_INFO;

/*tag*/

strcpy(entry->msg+1, KERNEL_TAG); /*message*/

strncpy(entry->msg+1+sizeof(KERNEL_TAG), buffer, ret);

entry->len = 1 + sizeof(KERNEL_TAG) + ret + 1;

entry->msg[entry->len] = '/0';

if (g_printBinary) {

printBinary(entry);

} else {

(void) processBuffer(entry);

}

}

}

}

}

当然经过此步的修改后,在源码编译中一定会出现问题,会出现什么呢?答案是在编译时找不到相应的类库。那么怎么办呢? 不要着急,第2步会说明。

2.在如上修改的.cpp文件的头文件中加入如下两个头文件:

#include <sys/klog.h>

#define KERNEL_TAG "Kernel"

great! 此时将可以顺利通过编译了哦。

3.即将修改后的源文件进行整体编译一边,方法见源码编译的相关方法。边以后生成的logcat文件放在/android/out/host /common/obj/JAVA_LIBRARIES/ddmuilib_intermediates/classes/com/android /ddmuilib/logcat目录下。

4.将上述目录下的logcat文件adb push到你的手机上的/system/bin目录下,然后adb reboot一下手机。之后就可以正常执行logcat了,相应的能够打印出kernel下的相关日志哦。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值