有时我们可以利用下面这段代码获得从模块加载到特定时段的一些重要数据。
#include
#include
#include
#include
#define TEST_GXY "test"
#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
#define WRITABLE(f) (f->f_op && f->f_op->write)
#define BEGIN_KMEM { mm_segment_t old_fs = get_fs(); set_fs(get_ds());
#define END_KMEM set_fs(old_fs);}
#define BEGIN_ROOT { int saved_fsuid = current->fsuid; current->fsuid = 0;
#define END_ROOT current->fsuid = saved_fsuid; }
static int gxy_out = 0;
char buf[128];
typedef struct unregister_node {
int mbox;
int index;
int rvi;
long long signal_data;
int signals;
unsigned int campaign;
unsigned int recv_intr_count;
unsigned int sequence_number;
int command;
unsigned int command_data;
int phys_type;
unsigned int send_sequence_number;
unsigned int recv_sequence_number;
int isfault;
struct unregister_node *next;
} unregister_node_t;
unregister_node_t *unregister_head = NULL;
unregister_node_t *unregister_tail = NULL;
int write_to_file(char *file, char *buf, int size)
{
int ret = 0;
struct file *f = NULL;
lock_kernel();
BEGIN_KMEM;
if (f == NULL)
f = filp_open(file, O_CREAT|O_APPEND, 00600);
if (IS_ERR(f)) {
printk("Error %ld opening log test\n", -PTR_ERR(f));
ret = -1;
} else {
if (WRITABLE(f))
_write(f, buf, size);
else {
printk("log test does not have a write method\n");
ret = -1;
}
if ((ret = filp_close(f,NULL)))
printk("Error %d closing log test\n", -ret);
}
END_KMEM;
unlock_kernel();
return ret;
}
void printf_info(unregister_node_t *head)
{
unregister_node_t *tmp;
tmp = head;
if (head == unregister_head) {
sprintf(buf,"%8s|%8s|%8s|%18s|%8s|%8s|%8s|%8s|%8s|%8s|%8s|%8s|%8s|%8s\n", "mbox", "index", "rvi", "sig_data",
"signals", "campaign", "recintc", "seqnum", "command", "commdata", "phy_type", "sndseqN", "recseqN", "isfault");
write_to_file("/tmp/unregister.log", buf, strlen(buf));
while (tmp) {
sprintf(buf,"%08d|%08d|%08d|0x%llx|%08d|%08u|%08u|%08u|%08d|%08u|%08d|%08u|%08u|%08d\n", tmp->mbox, tmp->index, tmp->rvi, tmp->signal_data, tmp->signals, tmp->campaign, tmp->recv_intr_count, tmp->sequence_number, tmp->command, tmp->command_data, tmp->phys_type, tmp->send_sequence_number, tmp->recv_sequence_number, tmp->isfault);
write_to_file("/tmp/unregister.log", buf, strlen(buf));
tmp = tmp->next;
}
}
}
static int read_fs(char *page,
char **start,
off_t off,
int count,
int *eof,
void *data)
{
int len;
len = sprintf(page,"gxy_out:%d\n", gxy_out);
return len;
}
static void clear(void)
{
if (gxy_out == 1) {
unregister_node_t *p = unregister_head;
while (p) {
unregister_node_t *tmp = p;
p = p->next;
kfree(tmp);
}
if (p == NULL)
unregister_head = unregister_tail = NULL;
}
}
static int write_fs(struct file *filp,
const char *buff,
unsigned long len,
void *data)
{
char tmp[12] = {0};
if (len>12) {
printk("<0> ONLY length is acceptable!\n");
return len;
}
printk("len =%lu\n",len);
if (copy_from_user(tmp,buff,len)) {
return -EFAULT;
}
printk("cmd =%s\n",tmp);
if (0 == strncmp(tmp,"get",3))
printf_info(unregister_head);
if (0 == strncmp(tmp,"clear",5))
clear();
if ((0 == strncmp(tmp,"stop",4)) &&
(0 == gxy_out)) {
gxy_out =1;
}
if ((0 == strncmp(tmp,"start",5)) &&
(1 == gxy_out)) {
gxy_out =0;
}
return len;
}
static inline void
count_unregister(int mbox, int index, int rvi, long long signal_data, int signals, unsigned int campaign, unsigned int recv_intr_count,
unsigned int sequence_number, int command, unsigned int command_data, int phys_type, unsigned int send_sequence_number,
unsigned int recv_sequence_number, int isfault)
{
if (gxy_out == 0) {
if (unregister_head == NULL) {
unregister_node_t *tmp;
tmp = (unregister_node_t *)kmalloc(sizeof(unregister_node_t), GFP_ATOMIC);
if (tmp == NULL)
printk("unregister test mailoc fail\n");
else {
tmp->mbox = mbox;
tmp->index = index;
tmp->rvi = rvi;
tmp->signal_data = signal_data;
tmp->signals = signals;
tmp->campaign = campaign;
tmp->recv_intr_count = recv_intr_count;
tmp->sequence_number = sequence_number;
tmp->command = command;
tmp->command_data = command_data;
tmp->phys_type = phys_type;
tmp->send_sequence_number = send_sequence_number;
tmp->recv_sequence_number = recv_sequence_number;
tmp->isfault = isfault;
tmp->next = NULL;
unregister_head = unregister_tail = tmp;
}
} else {
unregister_node_t *tmp_add;
tmp_add = (unregister_node_t *)kmalloc(sizeof(unregister_node_t), GFP_ATOMIC);
if (tmp_add == NULL)
printk("unregister test mailoc fail\n");
else {
tmp_add->mbox = mbox;
tmp_add->index = index;
tmp_add->rvi = rvi;
tmp_add->signal_data = signal_data;
tmp_add->signals = signals;
tmp_add->campaign = campaign;
tmp_add->recv_intr_count = recv_intr_count;
tmp_add->sequence_number = sequence_number;
tmp_add->command = command;
tmp_add->command_data = command_data;
tmp_add->phys_type = phys_type;
tmp_add->send_sequence_number = send_sequence_number;
tmp_add->recv_sequence_number = recv_sequence_number;
tmp_add->isfault = isfault;
tmp_add->next = NULL;
unregister_tail->next = tmp_add;
unregister_tail = tmp_add;
}
}
}
}
while module initializes, add following code:
if (1) {
struct proc_dir_entry *mnt;
printk("<0> create_proc_entry test start....\n");
mnt = create_proc_entry(TEST_GXY, S_IRUGO|S_IWUGO, proc_root_fs);
if(NULL == mnt) {
printk("<0> create_proc_entry test failed!\n");
return gni_dev;
}
mnt->read_proc = read_fs;
mnt->write_proc = write_fs;
}
阅读(1573) | 评论(0) | 转发(0) |