当前位置:网站首页>通过proc接口调试内核代码
通过proc接口调试内核代码
2022-07-05 16:28:00 【酸菜。】
根据linux设备驱动一书的介绍,除了最基本printk打印调试以外,还可以
通过创建proc接口进行内核代码的调试。
在具体的驱动程序里,可以将read函数嵌套在某个函数里面,选择自己打印的内容进行打印。
(1)通过struct proc_ops
proc_create创建proc接口(头文件,#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;
/*将剩下的字节返回即可*/
if(*f_pos + count > n)
count = n - *f_pos;
/*拷贝数据到用户空间*/
if(copy_to_user(buff, str, count)) {
retval = -EFAULT;
goto out;
}
/*更新偏移量*/
*f_pos += count;
return count;
out:
return retval;
}
/*仅实现open和read功能*/
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");
/*创建这个接口,通过proc目录可以看到这个接口*/
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)通过
struct seq_operations
proc_create_seq() 创建proc接口(头文件,#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函数里面不做什么事情,如果在start函数里面有 例如申请什么内存之类的,可以在stop函数里面进行释放*/
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]; /*这个一般是传给show函数的v形参*/
}
int
hp_seq_show(struct seq_file *m, void *v)
{
printk(KERN_INFO "seq_show\n");
seq_putc(m,*(char*)v); /*cat时,将字符串打印出来*/
return 0;
}
/*一般是实现这几个函数*/
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
边栏推荐
- How was the middle table destroyed?
- Accès aux données - intégration du cadre d'entité
- 【Web攻防】WAF检测技术图谱
- Facing new challenges and becoming a better self -- attacking technology er
- sqlserver 做cdc 要对数据库性能有什么要求么
- Binary tree related OJ problems
- 齐宣王典故
- 手机开证券账户安全吗?怎么买股票详细步骤
- composer安装报错:No composer.lock file present.
- [first lecture on robot coordinate system]
猜你喜欢
项目引入jar从私服Nexus 拉去遇到的一个问题
Global Data Center released DC brain system, enabling intelligent operation and management through science and technology
Summary of PHP pseudo protocol of cisp-pte
【729. 我的日程安排表 I】
How does win11 change icons for applications? Win11 method of changing icons for applications
Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
How to set the WiFi password of the router on the computer
兰空图床苹果快捷指令
拷贝方式之DMA
Summary of methods for finding intersection of ordered linked list sets
随机推荐
PHP 严格模式
齐宣王典故
easyNmon使用汇总
【Web攻防】WAF检测技术图谱
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
Deep learning plus
阈值同态加密在隐私计算中的应用:解读
Benji Bananas 会员通行证持有人第二季奖励活动更新一览
Iphone14 with pill screen may trigger a rush for Chinese consumers
How to install MySQL
Bs-xx-042 implementation of personnel management system based on SSM
Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert
Get ready for the pre-season card game MotoGP ignition champions!
NPM installation
Etcd 构建高可用Etcd集群
The difference between searching forward index and inverted index
Can you help me see what the problem is? [ERROR] Could not execute SQL stateme
Summary of PHP pseudo protocol of cisp-pte
Jarvis OJ simple network management protocol
[Jianzhi offer] 66 Build product array