当前位置:网站首页>Debug kernel code through proc interface
Debug kernel code through proc interface
2022-07-05 17:12:00 【pickled cabbage】
according to linux Introduction to device drive , Except for the most basic printk Other than print debugging , just so so
By creating a proc Interface to debug kernel code .
In the specific driver , Can be read Functions are nested in a function , Choose your own content to print .
(1) adopt struct proc_ops
proc_create establish proc Interface ( The header file ,#include<linux/proc_fs.h>)
proc.c
#include<linux/module.h>
#include<linux/init.h>
#include<linux/string.h>
#include<linux/proc_fs.h>
#include<linux/seq_file.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Tan xujia");
MODULE_VERSION("V1");
char *str = "hello proc\n";
int
hp_open(struct inode *inode, struct file *filp)
{
printk(KERN_INFO "open %ld\n", strlen(str));
return 0;
}
ssize_t
hp_read(struct file *filp, char __user *buff, size_t count, loff_t *f_pos)
{
ssize_t retval = 0;
int n = strlen(str);
if(*f_pos >= n)
goto out;
/* Return the remaining bytes */
if(*f_pos + count > n)
count = n - *f_pos;
/* Copy data to user space */
if(copy_to_user(buff, str, count)) {
retval = -EFAULT;
goto out;
}
/* Update offset */
*f_pos += count;
return count;
out:
return retval;
}
/* Only realize open and read function */
struct proc_ops hp_ops = {
.proc_open = hp_open,
.proc_read = hp_read,
};
static int
__init hello_init(void)
{
printk(KERN_INFO "hello_init\n");
/* Create this interface , adopt proc The directory can see this interface */
proc_create("hello_proc", 0, NULL, &hp_ops);
return 0;
}
static void
__exit hello_exit(void)
{
remove_proc_entry("hello_proc",NULL);
printk(KERN_INFO "hello_exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
obj-m :=proc.o
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.mod *.order *.symvers
(2) adopt
struct seq_operations
proc_create_seq() establish proc Interface ( The header file ,#include<linux/seq_file.h>)
proc.c
#include<linux/module.h>
#include<linux/init.h>
#include<linux/uaccess.h>
#include<linux/string.h>
#include<linux/proc_fs.h>
#include<linux/seq_file.h>
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Tan xujia");
MODULE_VERSION("V1");
char *str = "hello proc\n";
void*
hp_seq_start(struct seq_file *m, loff_t *pos)
{
printk(KERN_INFO "seq start\n");
if(*pos >= strlen(str))
return NULL;
return &str[*pos];
}
/*stop Nothing is done in the function , If in start The function has For example, what kind of memory to apply for , Can be in stop Function to release */
void
hp_seq_stop(struct seq_file *m, void *v)
{
printk(KERN_INFO "seq stop\n");
}
void*
hp_seq_next(struct seq_file *m, void *v, loff_t *pos)
{
printk(KERN_INFO "seq next\n");
(*pos)++;
if(*pos >= strlen(str))
return NULL;
return &str[*pos]; /* This is usually passed to show Functional v Shape parameter */
}
int
hp_seq_show(struct seq_file *m, void *v)
{
printk(KERN_INFO "seq_show\n");
seq_putc(m,*(char*)v); /*cat when , Print string */
return 0;
}
/* Generally, these functions are implemented */
const struct seq_operations seq_ops={
.start = hp_seq_start,
.stop = hp_seq_stop,
.next = hp_seq_next,
.show = hp_seq_show,
};
static int
__init hello_init(void)
{
printk(KERN_INFO "hello_init\n");
proc_create_seq("seq_proc", 0, NULL, &seq_ops);
return 0;
}
static void
__exit hello_exit(void)
{
remove_proc_entry("seq_proc", NULL);
printk(KERN_INFO "hello_exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
Makefile
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
obj-m :=proc.o
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.mod *.order *.symvers
边栏推荐
- 麻烦问下,DMS中使用Redis语法是以云数据库Redis社区版的命令为参考的嘛
- CMake教程Step3(添加库的使用要求)
- 2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
- 精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
- 【testlink】TestLink1.9.18常见问题解决方法
- Machine learning compilation lesson 2: tensor program abstraction
- 中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
- 飞桨EasyDL实操范例:工业零件划痕自动识别
- [wechat applet] read the life cycle and route jump of the applet
- Deep learning plus
猜你喜欢
项目引入jar从私服Nexus 拉去遇到的一个问题
Embedded UC (UNIX System Advanced Programming) -3
基于Redis实现延时队列的优化方案小结
激动人心!2022开放原子全球开源峰会报名火热开启!
高数 | 旋转体体积计算方法汇总、二重积分计算旋转体体积
Learnopongl notes (I)
stirring! 2022 open atom global open source summit registration is hot!
Cs231n notes (bottom) - applicable to 0 Foundation
Games101 notes (III)
Embedded-c Language-1
随机推荐
美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
阈值同态加密在隐私计算中的应用:解读
Jarvis OJ webshell analysis
【testlink】TestLink1.9.18常见问题解决方法
Embedded-c Language-4
C#实现水晶报表绑定数据并实现打印3-二维码条形码
云安全日报220705:红帽PHP解释器发现执行任意代码漏洞,需要尽快升级
Games101 notes (II)
Machine learning compilation lesson 2: tensor program abstraction
【微信小程序】一文读懂小程序的生命周期和路由跳转
项目引入jar从私服Nexus 拉去遇到的一个问题
张平安:加快云上数字创新,共建产业智慧生态
干货!半监督预训练对话模型 SPACE
thinkphp模板的使用
ECU简介
【剑指 Offer】62. 圆圈中最后剩下的数字
国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
Wsl2.0 installation
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
Learnopongl notes (II) - Lighting