2016-09-05 7 views
0

私は現在、LinuxデバイスドライバのプログラミングをLinuxで始めました。私はprintk()機能を使ってこんにちは世界のコード印刷のこの小さな部分を発見した。 makeコマンドを使用して負荷駆動insmodコマンドを使用してコードをコンパイルした後printkを使ってLinux端末でデータを印刷する

#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); 

「Hello world」がログファイル/var/log/kern.logでのみ表示される代わりに、画面に表示されません。しかし、私はprintkを私のubuntu端末に印刷します。私はubuntu(14.04)を使用しています。出来ますか?

+0

いくつかのトリックがありますが、おそらくそれは必要ありません。 – 0andriy

+0

出力は*システムコンソール*に表示されます。組み込みシステムおよびSBCの場合、コンソールは通常、特定のシリアルポートです。カーネルパラメータ 'console = ...'は、オプションの属性を持つデバイスを指定するために使用されます。 Ubuntuディストリビューションは通常、コマンドラインにコンソールを定義しません。 – sawdust

+0

https://wiki.ubuntu.com/Kernel/KernelDebuggingTricks – sawdust

答えて

0

printkカーネルログに出力します。カーネルに関する限り、「スクリーン」はありません。 printkの出力をリアルタイムで表示するには、端末を開いて次のように入力します。dmesg -w-wフラグは、最新バージョンのdmesgutil-linuxパッケージによって提供されています)でのみサポートされています。

+0

* "カーネルに関する限り"画面はありません "* - (システム)コンソール(カーネルコマンドラインのパラメータ)? – sawdust

+0

カーネルに、 'dmesg -n9'を使ってカーネルログからすべてのメッセージをメインコンソールに出力するようにカーネルに依頼するか、より小さい値を使っていくつかのメッセージだけを選択することができます。 – redneb

+0

* "カーネルログからメインコンソールにすべてのメッセージを出力するようにカーネルに依頼することができます。" * - いいえ、システムコンソール上でメッセージを*求める*必要はありません。カーネルデバッグと[コンソールログレベルに関するこの質問](http://stackoverflow.com/questions/16390004/change-default-console-loglevel-during-boot-up)については、上記のリンクを参照してください。おそらくGUIフロントエンドでLinuxを使用しているだけでしょうか? – sawdust

1

カーネルのログとマッサージをgnome-terminalにリダイレクトすることはできません。そこで、dmesgを使用する必要があります。 しかし、仮想端末(ctrl+F1-F6で開いているもの)では標準出力にリダイレクトできます。
仮想端末にttyコマンドを入力して、最初にtty番号を決定します。出力は/dev/tty(1-6)です。
このコードを指定した引数でコンパイルして実行します。

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

これはカーネルメッセージを受け取るためにあなたの端末を設定します:

/* 
* 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); 
} 

は、例えば ttyコマンドのためのあなたの出力は、 の/ dev/tty1のは、この2つのコマンドを入力した場合。
このコードをコンパイルして実行します。

/* 
* 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); 
} 

あなたのコードでを指定するとカーネルメッセージの8レベルがKERN_ALERTは、コンソールを使用すると、arguementとして8で上記のコードを実行する必要があり、それらのすべてを受信しますthem.Toの一つであるがあります。
gcc setlevel.c -o setlevel
sudo ./setlevel 8

は今、あなたはカーネルとコンソールでカーネルログを見るためにあなたのモジュールを挿入することができます。
ところでこれらのコードはldd3の例です。

関連する問題