Proc/でのファイルの作成、読み取り、書き込み


Kernelで作成したコンテンツをuser(shellなど)で検証するには、次の手順に従います。


シェルでKernelデータを表示する方法->procファイルシステムを使用します.
カーネルで作成した値をuserで表示および保存できます.以下に示します.
 $ adb shell
 $ echo 1 > /sys/class/aa/somevalue_checker_enable
 $ cat /proc/somevalue_checker_log

cpu[0] entry(ns): 102959633222       comm: Binder:4743_7     pid:  5469      disabled_period(ns):
cpu[1] entry(ns): 102959644529       comm: Binder:7011_4     pid:  7855      disabled_period(ns):
cpu[1] entry(ns): 102962896799       comm: Thread-2          pid:  9961      disabled_period(ns):
cpu[0] entry(ns): 102963167260       comm: logd.writer       pid:  4240      disabled_period(ns):
cpu[0] entry(ns): 102963232606       comm: logd.writer       pid:  4240      disabled_period(ns):

 $ cat /proc/somevalue_checker_log > /data/saved.log
注意:
http://pointer-overloading.blogspot.com/2013/09/linux-creating-entry-in-proc-file.html
http://jake.dothome.co.kr/proc/

インプリメンテーション

#include <linux/seq_file.h>
#include <linux/proc_fs.h>

...

static int somevalue_checker_proc_show(struct seq_file *m, void *v)
{
        int i = 0;

        for (i = 0; i < PRMPT_LOG_BUF_SIZE; i++) {
                seq_printf(m, "cpu[%d] entry(ns): %12llu\t comm: %-16s\t pid: %6d\t disabled_period(ns):%10llu\t isr: %#x\n", \
                                somevalue_log_buf[i].cpu, somevalue_log_buf[i].entry, \
                                somevalue_log_buf[i].task_name, somevalue_log_buf[i].pid, \
                                somevalue_log_buf[i].duration, somevalue_log_buf[i].is_in_irq);
        }
        return 0;
}

static int somevalue_checker_proc_open(struct inode *inode, struct  file *file)
{
        return single_open(file, somevalue_checker_proc_show, NULL);
}

static const struct file_operations somevalue_checker_proc_fops = {
        .owner = THIS_MODULE,
        .open = somevalue_checker_proc_open,
        .read = seq_read,
        .llseek = seq_lseek,
        .release = single_release,
};

static int somevalue_checker_proc_init(void)
{
        proc_create("somevalue_checker_log", 0, NULL, &somevalue_checker_proc_fops);
        return 0;
}

static void somevalue_checker_proc_exit(void)
{
        remove_proc_entry("somevalue_checker_log", NULL);
}