Jon Snow
Jon Snow

Reputation: 11

linue kernel driver, dereference pointer and printk

this is a testing device driver code. I have kernel space data which is dptr->data. Afte I copyt it to user. I would like to printk it.

But my printk seems prints out address.

if (copy_to_user(buf, dptr->data[s_pos] + q_pos, count)) {

for(i=0;i<count;i++){
    printk(KERN_NOTICE "data:%p\n ",(dptr->data[s_pos]+q_pos+i));
}

If I change it to

printk(KERN_NOTICE "data:%p\n ",(void*)*(dptr->data[s_pos]+q_pos+i));

compile failed. please help

Upvotes: 0

Views: 3260

Answers (2)

shuckc
shuckc

Reputation: 2993

Take a look at the printk-formats.txt kernel documentation:

If variable is of Type,             use printk format specifier:
---------------------------------------------------------
            int                     %d or %x
            unsigned int            %u or %x
            long                    %ld or %lx
            unsigned long           %lu or %lx
            long long               %lld or %llx
            unsigned long long      %llu or %llx
            size_t                  %zu or %zx
            ssize_t                 %zd or %zx

Raw pointer value SHOULD be printed with %p.

Upvotes: 0

Chris Stratton
Chris Stratton

Reputation: 40397

*p is a format specifier for a pointer, ie, an address.

If you want to print data, you will need an appropriate format specifier for the type of data you want to print - for example %x though this depends on the data type.

To get at the data you will have to dereference a pointer. You are trying to cast your pointer to a (void *) and de-reference that, but void pointers by definition canot be de-referenced. So you'll need to cast to an appropriate data type.

For example:

printk(KERN_NOTICE "data:%x\n ",
       *((u8*)(dptr->data[s_pos]+q_pos+i)); //print as byte

printk(KERN_NOTICE "data:%x\n ",
       *((u32*)(dptr->data[s_pos]+q_pos+i)); //print as 32-bit word

Upvotes: 1

Related Questions