问题
Possible Duplicate:
Unsigned long with negative value
I have written one kernel module which interrupts any system call, prints its current user_id and input parameters passed to the system call function. Among them, one is sys_ioctl() as below:
asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd,unsigned long arg);
which means all input parameters are unsigned int numbers.
But when I print input parameters, I get the following output:
fd=21, cmd=-1072143871 and arg=3202983648
fd=21, cmd=-1072143871 and arg=3202983648
fd=21, cmd=-1072143871 and arg=3202983648
----------
Here is my function definition:
asmlinkage long our_sys_ioctl(unsigned int fd , unsigned int cmd , unsigned long arg)
{
uid_t gtuid ;
gtuid = getuid_call();
printk ("our_sys_ioctl ---> uid = %d with fd=%i, cmd=%i and arg=%lu \n ", gtuid, fd, cmd, arg);
return original_call_ioctl(fd , cmd , arg);
}
Any idea why cmd value is negative and what these values mean?
回答1:
When you use %i
to print the cmd
you are casting it to signed int
as @Mario previously guessed. This is why it is negative.
You need to use %u
to do the printing for it to remain an unsigned int
d or i - Signed decimal integer
u - Unsigned decimal integer
(from: http://www.cplusplus.com/reference/cstdio/printf/)
This will work as you would expect.
printk ("our_sys_ioctl ---> uid = %d with fd=%i, cmd=%u and arg=%lu \n ", gtuid, fd, cmd, arg);
^^^^
回答2:
You print with %i
which converts the argument to a signed int. Print with %u
.
来源:https://stackoverflow.com/questions/14181691/why-does-printf-show-negative-values-for-unsigned-int