Adb logcat prints kernel messages

Source: Internet
Author: User

By default, adb logcat can only display the debugging information of the application. I modified logcat. cpp so that it can print the kernel debugging information at the same time:

Modified file: system/core/logcat. cpp

1. First add the header file
# Include <sys/klog. h> // add

2. Define the TAG used
# Define KERNEL_TAG "Kernel"

3. Replace the readLogLines Function

Static void readLogLines (log_device_t * devices)
{
Log_device_t * dev;
Int max = 0;
Int ret;
Int queued_lines = 0;
Bool sleep = true;
Char buffer [256] = {0}; // add by zhaofei
 
 
Int result;
Fd_set readset;
 
 
For (dev = devices; dev = dev-> next ){
If (dev-> fd> max ){
Max = dev-> fd;
}
}
 
 
While (1 ){
Do {
Timeval timeout = {0, 5000/* 5 ms */}; // If we oversleep it's OK, I. e. ignore EINTR.
FD_ZERO (& readset );
For (dev = devices; dev = dev-> next ){
FD_SET (dev-> fd, & readset );
}
Result = select (max + 1, & readset, NULL, NULL, sleep? NULL: & timeout );
} While (result =-1 & errno = EINTR );
 
 
If (result> = 0 ){
For (dev = devices; dev = dev-> next ){
If (FD_ISSET (dev-> fd, & readset )){
Queued_entry_t * entry = new queued_entry_t ();
/* NOTE: driver guarantees we read exactly one full entry */
Ret = read (dev-> fd, entry-> buf, LOGGER_ENTRY_MAX_LEN );
If (ret <0 ){
If (errno = EINTR ){
Delete entry;
Goto next;
}
If (errno = EAGAIN ){
Delete entry;
Break;
}
Perror ("logcat read ");
Exit (EXIT_FAILURE );
}
Else if (! Ret ){
Fprintf (stderr, "read: Unexpected EOF! \ N ");
Exit (EXIT_FAILURE );
}
 
 
Entry-> entry. msg [entry-> entry. len] = '\ 0 ';
 
 
Dev-> enqueue (entry );
++ Queued_lines;
 
 
# If 1 // read kernel log
If (ret = klogctl (9, buffer, sizeof (buffer)> 0 ){
If (ret = klogctl (2, buffer, sizeof (buffer)> 0 ){
Entry-> entry. tid = 0;
Entry-> entry. pid = getpid ();
/* Priority */
Entry-> entry. msg [0] = Android_LOG_INFO;
/* Tag */
Strcpy (entry-> entry. msg + 1, KERNEL_TAG );
/* Message */
Strncpy (entry-> entry. msg + 1 + sizeof (KERNEL_TAG), buffer, ret );
Entry-> entry. len = 1 + sizeof (KERNEL_TAG) + ret + 1;
Entry-> entry. msg [entry-> entry. len] = '/0 ';
/*
If (g_printBinary ){
PrintBinary (dev, entry-> entry );
} Else {
(Void) processBuffer (dev, entry-> entry );
}
*/
PrintNextEntry (dev );
}
}
# Endif
}
}
 
 
If (result = 0 ){
// We did our short timeout trick and there's nothing new
// Print everything we have and wait for more data
Sleep = true;
While (true ){
ChooseFirst (devices, & dev );
If (dev = NULL ){
Break;
}
If (g_tail_lines = 0 | queued_lines <= g_tail_lines ){
PrintNextEntry (dev );
} Else {
SkipNextEntry (dev );
}
-- Queued_lines;
}
 
 
// The caller requested to just dump the log and exit
If (g_nonblock ){
Exit (0 );
}
} Else {
// Print all that aren't the last in their list
Sleep = false;
While (g_tail_lines = 0 | queued_lines> g_tail_lines ){
ChooseFirst (devices, & dev );
If (dev = NULL | dev-> queue-> next = NULL ){
Break;
}
If (g_tail_lines = 0 ){
PrintNextEntry (dev );
} Else {
SkipNextEntry (dev );
}
-- Queued_lines;
}
}
}
Next:
;
}
}
The kernel debugging information level is not converted to the LOG level of Androind, entry-> entry. msg [0] = Android_LOG_INFO; The ANDROID_LOG_INFO level is used, and the process ID uses the current process ID.

Then you can use logcat to capture the log of the kernel!

To print only Kernel messages, use: adb logcat-s Kernel: I

From the column zhangjie201412


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.