In section 2.6 of the doc "passing command line args to modules", I
get confused at the output of the module. The output shows it say
something like "myintArray is -1 to 420". But I don't see anywhere in
the source that would do this.
Is this doc just incomplete or am I missing something here?
Also what is the point of passing command line args to modules??
I think the sample is a bit inaccurate !
Ref. http://www.tldp.org/LDP/lkmpg/2.6/html/x333.html
myintArray[] is not defined by "module_parm_array" in that sample.
------------------------------------
insmod will output an error if unknown variable. So the sammple is bogus!
Even the tiny array sample is slightly wrong (small issue, but confusing
for a newbie)
int myshortArray[4];
MODULE_PARM (myintArray, "3-9i");
should be
int myshortArray[4];
MODULE_PARM (myshortArray, "3-9h");
--------------------------------------
Try this (7 steps)
1) The code:
// ---- beg -----------------
/*
hello-5.c - Demonstrates command line argument passing to a module.
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Peter Jay Salzman");
static short int myshort = 1;
static int myint = 420;
static long int mylong = 9999;
static char *mystring = "blah";
#define MAX_ARR 10
static int arrSize = MAX_ARR;
static int myintArray[MAX_ARR] = {[0 ... (MAX_ARR - 1)] = 0};
/*
* module_param(foo, int, 0000)
* The first param is the parameters name
* The second param is it's data type
* The final argument is the permissions bits,
* for exposing parameters in sysfs (if non-zero) at a later stage.
*/
module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
// MODULE_PARM(myshort, "h");
MODULE_PARM_DESC(myshort, "A short integer");
module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
// MODULE_PARM(myint, "i");
MODULE_PARM_DESC(myint, "An integer");
module_param(mylong, long, S_IRUSR);
// MODULE_PARM(mylong, "l");
MODULE_PARM_DESC(mylong, "A long integer");
module_param(mystring, charp, 0000);
// MODULE_PARM(mystring, "s");
MODULE_PARM_DESC(mystring, "A character string");
module_param_array(myintArray, int, arrSize , 0444);
// MODULE_PARM (myintArray, "0-10i"); ~~ Not sure how this min-max
notation should be !! 0-9i ??
MODULE_PARM_DESC(myintArray, "An int array test");
static int __init hello_5_init(void)
{
int i;
printk(KERN_INFO "hello-5: Hello, world 5\n=============\n");
printk(KERN_INFO "hello-5: myshort is a short integer: %hd\n",
myshort);
printk(KERN_INFO "hello-5: myint is an integer: %d\n", myint);
printk(KERN_INFO "hello-5: mylong is a long integer: %ld\n", mylong);
printk(KERN_INFO "hello-5: mystring is a string: %s\n", mystring);
for (i=0; i< MAX_ARR; i++)
printk(KERN_INFO "hello-5: myIntArray[%d]: %d\n", i,
myintArray[i]);
return 0;
}
static void __exit hello_5_exit(void)
{
printk(KERN_ALERT "Goodbye, world 5\n");
}
module_init(hello_5_init);
module_exit(hello_5_exit);
// ------- end ---------------
=========================================
2) cat Makefile
obj-m += hello-5.o
=========================================
3) Compile
# make -C /usr/src/linux-`uname -r` SUBDIRS=$PWD modules
4) insmod
# insmod hello-5.o myshort=85 myint=20471 mylong=21436587 mystring=
"paris" myintArray=9,8,7,6,5,4
BUT THIS MAY NOT WORK !
# insmod hello-5.o myshort=85 myint=20471 mylong=21436587 mystring=
"Paris Hilton Love" myintArray=9,8,7,6,5,4
5) Note: remove it
# rmmod hello-5
6) List modules
# lsmod
# lsmod | grep hello
7) Study the OUTPUT (list /var/log/messages !)
# grep hello-5 /var/log/messages
// moma
http://www.futuredesktop.org/how2burn.html
http://www.futuredesktop.org/migration_path.html
<snip>
>
> 7) Study the OUTPUT (list /var/log/messages !)
>
> # grep hello-5 /var/log/messages
>
How to print the (KERN_DEBUG, _INFO, _ALERT) messages on the screen ?
# dmesg -n 5 ???
# cat /proc/sys/kernel/printk
If you know, us you show -;)
--------------
BTW: The args cannot take space after i.e mystring=
# insmod hello-5.o myshort=85 myint=20471 mylong=21436587 mystring=
"paris" myintArray=9,8,7,6,5,4
// moma
I'm using a script like this:
#!/bin/sh
/etc/init.d/klogd stop
tee debug.log < /proc/kmsg
It displays all messages in screen and saves them into file too.
Since it's used while debugging kernel modules, the kernel
standard logging is disabled (don't want own debugging messages
into kernel log file).
If you wan't only certain debug levels to show up, you could
do something like
grep '^<6>' < /proc/kmsg | tee debug.log
Would capture messages at level 6 (not tested).
Thanks.
> It displays all messages in screen and saves them into file too.
> Since it's used while debugging kernel modules, the kernel
> standard logging is disabled (don't want own debugging messages
> into kernel log file).
Agree.
> I was lookireferredlinux doc project site and found this kernel
Hello,
I'm going through that book myself.
I just want to say that the code referred to in the original message from
init_module can be tested as is, although not with the examples given by
the author. It looks like the variable names and types got mixed up between
the code and the examples.
My tests went as follows:
Cheers,
Nilo
host:/prog # insmod hello-5.ko
mystring="bebop" myint=255
host:/prog # rmmod hello-5
host:/prog # cat /var/log/messages | tail
| more
Sep 15 16:07:32 host kernel: Hello, world 5
Sep 15 16:07:32 host kernel: =============
Sep 15 16:07:32 host kernel: myshort is a short
integer: 1
Sep 15 16:07:32 host kernel: myint is an integer: 255
Sep 15 16:07:32 host kernel: mylong is a long
integer: 9999
Sep 15 16:07:32 host kernel: mystring is a string:
bebop
Sep 15 16:09:29 host kernel: Goodbye, world 5
host:/prog # insmod hello-5.o
mystring="supercalifragilisticexpialidocious"
host:/prog # rmmod hello-5
host:/prog # cat /var/log/messages | tail
| more
Sep 15 16:11:29 host kernel: Hello, world 5
Sep 15 16:11:29 host kernel: =============
Sep 15 16:11:29 host kernel: myshort is a short
integer: 1
Sep 15 16:11:29 host kernel: myint is an integer: 420
Sep 15 16:11:29 host kernel: mylong is a long
integer: 9999
Sep 15 16:11:29 host kernel: mystring is a string:
supercal
ifragilisticexpialidocious
Sep 15 16:11:33 host kernel: Goodbye, world 5
host:/prog # insmod hello-5.o
mystring="supercalifragilisticexpialidocious" myint=256
host:/prog # rmmod hello-5
host:/prog # cat /var/log/messages | tail
| more
Sep 15 16:13:23 host kernel: Hello, world 5
Sep 15 16:13:23 host kernel: =============
Sep 15 16:13:23 host kernel: myshort is a short
integer: 1
Sep 15 16:13:23 host kernel: myint is an integer: 256
Sep 15 16:13:23 host kernel: mylong is a long
integer: 9999
Sep 15 16:13:23 host kernel: mystring is a string:
supercal
ifragilisticexpialidocious
Sep 15 16:13:27 host kernel: Goodbye, world 5
host:/prog # insmod hello-5.o mylong=hello
insmod: error inserting 'hello-5.o': -1 Invalid parameters
host:/prog # insmod hello-5.o mylong=2456
host:/prog # rmmod hello-5
host:/prog # cat /var/log/messages | tail
| more
Sep 15 16:14:41 host kernel: hello_5: no version
magic, tai
nting kernel.
Sep 15 16:14:41 host kernel: hello_5: `hello' invalid
for p
arameter `mylong'
Sep 15 16:15:12 host kernel: hello_5: no version
magic, tai
nting kernel.
Sep 15 16:15:12 host kernel: Hello, world 5
Sep 15 16:15:12 host kernel: =============
Sep 15 16:15:12 host kernel: myshort is a short
integer: 1
Sep 15 16:15:12 host kernel: myint is an integer: 420
Sep 15 16:15:12 host kernel: mylong is a long
integer: 2456
Sep 15 16:15:12 host kernel: mystring is a string:
blah
Sep 15 16:15:15 host kernel: Goodbye, world 5
--
i am nilo