使用/dev/kmem读取内核变量的值
【摘要】
调试嵌入式Linux内核时,可能需要查看某个内核变量的值。/dev/kmem正好提供了访问内核虚拟内存的途径。现在的内核大都默认禁用了/dev/kmem,打开的方法是在 make menuconfig中选中 device drivers --> Character devices -->/dev/kmem virtual d...
调试嵌入式Linux内核时,可能需要查看某个内核变量的值。/dev/kmem正好提供了访问内核虚拟内存的途径。现在的内核大都默认禁用了/dev/kmem,打开的方法是在 make menuconfig中选中 device drivers --> Character devices -->/dev/kmem virtual device support.
用 cat /proc/kallsyms | grep *** 找到要查看的变量的地址, 这个地址作为本程序的输入参数。
int snd_write_bypass = 0;
EXPORT_SYMBOL_GPL(snd_write_bypass);
/dav/test # cat /proc/kallsyms | grep snd_write_bypass
8051e26c r __ksymtab_snd_write_bypass
80530219 r __kstrtab_snd_write_bypass
app code
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#define DEVKMEM "/dev/kmem"
#define PAGE_SIZE 0x1000
#define PAGE_MASK (~(PAGE_SIZE-1))
int
main(
int
argc,
char
* argv[])
{
int
fd;
char
*mbase;
char
read_buf[10];
unsigned
int
regAddr;
varAddr =
strtoul
(argv[1], 0, 16);
unsigned
int
ptr = varAddr & ~(PAGE_MASK);
fd = open(DEVKMEM, O_RDONLY);
if
(fd == -1) {
perror
(
"open"
);
exit
(-1);
}
mbase = mmap(0,PAGE_SIZE,PROT_READ,MAP_SHARED,fd, (varAddr & PAGE_MASK));
if
(mbase == MAP_FAILED) {
printf
(
"map failed %s\n"
,
strerror
(
errno
));
}
printf
(
"varAddr = 0x%X \n"
, varAddr);
printf
(
"mapbase = 0x%X \n"
, (unsigned
int
)mbase);
printf
(
"value = 0x%X \n"
,*(unsigned
int
*)(mbase+ptr));
close(fd);
munmap(mbase,PAGE_SIZE);
return
0;
}
/dav/test # ./devkmem __kstrtab_snd_write_bypass
map failed Input/output error
varAddr = 0x0
mapbase = 0xFFFFFFFF
Segmentation fault
文章来源: blog.csdn.net,作者:悟空胆好小,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/xushx_bigbear/article/details/49763951
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)