Linux基础培训(2)-驱动开发最佳实践-1_第1页
Linux基础培训(2)-驱动开发最佳实践-1_第2页
Linux基础培训(2)-驱动开发最佳实践-1_第3页
Linux基础培训(2)-驱动开发最佳实践-1_第4页
Linux基础培训(2)-驱动开发最佳实践-1_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Linux基础培训(2)模块、字符驱动开发最佳实践2021/5/6/file:tr_demo/lib/static-lib-l.h #ifndef STATIC_LIB_1_Hint static_lib_fun_l(int i_val);#endif STATIC_LIB_1_H/file:tr_demo/app/app_l.c/gcc -o app_l app_l.c -L ./lib -lstatic-lib-1#include <stdio.h>#include w./lib/static-lib-l.hwint mainfint argc, char* argv)int

2、i_val_local = 0;printf(/zapp_l.c info. nn");i val local = staticjib fun l(lOO); p r i ntf (/zma i n: i_va lloca I = %dnn", i_val_local);return 0;/file:tr_demo/lib/static-lib-l.c/gcc -c static-lib-l.c -o static-lib-1.o /ar -rc libstatic-lib-l.a static-lib-l.o#in elude <stdio.h>int sta

3、ticjib_fun_l(int i_val) int i_val_local 二 19;printf(z/static-lib-l.c staticjibun_2 called. nn"); return i val local + i_val; /file:tr_demo/app/app_2.c/gcc -o app_l app_l.c -L ./lib -lstatic-lib-1/file:tr_demo/lib/dll-lib-l.h#ifndef DLL_LIB_1_H2021/5/62021/5/6int dll_lib_fun_l(int i_val);#endif

4、DLL_LIB_1_H#include <stdio.h>#include "./lib/static-lib-l.h"int mein(int argc, char* argv)int i_val_local = 0; printf(/zapp_l.c info. nnw); i_val_local = staticjib fun l(lOO); printf(/zmain:i_val_local = %dnn", i_val_local);return 0;/file:tr_demo/lib/dll-lib-l.c/gcc -c dll-lib-l

5、.c -o dll-lib-l.o/gcc -shared -fPCI -o libdll-lib-l.so dll-lib-l.o#include <stdio.h>int dll_lib_fun_l(int i_val)int i_valocel = 19;printf(z/dll-lib-l.c dlljib fun l called. nn?,); return i_val_local + i_val;2021/5/6/file:tr_demo/mod/mod_test.c/make #inelude vlinux/kernel.h> #inelude vlinux/

6、module.h> #include vlinux/init.h>MODULE_LICENSE(,GPLn);MODULE_DESCRIPTION(Htest module for training.");MODULE_AUTHORrgaolzhvg3olzh>”);static int _init mod_testit(void) "current-path: tr_demo/mod >make>insmod mod_test.ko >dmesg>tail /var/log/messages >lsmod | more>

7、;rmmod test_mod>dmesg>tail /var/log/messagesreturn 0;/file:tr_demo/mod/Makefileprintkf'modestjnit. nnnH);static void _exit mod_test_exit(void) "printk("nnn");printk("mod_test_exit. nnn"); -module _init(mod_test_init); module_exit(mod_test_exit);2021/5/6"ifneq

8、($(KERNELRELEASE),)obj-m := mod test.oelseKBUILD := /lib/modules/'uname -r'/buildmodules:make -C $(KBUILD) M=$(shell pwd) modulesclean:rm f *.ocore depend *.cmd *.ko *.mod.crm -rf .tmp versionsendifapt-get in stall build esse ntialapt-get in stall li nu x-source 2626cd /usr/srctar jxvf linux

9、-source2626 cp/boot/config-* ./.config apt-get in stall n curses dev #vim Makefile for version change make menuconfig#change kernel config file for the kernel debug make bzlmage make modulesmake modules_installmake installupdate-initramfs -c -k update-grub#title #root #kernelDebian GNU/Linu

10、x, kernel 2.6.26-kgdb-01(hdO,O)/boot/vmlinuz-2.6.26-kgdb-01 root=/dev/sdal ro quiet kgdboc=ttySO#115200edit /boot/grub/men ustkgdbwait#initrd/boot/initrd.img-2.6.26-kgdb-01目标机gdb>set remotebaud 115200 gdb>target remote /dev/ttySlgdb>c ontinue如何迺行强制扌断目标机,以便进行继续调试? 在冃标机中执行:#echo g > /proc/

11、sysrq triggerVirtual lachine SettingsHardware Opticns2021/5/62021/5/6DeviceStrnffiaiyIB Memory512 MB曬 Proccsscrs1Q Hard Disk (SCSI)16 GBOCDQVD (ICE)Ushg file D:setup.pkadebian-.H FloppyAuto detectNet?A?ctk AdapterNATU5B ContrdlerPresent电 Sound CordAuto detect©PrirterPresentSeriaiPoftiUsna named

12、 Dice . 'pipecom2 I口 DisploYAuto detectRdd.门Device ctatus0 Connected0 Connect at pcwer onConnectionO Use physicci serial port;comyO Use output file:Browse.(£)Use named pipe:UAppekom_2This end is the server yThe other end is a vrtual machnevI/O modeI I Yield CPU on pollAllow th© quest o

13、perating system to use this serial port in pollod mode (as opposed to interrupt mode).目标机2021/5/62021/5/6 OK Cancel Help 2021/5/6Virtual Iachine Settings2021/5/6GNU GRUB version 0.97(638K lower / 522176K upper MeMory)Debian Debian Debian DebianGNU/Linux, GNU/Linux, GNU/Linux, GNU/Linux,kernel kernel

14、 kernel kernel2.6.26-2-6862.6.26-2-686 (single-user Mode)2.6.26d-012.6.26d-01 (single-user Mode)2021/5/62021/5/6Use the t and I keys to select which entry is highlightzd Press “ter to boot the selected OS, Je J to edit thz coMMands before booting, orfor a coMMand-1ine2021/5/62021/5/6标机2021/5/6-rw-r-

15、r一rootroot3172Z008-07-1405:51REPORTING-BUGSdrwxr?crXrootroot4096Z010-10-16Z0:25samplesdru/xv xrXrootroot4096Z010-10-16Z0:27scriptsdrvjxr xrXrootroot4096Z010-10-16Z0:34secur itydrwxj:TWXZ0rootroot4096Z010-10-1621:27sound-rw-r-r一rootroot928495Z010-10-16Z0:25System mapdrwxr-xrXrootroot4uybZU1U-1W-16usr

16、-rw-r-r一rootroot10Z010-06-2104:05version Debiandrwxr-xrXrootroot4096Z008-07-1405:51virt-rwxr-xrXrootroot44599999Z010-10-16Z0:25umlinux-rw-r-r一rootroot77016680Z010-10-16Z0:25umlinux o2021/5/6Boot ing 'Debian GHU/Linux, kerne 1 2.6.26d-01 root Chd0,0)FilesysteM type is ext2fs, part it ion type 0x8

17、3kernel /boot/VMlinuz-2.6.26d-01 root=/dev/sdal ro quiet kgdboc=ttSl,115200 kg dbwa itL inux-bz iMage ? setup = 0x3000 ? s ize=0xl7cicb0 1in itrd /boot/in itrd. iMg-2.6.26d-01Linux-in itrd 0xlccd80B0, 0x32073dl bytesDeconpress ing Linux Pars ing ELF done Boot ing the kerne 1.6.134290 kgdb: Wait ing

18、for connect ion ftom reMote gdb.目标机drwxr-xr-x 5 wot root 4096 drwxr-xr-x Z0 wot root 4096 -rw-r-r- 1 wot root 928495 drwxr-xr-x Z root root 4096 -rw-r-r- 1 root root 10 drwxr-xr-x 3 root root 4096 -rwxr-xr-x< foot root 44599999 -rw-r-r-root root 77016680 db55 :/usr/Si?&/l inux-source-Z 6 .Z20

19、10-10-16Z0:34secwr ity2010-10-1621:27sound2010-10-16Z0:Z5SystemmapZ010-10-16Z0:Z7usrZ010-06-Z104:05version.DebianZ008-07-1405:51uirt2010-10-1620:25umlinux2010-10-16Z0:Z5umlinux.ogdb vrrtl inuxGNU gdb 6.8-debianCopyright (C) 2008 Free Sof tware Foundat ion, Inc License GPLu3+: GMU GPL uers ion 3 or l

20、ater <http://1icensex/gp1html>This is free software: you are free to change and redistributg it.Diere is NO WARRANTY, to the extent permitted by Law. Type ushow copying” and ushow warraiity,p f or detai Is .This GDB was conf igured as Ui48&-1inux-gnuM.(gdb) wet remotebaud 115200(gdb

21、) targ巳t remot巳 zdev/ttySlRemotE debugging using /dev/ttySlIgnoring packet error j continuing.TT ' /、口warning: unrecognized item ut irrteoutM in MqSupported,p response7 L kgdb_register_io_rrtodule (new_kgdb_io_ops = <value optimized out>)at kerne1/kgdb c:16?41674wmb C): /* Sync point after

