在标准的linux内核开发过程中,使用 printk ,这是一个与printf输出打印齐名的函数,同样提供格式化输出功能,只是其有
打印级别且将信息保存到 /proc/kmsg 日志中,使用cat命令查看其信息[cat /proc/kmsg]
- <span style="font-size:14px;color:#003333;">#define KERN_EMERG "<0>" /* system is unusable */
- #define KERN_ALERT "<1>" /* action must be taken immediately */
- #define KERN_CRIT "<2>" /* critical conditions */
- #deinfe KERN_ERR "<3>" /* error conditions */
- #deinfe KERN_WARNING "<4>" /* warning conditions */
- #deinfe KERN_NOTICE "<5>" /* normal but significant condition */
- #deinfe KERN_INFO "<6>" /* informational */
- #deinfe KERN_DEBUG "<7>" /* debug-level messages */ </span>
Android系统在用户空间中提供了轻量级的logger日志系统,它是在内核中实现的一种设备驱动,与用户空间的logcat工具配合使用能够方便地跟踪调试程序。
Android系统中的C/C++日志接口是通过宏来使用的。在system/core/include/android/log.h定义了日志的级别:
/*
* Android log priority values, in ascending priority order.
*/
typedef enum android_LogPriority {
ANDROID_LOG_UNKNOWN = 0,
ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
ANDROID_LOG_VERBOSE,
ANDROID_LOG_DEBUG,
ANDROID_LOG_INFO,
ANDROID_LOG_WARN,
ANDROID_LOG_ERROR,
ANDROID_LOG_FATAL,
ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
} android_LogPriority;
为了使用方便,在system/core/include/cutils/log.h定义了相对应的宏:
#define LOGV(...) ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
#define LOGD(...) ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#define LOGI(...) ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
#define LOGW(...) ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
#define LOGE(...) ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
因为如果需要使用log输出,包含其头文件:#include <cutils/log.h> 并link其动态库:liblog.so 即可
#define LOG_TAG "XX_LOG_TAG" // 这里可以定义其输出的TAG
#include <cutils/log.h>
JAVA层打印:
import android.util.Log;
private static final String TAG = "XX_LOG_TAG";
Log.e(TAG, "This is the error log printed by Log.i in android user space.");
内核代码路径:
kernel/drivers/staging/android/logger.h
kernel/drivers/staging/android/logger.c
1、Logger驱动程序的相关数据结构
首先来看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 */
其中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实现文件:
- /*
- * 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_queue_head_t wq; /* wait queue for readers */
- struct list_head readers; /* this log's readers */
- struct mutex mutex; /* mutex protecting buffer */
- size_t w_off; /* current write head offset */
- size_t head; /* new readers start here */
- size_t size; /* size of the log */
- };
buffer是一个循环使用的环形缓冲区,缓冲区中保存的内容是以struct logger_entry为单位的,其组成方式是:
struct logger_entry | priority | tag | msg
/*
- * struct logger_reader - a logging device open for reading
- *
- * This object lives from open to release, so we don't need additional
- * reference counting. The structure is protected by log->mutex.
- */
- truct logger_reader {
- struct logger_log *log; /* associated log */
- struct list_head list; /* entry in logger_log's list */
- size_t r_off; /* current read head offset */
- ;
2、模块初始化过程:
logger是一个misc设备,那么misc设备是个什么东东呢?网上有很多资料,这里简要说明一下:
杂设备——misc
简单的说,杂设备就是内核自动帮你分配设备号并且自动创建设备文件。
1、自动分配设备号,是指所有注册为杂设备的设备的主设备号为10,而次设备号内核自动分配。
2、自动创建设备文件是指,内核会使用udev(前提是你已经移植udev),动态创建设备节点。
利用:
int misc_register(struct miscdevice * misc); //注册
int misc_deregister(struct miscdevice *misc); //注销
执行cat /proc/devices命令可以查看此类设备
# cat /proc/devices
Character devices:
1 mem
4 /dev/vc/0
4 tty
5 /dev/tty
5 /dev/console
5 /dev/ptmx
10 misc //创建的设备在misc中
在logger这里定义了三个日志设备:
- /*
- * Defines a log structure with name 'NAME' and a size of 'SIZE' bytes, which
- * must be a power of two, greater than LOGGER_ENTRY_MAX_LEN, and less than
- * LONG_MAX minus LOGGER_ENTRY_MAX_LEN.
- */
- #define DEFINE_LOGGER_DEVICE(VAR, NAME, SIZE) \
- static unsigned char _buf_ ## VAR[SIZE]; \
- static struct logger_log VAR = { \
- .buffer = _buf_ ## VAR, \
- .misc = { \
- .minor = MISC_DYNAMIC_MINOR, \
- .name = NAME, \
- .fops = &logger_fops, \
- .parent = NULL, \
- }, \
- .wq = __WAIT_QUEUE_HEAD_INITIALIZER(VAR .wq), \
- .readers = LIST_HEAD_INIT(VAR .readers), \
- .mutex = __MUTEX_INITIALIZER(VAR .mutex), \
- .w_off = 0, \
- .head = 0, \
- .size = SIZE, \
- };
- DEFINE_LOGGER_DEVICE(log_main, LOGGER_LOG_MAIN, 64*1024)
- DEFINE_LOGGER_DEVICE(log_events, LOGGER_LOG_EVENTS, 256*1024)
- DEFINE_LOGGER_DEVICE(log_radio, LOGGER_LOG_RADIO, 64*1024)
- DEFINE_LOGGER_DEVICE(log_system, LOGGER_LOG_SYSTEM, 64*1024)
这三个不同名称的设备文件操作方法如下:
- static const struct file_operations logger_fops = {
- .owner = THIS_MODULE,
- .read = logger_read,
- .aio_write = logger_aio_write,
- .poll = logger_poll,
- .unlocked_ioctl = logger_ioctl,
- .compat_ioctl = logger_ioctl,
- .open = logger_open,
- .release = logger_release,
- };
日志驱动程序模块的初始化函数为logger_init:
- static int __init init_log(struct logger_log *log)
- {
- int ret;
- ret = misc_register(&log->misc);
- if (unlikely(ret)) {
- printk(KERN_ERR "logger: failed to register misc "
- "device for log '%s'!\n", log->misc.name);
- return ret;
- }
- printk(KERN_INFO "logger: created %luK log '%s'\n",
- (unsigned long) log->size >> 10, log->misc.name);
- return 0;
- }
- static int __init logger_init(void)
- {
- int ret;
- ret = init_log(&log_main);
- if (unlikely(ret))
- goto out;
- ret = init_log(&log_events);
- if (unlikely(ret))
- goto out;
- ret = init_log(&log_radio);
- if (unlikely(ret))
- goto out;
- ret = init_log(&log_system);
- if (unlikely(ret))
- goto out;
- out:
- return ret;
- }
- device_initcall(logger_init);
3、日志写入重要过程分析:
注册的写入日志设备文件的方法为logger_aio_write
- <span style="color:#333333;">/*
- * logger_aio_write - our write method, implementing support for write(),
- * writev(), and aio_write(). Writes are our fast path, and we try to optimize
- * them above all else.
- */
- ssize_t logger_aio_write(struct kiocb *iocb, const struct iovec *iov,
- unsigned long nr_segs, loff_t ppos)
- {
- struct logger_log *log = file_get_log(iocb->ki_filp);
- size_t orig = log->w_off;
- </span><span style="color:#cc0000;">struct logger_entry header</span><span style="color:#333333;">;
- struct timespec now;
- ssize_t ret = 0;
- </span><span style="color:#ff6600;"><span style="white-space: pre;"> </span>// 下面重点构造 <span style="background-color: rgb(230, 230, 230); font-family: Arial;">struct logger_entry header 结构体</span></span><span style="color:#333333;">
- now = current_kernel_time();
- header.pid = current->tgid;
- header.tid = current->pid;
- header.sec = now.tv_sec;
- header.nsec = now.tv_nsec;
- header.len = min_t(size_t, iocb->ki_left, LOGGER_ENTRY_MAX_PAYLOAD);
- /* null writes succeed, return zero */
- if (unlikely(!header.len))
- return 0;
- mutex_lock(&log->mutex);
- /*
- * Fix up any readers, pulling them forward to the first readable
- * entry after (what will be) the new write offset. We do this now
- * because if we partially fail, we can end up with clobbered log
- * entries that encroach on readable buffer.
- */
- fix_up_readers(log, sizeof(struct logger_entry) + header.len);
- do_write_log(log, &header, sizeof(struct logger_entry));
- while (nr_segs-- > 0) {
- size_t len;
- ssize_t nr;
- /* figure out how much of this vector we can keep */
- len = min_t(size_t, iov->iov_len, header.len - ret);
- /* write out this segment's payload */
- nr = do_write_log_from_user(log, iov->iov_base, len);
- if (unlikely(nr < 0)) {
- log->w_off = orig;
- mutex_unlock(&log->mutex);
- return nr;
- }
- iov++;
- ret += nr;
- }
- mutex_unlock(&log->mutex);
- /* wake up any blocked readers */
- wake_up_interruptible(&log->wq);
- return ret;
- }</span>
然后根据nr_segs数目,通过一个while循环把iov的内容写入到日志缓冲区中,也就是日志的优先级别priority、日志Tag和日志主体Msg。
最后一个重要函数说明:
- /*
- * Fix up any readers, pulling them forward to the first readable
- * entry after (what will be) the new write offset. We do this now
- * because if we partially fail, we can end up with clobbered log
- * entries that encroach on readable buffer.
- */
- fix_up_readers(log, sizeof(struct logger_entry) + header.len);
- /*
- * fix_up_readers - walk the list of all readers and "fix up" any who were
- * lapped by the writer; also do the same for the default "start head".
- * We do this by "pulling forward" the readers and start head to the first
- * entry after the new write head.
- *
- * The caller needs to hold log->mutex.
- */
- static void fix_up_readers(struct logger_log *log, size_t len)
- {
- size_t old = log->w_off;
- size_t new = logger_offset(old + len);
- struct logger_reader *reader;
- if (clock_interval(old, new, log->head))
- log->head = get_next_entry(log, log->head, len);
- list_for_each_entry(reader, &log->readers, list)
- if (clock_interval(old, new, reader->r_off))
- reader->r_off = get_next_entry(log, reader->r_off, len);
- }
由于日志缓冲区是循环使用的,即旧的日志记录如果没有及时读取,而缓冲区的内容又已经用完时,就需要覆盖旧的记录来容纳新的记录。而这部分将要被覆盖的内容,有可能是某些reader的下一次要读取的日志所在的位置,以及为新的reader准备的日志开始读取位置head所在的位置。因此,需要调整这些位置,使它们能够指向一个新的有效的位置。
4、日志读取重要过程分析:
注册的读取日志设备文件的方法为logger_read
- /*
- * logger_read - our log's read() method
- *
- * Behavior:
- *
- * - O_NONBLOCK works
- * - If there are no log entries to read, blocks until log is written to
- * - Atomically reads exactly one log entry
- *
- * Optimal read size is LOGGER_ENTRY_MAX_LEN. Will set errno to EINVAL if read
- * buffer is insufficient to hold next entry.
- */
- static ssize_t logger_read(struct file *file, char __user *buf,
- size_t count, loff_t *pos)
- {
- struct logger_reader *reader = file->private_data;
- struct logger_log *log = reader->log;
- ssize_t ret;
- DEFINE_WAIT(wait);
- start:
- while (1) {
- prepare_to_wait(&log->wq, &wait, TASK_INTERRUPTIBLE);
- mutex_lock(&log->mutex);
- ret = (log->w_off == reader->r_off);
- mutex_unlock(&log->mutex);
- if (!ret)
- break;
- if (file->f_flags & O_NONBLOCK) {
- ret = -EAGAIN;
- break;
- }
- if (signal_pending(current)) {
- ret = -EINTR;
- break;
- }
- schedule();
- }
- finish_wait(&log->wq, &wait);
- if (ret)
- return ret;
- mutex_lock(&log->mutex);
- /* is there still something to read or did we race? */
- if (unlikely(log->w_off == reader->r_off)) {
- mutex_unlock(&log->mutex);
- goto start;
- }
- /* get the size of the next entry */
- ret = get_entry_len(log, reader->r_off);
- if (count < ret) {
- ret = -EINVAL;
- goto out;
- }
- /* get exactly one entry from the log */
- ret = do_read_log_to_user(log, reader, buf, ret);
- out:
- mutex_unlock(&log->mutex);
- return ret;
- }
- <span style="color:#333333;">/*
- * logger_open - the log's open() file operation
- *
- * Note how near a no-op this is in the write-only case. Keep it that way!
- */
- static int logger_open(struct inode *inode, struct file *file)
- {
- struct logger_log *log;
- int ret;
- ret = nonseekable_open(inode, file);
- if (ret)
- return ret;
- log = get_log_from_minor(MINOR(inode->i_rdev));
- if (!log)
- return -ENODEV;
- if (file->f_mode & FMODE_READ) {
- struct logger_reader *reader;
- reader = kmalloc(sizeof(struct logger_reader), GFP_KERNEL);
- if (!reader)
- return -ENOMEM;
- reader->log = log;
- INIT_LIST_HEAD(&reader->list);
- mutex_lock(&log->mutex);
- reader->r_off = log->head; </span><span style="color:#ff0000;">// 从<span style="font-family: Arial; line-height: 26px;">log->head位置开始读取日志的,保存在struct logger_reader的成员变量r_off中</span></span><span style="color:#333333;">
- list_add_tail(&reader->list, &log->readers);
- mutex_unlock(&log->mutex);
- </span><span style="color:#ff0000;">file->private_data = reader; // 这里对</span><span style="color: rgb(255, 0, 0); background-color: rgb(230, 230, 230); font-family: Arial;">private_data进行了赋值</span><span style="color:#ff0000;">
- </span><span style="color:#333333;">
- } else
- file->private_data = log;
- return 0;
- }</span>
ret = (log->w_off == reader->r_off);
即判断当前缓冲区的写入位置和当前读进程的读取位置是否相等,如果不相等,则说明有新的日志可读。
首先通过get_entry_len获取下一条可读的日志记录的长度(日志读取进程是以日志记录为单位进行读取的,一次只读取一条记录):
/* get the size of the next entry */
ret = get_entry_len(log, reader->r_off);
如果其中有数据时则利用do_read_log_to_user执行真正的读取动作:
/* get exactly one entry from the log */
ret = do_read_log_to_user(log, reader, buf, ret);
下面我们仔细看下get_entry_len函数:
- /*
- * get_entry_len - Grabs the length of the payload of the next entry starting
- * from 'off'.
- *
- * Caller needs to hold log->mutex.
- */
- static __u32 get_entry_len(struct logger_log *log, size_t off)
- {
- __u16 val;
- switch (log->size - off) {
- case 1:
- memcpy(&val, log->buffer + off, 1);
- memcpy(((char *) &val) + 1, log->buffer, 1);
- break;
- default:
- memcpy(&val, log->buffer + off, 2);
- }
- return sizeof(struct logger_entry) + val;
- }
我们知道每一条日志记录由两大部分组成,一部分是结构体:struct logger_entry,另外一部是payload有效负载即打印主体数据。
有效负载长度记录在struct logger_entry中的len字段中,占用两个字节,与结构的struct logger_entry的首地址相同。因此只要读取记录
最前面两个字节就可以了。
1、两个字节连在一起,直接读取即可,所以直接使用 memcpy(&val, log->buffer + off, 2);
2、两个字节不连在一起,则需要分别读取,这种情况就是读取缓冲区最后一个字节和第一个字节来获取其长度,而此时r_off与size的长度相差1
ok,继续分析真正的数据读取函数:
- /*
- * do_read_log_to_user - reads exactly 'count' bytes from 'log' into the
- * user-space buffer 'buf'. Returns 'count' on success.
- *
- * Caller must hold log->mutex.
- */
- static ssize_t do_read_log_to_user(struct logger_log *log,
- struct logger_reader *reader,
- char __user *buf,
- size_t count)
- {
- size_t len;
- /*
- * We read from the log in two disjoint operations. First, we read from
- * the current read head offset up to 'count' bytes or to the end of
- * the log, whichever comes first.
- */
- len = min(count, log->size - reader->r_off);
- if (copy_to_user(buf, log->buffer + reader->r_off, len))
- return -EFAULT;
- /*
- * Second, we read any remaining bytes, starting back at the head of
- * the log.
- */
- if (count != len)
- if (copy_to_user(buf + len, log->buffer, count - len))
- return -EFAULT;
- reader->r_off = logger_offset(reader->r_off + count);
- return count;
- }
5、其它函数:
logger_poll 用于log用户态调用select函数进行查询,利用
if (log->w_off != reader->r_off)
ret |= POLLIN | POLLRDNORM;
通知用户是否有有日记需要读取
logger_ioctl 用于一些常用的信息查询,
#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 */
获取缓冲区中数据长度,下一个日志的记录,比较有意义的是 LOGGER_FLUSH_LOG:
list_for_each_entry(reader, &log->readers, list)
reader->r_off = log->w_off;
log->head = log->w_off;
清除缓冲区中的所有数据