LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2011, 08:30 AM   #1
vishaluttamsalve
LQ Newbie
 
Registered: Dec 2011
Posts: 4

Rep: Reputation: Disabled
Regarding how to use Global variable using our created modules


Hi.. We have defined one global variable named as "trace_flag". we have also check that, its entry in kernel global symbol table,Yes it has inserted in table.
now our problem is that,we want to create module. Through that module we want to set that flag value to 1. But we are unable to use that "trace_flag" variable. We are thinking about ioctl() function. But still we are not getting it.. Please suggest.
 
Old 12-21-2011, 04:10 AM   #2
jb_gpk
Member
 
Registered: Dec 2010
Distribution: Debian
Posts: 30

Rep: Reputation: 13
this variable is exported?

AFAIK, the global symbols have to be exported, so, all kernel can access it.

grep for EXPORT_SYMBOL() macro, the kernel use it everywhere.
 
Old 12-21-2011, 08:09 AM   #3
vishaluttamsalve
LQ Newbie
 
Registered: Dec 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Yes we have use EXPORT_SYMBOL. And we have defined one more function to access this "trace_flag",the function name is "set_trace(int)".

void set_trace(int pass)
{
trace_flag=pass
}

For this function also,we have used EXPORT_SYMBOL. And we have checked its existence in global table, YES,both are present..
Now, we have created one module to set this flag to 1,named "tracemod.c"

now in tracemod.c

in init_module()
{
set_trace(1);
}
and
in cleanup_module()
{
set_trace(0);
}


Ok..

Now our problem is ,the value of trace_flag is remain zero itself. It is not get updated...

Please suggest..
 
Old 12-21-2011, 04:35 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
I'd just add some printk() in the set_trace function and in the module init/cleanup.
 
Old 12-21-2011, 11:53 PM   #5
vishaluttamsalve
LQ Newbie
 
Registered: Dec 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
We have done that also. Only problem is that trace_flag is not get updated though it is global...
 
Old 12-23-2011, 07:35 PM   #6
jb_gpk
Member
 
Registered: Dec 2010
Distribution: Debian
Posts: 30

Rep: Reputation: 13
Hello,

I can not understand why it is not working on your module, perhaps you are forgetting some basic step:

I tried to simulate your problem this way:

1) put the global variable and the exported function on somewhere on the kernel, I choose the init/main.c file:

Code:
diff --git a/init/main.c b/init/main.c
index 63f5f6f..e4e0d33 100644
--- a/init/main.c
+++ b/init/main.c
@@ -844,3 +844,11 @@ static int __init kernel_init(void * unused)
        init_post();
        return 0;
 }
+int trace_flag = 0;
+void my_set_trace(int pass)
+{
+       printk(KERN_INFO "setting trace_flag to %d\n",pass);
+       trace_flag = pass;
+       printk(KERN_INFO "trace_flag is now %d\n",trace_flag);
+} 
+EXPORT_SYMBOL(my_set_trace);
2) put the function prototype on some header, so my module can get it, I choose the header linux/kernel.h:
Code:
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 4c0d3b2..4696ec1 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -728,7 +728,7 @@ extern int __build_bug_on_failed;
 
 struct sysinfo;
 extern int do_sysinfo(struct sysinfo *info);
-
+void my_set_trace (int pass);
 #endif /* __KERNEL__ */
 
 #define SI_LOAD_SHIFT  16
3) create a module to call the function:

Code:
#include <linux/kernel.h>
#include <linux/module.h>


int trace_init(void)
{
        printk(KERN_ERR "hello\n");
        my_set_trace(23);
        return 0;
}

void trace_exit(void)
{
        printk(KERN_ERR "bye\n");
}
module_init(trace_init);
module_exit(trace_exit);
so, when I insmod the module it I got:

Quote:
[ 5575.130326] hello
[ 5575.130761] setting trace_flag to 23
[ 5575.130779] trace_flag is now 23
is something like this that you are trying to do?
if yes, this is quite simple, there is no reason to fail.
please review your code and write here what you found.

happy hollyday!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Global Variable? ElementNine Programming 5 11-05-2007 12:53 PM
Where should I declare the global variable ahm_irf Linux - Kernel 5 05-11-2007 11:36 AM
Define global variable maxmil Linux - Newbie 4 03-07-2006 10:36 AM
How can I define a global variable? kloss Linux - General 4 02-21-2006 04:30 AM
problems with a global variable javier_ccs Programming 2 06-21-2005 12:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration