打印内核与用户段地址

 如下图所示,Linux虚拟内存的高16位全0代表此地址为用户空间地址,全1代表此地址为内核空间地址

可以通过以下代码进行验证

int i = 5;
printf("address of i is %p\n", &i)

内核打印用printk

int i=5;
printk(KERN_INFO "address of i is 0x%p\n", &i);

打印用户段地址

[root@ecs-3370 ~]# cat address.c 
#include <unistd.h>    /*sleep()所在的头文件*/
#include <stdio.h>

 
int main(int argc, char const *argv[])
{
    int i=5;
    printf("address of i is %p\n", &i);
    sleep(300);
    return 0;
}

[root@ecs-3370 ~]# gcc address.c 
[root@ecs-3370 ~]# ./a.out 
address of i is 0x7ffcfd094ffc

printf()函数打印地址会自动添加0x符号标记该值为16进制,可以发现最高的16位全为0,所以忽略不写。

打印内核段地址

[root@ecs-3370 tmp]# cat test.c
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

MODULE_AUTHOR("<insert your name here>");
MODULE_DESCRIPTION("LLKD book:ch4/helloworld_lkm: hello, world, our first LKM");
MODULE_LICENSE("Dual MIT/GPL");
MODULE_VERSION("0.1");

static int __init helloworld_lkm_init(void)
{
    int i=5;
    printk(KERN_INFO "address of i is 0x%p\n", &i);
    return 0; /* success */
}

static void __exit helloworld_lkm_exit(void)
{
    printk(KERN_INFO "Goodbye, world\n");
}

module_init(helloworld_lkm_init);
module_exit(helloworld_lkm_exit);

[root@ecs-3370 tmp]# make
make -C /lib/modules/3.10.0-1062.el7.x86_64/build/ M=/tmp modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-1062.el7.x86_64'
  Building modules, stage 2.
  MODPOST 1 modules
make[1]: Leaving directory `/usr/src/kernels/3.10.0-1062.el7.x86_64'

[root@ecs-3370 tmp]# insmod test.ko 

[root@ecs-3370 tmp]# dmesg 
[150741.097975] address of i is 0xffff9fe35a313d24

可以发现高16位都为1(0xffff ....),由于内核printk函数不会自动添加0x注释符,因此为了好看,打印的时候写为0x%p。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值