-1
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include "test.h"

MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void) {
     printk("DRIVER LOADED\n");
     printk("DRIVER INIT\n");
     printk("result : %d",add(12,234));
     return 0;
}
static void hello_exit(void) {
     printk("DRIVER REMOVED\n");
}

module_init(hello_init);
module_exit(hello_exit);

here the add function output doesn't print by printk when the insmod issuing I am a fresh ldd lerner, plz can anybody help me to understand why this happening?

my test.c containing the following function body

int add(int a , int b){

    return a+b;
}

and Makefile is as follows

obj-m = initial.o
all:
            make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) modules
clean:
            make -C /lib/modules/$(shell uname -r)/build/ M=$(PWD) clean

and my output shows:

Aug 16 11:15:44 beaglebone kernel: [13616.191741] DRIVER LOADED
Aug 16 11:15:44 beaglebone kernel: [13616.191758] DRIVER INIT
Aug 16 11:15:47 beaglebone kernel: [13616.191784] result : 246
Aug 16 11:15:47 beaglebone kernel: [13619.465555] DRIVER REMOVED
  • I do not understand you. Can you please rephrase your sentences? What is happening? What are you issuing? The dmesg output seems ok, your function added 12 with 236 and printed that out. Is there any reason why your header `test.h` file contains the function definition, not just function declaration? – KamilCuk Aug 16 '18 at 11:36
  • add function results the 246 that's okay but it executed while I issued rmmod command not when I'm inserting insmod. As i have written this function in the module_init func so it should be executed at a time of insertion using insmod, shouldn't it? @KamilCuk – Desarrollador Rucha Aug 16 '18 at 11:47
  • are the timestamps from the /var/log/messages file? – P.W Aug 16 '18 at 12:09
  • Possible duplicate of [Why printk doesn't print message in kernel log(dmesg)](https://stackoverflow.com/questions/38822599/why-printk-doesnt-print-message-in-kernel-logdmesg) – Tsyvarev Aug 29 '18 at 08:57
  • As noted in the referenced question, you forget to add newline (`\n`) at the end of printed string. Because of that, the printed message is appeared in the log with a delay. – Tsyvarev Aug 29 '18 at 08:59

1 Answers1

1

printk output appears once the line is flushed. As you're missing a \n at the end of the printk the string generated it printed at the next flush that will happen when you remove the kernel module. so printk("result : %d\n",add(12,234)); should work.

jklmnn
  • 481
  • 1
  • 5
  • 11