HELLO.C Kernel module Compilation--Linux kernel

Source: Internet
Author: User

Linux development module, the method of viewing debugging information on this machine has gone through. Current version 2.6.32-32-generic

Uname–r

can query

Take Module_param () as an example here.

the macro is defined in the Include/linux/moduleparam.h file and is defined as follows:

#define MODULE_PARAM (name, type, perm)
Module_param_named (name, name, type, perm)

3 parameters are used: The parameter variable name to pass, the data type of the variable, and the permission to access the parameter.

hello.c

#include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h>module_license (" Dual BSD/GPL "); static char *flag=" world "; static int times = 5;module_param (TIMES,INT,S_IRUSR), Module_param (FLAG,CHARP,S_IRUSR), static int hello_init (void) { int I;for (i=0;i<=times;i++) {PRINTK ("(%d) Hello,%s\n", I,flag);  Kern_debug}return 0;} static void Hello_exit (void) {PRINTK ("goodbye,%s\n", Flag),//kern_debug}module_init (Hello_init); Module_exit (Hello_ Exit);

This file needs to be compiled into a module, using

Makefile

obj-m:= Hello.okerneldir: =/lib/modules/$ (Shell uname-r)/buildpwd       : = $ (shell pwd) default:$ (make)-C $ (Kerneldir) m =$ (PWD) MODULESCLEAN:RM-RF *.o *~ core depend. *.cmd *.ko *.mod.c. tmp_versions


$ (make)-C $ (Kerneldir) m=$ (PWD) modules
1),-C $ (Kerneldir)
Represents the Execute make command in the $ (kerneldir) directory.
2), m=$ (PWD)
Represents a makefile file that contains $ (PWD).
3), modules
Represents a module compilation

In the terminal

Make

Generating the Hello.ko module

Insmod Hello.ko flag= "Daniu" times=5

[Email protected]:/mnt/hgfs/test_curl/core# dmesg-c
[9057.070444] Goodbye,\xffffffe2\xffffff80\xffffff9d\xffffff80\xffffff9ddaniu\xffffffe2\xffffff80\xffffff9d\ xffffff80\xffffff9d
[9059.357777] (0) Hello, daniu
[9059.357781] (1) Hello, daniu
[9059.357783] (2) Hello, Daniu
[9059.357784] (3) Hello, Daniu
[9059.357785] (4) Hello, Daniu
[9059.357786] (5) Hello, Daniu

Rmmod Hello.ko


HELLO.C Kernel module Compilation--Linux kernel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.