步驟如下: 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下的相關日誌哦。