wsl + qemu + busybox/buildroot 调试:linux应用->驱动接口->linux内核

目标

这篇博客是在上篇博客的基础上写的:wsl + qemu + busybox/buildroot 调试:linux应用->系统调用->linux内核

应用调用系统调用,最后会运行内核里的代码

如果我们在内核里自己写了一个模块,然后应用调用驱动接口,最后也会运行到内核里的代码。

可以使用tar命令来压缩文件夹。具体步骤如下:

tar -czvf folder.tar.gz folder/

其中,-c表示创建一个新的压缩文件,-z表示使用gzip算法进行压缩,-v表示在压缩的过程中显示详细信息,-f表示指定压缩后的文件名,folder.tar.gz为压缩后的文件名,folder/为要压缩的文件夹路径。

先把上个博客的工程压缩备份,然后开始今天的博客:

tar -czvf linux.tar.gz linux/

或者,嫌压缩费时间

cp  -rf  linux  linux_bak

编译带驱动的内核

命令行形式配置

cd /root/uml/kernel/drivers/ldd3

//顶层source
搜索source "drivers/char/Kconfig"
source "drivers/ldd3/Kconfig"
//顶层Makefile
搜索obj-y				+= char/
obj-y				+= ldd3/

//图形化配置
cp /root/uml/kernel/drivers/char/Kconfig ./

# SPDX-License-Identifier: GPL-2.0
#
# ldd3 device configuration
#

menu "ldd3 devices"

# 字符设备驱动
source "drivers/ldd3/scull/Kconfig"

endmenu

//编译新的配置
cp /root/uml/kernel/drivers/char/Makefile ./

# SPDX-License-Identifier: GPL-2.0
#
# Makefile for the kernel ldd3 device drivers.
#

obj-$(CONFIG_SCULL)		    += scull/

//暂时只有大选项,没有小的选项,留着不用
touch Kconfig

menuconfig SCULL
	tristate "chapter 3 scull devices"
	default y
	help
	  LDD.

if SCULL

config SCULL_DEBUG
	tristate "chapter 3 scull devices debug"
	help
	  LDD.

endif # SCULL

//修改Makefile
obj-$(CONFIG_SCULL)	+= main.o

分开的形式配置

1 cd linux-4.14.331/drivers

2 git clone https://github.com/martinezjavier/ldd3.git

把ldd3当成跟char一样的模块来编译进内核里:

1.【顶层Kconfig】搜索source "drivers/char/Kconfig"

在下方添加一行 source "drivers/ldd3/Kconfig"

//linux-4.14.331/drivers/Kconfig
source "drivers/char/Kconfig"
source "drivers/ldd3/Kconfig"

2.【顶层makefile】搜索obj-y               += char/

在下方添加一行 obj-y               += ldd3/

//linux-4.14.331/drivers/Makefile
obj-y				+= char/
obj-y				+= ldd3/

3.【菜单ldd3 Kconfig】(注意最后一行要有换行)

cd ldd3

cp ../char/Kconfig ./

参考char的Kconfig修改

//linux-4.14.331/drivers/ldd3/Kconfig
# SPDX-License-Identifier: GPL-2.0
#
# ldd3 device configuration
#

menu "ldd3 devices"

# 字符设备驱动
source "drivers/ldd3/scull/Kconfig"

endmenu

4.【菜单ldd3 makefile】

cd ldd3

cp ../char/Makefile ./

参考char的makefile修改

//linux-4.14.331/drivers/ldd3/Makefile
# SPDX-License-Identifier: GPL-2.0
#
# Makefile for the kernel ldd3 device drivers.
#

obj-$(CONFIG_SCULL)		    += scull/

5.【子菜单ldd3/scull Kconfig】

cd ldd3/scull

touch Kconfig

linux-4.14.331/drivers/ldd3/scull/Kconfig

menuconfig SCULL
	tristate "chapter 3 scull devices"
	default y
	help
	  LDD.

if SCULL

config SCULL_DEBUG
	tristate "chapter 3 scull devices debug"
	help
	  LDD.

endif # SCULL

6.【子菜单ldd3/scull makefile】

