static inline void print_modules(void)用于曾经在kernel中存在的模块的name和flag
其使用的例子如下:
static noinline void __schedule_bug(struct task_struct *prev)
{
/* Save this before calling printk(), since that will clobber it */
unsigned long preempt_disable_ip = get_preempt_disable_ip(current);
if (oops_in_progress)
return;
printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
prev->comm, prev->pid, preempt_count());
debug_show_held_locks(prev);
print_modules();
}
其源码如下:
/* Don't grab lock, we're oopsing. */
void print_modules(void)
{
struct module *mod;
char buf[MODULE_FLAGS_BUF_SIZE];
printk(KERN_DEFAULT "Modules linked in:");
/* Most callers should already have preempt disabled, but make sure */
preempt_disable();
//遍历module list,将处于非MODULE_STATE_UNFORMED 状态的模块打印出来。其中包括已经rmod的ko
list_for_each_entry_rcu(mod, &modules, list) {
if (mod->state == MODULE_STATE_UNFORMED)
continue;
pr_cont(" %s%s", mod->name, module_flags(mod, buf));
}
preempt_enable();
//last_unloaded_module[0] 表示最后一个unload的模块的name,如果其不为空的话,则将其打印出来
if (last_unloaded_module[0])
pr_cont(" [last unloaded: %s]", last_unloaded_module);
pr_cont("\n");
}
last_unloaded_module 定义如下:
static char last_unloaded_module[MODULE_NAME_LEN+1];
#define MODULE_NAME_LEN MAX_PARAM_PREFIX_LEN
/* Chosen so that structs with an unsigned long line up. */
#define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
从这里也可以看出模块的name 最长不能超过64个bytes
模块卸载时都会调用delete_module,在这个系统调用中会通过strlcpy 将最后一个卸载的模块的name保存在last_unloaded_module 中。因此
last_unloaded_module中永远保存着最后一个卸载的模块.
SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
unsigned int, flags)
{
struct module *mod;
char name[MODULE_NAME_LEN];
/* Store the name of the last unloaded module for diagnostic purposes */
strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
}
模块API之print_modules
最新推荐文章于 2023-05-18 21:34:23 发布