Kernel - proc/ 에 파일 생성하고 읽고 쓰기

숲사람·2022년 3월 24일
0

Linux Kernel

목록 보기
4/10

Kernel 에서 작성된 내용을 user (shell등) 에서 확인하기 위한 방법

Kernel 데이터를 Shell에서 확인할 수 있는 방법 -> proc file system 사용.

다음과 같이 커널에서 생성한 값을 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);
}

profile
기록 & 정리 아카이브 용도 (보다 완성된 글은 http://soopsaram.com/documentudy)

0개의 댓글