修改git上直接下载的的makefile

//linux-4.14.331/drivers/ldd3/scull/Makefile
obj-$(CONFIG_SCULL)	+= main.o

修改源代码

linux-4.14.331/drivers/ldd3/scull/main.c

/*
 * main.c -- the bare scull char module
 *
 * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
 * Copyright (C) 2001 O'Reilly & Associates
 *
 * The source code in this file can be freely used, adapted,
 * and redistributed in source or binary form, so long as an
 * acknowledgment appears in derived source files.  The citation
 * should list that the code comes from the book "Linux Device
 * Drivers" by Alessandro Rubini and Jonathan Corbet, published
 * by O'Reilly & Associates.   No warranty is attached;
 * we cannot take responsibility for errors or fitness for use.
 *
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>

#include <linux/kernel.h>	/* printk() */
#include <linux/slab.h>		/* kmalloc() */
#include <linux/fs.h>		/* everything... */
#include <linux/errno.h>	/* error codes */
#include <linux/types.h>	/* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h>	/* O_ACCMODE */
#include <linux/seq_file.h>
#include <linux/cdev.h>

#include <linux/uaccess.h>	/* copy_*_user */

#include "scull.h"		/* local definitions */
#include "../include/access_ok_version.h"
#include "../include/proc_ops_version.h"

/*
 * Our parameters which can be set at load time.
 */

int scull_major =   SCULL_MAJOR;
int scull_minor =   0;
int scull_nr_devs = SCULL_NR_DEVS;	/* number of bare scull devices */
int scull_quantum = SCULL_QUANTUM;
int scull_qset =    SCULL_QSET;

module_param(scull_major, int, S_IRUGO);
module_param(scull_minor, int, S_IRUGO);
module_param(scull_nr_devs, int, S_IRUGO);
module_param(scull_quantum, int, S_IRUGO);
module_param(scull_qset, int, S_IRUGO);

MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");

struct scull_dev *scull_devices;	/* allocated in scull_init_module */


/*
 * Empty out the scull device; must be called with the device
 * semaphore held.
 */
int scull_trim(struct scull_dev *dev)
{
	struct scull_qset *next, *dptr;
	int qset = dev->qset;   /* "dev" is not-null */
	int i;

	for (dptr = dev->data; dptr; dptr = next) { /* all the list items */
		if (dptr->data) {
			for (i = 0; i < qset; i++)
			    //释放量子的内存
				kfree(dptr->data[i]);
			//释放数组	
			kfree(dptr->data);
			dptr->data = NULL;
		}
		next = dptr->next;
		//释放一个链表节点
		kfree(dptr);
	}
	dev->size = 0;
	dev->quantum = scull_quantum;
	dev->qset = scull_qset;
	dev->data = NULL;
	return 0;
}
#ifdef SCULL_DEBUG /* use proc only if debugging */
/*
 * The proc filesystem: function to read and entry
 */

int scull_read_procmem(struct seq_file *s, void *v)
{
        int i, j;
        int limit = s->size - 80; /* Don't print more than this */

        for (i = 0; i < scull_nr_devs && s->count <= limit; i++) {
                struct scull_dev *d = &scull_devices[i];
                struct scull_qset *qs = d->data;
                if (mutex_lock_interruptible(&d->lock))
                        return -ERESTARTSYS;
                seq_printf(s,"\nDevice %i: qset %i, q %i, sz %li\n",
                             i, d->qset, d->quantum, d->size);
                for (; qs && s->count <= limit; qs = qs->next) { /* scan the list */
                        seq_printf(s, "  item at %p, qset at %p\n",
                                     qs, qs->data);
                        if (qs->data && !qs->next) /* dump only the last item */
                                for (j = 0; j < d->qset; j++) {
                                        if (qs->data[j])
                                                seq_printf(s, "    % 4i: %8p\n",
                                                             j, qs->data[j]);
                                }
                }
                mutex_unlock(&scull_devices[i].lock);
        }
        return 0;
}



/*
 * Here are our sequence iteration methods.  Our "position" is
 * simply the device number.
 */
