当前位置:操作系统 > 安卓/Android >>

Android 日志系统logcat内核代码分析

本文重点分
析其log内核驱动代码,使得我们对Android日志系统有一个深刻的认识。
 
 
内核代码路径:
 
kernel/drivers/staging/android/logger.h
kernel/drivers/staging/android/logger.c
 
 
1、Logger驱动程序的相关数据结构
 
首先来看logger.h头文件的内容:
 
 
[cpp] 
/* include/linux/logger.h 
 * 
 * Copyright (C) 2007-2008 Google, Inc. 
 * Author: Robert Love <rlove@android.com> 
 * 
 * This software is licensed under the terms of the GNU General Public 
 * License version 2, as published by the Free Software Foundation, and 
 * may be copied, distributed, and modified under those terms. 
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details. 
 * 
 */  
  
#ifndef _LINUX_LOGGER_H   
#define _LINUX_LOGGER_H   
  
#include <linux/types.h>   
#include <linux/ioctl.h>   
  
struct logger_entry {  
    __u16       len;    /* length of the payload */  
    __u16       __pad;  /* no matter what, we get 2 bytes of padding */  
    __s32       pid;    /* generating process's pid */  
    __s32       tid;    /* generating process's tid */  
    __s32       sec;    /* seconds since Epoch */  
    __s32       nsec;   /* nanoseconds */  
    char        msg[0]; /* the entry's payload */  
};  
  
#define LOGGER_LOG_RADIO    "log_radio" /* radio-related messages */   
#define LOGGER_LOG_EVENTS   "log_events"    /* system/hardware events */   
#define LOGGER_LOG_SYSTEM   "log_system"    /* system/framework messages */   
#define LOGGER_LOG_MAIN     "log_main"  /* everything else */   
  
#define LOGGER_ENTRY_MAX_LEN        (4*1024)   
#define LOGGER_ENTRY_MAX_PAYLOAD    \   
    (LOGGER_ENTRY_MAX_LEN - sizeof(struct logger_entry))  
  
#define __LOGGERIO  0xAE   
  
#define LOGGER_GET_LOG_BUF_SIZE     _IO(__LOGGERIO, 1) /* size of log */   
#define LOGGER_GET_LOG_LEN      _IO(__LOGGERIO, 2) /* used log len */   
#define LOGGER_GET_NEXT_ENTRY_LEN   _IO(__LOGGERIO, 3) /* next entry len */   
#define LOGGER_FLUSH_LOG        _IO(__LOGGERIO, 4) /* flush log */   
  
#endif /* _LINUX_LOGGER_H */  
 
/* include/linux/logger.h
 *
 * Copyright (C) 2007-2008 Google, Inc.
 * Author: Robert Love <rlove@android.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
 
#ifndef _LINUX_LOGGER_H
#define _LINUX_LOGGER_H
 
#include <linux/types.h>
#include <linux/ioctl.h>
 
struct logger_entry {
__u16 len; /* length of the payload */
__u16 __pad; /* no matter what, we get 2 bytes of padding */
__s32 pid; /* generating process's pid */
__s32 tid; /* generating process's tid */
__s32 sec; /* seconds since Epoch */
__s32 nsec; /* nanoseconds */
char msg[0]; /* the entry's payload */
};
 
#define LOGGER_LOG_RADIO "log_radio" /* radio-related messages */
#define LOGGER_LOG_EVENTS "log_events" /* system/hardware events */
#define LOGGER_LOG_SYSTEM "log_system" /* system/framework messages */
#define LOGGER_LOG_MAIN "log_main" /* everything else */
 
#define LOGGER_ENTRY_MAX_LEN (4*1024)
#define LOGGER_ENTRY_MAX_PAYLOAD \
(LOGGER_ENTRY_MAX_LEN - sizeof(struct logger_entry))
 
#define __LOGGERIO 0xAE
 
#define LOGGER_GET_LOG_BUF_SIZE _IO(__LOGGERIO, 1) /* size of log */
#define LOGGER_GET_LOG_LEN _IO(__LOGGERIO, 2) /* used log len */
#define LOGGER_GET_NEXT_ENTRY_LEN _IO(__LOGGERIO, 3) /* next entry len */
#define LOGGER_FLUSH_LOG _IO(__LOGGERIO, 4) /* flush log */
 
#endif /* _LINUX_LOGGER_H */
 
struct logger_entry是一个用于描述一条Log记录的结构体。
其中len成员变量记录了这条记录的有效负载的长度,有效负载指定的日志记录本身的长度,但是不包括用于描述这个记录的struct logger_entry结构体。
 
从struct logger_entry中也可以看出:优先级别Priority、Tag字符串以及Msg字符串,pid和tid成员变量分别用来记录是哪条进程写入了这条记录。sec和nsec成员变量记录日志写的时间。msg成员变量记录的就有效负载的内容了,它的大小由len成员变量来确定
 
 
 
 
 
#define LOGGER_ENTRY_MAX_LEN(4*1024)
#define LOGGER_ENTRY_MAX_PAYLOAD \
(LOGGER_ENTRY_MAX_LEN - sizeof(struct logger_entry))
 
 
这两个宏定义记录了 最大有效负载长度。
 
 
 
 
再分析下logger.c实现文件:
 
 
[cpp]  
/* 
 * struct logger_log - represents a specific log, such as 'main' or 'radio' 
 * 
 * This structure lives from module insertion until module removal, so it does 
 * not need additional reference counting. The structure is protected by the 
 * mutex 'mutex'. 
 */  
struct logger_log {  
    unsigned char       *buffer;/* the ring buffer itself */  
    struct miscdevice   misc;   /* misc device representing the log */  
    wait_qu
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,