File: memory1_module.c

package info (click to toggle)
systemtap 5.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 47,964 kB
  • sloc: cpp: 80,838; ansic: 54,757; xml: 49,725; exp: 43,665; sh: 11,527; python: 5,003; perl: 2,252; tcl: 1,312; makefile: 1,006; javascript: 149; lisp: 105; awk: 101; asm: 91; java: 70; sed: 16
file content (96 lines) | stat: -rw-r--r-- 2,310 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* -*- linux-c -*- 
 * Systemtap Memory1 Test Module
 * Copyright (C) 2013 Red Hat Inc.
 *
 * This file is part of systemtap, and is free software.  You can
 * redistribute it and/or modify it under the terms of the GNU General
 * Public License (GPL); either version 2, or (at your option) any
 * later version.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/compiler.h>
#include <linux/sched.h>
#include <linux/uaccess.h>

/* The purpose of this module is to provide a kernel string we can
 * safely read. We use a /proc file to trigger the function call from
 * user context.  Then systemtap scripts set probes on the functions
 * and run tests to see if the expected output is received. This is
 * better than using the kernel because kernel internals frequently
 * change. */

static struct proc_dir_entry *stm_ctl = NULL;
static char buffer[100] = "";

void noinline stp_memory1_set_str(const char *string, size_t bytes)
{
        asm ("");
	memcpy(buffer, string, bytes);
}

char * noinline stp_memory1_get_str(void)
{
        asm ("");
	return buffer;
}

EXPORT_SYMBOL(stp_memory1_set_str);
EXPORT_SYMBOL(stp_memory1_get_str);

static ssize_t stm_write (struct file *file, const char __user *buf,
			  size_t count, loff_t *ppos)
{
	char tmp[100];
	size_t bytes;

	bytes = (count >= sizeof(tmp) - 1) ? sizeof(tmp) - 1 : count;

	if (copy_from_user(tmp, buf, bytes))
		return -EFAULT;

	tmp[bytes] = '\0';
	stp_memory1_set_str(tmp, bytes);
	return bytes;
}

static ssize_t stm_read(struct file *file, char __user *buffer,
			size_t buflen, loff_t *fpos)
{
	char *data = stp_memory1_get_str();
	size_t bytes = strlen(data);

	if (buflen == 0 || *fpos >= bytes)
		return 0;

	bytes = min(bytes - (size_t)*fpos, buflen);
	if (copy_to_user(buffer, data + *fpos, bytes))
		return -EFAULT;
	*fpos += bytes;
	return bytes;
}

static struct file_operations stm_fops_cmd = {
	.owner = THIS_MODULE,
	.write = stm_write,
	.read = stm_read,
};

int init_module(void)
{
	stm_ctl = proc_create ("stap_memory1_test", 0666, NULL, &stm_fops_cmd);
	if (stm_ctl == NULL) 
		return -1;
	return 0;
}

void cleanup_module(void)
{
	if (stm_ctl)
		remove_proc_entry ("stap_memory1_test", NULL);
}

MODULE_DESCRIPTION("systemtap memory1 test module");
MODULE_LICENSE("GPL");