본문 바로가기

Kernel/Practice6

misc device 例 기본적인 read(), write(), ioctl(), poll() 종류의 system call 작업이 가능한 misc device 예제를 작성한다. Driver Source/* file: xxx_device.c */#include #include #include #include #include #define DEVICE_NAME "xxx"#define XXX_IOCTL_CMD_READ _IOR('j', 1, int)#define XXX_IOCTL_CMD_WRITE _IOW('j', 2, int)#define XXX_IOCTL_CMD_WAKE_UP _IO('j', 3)int xxx_var = 1123;atomic_t xxx_poll_ready = ATOMIC_INIT(0);struct wait_queue.. 2024. 9. 21.
kwork 사용 例 kernel work interface를 이용하여 worker thread를 생성하는 방법 (kernel module)매 1초마다 thread 실행 /* * example_work.c */#include #include #include #define TEST_WORK_QUEUE_NAME "Test_Work_Queue"#define TEST_WORK_PERIOD (1 * HZ) // every 1 secstatic ulong test_number_iteration;static bool test_work_die;static void test_work_handler(struct work_struct * wrk);static struct workqueue_struct *test_workqueue;static D.. 2024. 2. 2.
hrtimer 사용 例 hrtimer를 이용하여 timer interrupt 발생하는 예 (kernel module)매 1000ms 마다 interrupt 발생 /* * example_hrtimer.c */#include #include #include #define TEST_HRTIMER_PERIOD_MS (1000)struct hrtimer test_timer;ulong test_nr_iteration = 0;enum hrtimer_restart test_irq_timer_handler(struct hrtimer *timer){ BUG_ON(!in_interrupt()); /* * DO SOMETHING */ test_nr_iteration++; // reset expire time .. 2024. 2. 2.
process 생성/종료 ftrace 얻기 ftrace 설정 #!/bin/bash echo 0 > /sys/kernel/debug/tracing/tracing_on sleep 1 echo "tracing_off" echo 0 > /sys/kernel/debug/tracing/events/enable sleep 1 echo "events disabled" echo secondary_start_kernel > /sys/kernel/debug/tracing/set_ftrace_filter sleep 1 echo "set_ftrace_filter init" echo function > /sys/kernel/debug/tracing/current_tracer sleep 1 echo "function tracer enabled" echo 1 > /sys/k.. 2024. 2. 2.
debugfs 생성 ( /sys/kernel/debug/) 다음과 같은 경로에 read/write 가능한 debug fs를 생성한다 cat /sys/kernel/debug/rpi_debug/val 아래의 코드를 module로 생성하여 빌드한다. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include uint32_t my_debug_state = 0x1000; static struct dentry *my_debugfs_root; static int my_stat_get(void *data, u64 *v.. 2024. 2. 2.
함수 ftrace 얻기 ftrace를 완전히 사용하기 위한 configuration 활성화 CONFIG_FTRACE=y CONFIG_DYNAMIC_FTRACE=y CONFIG_FUNCTION_TRACER=y CONFIG_FUNCTION_GRAPH_TRACER=y CONFIG_IRQSOFF_TRACER=y CONFIG_SCHED_TRACER=y CONFIG_FUNCTION_PROFILER=y CONFIG_STACK_TRACER=y CONFIG_TRACER=SNAPSHOT=y 먼저 아래의 명령어로 trace 가능한 함수 확인 cat /sys/kernel/debug/tracing/available_filter_functions show_interrupt() 함수 정보를 얻기위해 아래 스크립트 사용 #!/bin/bash # how t.. 2024. 2. 2.