1

I am trying to print the following variables in my custom Linux Kernel Module for debugging purposes:

__le16 length; // The length of the data
u8 data[128];

The data variable has a maximum length of 128 bytes but we might not use all if it. The length variable determines how much we used of the data variable. I want to print the content of data variable in Hex format. I am doing so using a for loop however, the following code prints each byte in a separate line when I inspect the logs using dmesg. I suppose this is because of the mechanism of how printk works. I can still print the content in the same line but as string using %s. However, I want to print all the bytes in the same line in Hex format. How can I do that?

u8 i = 0;
for (i = 0 ; i < length ; i++)
{
    printk("0x%02x ", data[i]);
}
printk("\n"); 
IoT
  • 607
  • 1
  • 11
  • 23
  • 1
    There are two approaches: `%*ph` specifier (up to 64 bytes to dump at once) and `print_hex_dump()`. Use one which is suitable. So, here is an example: https://stackoverflow.com/a/34164581/2511795 – 0andriy May 12 '18 at 19:02

0 Answers0