22、 breakpoint */2021/5/6(gdb)2021/5/6一1rootroot928495Z010-10-16Z0:25System mapdrwx '-xrX2rootroot4096Z010-10-16Z0:27usr一1rootroot10Z010-06-2104:05version Debiandrwxr<rX3rootroot4096Z008-07-1405:51virt-rwxr-xrX1rootroot44599999Z010-10-16Z0:25umlinux-rw-r-r一1rootroot77016680Z010-10-16Z0:25umlinux

23、 o2021/5/62021/5/6db55 :/usr/src# Is total 47608 dptjxrxr-x 22 -rw-/-7r-1drw' .r-.sr-x 7root root rootroot root src4096 486867874096Z010-10-16Z010-06-21Z010-10-17Z1 :38 1 inux-source-ZEZB1 inux-source-Z £ 串&bz3 tr demo05:1915:33db55:/usr/src/tr_demo#Is-1tota1 20drwxr-sr-x2rootsrc4096201

24、0-10-1715:21drwxr-sr-x2rootsrc40962010-10-1714:28drwxr-sr-x2rootsrc40962010 10 -1?14:1?drwxr-sr-x2rootsrc40962010-10-1716:47drwxr-sr-x3rootsrc40962010-10-1717:23db55:/usr/src/trdemo#db55/usr/src# cd tr_demo/ db55:/usr/src/tr_demott Is app dll 1ib mod mod.vl2021/5/6db55 :/usr/src/tr_demo#total 24drwx

