Home > Back-end >  why the out of %p and %x is different in linux driver
why the out of %p and %x is different in linux driver

Time:12-26

I have write a linux driver program. and the main code is:

struct file_operations dummy_fops = {
    open:       dummy_open,
    release:    dummy_release,
    read:       dummy_read,
    write:      dummy_write,
};

ssize_t dummy_read (struct file *filp, char __user * buf, size_t count,
                                loff_t * offset)
{
    int c = 0;
    pr_info("Nothing to read guy\n");
    pr_info("address %p %x", &c, &c); // this is output
    return 0;
}

I tapped the command:

make
sudo insmod my_module
sudo cat /dev/my_module
dmesg

the main output is:

[10004.473406] Someone tried to open me
[10004.473425] Nothing to read guy
[10004.473431] address 32ba0e06 eef1ff1c
[10004.473446] Someone closed me

Why the output %p 32ba0e06 and the output %x eef1ff1c is different?

CodePudding user response:

Because the pointers in the Linux kernel are hashed by default for the security reasons. So, taking into account the 32-bit architecture (or the kernel compiled for 32-bit mode) there is no difference between %px and %x. The %px is the special pointer specifier extension that tells “I do want to print the real address”.

PS for 64-bit architecture the %x will print only least 32 bits, so you need to use %lx instead.

  • Related