Thursday, December 18, 2008

Using procfs for debug information

In a kernel module we can dynamically create entries under /proc . Each entry (file or directory) under /proc is represented in kernel by a struct proc_dir_entry. This structure is dynamically allocated by various functions used to create directory entries. Here are some useful examples :-

To create a directory "home" under /proc

struct proc_dir_entry *home = proc_mkdir("home", NULL);

To create directory "kitchen" under /proc/home

struct proc_dir_entry *kitchen = proc_mkdir("kitchen", home);

Ok that was enough fun creating directories. Directories can only help that much. What we really want is some files which can be read to get some information from kernel. To create files, we have to specify name of the file, it's permissions, it's parent directory, and a function which will generate data to be given to user through that file. To create a file "name" under /proc/home.

struct proc_dir_entry *name = create_proc_read_entry("name", 0444, home, proc_read_name, NULL);

proc_read_name is the function which generates data

static int proc_read_name(char *page, char **start, off_t off, int count, int *eof, void *data)
{
u32 nbytes = 0;

/* Never worry about off or count, or else you will get a headache */
*start = NULL; /* We don't want kernel to use *start */

/* Here we print entire contents of file into page */
nbytes = sprintf(page, "Rama Nivas\n");

/* Signal EOF */
*eof = 1;

/* Size of file */
return nbytes;
}

These files can only handle PAGE_SIZE amount of data (actually more is possible but the effort is not worth it). If you have more than PAGE_SIZE bytes, it's better to split data across files.

Kermit scripting - 2

A kermit script consists of kermit commands that have to be executed. To
execute a kermit script use

$kermit + kscript

Arguments can be passed to script as

$kermit + kscript arg1 arg2

These arguments can be referenced within the script as \%1, \%2 etc.
Here \%1 = arg1 and \%2 = arg2

Conditionals

kermit scripts support conditional execution using "if" and looping
using "while".


if ! equal \%1 "" {
# Some piece of code to be executed when first argument is non-null
}

I have never actually used a "while" but here is one example I found
in a script.


while ! \F_eof(\m(f)) {
fread \m(f) l
out \m(l)\13
in 60 >
}

Wednesday, December 17, 2008

Command to generate emacs TAGS file for a directory tree

find . -name '*.[chSs]' -print | etags -

The find command searches for files matching the specified criteria and prints those files to standard output. The '-' argument asks etags to read list of files from standard input.

The part -name '*.[chSs]' -print asks find to print files with extension c, h, S, or s. The first argument is the root of the directory tree in which to search for files.

Kermit Scripting

In embedded systems development, kermit is commonly used to communicate to boards through serial port. Many of interactive dialog sessions between user and board can be automated using kermit scripts. If kscript is a file containing a kermit script it can be run by the following command "kermit -y kscript"

Here are some very useful kermit scripting commands

Basic interaction



To wait for a particular prompt and then issue a command
input 1000 bash$
lineout ls
Will wait for the prompt "bash$" to appear and then issue the "ls" command to board through serial port
minput <timeout> <s1> <s2> ...
Used to Wait for any of the given string
Ex:
minput 1000 bash$ >

Variables



First, define some variables
define delay 1000
define prompt bash$
define command ls
And use it
input \m(delay) \m(prompt)
lineout \m(command)

And when finally you are finished use
exit 0 "Over and out"

To add timestamped logging to kermit


set session-log timestamped-text
log session kermit-log.txt