It is a problem regarding to the C syntax.
In C language, two strings (i.e. characters compassed by a pair of brackets) separated by a comma are treated as two parameters in a parameter list. Two strings separated by merely blank spaces, carriage return, or tabs are treated as a single string generated by concatenating the original two.
In your case, what printk() expects is to concatenate KERN_xxx with your format string as the first parameter. What you did is to pass KERN_xxx as the first parameter, and pass what should be part of the first parameter as the second parameter.
2006/12/13, Smith Norton <smith.norton@xxxxxxxxx>:
I am sorry, I couldn't find answers to the two questions I have asked
in the link you have given.
Can someone kindly answer the two questions I have asked? I am a
newbie in this field, hence I need a little help to get started with
this.
- Smith.
On 12/12/06, Christophe Lucas < clucas@xxxxxxxxxxxxx> wrote:
> Smith Norton (smith.norton@xxxxxxxxx) wrote:
> > I am trying the following kernel module in Ubuntu-6.06 LTS. I am using
> > the kernel that came along with Ubuntu.
> >
> > #include <linux/module.h>
> > #include <linux/kernel.h>
> > #include <linux/init.h>
> >
> > MODULE_LICENSE("GPL");
> >
> > static int __init helloinit(void)
> > {
> > printk(KERN_ALERT, "[helloinit1]\n");
>
> printk(KERN_ALERT "[helloinit1]\n");
> => without coma.
>
>
> > printk("[helloinit2]\n");
> > return 0;
> > }
> >
> > static void __exit helloexit(void)
> > {
> > printk(KERN_ALERT, "[helloexit1]\n");
> > printk("[helloexit2]\n");
> > }
> >
> > module_init(helloinit);
> > module_exit(helloexit);
> >
> > After compiling it with 'make', I 'insmod' it and then 'rmmod' it
> > twice. The last few lines of /var/log/kern.log are as follows:-
> >
> > Dec 11 23:14:40 localhost kernel: [4295711.583000] <1><1>[helloinit2]
> > Dec 11 23:14:45 localhost kernel: [ 4298099.296000] [helloexit2]
> > Dec 11 23:15:22 localhost kernel: [4298136.092000] [helloinit2]
> > Dec 11 23:15:27 localhost kernel: [4298141.000000] [helloexit2]
> >
> > 1. According to LKMPG '[helloinit1]' and '[helloexit1]' should have
> > been printed. Why weren't they printed. Instead the <1> (KERN_ALERT)
> > was printed. Here are all the macro defintions in my Ubuntu box:-
> >
> > smith@smithbox:~/kernel/exp$ grep "define"
> > /usr/src/linux-headers-`uname -r`/include/linux/kernel.h | grep
> > "KERN_"
> > #define KERN_EMERG "<0>" /* system is unusable */
> > #define KERN_ALERT "<1>" /* action must be taken immediately */
> > #define KERN_CRIT "<2>" /* critical conditions */
> > #define KERN_ERR "<3>" /* error conditions */
> > #define KERN_WARNING "<4>" /* warning conditions */
> > #define KERN_NOTICE "<5>" /* normal but significant condition */
> > #define KERN_INFO "<6>" /* informational */
> > #define KERN_DEBUG "<7>" /* debug-level messages */
> >
> > smith@smithbox:~/kernel/exp$ grep "int printk("
> > /usr/src/linux-headers-`uname -r`/include/linux/kernel.h
> > asmlinkage int printk(const char * fmt, ...)
> > static inline int printk(const char *s, ...)
> > static inline int printk(const char *s, ...) { return 0; }
> >
> > Why aren't '[helloinit1]' and '[helloexit1]' getting printed?
> >
> > 2. According to the sequence of statements, the output should have been:-
> >
> > <1>[helloinit2]
> > <1>[helloexit2]
> > <1>[helloinit2]
> > <1>[helloexit2]
> >
> > But the output instead is:-
> >
> > <1><1>[helloinit2]
> > [helloexit2]
> > [helloinit2]
> > [helloexit2]
> >
> > Why?
>
> See : http://www.xml.com/ldd/chapter/book/ch04.html#t1
>
> Have a nice day,
>
> Regards,
>
> Christophe
>
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive: http://mail.nl.linux.org/kernelnewbies/
FAQ: http://kernelnewbies.org/faq/