Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 10: Filesystems without Persistent Storage


kernel/kprobes.c
static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
{
/* Nothing to do */
}

Thenextfunction is called when the cursor must be updated to the next position. Besides incrementing
the array index, the function must check that it does not go out of bounds:


kernel/kprobes.c
static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
{
(*pos)++;
if (*pos >= KPROBE_TABLE_SIZE)
return NULL;
return pos;
}

ANULLpointer indicates that the end of the file is reached.


The most interesting function isshowsince the actual contents of the sequential file are generated here.
For the sake of illustration, I present a slightly simplified version that abstracts some of the difficulties
associated with kprobes that would detract from theseq_fileissues:


kernel/kprobes.c
static int show_kprobe_addr(struct seq_file *pi, void *v)
{
struct hlist_head *head;
struct hlist_node *node;
struct kprobe *p;
const char *sym = NULL;
unsigned int i = *(loff_t *) v;
unsigned long offset = 0;
char *modname, namebuf[128];

head = &kprobe_table[i];

hlist_for_each_entry_rcu(p, node, head, hlist) {
sym = kallsyms_lookup((unsigned long)p->addr, NULL,
&offset, &modname, namebuf);
if (sym)
seq_printf(pi, "%p %s+0x%x %s\n", p->addr,
sym, offset, (modname? modname : " "));
else
seq_printf(pi, "%p\n", p->addr);
}
return 0;
}

The current value of the file cursor is in the argumentv, and the function converts it into the array index
i. Data generation is done by iterating over all elements hashed on this array index. An output line is
constructed for each element. Information about the probe point and the symbol that is possibly associ-
ated with the point is generated, but this is not really relevant for the example. What does matter is that

Free download pdf