static void *scull_seq_start(struct seq_file *s, loff_t *pos)
{
	if (*pos >= scull_nr_devs)
		return NULL;   /* No more to read */
	return scull_devices + *pos;
}

static void *scull_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
	(*pos)++;
	if (*pos >= scull_nr_devs)
		return NULL;
	return scull_devices + *pos;
}

static void scull_seq_stop(struct seq_file *s, void *v)
{
	/* Actually, there's nothing to do here */
}

static int scull_seq_show(struct seq_file *s, void *v)
{
	struct scull_dev *dev = (struct scull_dev *) v;
	struct scull_qset *d;
	int i;

	if (mutex_lock_interruptible(&dev->lock))
		return -ERESTARTSYS;
	seq_printf(s, "\nDevice %i: qset %i, q %i, sz %li\n",
			(int) (dev - scull_devices), dev->qset,
			dev->quantum, dev->size);
	for (d = dev->data; d; d = d->next) { /* scan the list */
		seq_printf(s, "  item at %p, qset at %p\n", d, d->data);
		if (d->data && !d->next) /* dump only the last item */
			for (i = 0; i < dev->qset; i++) {
				if (d->data[i])
					seq_printf(s, "    % 4i: %8p\n",
							i, d->data[i]);
			}
	}
	mutex_unlock(&dev->lock);
	return 0;
}
	
/*
 * Tie the sequence operators up.
 */
static struct seq_operations scull_seq_ops = {
	.start = scull_seq_start,
	.next  = scull_seq_next,
	.stop  = scull_seq_stop,
	.show  = scull_seq_show
};

/*
 * Now to implement the /proc files we need only make an open
 * method which sets up the sequence operators.
 */
static int scullmem_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, scull_read_procmem, NULL);
}

static int scullseq_proc_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &scull_seq_ops);
}

/*
 * Create a set of file operations for our proc files.
 */
static struct file_operations scullmem_proc_ops = {
	.owner   = THIS_MODULE,
	.open    = scullmem_proc_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = single_release
};

static struct file_operations scullseq_proc_ops = {
	.owner   = THIS_MODULE,
	.open    = scullseq_proc_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};
	

/*
 * Actually create (and remove) the /proc file(s).
 */

static void scull_create_proc(void)
{
	proc_create_data("scullmem", 0 /* default mode */,
			NULL /* parent dir */, proc_ops_wrapper(&scullmem_proc_ops, scullmem_pops),
			NULL /* client data */);
	proc_create("scullseq", 0, NULL, proc_ops_wrapper(&scullseq_proc_ops, scullseq_pops));
}

static void scull_remove_proc(void)
{
	/* no problem if it was not registered */
	remove_proc_entry("scullmem", NULL /* parent dir */);
	remove_proc_entry("scullseq", NULL);
}


#endif /* SCULL_DEBUG */





/*
 * Open and close
 */

//i 节点,文件表项
int scull_open(struct inode *inode, struct file *filp)
{
	struct scull_dev *dev; /* device information */

	printk(KERN_WARNING "[川] scull_open\n");

	dev = container_of(inode->i_cdev, struct scull_dev, cdev);
	filp->private_data = dev; /* for other methods */

	/* now trim to 0 the length of the device if open was write-only */

	if ( (filp->f_flags & O_ACCMODE) == O_WRONLY) {
		printk(KERN_WARNING "[川] scull_open:只写\n");
		if (mutex_lock_interruptible(&dev->lock))
			return -ERESTARTSYS;
		scull_trim(dev); /* ignore errors */
		mutex_unlock(&dev->lock);
	}
	else if((filp->f_flags & O_ACCMODE) == O_RDONLY){
		printk(KERN_WARNING "[川] scull_open:只读\n");
	}
	else if((filp->f_flags & O_ACCMODE) == O_RDWR){
		printk(KERN_WARNING "[川] scull_open:可读可写\n");
	}
	return 0;          /* success */
}

int scull_release(struct inode *inode, struct file *filp)
{
	printk(KERN_WARNING "[川] scull_release\n");
	return 0;
}
/*
 * Follow the list
 */
