使用proc文件打印mem_map的虚拟地址 [英] Print Virtual Address of mem_map using a proc file

查看:206
本文介绍了使用proc文件打印mem_map的虚拟地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to print the contents of the mem_map variable in the kernel.

但是,当我通过发出make来编译代码时,我会看到:

However when I compile my code by issuing make I see:

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我不知道为什么它没有选择变量.

There are the headers I have included, my understanding is that is mem_map is supposed to be in the mmzone.h I can't figure out why its not picking up the variable.

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

推荐答案

在链接器阶段的警告,如

Warning at linker stage like

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

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

means that mem_map is declared, but its definition is not accessible for modules.

因此,您需要在源文件(.c文件)中使用诸如EXPORT_SYMBOL(mem_map)EXPORT_SYMBOL_GPL(mem_map)之类的搜索语句.

So you need search statements like EXPORT_SYMBOL(mem_map) or EXPORT_SYMBOL_GPL(mem_map) in sources (.c files).

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

Found that for kernel 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.

Probably, you haven't CONFIG_NEED_MULTIPLE_NODES kernel's configuration set, so symbol mem_map is not exported(and even not defined).

这篇关于使用proc文件打印mem_map的虚拟地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