'How to debug in kernel mode using printk

I am trying to add some stuff to Linux task_struct.

In this area I copy a string from the user and try to store it in my struct.

I try to debug my code by adding printk that will print the copied string.

this is the debuging part of the code:

newTODO->TODO_description=(char*)(kmalloc(in_description_size+1,0));
    if( newTODO->TODO_description){
        kfree(newTODO);
        return -1;
    }

    res=copy_from_user(newTODO->TODO_description, in_TODO_description, in_description_size);
        if (res)                                //  error copying from user space, 1 or more char werent copied.
        {
            printk(KERN_ALERT "function: create element failed to copy from user\n");
            return -EFAULT;
        }
    newTODO->TODO_description[in_description_size]='\o';
    printk(KERN_ALERT "the copied string is: %s \n",newTODO->TODO_description);

the must inportent print for me is

printk(KERN_ALERT "the copied string is: %s \n",newTODO->TODO_description);

will it work?

to understand printk:

when i will run my test file from the terminal whenever the printk will be invoked it will print the output right to the working terminal?



Solution 1:[1]

printk function will append message in kernel message buffer, but the contain of this buffer would NOT be showed on terminal unless you give a command.

As Ilya Matveychikov saying, you can use dmesg command to dump kernel message buffer.

or use following commands to get a realtime kernel message observation.

echo 8 > /proc/sys/kernel/printk
tail -f /var/log/kern.log &

or

cat /proc/kmsg & (Android Environment)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community