//如何处理文件空洞?
struct scull_qset *scull_follow(struct scull_dev *dev, int n)
{
	struct scull_qset *qs = dev->data;

        /* Allocate first qset explicitly if need be */
	if (! qs) {
		qs = dev->data = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
		if (qs == NULL)
			return NULL;  /* Never mind */
		memset(qs, 0, sizeof(struct scull_qset));
	}

	/* Then follow the list */
	while (n--) {
		if (!qs->next) {
			qs->next = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
			if (qs->next == NULL)
				return NULL;  /* Never mind */
			memset(qs->next, 0, sizeof(struct scull_qset));
		}
		qs = qs->next;
		continue;
	}
	return qs;
}

/*
 * Data management: read and write
 */

//多个file*代表多个文件表项,inode代表唯一的i节点:多个进程通过多个file*,竞争唯一的inode,信号或进程间通信
//文件结构体:文件的加载态/dev/scull0,文件的运行态 innode
//不是C库的FILE
//应该是应用书P80的i节点(文件表项是内核对于打开的i节点的管理,超级块是对硬盘上i节点的管理)(文件长度)
//或者p112   i节点含有文件属性和对block的指针  

//基于当前的f_pos可能读count个字节(少于count的原因,文件就这么大,读完了)
//应用层读写多余一个量子是什么表现呢?
ssize_t scull_read(struct file *filp, char __user *buf, size_t count,
                loff_t *f_pos)
{
	int countBak=count;
	struct scull_dev *dev = filp->private_data; 
	struct scull_qset *dptr;	/* the first listitem */
	int quantum = dev->quantum, qset = dev->qset;
	int itemsize = quantum * qset; /* how many bytes in the listitem */
	int item, s_pos, q_pos, rest;
	ssize_t retval = 0;

	printk(KERN_WARNING "[川] scull_read\n");

	if (mutex_lock_interruptible(&dev->lock))
		return -ERESTARTSYS;
	if (*f_pos >= dev->size)//size是尾后
		goto out;
	if (*f_pos + count > dev->size)
		count = dev->size - *f_pos;

	/* find listitem, qset index, and offset in the quantum */
	item = (long)*f_pos / itemsize; //第几个链表节点
	rest = (long)*f_pos % itemsize;
	s_pos = rest / quantum; q_pos = rest % quantum; //(量子几,偏移几)

	/* follow the list up to the right position (defined elsewhere) */
	dptr = scull_follow(dev, item);

	if (dptr == NULL || !dptr->data || ! dptr->data[s_pos])
		goto out; /* don't fill holes */

	/* read only up to the end of this quantum */
	if (count > quantum - q_pos)
		count = quantum - q_pos;

	if (copy_to_user(buf, dptr->data[s_pos] + q_pos, count)) {
		retval = -EFAULT;
		goto out;
	}

	*f_pos += count;
	printk(KERN_WARNING "[川] (链表%d,量子%d,偏移%d,实际读%d字节(尝试读%d字节),首字节是%c,新的偏移是%d)\n",item,s_pos,q_pos,count,countBak,*(char*)(dptr->data[s_pos]+q_pos),*f_pos);
	retval = count;

  out:
	mutex_unlock(&dev->lock);
	return retval;
}

