#!/bin/bash
#load module helper script
#COPYRIGHT (C) HITACHI,LTD. 2005
#Created by S.Moriya<s-moriya@sdl.hitachi.co.jp>


function usage() {
    echo "Usage : kernel-rec <start/stop> <module parameter(number of buffer entry)>"
    echo "Example1) ./record.sh start 2000"
    echo "Example2) ./record.sh start"
}

function create_IF() {
    local TMP_MAJOR=`grep kstrax /proc/devices`
    local MAJOR="${TMP_MAJOR%% kstrax}"
    local MINOR="0"
    local DEV_NAME="/dev/kstrax"
    mknod $DEV_NAME c $MAJOR $MINOR
    chmod o-rw $DEV_NAME 
}

function load_module() {
    local VERSION=`uname -r`
    local FSYSCALL_TABLE=`grep fsyscall_table /boot/System.map-$VERSION | cut -f 1 -d \ `
    local SYSCALL_TABLE=`grep sys_call_table /boot/System.map-$VERSION | cut -f 1 -d \ `
    local CTR_MOD=`/sbin/lsmod | grep -c ctr_mod`
    local KSTRAX_STUB=`/sbin/lsmod | grep -c kstrax_stub`
    local KSTRAX_BUF=`/sbin/lsmod | grep -c kstrax_buf`
    local KERNEL_GLOBAL_PTR=`grep __gp /boot/System.map-$VERSION | cut -f 1 -d \ `
    
    if [ $CTR_MOD = "0" ]; then
	/sbin/modprobe ctr_mod ks_gp=0x$KERNEL_GLOBAL_PTR
    fi
    
    if [ $KSTRAX_STUB = "0" ]; then
	/sbin/modprobe kstrax_stub s_addr=0x$SYSCALL_TABLE fs_addr=0x$FSYSCALL_TABLE
    fi

    if [ $KSTRAX_BUF = "0" ];then
	if [ $# = "0" ]; then
	    /sbin/modprobe kstrax_buf
	elif [ $# = "1" ]; then
	    /sbin/modprobe kstrax_buf kbuffer_entry=$1
	fi
    fi
    create_IF
}

function unload_module() {
    rm -f /dev/kstrax
    /sbin/rmmod kstrax_buf.ko
}

if [ $# = "0" ]; then
    usage
else
    if [ $1 = "start" ]; then
	load_module $2
    elif [ $1 = "stop" ]; then
	unload_module
    else
	usage
    fi
fi
