Linux查看驱动模块之间的依赖关系的方法

Linux 查看驱动模块之间的依赖关系的方法

编写两个有依赖关系的驱动模块

Makefile  scull.c  scull.h  use_scull.c modprobe_use.sh
#Makefile 
DEBUG = y
ifeq ($(DEBUG), y)
        DEBFLAGS = -O -g
else
        DEBFLAGS = -O2
endif
CFLAGS +=$(DEBFLAGS)
EXTRA_CFLAGS += $(DEBFLAGS)
ifeq ($(KERNELRELEASE),)

KDIR ?= /lib/modules/$(shell uname -r)/build

PWD :=$(shell pwd)
modules:
	$(MAKE) -C $(KDIR) M=$(PWD) modules
modules_install:
	$(MAKE) -C $(KDIR) M=$(PWD) modules_install
clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean
.PHONY: modules modules_install clean
else
 		obj-m := scull.o
    obj-m += use_scull.o
endif
############# Makefile end here
//scull.c
#include <linux/init.h>   //mod init include
#include <linux/module.h> //mod must include
#include <linux/kernel.h> //printk
static int num = 10;
void show(void) {
	printk(KERN_INFO"show(),num=%d\n",num);
}
static __init int hello_init(void)
{
    printk(KERN_INFO"%s\n",__func__;
    return 0;
}
static __exit void hello_exit(void)
{
    printk("%s\n",__func__);
}
EXPORT_SYMBOL_GPL(show);
module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_VERSION("1.0.2");
MODULE_DESCRIPTION("A example module");
// use_scull.c
#include <linux/init.h>   //mod init include
#include <linux/module.h> //mod must include
#include <linux/kernel.h> //printk
#include "scull.h"
extern void show(void);
static __init int use_init(void)
{
    printk(KERN_INFO"%s\n",__func__);
    show();
    return 0;
}

static __exit void use_exit(void)
{
    printk("%s\r\n",__FUNCTION__);
}
module_init(use_init);
module_exit(use_exit);

MODULE_LICENSE("GPL");
MODULE_VERSION("1.0.2");
MODULE_DESCRIPTION("A example module");
// scull.h
#ifndef __SCULL_H
#define __SCULL_H

void show(void);
#endif

查看模块之间的依赖关系方法

# 方法一、modules.dep
cat /lib/modules/$(uname -r)/modules.dep 
# 方法二、lsmod | grep modname
lsmod | grep modname
# 方法三、modinfo modname.ko
modinfo simple.ko
# 方法四、modprobe modname
modprobe modname
方法一、module.dep 是指默认安装的驱动模块的查看方法
root@ubuntu:/tmp# cat /lib/modules/$(uname -r)/modules.dep 
kernel/arch/x86/events/intel/intel-cstate.ko:
kernel/arch/x86/events/rapl.ko:
kernel/arch/x86/kernel/cpu/mce/mce-inject.ko:
kernel/arch/x86/kernel/msr.ko:
kernel/arch/x86/kernel/cpuid.ko:
kernel/arch/x86/crypto/twofish-x86_64.ko: kernel/crypto/twofish_common.ko
kernel/arch/x86/crypto/twofish-x86_64-3way.ko: kernel/arch/x86/crypto/twofish-x86_64.ko kernel/crypto/twofish_common.ko
kernel/arch/x86/crypto/twofish-avx-x86_64.ko: kernel/crypto/crypto_simd.ko kernel/crypto/cryptd.ko kernel/arch/x86/crypto/twofish-x86_64-3way.ko kernel/arch/x86/crypto/twofish-x86_64.ko kernel/crypto/twofish_common.ko
kernel/arch/x86/crypto/serpent-sse2-x86_64.ko: kernel/crypto/serpent_generic.ko kernel/crypto/crypto_simd.ko kernel/crypto/cryptd.ko
方法二、lsmod | grep modname

此种方法是只能查看加载驱动完成后。

root@ubuntu:/home/upwards/Desktop/modprobe/driver_sample# lsmod | grep use
use_scull              16384  0
scull                  16384  1 use_scull
psmouse               159744  0
方法三、modinfo modname.ko
root@ubuntu:/home/upwards/Desktop/modprobe/driver_sample# modinfo use_scull.ko 
filename:       /home/upwards/Desktop/modprobe/driver_sample/use_scull.ko
description:    A example module
version:        1.0.2
license:        GPL
srcversion:     036296573EBA9C7BB964625
depends:        scull
retpoline:      Y
name:           use_scull
vermagic:       5.13.0-41-generic SMP mod_unload modversions 

就能看出上面的depends 后面显示的就是此模块所依赖的驱动模块。

方法四、使用modprobe 来加载模块
# modprobe 使用方法
modprobe modname #自动加载所依赖的模块
modprobe -r modname # 自动卸载所依赖的模块

modprobe 加载ko的脚本

# root@ubuntu:/home/upwards/Desktop/modprobe/driver_sample# cat modprobe_use.sh 
#/bin/bash
path=$PWD
cp $path/*.ko /lib/modules/$(uname -r) || exit
echo "copy to path"

/sbin/depmod -a || exit
echo "depmod done"

echo "Please input modules_name:"
read modules_name
echo $modules_name
/sbin/modprobe $modules_name || exit
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值