//基于当前的文件偏移量,可能写count个字节
//空洞,一次只分配一个量子而已
ssize_t scull_write(struct file *filp, const char __user *buf, size_t count,
                loff_t *f_pos)
{
	int countBak=count;
	struct scull_dev *dev = filp->private_data;
	struct scull_qset *dptr;
	int quantum = dev->quantum, qset = dev->qset;
	//一个链表节点的大小=量子大小*数组大小
	int itemsize = quantum * qset;
	int item, s_pos, q_pos, rest;
	ssize_t retval = -ENOMEM; /* value used in "goto out" statements */

	printk(KERN_WARNING "[川] scull_write\n");

	if (mutex_lock_interruptible(&dev->lock))
		return -ERESTARTSYS;

	/* find listitem, qset index and offset in the quantum */
	item = (long)*f_pos / itemsize;//第几个链表节点
	rest = (long)*f_pos % itemsize;
	s_pos = rest / quantum;//data[s_pos] 
	q_pos = rest % quantum;//量子的位操作

	/* follow the list up to the right position */
	dptr = scull_follow(dev, item);
	if (dptr == NULL)
		goto out;
	//qset个空指针
	if (!dptr->data) {
		dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL);
		if (!dptr->data)
			goto out;
		memset(dptr->data, 0, qset * sizeof(char *));
	}
	//只分配一个量子?万一一个量子装不下用户这次写的数据呢
	if (!dptr->data[s_pos]) {
		dptr->data[s_pos] = kmalloc(quantum, GFP_KERNEL);
		if (!dptr->data[s_pos])
			goto out;
	}
	/* write only up to the end of this quantum */
	//下次再写
	if (count > quantum - q_pos)
		count = quantum - q_pos;

	if (copy_from_user(dptr->data[s_pos]+q_pos, buf, count)) {
		retval = -EFAULT;
		goto out;
	}
    
	//驱动没法调式,在这里加一句关键打印就好了
	//不能直接用buf
	/*
	[  +0.000018] BUG: unable to handle page fault for address: 00007fffffffdc43
	[  +0.000005] #PF: supervisor read access in kernel mode
	[  +0.000002] #PF: error_code(0x0001) - permissions violation
	*/
	//printk(KERN_WARNING "[川] (链表%d,量子%d,偏移%d,写%d字节,首字节是%c)\n",item,s_pos,q_pos,count,*buf);
	

	//修改当前文件偏移量
	*f_pos += count;

	printk(KERN_WARNING "[川] (链表%d,量子%d,偏移%d,实际写%d字节(尝试写%d字节),首字节是%c,新的偏移是%d)\n",item,s_pos,q_pos,count,countBak,*(char*)(dptr->data[s_pos]+q_pos),*f_pos);

	retval = count;

        /* update the size */
	if (dev->size < *f_pos)
		dev->size = *f_pos;

  out:
	mutex_unlock(&dev->lock);
	return retval;
}

/*
 * The ioctl() implementation
 */



/*
 * The "extended" operations -- only seek
 */

loff_t scull_llseek(struct file *filp, loff_t off, int whence)
{
	struct scull_dev *dev = filp->private_data;
	loff_t newpos;

	switch(whence) {
	  case 0: /* SEEK_SET */
		newpos = off;
		break;

	  case 1: /* SEEK_CUR */
		newpos = filp->f_pos + off;
		break;

	  case 2: /* SEEK_END */
		newpos = dev->size + off;
		break;

	  default: /* can't happen */
		return -EINVAL;
	}
	if (newpos < 0) return -EINVAL;
	filp->f_pos = newpos;
	printk(KERN_WARNING "[川] 设置当前文件偏移量(%d,%d)=%d\n",whence,off,newpos);

	return newpos;
}

//驱动操作结构体,反指驱动
//证道求一:一就是P54
//全集:也许在某个网站/手册/源码
//人生如何证道求一呢
struct file_operations scull_fops = {
	.owner =    THIS_MODULE,
	.llseek =   scull_llseek,
	.read =     scull_read,
	.write =    scull_write,
	.unlocked_ioctl = NULL,
	.open =     scull_open,
	.release =  scull_release,
};

/*
 * Finally, the module stuff
 */

/*
 * The cleanup function is used to handle initialization failures as well.
 * Thefore, it must be careful to work correctly even if some of the items
 * have not been initialized
 */
void scull_cleanup_module(void)
{
	int i;
	dev_t devno = MKDEV(scull_major, scull_minor);

	printk(KERN_WARNING "[川] 因果早沾身。\n");

	/* Get rid of our char dev entries */
	if (scull_devices) {
		for (i = 0; i < scull_nr_devs; i++) {
			scull_trim(scull_devices + i);
			cdev_del(&scull_devices[i].cdev);
		}
		kfree(scull_devices);
	}

#ifdef SCULL_DEBUG /* use proc only if debugging */
	scull_remove_proc();
#endif

	/* cleanup_module is never called if registering failed */
	//[第一个设备的设备号,个数]
	unregister_chrdev_region(devno, scull_nr_devs);

	/* and call the cleanup functions for friend devices */
	//scull_p_cleanup();
	//scull_access_cleanup();

}


