1

I am trying to teach myself Linux kernel development by working through Linux Device Drivers, and I'm currently on the chapter about debugging. From everything I've read, printk() messages are supposed to go to "the console", which from what I understand, is usually represented by the device "/dev/console".

The problem is that I can not get any of my pritnk statements to appear when I monitor /dev/console using the xconsole utility. My messages DO appear in the system logs, and I can see my messages when I use dmesg. So I am not asking about this for practical purposes, but rather to fill in my own gaps in understanding the Linux system.

I set my printk messages to the highest log level (KERNEL_EMERG), just to be sure they weren't getting filtered. I used dmesg -E which supposedly "enables console logging", but nothing worked.

I am running Kubuntu 20, with custom kernel 5.4.55. with the kernel debugging config option enabled. Here is my /proc/cmdline file:

BOOT_IMAGE=/boot/vmlinuz-5.4.55 root=UUID=3978ed71-51b0-4505-83b9-58401946ed0f ro console=tty0 vt.handoff=7

Here is my /proc/sys/kernel/printk :

4 4 1 7

And here is my kernel configurations:

https://pastebin.com/ijAS6tVa

Thank you for your help

Edit

To addition to @user433151's answer, I found that printk() messages only appear on the currently active virtual terminal, even when the console is explicitly set during boot. For example, booting with console=tty2 only seems to effect the fact that /dev/console "points" to tty2, it does not make tty2 the "target" console for kernel messages. It appears the default "target" console for kernel messages is always tty0, so in order for printk messages to appear on tty2, tty2 must the be the currently active virtual terminal at the time the printk message is written. Part of my confusion came from running my modules on tty1, then switching to tty2 only to find my printk statements not appearing. My solution was to start the modules in tty2, or to redirect messages with ioctl(TIOCL_SETKMSGREDIRECT)

2

xconsole (1) can only get what user-land programs write to /dev/console, but not any kernel messages written to any of the terminals /dev/console is a kind of alias for (2).

The printk kernel messages can only go to

  1. a linux virtual terminal like /dev/tty0 (but not to any pseudo-terminal or terminal emulator) (3). "Virtual terminals" are those "black" non-gui terminals you can switch through with Ctrl-Alt-Fx on a PC running Linux.

  2. a serial console like /dev/ttyS0 (but not to any serial-over-USB console or similar).

  3. the dmesg buffer, from where they're picked by the system logger (whether rsyslog or some systemd thing) or by programs like dmesg.

(1) xconsole works by creating a pseudo terminal, and redirecting there with ioctl(TIOCCONS) any userland writes to /dev/console.

(2) Linux has no interface to get which terminal /dev/console refers to; the only way to get that info is by live-debugging the kernel ;-)

(3) /dev/tty0 is an alias for the currently "focused" virtual terminal. You can redirect kernel messages to another virtual terminal with ioctl(TIOCL_SETKMSGREDIRECT).

4
  • That clarifies so much, thank you! Sep 16 '20 at 21:35
  • You mentioned that kernel messages are directed to virtual terminals such as /dev/ttyx, so to test that I booted with console=tty2 , however my printk statements do not appear in tty2 (which I check by pressing ctrl+alt+f2). If I run dmesg -n 8 then tty2 will log button inputs, but none of the statements in my kernel module get displayed Sep 17 '20 at 1:53
  • 1
    Yes it has an interface, and a console can also be a parallel port. jdebp.uk./Softwares/nosh/guide/commands/linux-console.xml
    – JdeBP
    Sep 17 '20 at 1:57
  • @JdeBP I know about TIOCGDEV and the tricks used by busybox's cttyhack. However, they aren't able to determine the terminal where /dev/console was redirwcted with TIOCCONS. I will edit the answer and clarify that (and other) confusing overly broad statemwnts.
    – user433151
    Sep 17 '20 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.