Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15350681
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: LINUX

2007-11-13 16:05:16

浅析module-init-tools-3.2下insmod()和rmmod()两个函数--之1--编译篇

文章来源:http://gliethttp.cublog.cn

insmod和rmmod()函数两个对应的源程序在module-init-tools-3.2.tar.bz2中,下载地址如下:


在redhat 9.0系统中,
//[root@localhost gliethttp]# ll /sbin/insmod /sbin/rmmod
-rwxr-xr-x 1 root root 146112 2003-02-12 /sbin/insmod
lrwxrwxrwx 1 root root 6 2007-04-09 /sbin/rmmod -> insmod
从上面的输出我们可以看到,rmmod是insmod的一个连接,那是硬连接还是符号连接呢,使用下面语句可以查看,
//[root@localhost gliethttp]# file /sbin/rmmod
/sbin/rmmod: symbolic link to insmod
所以我们就知道了,rmmod是insmod的一个符号连接,
来看一个例子:
比如我要安装编译好的led.o驱动文件
//[root@localhost gliethttp]# insmod led.o
那么module-init-tools-3.2对应的
int main(int argc, char *argv[])入口函数将会接收到2个参数,
argc=2,表示传进来了2个参数
argv[0]="insmod" 字符串
argv[1]="led.o"  字符串
同样
//[root@localhost gliethttp]# rmmod led.o
那么module-init-tools-3.2对应的
int main(int argc, char *argv[])入口函数也将会接收到2个参数,
argc=2,表示传进来了2个参数
argv[0]="rmmod" 字符串
argv[1]="led.o" 字符串
当然,如果这样来执行的话
//[root@localhost gliethttp]# /sbin/insmod led.o
那么module-init-tools-3.2对应的
int main(int argc, char *argv[])入口函数同样会接收到2个参数,
argc=2,表示传进来了2个参数
argv[0]="/sbin/insmod"     字符串
argv[1]="led.o"            字符串
所以我们的理解是:int main(int argc, char *argv[])函数实现部分
通过检查argv[0]的函数名来识别insmod和rmmod,不过来看看module-init-tools-3.2/insmod.c
具体语句为:
int main(int argc, char *argv[])
{
...
    if (strstr(argv[0], "insmod.static"))//检查程序名中是否有该字符串子串
        try_old_version("insmod.static", argv);
    else
        try_old_version("insmod", argv);//没有,那么执行这里的函数
...
}
没有这个辨别argv[0]"rmomod"或者"insmod"的咚咚,好像就是把insmod和rmmod分别当成一个单独的可执行文件来看待(这是对的,因为module-init-tools-3.2也确实是生成了两个独立的rmmod和insmod可执行文件)
  要想找到问题的答案,接下来我们先编译下载的module-init-tools-3.2.tar.bz2文件,看看编译结果是什么,
//[root@localhost gliethttp]# tar jxvf module-init-tools-3.2.tar.bz2
//[root@localhost gliethttp]# cd module-init-tools-3.2
//[root@localhost module-init-tools-3.2]# ./configure
在安装在winxp,vmware5.5虚拟机中的redhat9.0操作系统会提示如下内容:
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
configure: Adding gcc options: -g -O2 -Wunused -Wall
configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands
//[root@localhost module-init-tools-3.2]#make 开始编译
成功之后会生成4个独立的文件
insmod、lsmod、rmmod和modprobe
这样来看,redhat9.0中使用的rmmod和module-init-tools-3.2中的rmmod性质不一样,
module-init-tools-3.2中的rmmod是一个独立的可执行文件,而redhat9.0中使用的rmmod
是insmod的一个符号链接,
  接下来使用gdb来看看代码的执行情况,不过在执行之前,还需要生成一个驱动程序,
可以参考《linux最简单的驱动模型:hello world!》-- http://blog.chinaunix.net/u1/38994/showart_340148.html
//[root@localhost module-init-tools-3.2]# gdb ./insmod hello.o
或者
//[root@localhost module-init-tools-3.2]# strace ./insmod hello.o
这两种方式都可以跟踪调用情况,具体细节,也没什么好说的,慢慢磕就行了:)

阅读(1957) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~