问题
I'm a bit confused by the variables available inside the Kernel. How would I go about iterating over all modules inside my own kernel module? I found modules
being used in the kernel code. Can I do something along the lines of
struct module *mod;
list_for_each_entry(mod, &modules, list) {
printk(KERN_INFO "%s\n", mod->name);
}
回答1:
There is no direct way for iterate over list of modules.
Head of the module's list is modules
variable, which is statically defined in kernel/module.c, so it is not accessible for code outside of this file.
If list of module's name is needed for debugging purposes, you may use WARN_ON() macro or similar:
WARN_ON(0);
this will print message about warning and some additional information, which among other things will contain list of modules.
回答2:
Found a workaround to solve this.
struct list_head *list;
struct module *mod;
struct module *mine = find_module("YOUR_MODULE_NAME");
list_for_each(list, &mine->list){
mod = list_entry(list, struct module, list);
do_stuff_with_mod(mod);
}
This uses the method find_module
from kernel/module. You will need to specify your module to be licensed under GPL or otherwise it will fail to load (compilation will be successful though).
Disclaimer
I don't think this method is the best approach but it is solid enough to work with simple tasks like printk
来源:https://stackoverflow.com/questions/43558217/iterate-over-all-modules-in-a-kernel-module