- modprobe, lsmod, modinfo Command Tutorial With Examples To Load, List Linux Kernel Modules
- List Available Kernel Modules
- List Loaded Modules
- Get Information About Kernel Module
- Load or Install New Kernel Modules
- Remove or Unload Loaded Kernel Module
- Howto: Display List of Modules or Device Drivers In the Linux Kernel
- Task: List or display loaded modules
- Finding more info about any module or driver
- Linux: Find out what kernel drivers (modules) are loaded
- lsmod command
- Get more information about the driver
- Kernel module
- Contents
- Obtaining information
- Automatic module loading with systemd
- Manual module handling
- Setting module options
- Manually at load time using modprobe
- Using files in /etc/modprobe.d/
- Using kernel command line
- Aliasing
- Blacklisting
- Using files in /etc/modprobe.d/
- Using kernel command line
- Troubleshooting
- Modules do not load
- Kernel modules¶
- Lab objectives¶
- Kernel Modules Overview¶
- An example of a kernel module¶
- Compiling kernel modules¶
- Loading/unloading a kernel module¶
- Kernel Module Debugging¶
- objdump¶
- addr2line¶
- minicom¶
- netconsole¶
- Printk debugging¶
- Dynamic debugging¶
- Dyndbg Options¶
- KDB: Kernel debugger¶
- Exercises¶
- 0. Intro¶
modprobe, lsmod, modinfo Command Tutorial With Examples To Load, List Linux Kernel Modules
What makes an Operating system Linux Distribution? All Linux distributions use same kernel named Linux Kernel. Linux kernel provides operating system services, hardware management, process management, memory management etc.
Linux kernel is a monolithic kernel which means single executable all in one. But the operating systems should provide dynamic environments to comply user needs. Linux provides mechanism to load some drivers, features etc. This is called kernel modules. In this tutorial we will look kernel modules operations with modprobe command. Most of the examples in this tutorial requires root privileges.
List Available Kernel Modules
Linux kernel came with a lot of default kernel modules. These modules are loaded according to requirements and kernel config provided by the distribution. There is also an option to add new kernel modules externally to the Linux. We can list all of these modules with lsmod command
List Available Kernel Modules
List Available Kernel Modules
List Loaded Modules
As we know kernel modules are loaded or unloaded. We can list only installed kernel modules by using previous command. but in this command we need some external help. We will use egrep to filter installed kernel modules.
List Loaded Modules
Get Information About Kernel Module
Kernel modules can get different parameter for configuration purposes. Also there are different type of information. Here are some of them which can be listed with modinfo command.
- filename what is modules file name and the path
- license modules license type like GPL,GPL2,Apache,TM
- description short description about the kernel module
- depends specify what other kernel modules are needed to load this module
- intree specify if this kernel module is maintained in kernel git repository
- vermagic specifies the version of the kernel module
- parm specifies parameter that can be used to configure this kernel module.
Get Information About Kernel Module
Load or Install New Kernel Modules
Generally Linux operating system automatically loads related kernel modules. There is no need to load them manually in most situations. But some times manual operation may be needed to load kernel modules. We will install module named ipx by using insmod in this example.
Remove or Unload Loaded Kernel Module
We can remove kernel modules. We will use modprobe command again with -r option by providing the kernel module name. In this example we unload ipx kernel module
Источник
Howto: Display List of Modules or Device Drivers In the Linux Kernel
H ow do I display the list of loaded Linux Kernel modules or device drivers on Linux operating systems?
You need to use lsmod program which show the status of loaded modules in the Linux Kernel. Linux kernel use a term modules for all hardware device drivers.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | lsmod |
Est. reading time | Less than a one minute |
Please note hat lsmod is a trivial program which nicely formats the contents of the /proc/modules , showing what kernel modules are currently loaded.
This is an important task. With lsmod you can verify that device driver is loaded for particular hardware. Any hardware device will only work if device driver is loaded.
Task: List or display loaded modules
Open a terminal or login over the ssh session and type the following command
$ less /proc/modules
Sample outputs:
To see nicely formatted output, type:
$ lsmod
Sample outputs:
First column is Module name and second column is the size of the modules i..e the output format is module name, size, use count, list of referring modules.
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Finding more info about any module or driver
Type the following command:
# modinfo driver-Name-Here
# modinfo thermal_sys
# modinfo e1000e
Sample outputs:
Источник
Linux: Find out what kernel drivers (modules) are loaded
lsmod command
You need to use lsmod command to show the status of modules in the Linux Kernel. Simply type the lsmod at a shell prompt to list all loaded modules:
$ lsmod
Sample outputs:
Get more information about the driver
To get more information about specific driver use modinfo command. The syntax is:
modinfo < driver-name >
To see information about a Linux Kernel module called e1000, enter:
$ modinfo e1000
Sample outputs:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
See modinfo and lsmod man pages for more info.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Kernel module
Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system.
To create a kernel module, you can read The Linux Kernel Module Programming Guide. A module can be configured as built-in or loadable. To dynamically load or remove a module, it has to be configured as a loadable module in the kernel configuration (the line related to the module will therefore display the letter M ).
Contents
Obtaining information
Modules are stored in /usr/lib/modules/kernel_release . You can use the command uname -r to get your current kernel release version.
To show what kernel modules are currently loaded:
To show information about a module:
To list the options that are set for a loaded module:
To display the comprehensive configuration of all the modules:
To display the configuration of a particular module:
List the dependencies of a module (or alias), including the module itself:
Automatic module loading with systemd
Today, all necessary modules loading is handled automatically by udev, so if you do not need to use any out-of-tree kernel modules, there is no need to put modules that should be loaded at boot in any configuration file. However, there are cases where you might want to load an extra module during the boot process, or blacklist another one for your computer to function properly.
Kernel modules can be explicitly listed in files under /etc/modules-load.d/ for systemd to load them during boot. Each configuration file is named in the style of /etc/modules-load.d/program.conf . Configuration files simply contain a list of kernel modules names to load, separated by newlines. Empty lines and lines whose first non-whitespace character is # or ; are ignored.
See modules-load.d(5) for more details.
Manual module handling
Kernel modules are handled by tools provided by kmod package. You can use these tools manually.
To load a module:
To load a module by filename (i.e. one that is not installed in /usr/lib/modules/$(uname -r)/ ):
To unload a module:
Setting module options
To pass a parameter to a kernel module, you can pass them manually with modprobe or assure certain parameters are always applied using a modprobe configuration file or by using the kernel command line.
Manually at load time using modprobe
The basic way to pass parameters to a module is using the modprobe command. Parameters are specified on command line using simple key=value assignments:
Using files in /etc/modprobe.d/
Files in /etc/modprobe.d/ directory can be used to pass module settings to udev, which will use modprobe to manage the loading of the modules during system boot. Configuration files in this directory can have any name, given that they end with the .conf extension. The syntax is:
Using kernel command line
If the module is built into the kernel, you can also pass options to the module using the kernel command line. For all common bootloaders, the following syntax is correct:
Simply add this to your bootloader’s kernel-line, as described in Kernel Parameters.
Aliasing
Aliases are alternate names for a module. For example: alias my-mod really_long_modulename means you can use modprobe my-mod instead of modprobe really_long_modulename . You can also use shell-style wildcards, so alias my-mod* really_long_modulename means that modprobe my-mod-something has the same effect. Create an alias:
Some modules have aliases which are used to automatically load them when they are needed by an application. Disabling these aliases can prevent automatic loading but will still allow the modules to be manually loaded.
Blacklisting
Blacklisting, in the context of kernel modules, is a mechanism to prevent the kernel module from loading. This could be useful if, for example, the associated hardware is not needed, or if loading that module causes problems: for instance there may be two kernel modules that try to control the same piece of hardware, and loading them together would result in a conflict.
Some modules are loaded as part of the initramfs. mkinitcpio -M will print out all automatically detected modules: to prevent the initramfs from loading some of those modules, blacklist them in a .conf file under /etc/modprobe.d and it shall be added in by the modconf hook during image generation. Running mkinitcpio -v will list all modules pulled in by the various hooks (e.g. filesystems hook, block hook, etc.). Remember to add that .conf file to the FILES array in /etc/mkinitcpio.conf if you do not have the modconf hook in your HOOKS array (e.g. you have deviated from the default configuration), and once you have blacklisted the modules regenerate the initramfs, and reboot afterwards.
Using files in /etc/modprobe.d/
Create a .conf file inside /etc/modprobe.d/ and append a line for each module you want to blacklist, using the blacklist keyword. If for example you want to prevent the pcspkr module from loading:
However, there is a workaround for this behaviour; the install command instructs modprobe to run a custom command instead of inserting the module in the kernel as normal, so you can force the module to always fail loading with:
This will effectively blacklist that module and any other that depends on it.
Using kernel command line
You can also blacklist modules from the bootloader.
Simply add module_blacklist=modname1,modname2,modname3 to your bootloader’s kernel line, as described in Kernel parameters.
Troubleshooting
Modules do not load
In case a specific module does not load and the boot log (accessible by running journalctl -b as root) says that the module is blacklisted, but the directory /etc/modprobe.d/ does not show a corresponding entry, check another modprobe source folder at /usr/lib/modprobe.d/ for blacklisting entries.
A module will not be loaded if the «vermagic» string contained within the kernel module does not match the value of the currently running kernel. If it is known that the module is compatible with the current running kernel the «vermagic» check can be ignored with modprobe —force-vermagic .
Источник
Kernel modules¶
Lab objectives¶
- creating simple modules
- describing the process of kernel module compilation
- presenting how a module can be used with a kernel
- simple kernel debugging methods
Kernel Modules Overview¶
A monolithic kernel, though faster than a microkernel, has the disadvantage of lack of modularity and extensibility. On modern monolithic kernels, this has been solved by using kernel modules. A kernel module (or loadable kernel mode) is an object file that contains code that can extend the kernel functionality at runtime (it is loaded as needed); When a kernel module is no longer needed, it can be unloaded. Most of the device drivers are used in the form of kernel modules.
For the development of Linux device drivers, it is recommended to download the kernel sources, configure and compile them and then install the compiled version on the test /development tool machine.
An example of a kernel module¶
Below is a very simple example of a kernel module. When loading into the kernel, it will generate the message «Hi» . When unloading the kernel module, the «Bye» message will be generated.
The generated messages will not be displayed on the console but will be saved in a specially reserved memory area for this, from where they will be extracted by the logging daemon (syslog). To display kernel messages, you can use the dmesg command or inspect the logs:
Compiling kernel modules¶
Compiling a kernel module differs from compiling an user program. First, other headers should be used. Also, the module should not be linked to libraries. And, last but not least, the module must be compiled with the same options as the kernel in which we load the module. For these reasons, there is a standard compilation method ( kbuild ). This method requires the use of two files: a Makefile and a Kbuild file.
Below is an example of a Makefile :
And the example of a Kbuild file used to compile a module:
As you can see, calling make on the Makefile file in the example shown will result in the make invocation in the kernel source directory ( /lib/modules/`uname -r`/build ) and referring to the current directory ( M = `pwd` ). This process ultimately leads to reading the Kbuild file from the current directory and compiling the module as instructed in this file.
For labs we will configure different KDIR, according to the virtual machine specifications:
A Kbuild file contains one or more directives for compiling a kernel module. The easiest example of such a directive is obj-m = module.o . Following this directive, a kernel module ( ko — kernel object) will be created, starting from the module.o file. module.o will be created starting from module.c or module.S . All of these files can be found in the Kbuild ‘s directory.
An example of a Kbuild file that uses several sub-modules is shown below:
For the example above, the steps to compile are:
- compile the module-a.c and module-b.c sources, resulting in module-a.o and module-b.o objects
- module-a.o and module-b.o will then be linked in supermodule.o
- from supermodule.o will be created supermodule.ko module
The suffix of targets in Kbuild determines how they are used, as follows:
- M (modules) is a target for loadable kernel modules
- Y (yes) represents a target for object files to be compiled and then linked to a module ( $(mode_name)-y ) or within the kernel ( obj-y )
- any other target suffix will be ignored by Kbuild and will not be compiled
These suffixes are used to easily configure the kernel by running the make menuconfig command or directly editing the .config file. This file sets a series of variables that are used to determine which features are added to the kernel at build time. For example, when adding BTRFS support with make menuconfig, add the line CONFIG_BTRFS_FS = y to the .config file. The BTRFS kbuild contains the line obj-$(CONFIG_BTRFS_FS):= btrfs.o , which becomes obj-y:= btrfs.o . This will compile the btrfs.o object and will be linked to the kernel. Before the variable was set, the line became obj:=btrfs.o and so it was ignored, and the kernel was build without BTRFS support.
For more details, see the Documentation/kbuild/makefiles.txt and Documentation/kbuild/modules.txt files within the kernel sources.
Loading/unloading a kernel module¶
To load a kernel module, use the insmod utility. This utility receives as a parameter the path to the *.ko file in which the module was compiled and linked. Unloading the module from the kernel is done using the rmmod command, which receives the module name as a parameter.
When loading the kernel module, the routine specified as a parameter of the module_init macro will be executed. Similarly, when the module is unloaded the routine specified as a parameter of the module_exit will be executed.
A complete example of compiling and loading/unloading a kernel module is presented below:
Information about modules loaded into the kernel can be found using the lsmod command or by inspecting the /proc/modules , /sys/module directories.
Kernel Module Debugging¶
Troubleshooting a kernel module is much more complicated than debugging a regular program. First, a mistake in a kernel module can lead to blocking the entire system. Troubleshooting is therefore much slowed down. To avoid reboot, it is recommended to use a virtual machine (qemu, virtualbox, vmware).
When a module containing bugs is inserted into the kernel, it will eventually generate a kernel oops. A kernel oops is an invalid operation detected by the kernel and can only be generated by the kernel. For a stable kernel version, it almost certainly means that the module contains a bug. After the oops appears, the kernel will continue to work.
Very important to the appearance of a kernel oops is saving the generated message. As noted above, messages generated by the kernel are saved in logs and can be displayed with the dmesg command. To make sure that no kernel message is lost, it is recommended to insert/test the kernel directly from the console, or periodically check the kernel messages. Noteworthy is that an oops can occur because of a programming error, but also a because of hardware error.
If a fatal error occurs, after which the system can not return to a stable state, a kernel panic is generated.
Look at the kernel module below that contains a bug that generates an oops:
Inserting this module into the kernel will generate an oops:
Although relatively cryptic, the message provided by the kernel to the appearance of an oops provides valuable information about the error. First line:
Tells us the cause and the address of the instruction that generated the error. In our case this is an invalid access to memory.
Tells us that it’s the first oops (#1). This is important in the context that an oops can lead to other oopses. Usually only the first oops is relevant. Furthermore, the oops code ( 0002 ) provides information about the error type (see arch/x86/include/asm/traps.h ):
- Bit 0 == 0 means no page found, 1 means protection fault
- Bit 1 == 0 means read, 1 means write
- Bit 2 == 0 means kernel, 1 means user mode
In this case, we have a write access that generated the oops (bit 1 is 1).
Below is a dump of the registers. It decodes the instruction pointer ( EIP ) value and notes that the bug appeared in the my_oops_init function with a 5-byte offset ( EIP: [ ] my_oops_init+0x5 ). The message also shows the stack content and a backtrace of calls until then.
If an invalid read call is generated ( #define OP_OOPS OP_READ ), the message will be the same, but the oops code will differ, which would now be 0000 :
objdump¶
Detailed information about the instruction that generated the oops can be found using the objdump utility. Useful options to use are -d to disassemble the code and -S for interleaving C code in assembly language code. For efficient decoding, however, we need the address where the kernel module was loaded. This can be found in /proc/modules .
Here’s an example of using objdump on the above module to identify the instruction that generated the oops:
Note that the instruction that generated the oops ( c89d4005 identified earlier) is:
That is exactly what was expected — storing value 3 at 0x0001234.
The /proc/modules is used to find the address where a kernel module is loaded. The —adjust-vma option allows you to display instructions relative to 0xc89d4000 . The -l option displays the number of each line in the source code interleaved with the assembly language code.
addr2line¶
A more simplistic way to find the code that generated an oops is to use the addr2line utility:
Where 0x5 is the value of the program counter ( EIP = c89d4005 ) that generated the oops, minus the base address of the module ( 0xc89c4000 ) according to /proc/modules
minicom¶
Minicom (or other equivalent utilities, eg picocom, screen) is a utility that can be used to connect and interact with a serial port. The serial port is the basic method for analyzing kernel messages or interacting with an embedded system in the development phase. There are two more common ways to connect:
- a serial port where the device we are going to use is /dev/ttyS0
- a serial USB port (FTDI) in which case the device we are going to use is /dev/ttyUSB .
For the virtual machine used in the lab, the device that we need to use is displayed after the virtual machine starts:
netconsole¶
Netconsole is a utility that allows logging of kernel debugging messages over the network. This is useful when the disk logging system does not work or when serial ports are not available or when the terminal does not respond to commands. Netconsole comes in the form of a kernel module.
To work, it needs the following parameters:
- port, IP address, and the source interface name of the debug station
- port, MAC address, and IP address of the machine to which the debug messages will be sent
These parameters can be configured when the module is inserted into the kernel, or even while the module is inserted if it has been compiled with the CONFIG_NETCONSOLE_DYNAMIC option.
An example configuration when inserting netconsole kernel module is as follows:
Thus, the debug messages on the station that has the address 192.168.191.130 will be sent to the eth0 interface, having source port 6666 . The messages will be sent to 192.168.191.1 with the MAC address 00:50:56:c0:00:08 , on port 6000 .
Messages can be played on the destination station using netcat:
Alternatively, the destination station can configure syslogd to intercept these messages. More information can be found in Documentation/networking/netconsole.txt .
Printk debugging¶
The two oldest and most useful debugging aids are Your Brain and Printf .
For debugging, a primitive way is often used, but it is quite effective: printk debugging. Although a debugger can also be used, it is generally not very useful: simple bugs (uninitialized variables, memory management problems, etc.) can be easily localized by control messages and the kernel-decoded oop message.
For more complex bugs, even a debugger can not help us too much unless the operating system structure is very well understood. When debugging a kernel module, there are a lot of unknowns in the equation: multiple contexts (we have multiple processes and threads running at a time), interruptions, virtual memory, etc.
You can use printk to display kernel messages to user space. It is similar to printf ‘s functionality; the only difference is that the transmitted message can be prefixed with a string of » » , where n indicates the error level (loglevel) and has values between 0 and 7 . Instead of » » , the levels can also be coded by symbolic constants:
The definitions of all log levels are found in linux/kern_levels.h . Basically, these log levels are used by the system to route messages sent to various outputs: console, log files in /var/log etc.
To display printk messages in user space, the printk log level must be of higher priority than console_loglevel variable. The default console log level can be configured from /proc/sys/kernel/printk .
For instance, the command:
will enable all the kernel log messages to be displayed in the console. That is, the logging level has to be strictly less than the console_loglevel variable. For example, if the console_loglevel has a value of 5 (specific to KERN_NOTICE ), only messages with loglevel stricter than 5 (i.e KERN_EMERG , KERN_ALERT , KERN_CRIT , KERN_ERR , KERN_WARNING ) will be shown.
Console-redirected messages can be useful for quickly viewing the effect of executing the kernel code, but they are no longer so useful if the kernel encounters an irreparable error and the system freezes. In this case, the logs of the system must be consulted, as they keep the information between system restarts. These are found in /var/log and are text files, populated by syslogd and klogd during the kernel run. syslogd and klogd take the information from the virtual file system mounted in /proc . In principle, with syslogd and klogd turned on, all messages coming from the kernel will go to /var/log/kern.log .
A simpler version for debugging is using the /var/log/debug file. It is populated only with the printk messages from the kernel with the KERN_DEBUG log level.
Given that a production kernel (similar to the one we’re probably running with) contains only release code, our module is among the few that send messages prefixed with KERN_DEBUG . In this way, we can easily navigate through the /var/log/debug information by finding the messages corresponding to a debugging session for our module.
Such an example would be the following:
The format of the messages must obviously contain all the information of interest in order to detect the error, but inserting in the code printk to provide detailed information can be as time-consuming as writing the code to solve the problem. This is usually a trade-off between the completeness of the debugging messages displayed using printk and the time it takes to insert these messages into the text.
A very simple way, less time-consuming for inserting printk and providing the possibility to analyze the flow of instructions for tests is the use of the predefined constants __FILE__ , __LINE__ and __func__ :
- __FILE__ is replaced by the compiler with the name of the source file it is currently being compiled.
- __LINE__ is replaced by the compiler with the line number on which the current instruction is found in the current source file.
- __func__ / __FUNCTION__ is replaced by the compiler with the name of the function in which the current instruction is found.
__FILE__ and __LINE__ are part of the ANSI C specifications: __func__ is part of specification C99; __FUNCTION__ is a GNU C extension and is not portable; However, since we write code for the Linux kernel, we can use it without any problems.
The following macro definition can be used in this case:
Then, at each point where we want to see if it is «reached» in execution, insert PRINT_DEBUG; This is a simple and quick way, and can yield by carefully analyzing the output.
The dmesg command is used to view the messages printed with printk but not appearing on the console.
To delete all previous messages from a log file, run:
To delete messages displayed by the dmesg command, run:
Dynamic debugging¶
Dynamic dyndbg debugging enables dynamic debugging activation/deactivation. Unlike printk , it offers more advanced printk options for the messages we want to display; it is very useful for complex modules or troubleshooting subsystems. This significantly reduces the amount of messages displayed, leaving only those relevant for the debug context. To enable dyndbg , the kernel must be compiled with the CONFIG_DYNAMIC_DEBUG option. Once configured, pr_debug() , dev_dbg() and print_hex_dump_debug() , print_hex_dump_bytes() can be dynamically enabled per call.
The /sys/kernel/debug/dynamic_debug/control file from the debugfs (where /sys/kernel/debug is the path to which debugfs was mounted) is used to filter messages or to view existing filters.
Debugfs is a simple file system, used as a kernel-space interface and user-space interface to configure different debug options. Any debug utility can create and use its own files /folders in debugfs.
For example, to display existing filters in dyndbg , you will use:
And to enable the debug message from line 1603 in the svcsock.c file:
The /debug/dynamic_debug/control file is not a regular file. It shows the dyndbg settings on the filters. Writing in it with an echo will change these settings (it will not actually make a write). Be aware that the file contains settings for dyndbg debugging messages. Do not log in this file.
Dyndbg Options¶
func — just the debug messages from the functions that have the same name as the one defined in the filter.
file — the name of the file(s) for which we want to display the debug messages. It can be just the source name, but also the absolute path or kernel-tree path.
module — module name.
format — only messages whose display format contains the specified string.
line — the line or lines for which we want to enable debug calls.
In addition to the above options, a series of flags can be added, removed, or set with operators + , — or = :
- p activates the pr_debug() .
- f includes the name of the function in the printed message.
- l includes the line number in the printed message.
- m includes the module name in the printed message.
- t includes the thread id if it is not called from interrupt context
- _ no flag is set.
KDB: Kernel debugger¶
The kernel debugger has proven to be very useful to facilitate the development and debugging process. One of its main advantages is the possibility to perform live debugging. This allows us to monitor, in real time, the accesses to memory or even modify the memory while debugging. The debugger has been integrated in the mainline kernel starting with version 2.6.26-rci. KDB is not a source debugger, but for a complete analysis it can be used in parallel with gdb and symbol files — see the GDB debugging section
To use KDB, you have the following options:
- non-usb keyboard + VGA text console
- serial port console
- USB EHCI debug port
For the lab, we will use a serial interface connected to the host. The following command will activate GDB over the serial port:
KDB is a stop mode debugger, which means that, while it is active, all the other processes are stopped. The kernel can be forced to enter KDB during execution using the following SysRq command
or by using the key combination Ctrl+O g in a terminal connected to the serial port (for example using minicom).
KDB has various commands to control and define the context of the debugged system:
- lsmod, ps, kill, dmesg, env, bt (backtrace)
- dump trace logs
- hardware breakpoints
- modifying memory
For a better description of the available commands you can use the help command in the KDB shell. In the next example, you can notice a simple KDB usage example which sets a hardware breakpoint to monitor the changes of the mVar variable.
Exercises¶
To solve exercises, you need to perform these steps:
- prepare skeletons from templates
- build modules
- copy modules to the VM
- start the VM and test the module in the VM.
The current lab name is kernel_modules. See the exercises for the task name.
The skeleton code is generated from full source examples located in tools/labs/templates . To solve the tasks, start by generating the skeleton code for a complete lab:
You can also generate the skeleton for a single task, using
Once the skeleton drivers are generated, build the source:
Then, copy the modules and start the VM:
The modules are placed in /home/root/skels/kernel_modules/ .
Alternatively, we can copy files via scp, in order to avoid restarting the VM. For additional details about connecting to the VM via the network, please check Connecting to the Virtual Machine .
Review the Exercises section for more detailed information.
Before starting the exercises or generating the skeletons, please run git pull inside the Linux repo, to make sure you have the latest version of the exercises.
If you have local changes, the pull command will fail. Check for local changes using git status . If you want to keep them, run git stash before pull and git stash pop after. To discard the changes, run git reset —hard master .
If you already generated the skeleton before git pull you will need to generate it again.
0. Intro¶
Using cscope or LXR find the definitions of the following symbols in the Linux kernel source code:
- module_init() and module_exit()
- what do the two macros do? What is init_module and cleanup_module ?
- ignore_loglevel
- What is this variable used for?
If you have problems using cscope, it is possible that the database is not generated. To generate it, use the following command in the kernel directory:
When searching for a structure using cscope, use only the structure name (without struct ). So, to search for the structure struct module , you will use the command
Источник