/*
 * Set up the char_dev structure for this device.
 */
static void scull_setup_cdev(struct scull_dev *dev, int index)
{
	int err, devno = MKDEV(scull_major, scull_minor + index);
    //inode :struct cdev		*i_cdev;
	cdev_init(&dev->cdev, &scull_fops);
	dev->cdev.owner = THIS_MODULE;
	//四个i节点和四个设备编号关联起来
	err = cdev_add (&dev->cdev, devno, 1);//四个设备一直在设备树里,每个设备只用一个设备编号
	/* Fail gracefully if need be */
	if (err)
		printk(KERN_NOTICE "Error %d adding scull%d", err, index);
}


int scull_init_module(void)
{
	int result, i;
	dev_t dev = 0;

	printk(KERN_WARNING "[川] 繁华风雪盛,\n");
	/*
	parm:           scull_p_nr_devs:int
	parm:           scull_p_buffer:int
	parm:           scull_major:int
	parm:           scull_minor:int
	parm:           scull_nr_devs:int
	parm:           scull_quantum:int
	parm:           scull_qset:int
	*/
	printk(KERN_WARNING "[川] scull_major=%d,scull_minor=%d,scull_nr_devs=%d,scull_quantum=%d,scull_qset=%d\n",
                              scull_major,scull_minor,scull_nr_devs,scull_quantum,scull_qset);

	/*
	 * Get a range of minor numbers to work with, asking for a dynamic
	 * major unless directed otherwise at load time.
	 */
	//[60,0] [60,1] [60,2] [60,3]
	if (scull_major) {
		dev = MKDEV(scull_major, scull_minor);
		result = register_chrdev_region(dev, scull_nr_devs, "scull");//一个范围的设备集合
	} 
	//[x,0] [x,1] [x,2] [x,3]
	else {
		result = alloc_chrdev_region(&dev, scull_minor, scull_nr_devs,"scull"); //已经分配的第一个设备的设备号,次设备号从scull_minor开始
		scull_major = MAJOR(dev);
	}
	if (result < 0) {
		printk(KERN_WARNING "scull: can't get major %d\n", scull_major);
		return result;
	}
	else{
		printk(KERN_WARNING "[川]第1个设备(共%d个):(%d,%d)\n", scull_nr_devs,scull_major,scull_minor);
	}

	/* 
	 * allocate the devices -- we can't have them static, as the number
	 * can be specified at load time
	 */
	scull_devices = kmalloc(scull_nr_devs * sizeof(struct scull_dev), GFP_KERNEL);
	if (!scull_devices) {
		result = -ENOMEM;
		goto fail;  /* Make this more graceful */
	}
	memset(scull_devices, 0, scull_nr_devs * sizeof(struct scull_dev));

        /* Initialize each device. */
	for (i = 0; i < scull_nr_devs; i++) {
		scull_devices[i].quantum = scull_quantum;
		scull_devices[i].qset = scull_qset;
		mutex_init(&scull_devices[i].lock);
		scull_setup_cdev(&scull_devices[i], i);
	}

        /* At this point call the init function for any friend device */
	/*
	dev = MKDEV(scull_major, scull_minor + scull_nr_devs);
	dev += scull_p_init(dev);
	dev += scull_access_init(dev);
	*/

#ifdef SCULL_DEBUG /* only when debugging */
	scull_create_proc();
#endif

	return 0; /* succeed */

  fail:
	scull_cleanup_module();
	return result;
}

module_init(scull_init_module);
module_exit(scull_cleanup_module);

增量编译

之前已经编译过内核了,现在只是多编译一个drivers/ldd3/scull/main.c而已

第一次添加模块ldd3,需要再次完整编译;之后修改main.c,就是增量编译了

cd linux-4.14.331

cat .config | grep CONFIG_STATIC  为空,代表我们需要更新.config

make mrproper是一个命令,用于删除内核源代码目录下的所有文件,包括以前进行过的内核功能选择文件和编译过程的目标文件(*.o)以及设置文件。这个命令通常只在第一次执行内核编译前才会使用,其他时候删除前一次编译残留文件,只需要使用make clean命令即可。

