Linux

 

 

 

 

Tools for Device Driver Development 

 

In this page, I will explain on various ways and commands to retrieve the information about the modules (device drivers) running on your system.  You will find these methods useful when you are developing and testing your own drivers (modules).

 

 

 

 

How to get information about modules ?

 

 

First try following command and see if the /proc/modules exists. As you see here, this is a read-only file.

 

 

 

you can see the contents of this file as shown below. This file contains the list of all the modules installed on your system and the execution of the status of each drivers.

 

    $ cat /proc/modules

     

    binfmt_misc 20480 1 - Live 0x0000000000000000

    vboxsf 49152 1 - Live 0x0000000000000000

    intel_powerclamp 16384 0 - Live 0x0000000000000000

    crct10dif_pclmul 16384 0 - Live 0x0000000000000000

    crc32_pclmul 16384 0 - Live 0x0000000000000000

    ghash_clmulni_intel 16384 0 - Live 0x0000000000000000

    ......

    hid 118784 2 hid_generic,usbhid, Live 0x0000000000000000

    e1000 143360 0 - Live 0x0000000000000000

    psmouse 139264 0 - Live 0x0000000000000000

    ahci 36864 2 - Live 0x0000000000000000

    libahci 32768 1 ahci, Live 0x0000000000000000

    fjes 77824 0 - Live 0x0000000000000000

    video 40960 0 - Live 0x0000000000000000

    pata_acpi 16384 0 - Live 0x0000000000000000

 

Even though you can get the list of modules and information from /proc/modules file, it will be easier to use lsmod command as shown below to find the same information. Actually, lsmod command extracts the information from /proc/modules.

 

 

    $ lsmod

     

    Module                  Size  Used by

    binfmt_misc            20480  1

    vboxsf                 49152  1

    intel_powerclamp       16384  0

    crct10dif_pclmul       16384  0

    crc32_pclmul           16384  0

    ghash_clmulni_intel    16384  0

    pcbc                   16384  0

    aesni_intel           167936  0

    aes_x86_64             20480  1 aesni_intel

    crypto_simd            16384  1 aesni_intel

    glue_helper            16384  1 aesni_intel

    .....

    parport                49152  3 lp,parport_pc,ppdev

    autofs4                40960  2

    hid_generic            16384  0

    usbhid                 53248  0

    hid                   118784  2 hid_generic,usbhid

    e1000                 143360  0

    psmouse               139264  0

    ahci                   36864  2

    libahci                32768  1 ahci

    fjes                   77824  0

    video                  40960  0

    pata_acpi              16384  0

     

 

 

Once you find a specific module from the list shown above, you can find more detailed information about a specific module as shown below using modinfo command.

 

    $ modinfo fb_sys_fops

     

    filename:       /lib/modules/4.10.0-28-generic/kernel/drivers/video/fbdev/core/fb_sys_fops.ko

    license:        GPL

    description:    Generic file read (fb in system RAM)

    author:         Antonino Daplas <adaplas@pol.net>

    srcversion:     208A81F7665624BC72DCB7D

    depends:        

    intree:         Y

    vermagic:       4.10.0-28-generic SMP mod_unload

 

 

 

Now you may look into /lib/module/(your linux version) as shown below. As shown here, these files are located in a specific directory name as linux kernel version on your system. If you are not sure about the kernel version on your Linux, the second method would work better.

 

    $ ls -al /lib/modules/4.10.0-28-generic

                       or

    $ ls -al /lib/modules/$(uname -r)

     

    total 4964

    drwxr-xr-x  5 root root    4096 Jan 27 03:12 .

    drwxr-xr-x  3 root root    4096 Aug  1 07:23 ..

    lrwxrwxrwx  1 root root      40 Jan 27 02:54 build -> /usr/src/linux-headers-4.10.0-28-generic

    drwxr-xr-x  2 root root    4096 Jul 20  2017 initrd

    drwxr-xr-x 14 root root    4096 Aug  1 07:23 kernel

    -rw-r--r--  1 root root 1166411 Jan 27 03:12 modules.alias

    -rw-r--r--  1 root root 1149737 Jan 27 03:12 modules.alias.bin

    -rw-r--r--  1 root root    7317 Jul 20  2017 modules.builtin

    -rw-r--r--  1 root root    9142 Jan 27 03:12 modules.builtin.bin

    -rw-r--r--  1 root root  523389 Jan 27 03:12 modules.dep

    -rw-r--r--  1 root root  741387 Jan 27 03:12 modules.dep.bin

    -rw-r--r--  1 root root     285 Jan 27 03:12 modules.devname

    -rw-r--r--  1 root root  196852 Jul 20  2017 modules.order

    -rw-r--r--  1 root root     429 Jan 27 03:12 modules.softdep

    -rw-r--r--  1 root root  558199 Jan 27 03:12 modules.symbols

    -rw-r--r--  1 root root  680971 Jan 27 03:12 modules.symbols.bin

    drwxr-xr-x  3 root root    4096 Aug  1 07:23 vdso

 

 

Among the various files listed above, a file especially helpful would be modules.dep. This file contains the location of binary files for each module and its dependencies.

 

    $ cat /lib/modules/4.10.0-28-generic/modules.dep

     

    kernel/arch/x86/events/intel/intel-rapl-perf.ko:

    kernel/arch/x86/events/intel/intel-cstate.ko:

    ...

    kernel/arch/x86/crypto/des3_ede-x86_64.ko: kernel/crypto/des_generic.ko

    kernel/arch/x86/crypto/camellia-x86_64.ko: kernel/crypto/lrw.ko kernel/arch/x86/crypto/glue_helper.ko

    kernel/arch/x86/crypto/blowfish-x86_64.ko: kernel/crypto/blowfish_common.ko

    kernel/arch/x86/crypto/twofish-x86_64.ko: kernel/crypto/twofish_common.ko

    ....

    kernel/crypto/crypto_simd.ko kernel/arch/x86/crypto/glue_helper.ko kernel/crypto/cryptd.ko

    kernel/arch/x86/crypto/ghash-clmulni-intel.ko: kernel/crypto/cryptd.ko

    kernel/arch/x86/crypto/sha1-ssse3.ko:

    kernel/arch/x86/crypto/crc32-pclmul.ko:

    kernel/arch/x86/crypto/sha256-ssse3.ko:

 

 

 

How to find a location of a specific header file ?

 

In device driver programming, you need to include various header files as shown below and sometimes you want to look into the contents of these header files.

 

#include <linux/module.h>

#include <linux/version.h>

#include <linux/kernel.h>

#include <linux/types.h>

#include <linux/kdev_t.h>

#include <linux/fs.h>

 

To see the contents of these files, you first need to figure out the location of a specific header files. You may use various ways to find the location of the file. Following is how I do to find the header file location that I want.

 

    $ sudo find / -name *.h | grep 'linux/module.h'

     

    /usr/src/linux-headers-4.10.0-28/include/uapi/linux/module.h

    /usr/src/linux-headers-4.10.0-28/include/linux/module.h

 

 

 

How to find header file or c source file containing a specific string ?

 

Following command search all the *.c and *.h files starting from /usr/src directory to all the sub directories to search for the string "module_init(x)".

 

    $ sudo grep --include=\*.{c,h} -rn /usr/src -e "module_init(x)"

     

    /usr/src/linux-headers-4.10.0-28/include/linux/module.h:86:#define module_init(x) ..;