Android架构分析之LOG模块
Android的LOG模块分为内核驱动部分和用户空间接口部分。
一、内核LOG模块分析
我们先来看内核驱动部分,其代码位于drivers/staging/android/logger.c文件中。按照分析Linux内核驱动程序的惯例,我们从模块初始化函数开始分析:
[cpp] 588static int __init logger_init(void)
589{
590 int ret;
591
592 ret =init_log(&log_main);
593 if (unlikely(ret))
594 goto out;
595
596 ret =init_log(&log_events);
597 if (unlikely(ret))
598 goto out;
599
600 ret =init_log(&log_radio);
601 if (unlikely(ret))
602 goto out;
603
604out:
605 return ret;
606}
607device_initcall(logger_init);
588static int __init logger_init(void)
589{
590 int ret;
591
592 ret =init_log(&log_main);
593 if (unlikely(ret))
594 goto out;
595
596 ret =init_log(&log_events);
597 if (unlikely(ret))
598 goto out;
599
600 ret =init_log(&log_radio);
601 if (unlikely(ret))
602 goto out;
603
604out:
605 return ret;
606}
607device_initcall(logger_init);
logger_init函数即是LOG模块初始化函数,其调用了3次init_log函数,注册了log_main,log_events,log_radio三个LOG设备,init_log函数定义如下:
[cpp] 571static int __init init_log(struct logger_log *log)
572{
573 int ret;
574
575 ret =misc_register(&log->misc);
576 if (unlikely(ret)) {
577 printk(KERN_ERR"logger: failed to register misc "
578 "device forlog '%s'!\n", log->misc.name);
579 return ret;
580 }
581
582 printk(KERN_INFO"logger: created %luK log '%s'\n",
583 (unsigned long)log->size >> 10, log->misc.name);
584
585 return 0;
586}
571static int __init init_log(struct logger_log *log)
572{
573 int ret;
574
575 ret =misc_register(&log->misc);
576 if (unlikely(ret)) {
577 printk(KERN_ERR"logger: failed to register misc "
578 "device forlog '%s'!\n", log->misc.name);
579 return ret;
580 }
581
582 printk(KERN_INFO"logger: created %luK log '%s'\n",
583 (unsigned long)log->size >> 10, log->misc.name);
584
585 return 0;
586}
575行,调用misc_register函数,注册misc设备。
init_log函数的参数是logger_log结构体类型,该类型代表一个LOG设备,其定义如下:
[cpp] 30/*
31 * struct logger_log -represents a specific log, such as 'main' or 'radio'
32 *
33 * This structure lives frommodule insertion until module removal, so it does
34 * not need additionalreference counting. The structure is protected by the
35 * mutex 'mutex'.
36 */
37struct logger_log {
38 unsigned char * buffer; /* the ring buffer itself */
39 struct miscdevice misc; /* misc device representing the log */
40 wait_queue_head_t wq; /* wait queue for readers */
41 struct list_head readers; /* this log's readers */
42 struct mutex mutex; /* mutex protecting buffer */
43 size_t w_off; /* current write head offset */
44 size_t head; /* new readers start here */
45 size_t size; /* size of the log */
46};
30/*
31 * struct logger_log -represents a specific log, such as 'main' or 'radio'
32 *
33 * This structure lives frommodule insertion until module removal, so it does
34 * not need additionalreference counting. The structure is protected by the
35 * mutex 'mutex'.
36 */
37struct logger_log {
38 unsigned char * buffer; /* the ring buffer itself */
39 struct miscdevice misc; /* misc device representing the log */
40 wait_queue_head_t wq; /* wait queue for readers */
41 struct list_head readers; /* this log's readers */
42 struct mutex mutex; /* mutex protecting buffer */
43 size_t w_off; /* current write head offset */
44 size_t head; /* new readers start here */
45 size_t size; /* size of the log */
46};
log_main,log_events,log_radio三个LOG设备都是logger_log结构体类型的变量,其定义如下:
[cpp] 533/*
534 * Defines a log structure with name 'NAME' and a size of 'SIZE'bytes, which
535 * must be a power of two, greater than LOGGER_ENTRY_MAX_LEN, andless than
536 * LONG_MAX minus LOGGER_ENTRY_MAX_LEN.
537 */
538#define DEFINE_LOGGER_DEVICE(VAR, NAME, SIZE)
补充:移动开发 , Android ,