打印super_block結構中的一些域的值

#include<linux/module.h>
#include<linux/fs.h>
#include<linux/init.h>
#include<linux/list.h>
#include<linux/spinlock.h>
#include<linux/kdev_t.h>

#define SUPER_BLOCKS_ADDRESS 0xffffffff81c77670
#define SB_LOCK_ADDRESS 0xffffffff820137b0

static int __init my_init(void)
{
    struct super_block *sb;
    struct list_head *pos;
    struct list_head *linode;
    struct inode *pinode;
    unsigned long long count = 0;
    
    printk("\nPrint some fields of super_blocks:\n");
    spin_lock((spinlock_t *)SB_LOCK_ADDRESS);
    list_for_each(pos, (struct list_head *)SUPER_BLOCKS_ADDRESS) {
        sb = list_entry(pos, struct super_block, s_list);
        printk("dev_t: %d : %d", MAJOR(sb->s_dev), MINOR(sb->s_dev));
        printk("  file_type name: %s\n",sb->s_type->name);

        list_for_each(linode, &sb->s_inodes) {
            pinode=list_entry(linode,struct inode,i_sb_list);
            count++;
            printk(" %lu\t",pinode->i_ino);
        }
    }
    spin_unlock((spinlock_t *)SB_LOCK_ADDRESS);
    printk("The number of inodes: %llu\n",sizeof(struct inode) *count);
    return 0;
}

static void __exit my_exit(void)
{
    printk("unloading...\n");
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");



在寫代碼之前需要先找到super_blocks和sb_lock的地址,操作如下:

$ cat /proc/kallsyms | grep super_blocks

ffffffff81c77670 d super_blocks

$ cat /proc/kallsyms | grep sb_lock

ffffffff820137b0 b sb_lock
實際上super_blocks和sb_lock在fs.h和spinlock.h頭文件中對此已經定義。但是,在linux內核中,並不是每個變量和函數都可以在其他子系統和模塊中被引用,只有導出後才能被引用,所以才需要上述的操作導出其地址。

發佈了64 篇原創文章 · 獲贊 16 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章