0

I currently started learning the Linux Device driver programming in Linux. where I found this small piece of code printing hello world using printk() function.

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
        printk(KERN_ALERT "Hello World!!!\n");
        return 0;
}

static void hello_exit(void)
{
        printk(KERN_ALERT "Goodbye Hello World!!!\n");
}

module_init(hello_init);
module_exit(hello_exit);

After compiling code using make command and load driver using insmod command. I'm not getting the "Hello world" printed on screen instead its printing only on the log file /var/log/kern.log. But I want printk to print on my ubuntu terminal. I'm using ubuntu(14.04). Is it possible?

msc
  • 33,420
  • 29
  • 119
  • 214
  • With some trick it's possible, but you perhaps do not need it. – 0andriy Sep 05 '16 at 14:22
  • The output would appear on the *system console*. For embedded systems and SBCs, the console is typically a specific serial port. The kernel parameter `console=...` is used to designate the device(s) with optional attributes. Ubuntu distros typically do not define a console in the command line. – sawdust Sep 05 '16 at 20:23
  • https://wiki.ubuntu.com/Kernel/KernelDebuggingTricks – sawdust Sep 05 '16 at 21:18

2 Answers2

3

It isn't possible to redirect kernel logs and massages to gnome-terminal and there you have to use dmesg. But in a virtual terminal(open one with ctrl+F1-F6) you can redirect them to standard output.
First determine tty number by entering tty command in virtual terminal.The output may be /dev/tty(1-6).
Compile and run this code with the argument you specified.

/*
* setconsole.c -- choose a console to receive kernel messages
*
* Copyright (C) 1998,2000,2001 Alessandro Rubini
* 
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation; either version 2 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program; if not, write to the Free Software
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
    char bytes[2] = {11,0}; /* 11 is the TIOCLINUX cmd number */
    if (argc==2) bytes[1] = atoi(argv[1]); /* the chosen console */
    else {
        fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);
    }
    if (ioctl(STDIN_FILENO, TIOCLINUX, bytes)<0) {    /* use stdin */
        fprintf(stderr,"%s: ioctl(stdin, TIOCLINUX): %s\n",
            argv[0], strerror(errno));
        exit(1);
    }
    exit(0);
}

For example if your output for tty command was /dev/tty1then type this two command:

gcc setconsole.c -o setconsole
sudo ./setconsole 1

This will set your tty to receive kernel messages.
Then compile and run this code.

/*
 * setlevel.c -- choose a console_loglevel for the kernel
 *
 * Copyright (C) 1998,2000,2001 Alessandro Rubini
 * 
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/klog.h>

int main(int argc, char **argv)
{
    int level;

    if (argc==2) {
    level = atoi(argv[1]); /* the chosen console */
    } else {
        fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);
    }
    if (klogctl(8,NULL,level) < 0) {
        fprintf(stderr,"%s: syslog(setlevel): %s\n",
                argv[0],strerror(errno));
        exit(1);
    }
    exit(0);
}

There are 8 level of kernel messages as you specify in your code KERN_ALERT is one of them.To make console receive all of them you should run above code with 8 as arguement.
gcc setlevel.c -o setlevel
sudo ./setlevel 8

Now you can insert your module to kernel and see kernel logs in console.
By the way these codes are from ldd3 examples.

Hamjam
  • 71
  • 4
0

printk prints to the kernel log. There is no "screen" as far as the kernel is concerned. If you want to see the output of printk in real time, you can open a terminal and type the following dmesg -w. Note that the -w flag is only supported by recent versions of dmesg (which is provided by the util-linux package).

redneb
  • 21,794
  • 6
  • 42
  • 54
  • *"There is no "screen" as far as the kernel is concerned"* -- Umm, what about the (system) console (which can be designated by a parameter in the kernel command line)? – sawdust Sep 05 '16 at 20:15
  • You can ask the kernel to output all messages from the kernel log to the main console with `dmesg -n9`, or you can use a smaller value to select only some messages. – redneb Sep 05 '16 at 20:20
  • *"You can ask the kernel to output all messages from the kernel log to the main console ..."* -- No, you do not have to *"ask"* for messages on the system console. See the link above about kernel debugging and [this question regarding the console log-level](http://stackoverflow.com/questions/16390004/change-default-console-loglevel-during-boot-up). Perhaps you have only used Linux with a GUI front-end? – sawdust Sep 06 '16 at 06:57
  • Does it look like that I have "_only used Linux with a GUI front-end_"? Read the OP's question and my answer again. It is clear that the OP had the expectation that if they run `modprobe`/`insmod` they would see the `printk` message in the terminal they typed `modprobe` from (the "_screen_"). I was trying to explain that this won't work in a way the OP would understand. – redneb Sep 06 '16 at 07:24