make clean
export ARCH=x86
make x86_64_defconfig  (这一步报错,多数是换行符的问题,打一个enter就行)
make menuconfig:4.14.331就改这三个,高版本改的更多,到时候配置失败就难受了……
 
Kernel hacking  ---> 
    Compile-time checks and compiler options  --->
        [*] Compile the kernel with debug info
        [*]   Provide GDB scripts for kernel debuggin
Processor type and features ---->
    [] Randomize the address of the kernel image (KASLR)

scull默认是打开的,不用管:

make -j $(nproc)

用vscode插件调试内核

1 把新的内核复制到外层目录

cp linux-4.14.331/arch/x86_64/boot/bzImage ./

cp linux-4.14.331/vmlinux ./

2启动qemu

qemu-system-x86_64 -kernel bzImage -hda rootfs.ext4 -hdb shadisk.img -append "root=/dev/sda console=ttyS0" -s -S -smp 1 -nographic

3 点击,运行与调试,启动绿色三角形调式

4 qemu继续运行,启动过程里,加载ldd3模块,有打印

调试scull_init_module

点击停止调试按钮,qemu也自动退出了

1重新启动

qemu-system-x86_64 -kernel bzImage -hda rootfs.ext4 -hdb shadisk.img -append "root=/dev/sda console=ttyS0" -s -S -smp 1 -nographic

2 scull_init_module用红点的方式直接打断点

3 点击,运行与调试,启动绿色三角形调式

4 触发断点,可以单步这个函数

 

调试scull_cleanup_module

在调试scull_cleanup_module的基础上

在scull_cleanup_module上打断点->继续运行->poweroff

eeee,没触发,看来是手动安装模块,手动卸载模块才会触发;

现在是把模块编译进内核里

运行scull_write和scull_read

root登录虚拟机后:

1.  mknod /dev/scull0 c 249 0      (不知道为什么有时复制失败,自己手打呢)

注意(249,0)分别是主设备号,跟次设备号

应该跟启动时模块注册的主设备号相同

搜索启动日志:[川]第1个设备(共4个):(249,0)


2 ls -l /dev/scull0
crw-r--r--    1 root     root      249,   0 Dec  8 09:28 /dev/scull0



3 echo woshisb > /dev/scull0

//调用驱动,触发断点,见下


4 cat /dev/scull0

//写完之后,可以再读一下
//shell命令和内核的打印混合在一起了

调试scull_read

加断点,再运行一次cat /dev/scull0即可

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你在Ubuntu 16.04上运行nvidia-smi命令时出现了“not found”错误,这可能是因为无法与NVIDIA驱动程序进行通信。这个问题可能与VMware虚拟机上的CUDA/CUDNN不兼容有关。 为了解决这个问题,你可以按照以下步骤尝试修复: 1. 在WSL2中运行以下两个命令: ``` cp /usr/lib/wsl/lib/nvidia-smi /usr/bin/nvidia-smi chmod ogu+x /usr/bin/nvidia-smi ``` 这将复制nvidia-smi文件并将其设置为可执行文件。 2. 然后,尝试再次运行nvidia-smi命令,看看问题是否解决了。 如果问题仍然存在,你可以尝试重新生成对应的NVIDIA驱动模块。首先,确定当前驱动版本,可以通过运行以下命令获取: ``` nvidia-smi ``` 在输出结果的最后一行,你将看到类似于“nvidia-418-418.56”的驱动版本。然后,使用以下命令重新生成驱动模块: ``` sudo dkms install -m nvidia -v 418-418.56 ``` 请确保将命令中的版本号替换为你实际使用的驱动版本。 通过以上步骤,你应该能够解决nvidia-smi命令未找到的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Linux nvidia 报错(1):nvidia-smi: command not found](https://blog.csdn.net/Robin_Pi/article/details/109473338)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [WSL2报错:nvidia-smi Command ‘nvidia-smi‘ not found, but can be installed with:](https://blog.csdn.net/whu_wmx/article/details/130268912)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值