Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which header file do I have to include to get printk() within a kernel source file?

Let's say I want to use printk() within arch/x86/boot/string.c before compiling the kernel. Which header file do I have to include so that the linker knows where to find printk()? I tried #include <linux/kernel.h> and #include <linux/printk.h> but I always get errors during make bzImage telling me the linker does not find printk:

arch/x86/boot/compressed/string.o: In function `memcmp`:
string.c:(.text+0x19): undefined reference to `printk`
like image 728
JohnnyFromBF Avatar asked Aug 18 '15 18:08

JohnnyFromBF


People also ask

What is printk in Linux kernel?

printk is a C function from the Linux kernel interface that prints messages to the kernel log. It accepts a string parameter called the format string, which specifies a method for rendering an arbitrary number of varied data type parameter(s) into a string. The string is then printed to the kernel log.

What folder does printk message go in?

printk messages go to the kernel log message buffer, which can be exposed in a variety of ways depending on system configuration. The shell command dmesg will show them, and they should also be being copied to files in /var/log by the syslog daemon.

What are kernel header files?

The linux kernel's exported header files describe the API for user space programs attempting to use kernel services. These kernel header files are used by the system's C library (such as glibc or uClibc) to define available system calls, as well as constants and structures to be used with these system calls.

Where are kernel header files?

The system's libc headers are usually installed at the default location /usr/include and the kernel headers in subdirectories under that (most notably /usr/include/linux and /usr/include/asm).


1 Answers

You are trying to instrument the boot stage of the kernel. It has its own really small library and doesn't have any headers like linux/printk.h available. The function is called printf() and it's implemented in arch/x86/boot/printf.c.

The output of this function goes to the channel whatever defined in BIOS (int 10h) and, if asked, to the legacy (you can't use UART which has 32-bit I/O, for example) serial line. For the details refer to the printf.c source file.

like image 85
0andriy Avatar answered Nov 06 '22 15:11

0andriy