使用proc文件打印mem_map的虚拟地址

时间:2015-07-06 01:44:00

标签: linux memory linux-kernel kernel-module procfs

我必须在内核中打印mem_map变量的内容。

然而,当我通过发布make编译我的代码时,我看到了:

WARNING: "mem_map" [/home/babak/code/module/mem_map.ko] undefined!

从:

make -C /home/babak/code/linux-3.19.5 M=/home/babak/code/module modules
make[1]: Entering directory '/home/babak/code/linux-3.19.5'
  CC [M]  /home/babak/code/module/mem_map.o
  Building modules, stage 2.
  MODPOST 1 modules
WARNING: "mem_map" [/home/babak/code/module/mem_map.ko] undefined!
  LD [M]  /home/babak/code/module/mem_map.ko
make[1]: Leaving directory '/home/babak/code/linux-3.19.5'

我有包含的标题,我的理解是mem_map应该在mmzone.h中。我无法弄清楚为什么它没有拿起变量。

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>

#include <linux/types.h> /* size_t */
#include <linux/fcntl.h> /* O_ACCMODE */
#include <asm/switch_to.h>
#include <asm/uaccess.h> /* copy_from/to_user */
#include <linux/fs.h>       // for basic filesystem
#include <linux/proc_fs.h>  // for the proc filesystem
#include <linux/seq_file.h> // for sequence files
#include <linux/mmzone.h>

MODULE_LICENSE("Dual BSD/GPL");

static struct proc_dir_entry* proc_file;


/* memory map functions */
int mem_map_show(struct seq_file *m, void *v);
//virtual_to_physical
inline unsigned long virt_to_phy(unsigned long addr);

inline unsigned long virt_to_phy(unsigned long addr){
    return __pa(addr);
}


int mem_map_show(struct seq_file *m, void *v){

    int ret_val = 0;

    printk(KERN_INFO "Proc file read \n");
    ret_val =  seq_printf(m, "mem_map virt addr: %p \n", mem_map);
    ret_val += seq_printf(m, "mem_map phys addr: %lu \n",virt_to_phy((unsigned long)mem_map));
    ret_val += seq_printf(m, "mem_map phys pages: %lu \n", (long unsigned int)get_num_physpages);

    return ret_val;
}

static int mem_map_open(struct inode *inode, struct file *file){
    return single_open(file, mem_map_show, NULL);
}

struct file_operations mem_map_fops = {
    .owner = THIS_MODULE,
    .open = mem_map_open,
    .read = seq_read,
    .llseek = seq_lseek,
    .release = single_release,
};

static int __init mem_map_init(void){
    printk(KERN_INFO "Loaded mem_map module\n");
    proc_file = proc_create("mem_map", 0, NULL, &mem_map_fops);
    if(!proc_file){
        printk(KERN_ALERT "Error: Could not initialize /proc/mem_map");
        return -ENOMEM;
    }   
    return 0;
}

static void __exit mem_map_exit(void){
    remove_proc_entry("mem_map",NULL);  
    printk(KERN_INFO "Proc file unloaded \n");
}


/* Declaration of the init and exit functions */
module_init(mem_map_init);
module_exit(mem_map_exit);

1 个答案:

答案 0 :(得分:0)

链接器阶段的警告,如

WARNING: "mem_map" [/home/babak/code/module/mem_map.ko] undefined!

表示声明mem_map,但模块无法访问其定义。

因此,您需要在来源(EXPORT_SYMBOL(mem_map)文件)中使用EXPORT_SYMBOL_GPL(mem_map).c等搜索语句。

发现内核3.19(mm/memory.c):

#ifndef CONFIG_NEED_MULTIPLE_NODES
/* use the per-pgdat data instead for discontigmem - mbligh */
unsigned long max_mapnr;
struct page *mem_map;

EXPORT_SYMBOL(max_mapnr);
EXPORT_SYMBOL(mem_map);
#endif

可能您没有CONFIG_NEED_MULTIPLE_NODES内核的配置集,因此不会导出符号mem_map(甚至未定义)。