2015-07-06 21 views
0

我必須在內核中打印mem_map變量的內容。使用proc文件打印mem_map的虛擬地址

然而,當我通過發行使編譯我的代碼,我看到:

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); 

回答

0

警告像

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

意味着mem_map聲明,但它的定義是不適合的模塊訪問。

因此,您需要在源文件(.c文件)中搜索語句,如EXPORT_SYMBOL(mem_map)EXPORT_SYMBOL_GPL(mem_map)

發現,內核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不會導出(甚至沒有定義)。