Aprobe 5

3. How do I apply a probe to a Linux kernel mode function named testmod_timer?

The probe: timerprobe.spc

#include <linux/kernel.h>

probe thread
   probe "testmod.c":"testmod_timer()"
         printk(KERN_INFO "SP->testmod_timer\n");
         printk(KERN_INFO "SP<-testmod_timer\n");
   } func_testmod_timer;

} // end probe thread

Main test: testmod.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

/* This is a command-line parameter passed when the module is loaded.. */
unsigned int testmod_timeout;
module_param_named (timeout, testmod_timeout, int, 0);

/* This is the structure used to set the timer. */
static struct timer_list testmod_timer_struct;
static unsigned int count = 0;

#define future_in_ms(n) (jiffies+(testmod_timeout * HZ ) / 1000)

/* This is the timer callback. */
static void testmod_timer (unsigned long dev_addr)
   printk (KERN_INFO "Timeout #%u!\n",count);
   testmod_timer_struct.expires = future_in_ms (testmod_timeout);
   add_timer (&testmod_timer_struct);

/* This creates the programmable timer */
void create_timers (void) 
   printk (KERN_INFO "Setting up timers\n");

   /* Initialize the timer */
   init_timer (&testmod_timer_struct);

   /* Set up the timer callback */
   testmod_timer_struct.function = &testmod_timer;

   /* Set up the timeout */
   testmod_timer_struct.expires = future_in_ms (testmod_timeout);

   /* Add the timer */
   add_timer (&testmod_timer_struct);

/* This frees up and cancels existing timers. */
void destroy_timers (void) 
   printk (KERN_INFO "Destroying timers\n");
   del_timer (&testmod_timer_struct);

/* This is the function called when the module is loaded. */
static int __init testmod_init (void)
   printk (KERN_INFO "Loading testmod\n");
   printk (KERN_INFO "timeout is %u\n", testmod_timeout);
   return 0;

/* This is the function called when the module is unloaded. */
static void testmod_exit (void) 
   printk (KERN_INFO "Unloaded testmod\n");

module_init (testmod_init);
module_exit (testmod_exit);

MODULE_DESCRIPTION("Timer example moddule");

Compile testmod.c to create kernel object code testmod.ko:

build_module testmod

Compile the timerprobe.spc probe to create timerprobe.usm:

spc -C -t linuxkm:x86 -K $KERNEL_BUILD_PATH/linux-2.6.14-cgl timerprobe.spc

Install and Run the probe on target:

Use spkmtool to load and run the timerprobe.usm probe.  Here, our target
machine is named "alfie".

# Load the target timermod kernel object module:
/sbin/insmod timermod.ko

# Install the probe library and probe services modules,
# and load the kernel symbols file that they may need:
spkmtool -v -d alfie.syms

# This turns on immediate formatting:
spkmtool -s -if 1

# Initialize the probe library module:
spkmtool -s init

# Load the probe module, and give it a handy ID (eg, 17):
spkmtool -v -s load timerprobe.usm 17

# Enable the probe module:
spkmtool -v -s enable 17

# Run the instrumented program:

# Disable the probe module:
spkmtool -v -s disable 17

# Unload the probe module:
spkmtool -v -s unload timerprobe.usm

# Finalize the probe library module:
spkmtool -s fini

# Remove the probe library and probe services modules:
spkmtool -v -c

# Unload the timermod kernel object module:
/sbin/rmmod timermod.ko