25、r-sr-x2rootsrcdrwxr-sr-x2rootsrcdrwxr-sr-x2rootsrcdrwxr-sr-x2rootsrcdrwxr-sr-x3rootsrc-rw-r-r-1rootsrclb55:/usr/src/tr_demo#525 .Z76964 SysRq :269046904690469042010-10-1?Z010-10-17Z010-10-17Z010-10-17ZW1U-1W-172010-10-1715:2114:2814:1716:4717:Z319:Z8app dll lib mod mod vlsysrq-triggeroh B c D e Gg &

26、gt; /proc/sysrq-trigger标机2021/5/62021/5/6ffindude <linux/fs.h> include <linux/module.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/init.h> include <linux/cdev.h> #include <linux/ioport.h> #include <linux/pci.h> #include <asm/

27、uaccess.h> #include <asm/io.h>MODULE_LICENSEGPr);M ODU LE.DESCRI PTION ("test module for train ing.M); MODULE_AUTHOR("gaolzh<gaolzh >");loff_t test_llseek (struct file * a, loff_t b, int c);ssize_t test_read (struct file * a, char _user * b, size_t c, loff_t d);ssize t

28、test write (struct file *az const char user *b, size t c z ioff_r*d); int testjoctl (struct inode *a, struct file * b, unsigned int c,unsigned long d);int test_open (struct inode *a, struct file *b);int test_release (struct inode *a, struct file *b);2021/5/6currentpath: tr_demo/mod.vl >make>in

