Complete Communications Engineering

One way is to use the kernel function get_current_tty().  This will return a pointer with type struct tty_struct which has methods that can be used to write directly to the console.  Specifically, the function driver->ops->write on most platforms should write a string directly out to the serial console.  Both the function and structs can be included through <linux/tty.h>.  The following C code demonstrates this method:

#include <linux/tty.h>

 

static void print_to_console(const char *str)

{

    struct tty_struct *tty;

    tty = get_current_tty();

    if (tty) {

        tty->driver->ops->write(tty, str, strlen(str));

    }

}

This function might be useful for kernel-level debugging.  It can be copied into any source file to generate prints that go directly out to the serial port.  This can be helpful when investigating issues such as kernel panics or freezing / locking up.  For those types of issues, it’s generally best to stick with short prints that can help trace the code path, so formatting is usually not necessary.  If formatting is required, it can be added using va_args.  Keep in mind that this may not work in all contexts.  For example, it might not work from an interrupt handler routine.