adb logcat 打印内核消息

在默认情况下,adb logcat只能显示应用程序的调试信息,我把logcat.cpp修改了一下,让它同时可以打印内核调试信息:

修改的文件:system/core/logcat/logcat.cpp

1、首先先加入头文件
#include <sys/klog.h> //add by Jay

2、定义所使用的TAG
#define KERNEL_TAG "Kernel"


3、替换readLogLines函数


  1. staticvoidreadLogLines(log_device_t*devices)
  2. {
  3. log_device_t*dev;
  4. intmax=0;
  5. intret;
  6. intqueued_lines=0;
  7. boolsleep=true;
  8. charbuffer[256]={0};//addby Jay Zhang
  9. intresult;
  10. fd_setreadset;
  11. for(dev=devices;dev;dev=dev->next){
  12. if(dev->fd>max){
  13. max=dev->fd;
  14. }
  15. }
  16. while(1){
  17. do{
  18. timevaltimeout={0,5000/*5ms*/};//Ifweoversleepit'sok,i.e.ignoreEINTR.
  19. FD_ZERO(&readset);
  20. for(dev=devices;dev;dev=dev->next){
  21. FD_SET(dev->fd,&readset);
  22. }
  23. result=select(max+1,&readset,NULL,NULL,sleep?NULL:&timeout);
  24. }while(result==-1&&errno==EINTR);
  25. if(result>=0){
  26. for(dev=devices;dev;dev=dev->next){
  27. if(FD_ISSET(dev->fd,&readset)){
  28. queued_entry_t*entry=newqueued_entry_t();
  29. /*NOTE:driverguaranteeswereadexactlyonefullentry*/
  30. ret=read(dev->fd,entry->buf,LOGGER_ENTRY_MAX_LEN);
  31. if(ret<0){
  32. if(errno==EINTR){
  33. deleteentry;
  34. gotonext;
  35. }
  36. if(errno==EAGAIN){
  37. deleteentry;
  38. break;
  39. }
  40. perror("logcatread");
  41. exit(EXIT_FAILURE);
  42. }
  43. elseif(!ret){
  44. fprintf(stderr,"read:UnexpectedEOF!\n");
  45. exit(EXIT_FAILURE);
  46. }
  47. entry->entry.msg[entry->entry.len]='\0';
  48. dev->enqueue(entry);
  49. ++queued_lines;
  50. #if1//readkernellog
  51. if((ret=klogctl(9,buffer,sizeof(buffer)))>0){
  52. if((ret=klogctl(2,buffer,sizeof(buffer)))>0){
  53. entry->entry.tid=0;
  54. entry->entry.pid=getpid();
  55. /*priority*/
  56. entry->entry.msg[0]=Android_LOG_INFO;
  57. /*tag*/
  58. strcpy(entry->entry.msg+1,KERNEL_TAG);
  59. /*message*/
  60. strncpy(entry->entry.msg+1+sizeof(KERNEL_TAG),buffer,ret);
  61. entry->entry.len=1+sizeof(KERNEL_TAG)+ret+1;
  62. entry->entry.msg[entry->entry.len]='/0';
  63. /*
  64. if(g_printBinary){
  65. printBinary(dev,entry->entry);
  66. }else{
  67. (void)processBuffer(dev,entry->entry);
  68. }
  69. */
  70. printNextEntry(dev);
  71. }
  72. }
  73. #endif
  74. }
  75. }
  76. if(result==0){
  77. //wedidourshorttimeouttrickandthere'snothingnew
  78. //printeverythingwehaveandwaitformoredata
  79. sleep=true;
  80. while(true){
  81. chooseFirst(devices,&dev);
  82. if(dev==NULL){
  83. break;
  84. }
  85. if(g_tail_lines==0||queued_lines<=g_tail_lines){
  86. printNextEntry(dev);
  87. }else{
  88. skipNextEntry(dev);
  89. }
  90. --queued_lines;
  91. }
  92. //thecallerrequestedtojustdumpthelogandexit
  93. if(g_nonblock){
  94. exit(0);
  95. }
  96. }else{
  97. //printallthataren'tthelastintheirlist
  98. sleep=false;
  99. while(g_tail_lines==0||queued_lines>g_tail_lines){
  100. chooseFirst(devices,&dev);
  101. if(dev==NULL||dev->queue->next==NULL){
  102. break;
  103. }
  104. if(g_tail_lines==0){
  105. printNextEntry(dev);
  106. }else{
  107. skipNextEntry(dev);
  108. }
  109. --queued_lines;
  110. }
  111. }
  112. }
  113. next:
  114. ;
  115. }
  116. }
这里没有把内核调试信息的级别转换成Androind的LOG级别,entry->entry.msg[0] =Android_LOG_INFO;使用了ANDROID_LOG_INFO级别,进程ID用了当前的进程ID。

然后就可以使用logcat来抓取kernel的log了!

若只打印内核消息,使用:adb logcat -s Kernel:I
http://www.linuxidc.com/Linux/2011-09/43674.htm

你可能感兴趣的:(logcat)