29、smod mod_test.ko>dmesg>mknod /dev/test c 224 35 >cat /dev/test>tail /var/log/messages>lsmod | more>rmmod test_mod>dmesg>tail /var/log/messages/file:tr_demo/mod/Makefileifneq ($(KERNELRELEASE)/)obj-m := mod_test.o elseKBUILD := /lib/modules/'uname -r'/build modules:mak

30、e -C $(KBUILD) M=$(shell pwd) modules clean:rm -f *.o core .depend .*.cmd *.ko *.mod.crm rf .tmp_versionsendifstruct test_dev *gp_test_devices;static unsigned char test_inc = 0;static u8 testBuffer256;#define TEST_MAJOR 224#defineTEST_MINOR35loff_t test_llseek (struct file * a, loff_t b, int c) 一pri

31、ntk("test_llseek called nnn");return (loff_t)0; "ssize_t test_read (struct file * a, char _user * b, size_t c, loff_t * d) "printktest_read called nnn");return (ssize_t)0; _ ssize_t test_write (struct file *a, const char _user *b, size_t c , loff_t *d) -printk(”test_write ca

32、lled nnn");return (ssize_t)0;int test_ioctl (struct inode *a, struct file * b, unsigned int c, unsigned long d) _printk("testoctl called nnn");return (int)0;int test_open (struct inode *a, struct file *b)"printk("test_open called nnn");return (int)0;int test_release (st

33、ruct inode *a, struct file *b) _printk("test_release called nnn");return (int)0;2021/5/6static int _init mod_testnit(void) int ret = 0; dev_t dev = 0;dev = MKDEV仃EST_MAJOR, TEST_MINOR);printk(Hmod_testjnitbegin . nnnH);ret = register_chrdev_region(dev, 1, "TESTDEMO");if(ret<0)

34、 printk("can't get major %d ", TEST_MAJOR);goto fail;gp_test_devices = kmalloc(sizeof(struct test_dev), GFP_KERNEL); if (!gp_test_devices) ret =-ENOMEM;goto fail;memset(gp_test_devices, 0, sizeof(struct test_dev); cdev_init(&gp_test_devices->test_cdev, &test_fops); gp_test_d

35、evices->test_cdev.ow ner = THIS_MODULE; gp_test_devices->test_cdev.ops = & test_fops;ret = cdev_add(&gp_test_devices>test_cdev, dev, 1);if(ret)printk(Herror %d adding TESTDEMO", ret);goto fail;202/躲intkmod_testnit end nnn");return 0;static void _exit mod_test_exit(void) -p

36、rintk("nnnn);printk(Hmod_test_exit begin. nnn");dev_t devno = MKDEV(TEST_MAJOR, TEST_MINOR) if(gp_test_devices)""cdev_del(&gp_test_devices->test_cdev); kfree(gp_test_devices);un register_chrdev_regi on( devno, 1);printk("mod_test_exitend., nnn"); 一 一module _init(

37、mod_test _init); module_exit(mod_test_exit);2021/5/6>dmesg 2086.301916-2086.3019192086.3019212086.301996-2086.3019992086.3020012118.742938-2118.7429462118.7429472118.7430112118.7430132118.7430152118.743039-2118.7430422118.7430432021/5/6mod_test_init begin .modtest initendtest_open calledtest read

38、 calledtest release called调试Wed Oct 20, 7:59 PM go O宅 Applications Places System 静-2021/5/6Pl TerminalPl Terminal(gdb) b kernel/module.c:2230Breakpoint 1 at 0xc01441eb: file kernel/module.c, line 2230.(gdb)(gdb) break kernel/module.c:2230Breakpoint 2 at 0xc01441eb: file kernel/module.c, line 2230.(g

39、db) c Continuing.(gdb) break kernel/module.c:2230Breakpoint 2 at 0xc01441eb: file kernel/module.c, line 2230.(gdb) cCon tinuing New Thread 2266Switching to Thread 2266Breakpoint 2, sysjnit_module (umod=0x804a018z len=90673, uargs=0x804a008 IM,) at kernel/rnodule.c:22302230if (mod->init != NULL)2225222622

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论