modules

advertisement
C questions
A great programmer codes excellent
code in C and Java. The code does video
decoding. Java code works faster then C
on my computer. how come?
I write a function pointer to file. I later
read it as char *. I cast to func pointer
and try to run. will it work?
Example for question 2
int hello()
{
int i=8;
return ++i;
}
typedef int (*hello_t)() ;
int main(int argc, char * argv[])
{
int fd;
char buf[2000];
int size = abs(&(main) - &(hello));
fd=open("./hello.bin", O_RDWR | O_CREAT | O_TRUNC | O_EXCL);
fchmod(fd,S_IRWXU);
write(fd, (void *)&hello, size);
close(fd);
fd=open("./hello.bin", O_RDWR);
int n = read(fd,buf,2000);
hello_t func= buf;
int i=(func)();
close(fd);
return 0;
}
Yet another C question
what does the following code do?
•
int main(int argc, char * argv[])
•
{
•
char a[10];
•
int i;
•
for (i=-5 ; i < 10 ; ++i )
a[i]=’\0’;
•
return 0;
•
•
}
More lkmpg
Sysfs (missing chapter)
As /proc became a twisty maze of
passages, all-alike....
Sysfs was built to represent the kernel
module structure (and only the kernel
module structure)
We can change parameters and
communicate with KM using sysfs
sysfs - references
http://www.kernel.org/pub/linux/kernel/p
eople/mochel/doc/papers/ols2005/mochel.pdf
http://docs.blackfin.uclinux.org/doku.ph
p?id=kernel_objects#kernel_object_sysf
s_example
sysfs - structure
Internal
Kernel Objects
External
Directories
Object Attributes
Regular Files
Object
Relationships
Symbolic Links
sysfs - API
int sysfs_create_dir(struct kobject ∗ k);
void sysfs_remove_dir(struct kobject ∗ k);
int sysfs_rename_dir(struct kobject ∗, const char ∗new_name)
int sysfs_create_file(struct kobject ∗, const struct attribute ∗);
void sysfs_remove_file(struct kobject ∗, const struct attribute ∗);
int sysfs_update_file(struct kobject ∗, const struct attribute ∗);
int device_create_file(struct device ∗device, struct device_attribute
∗entry);
void device_remove_file(struct device ∗dev, struct device_attribute
∗attr);
struct device_attribute { struct attribute
attr;
ssize_t (∗show)(struct device ∗dev, char ∗buf);
ssize_t (∗store)(struct device ∗dev, const char ∗buf, size_t count);
};
struct attribute { char ∗name;
struct module ∗owner;
mode_t mode;
}
Example
review :
http://docs.blackfin.uclinux.org/doku.ph
p?id=kernel_objects#kernel_object_sysf
s_example
Working with /proc
/proc is not supposed to be used for
communication with modules. use /sys
for that.
API
create_proc_entry(PROC_ENTRY_FILENAME, 0644, NULL);
remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
Our_Proc_File->read_proc = procfile_read
ssize_t procfile_read(char *buffer,
•
•
char **buffer_location, off_t offset,
int buffer_length, int *eof, void *data)
Chapter 9 - blocking processes
We can cause a process to block by
calling
wait_event_interruptible
wait_event_interruptible
puts the calling process in an event
queue until something (specified in the
second argument) happens.
blocking process - partial example
while (Already_Open) {
int i, is_sig = 0;
/*
* This function puts the current process, including any
system
* calls, such as us, to sleep.
Execution will be resumed
right
* after the function call, either because somebody called
* wake_up(&WaitQ) (only module_close does that, when the
file
* is closed) or when a signal, such as Ctrl-C, is sent
* to the process
*/
wait_event_interruptible(WaitQ, !Already_Open);
/*
* If we woke up because we got a signal we're not
blocking,
* return -EINTR (fail the system call).
processes
This allows
work queues
We can scheduale work to be done
periodically or within a certain time
using queue_delayed_work
API’s
init :
•
static struct work_struct Task;
•
static DECLARE_WORK(Task, intrpt_routine, NULL);
•
static void intrpt_routine(void *irrelevant)
register :
•
my_workqueue = create_workqueue(MY_WORK_QUEUE_NAME);
•
queue_delayed_work(my_workqueue, &Task, 100);
unregister :
•
•
cancel_delayed_work(&Task); /* no "new ones" */
flush_workqueue(my_workqueue);
•
•
destroy_workqueue(my_workqueue);
And a little bit about linking
Static libs
Dynamic libs
Linker configuration
Static libs
static libraries are created using ar(1)
we can create library using ar crv
Exercise
create some objects
create library using ar(1)
ar crl mylib.a *.o
ranlib mylib.a
Create source that use your objects
Link with library with -L<path> and -lmylib.a
use nm(1) for names
dylib
use libtool -dynamic -o <output> *.o
default extension on the mac is .dylib
default on linux is so
default on windows is .dll
reading names from library
We can read the names exported by
library using nm(1)
We can also see what dynamic libraries
are used by using nm -mg
Try running nm on libc and on iTunes
Striping binaries
we can remove symbols using strip(1)
command
This will prevent debugging
We can also save striped info to file
More info on object files
Almost everything you may want is done
using nm(1), strip(1), libtool(1), otool(1)
and ar(1).
You can spy on any system call being
used using strace(1) - stack trace.
Examine the man page for more
information
Dynamic libs
Linked at compile time and used at
runtime.
Loaded on the project startup
We can also load a lib completely
dynamically using dlopen (same as
loadLibrary)
dlopen(2)
NAME dlopen -- load and link a dynamic
library or bundleSYNOPSIS #include
<dlfcn.h> void* dlopen(const char* path,
int mode);
dlsym(3)
NAME
dlsym -- get address of a symbolSYNOPSI
dlclose(3)
NAME
dlclose -- close a dynamic library or bun
dlerror(2)
NAME
dlerror -- get diagnostic informationSYN
Display all Linked Library
loaded
export DYLD_PRINT_LIBRARIES=1
(or setenv DYLD_PRINT_LIBRARIES 1)
start your app
Start iTunes
(/Applications/iTunes.app/Content/Mac
OS/iTunes
Preferring static linking
When give the choice (using -L and -l
flag) the linker will prefer DYNAMIC
linking
If you want to force linking with static
lib give the library name directly to the
linker (give complete path to .a file just
like another .o file)
What code runs before main?
What code runs before main()
constructors to static/global objects.
(PTHREAD_MUTEX_INITIALIZER)
dll_main()/XXX_init.
Download