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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
#ifndef _STP_PROCFS_PROBES_C_
#define _STP_PROCFS_PROBES_C_
#include <linux/mutex.h>
#include <linux/fs.h>
#if 0
// Currently we have to output _stp_procfs_data early in the
// translation process. It really should go here.
struct _stp_procfs_data {
char *buffer;
size_t bufsize;
size_t count;
};
#endif
struct stap_procfs_probe {
const char *path;
const char *read_pp;
void (*read_ph) (struct context*);
const char *write_pp;
void (*write_ph) (struct context*);
char *buffer;
const size_t bufsize;
size_t count;
int needs_fill;
struct mutex lock;
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,16)
atomic_t lockcount;
#endif
};
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,16)
/*
* Kernels 2.6.16 or less don't really have mutexes. The 'mutex_*'
* functions are defined as their similar semaphore equivalents.
* However, there is no semaphore equivalent of 'mutex_is_locked'.
* So, we'll fake it with an atomic counter.
*/
static inline void _spp_lock_init(struct stap_procfs_probe *spp)
{
atomic_set(&spp->lockcount, 0);
mutex_init(&spp->lock);
}
static inline int _spp_trylock(struct stap_procfs_probe *spp)
{
int ret = mutex_trylock(&spp->lock);
if (ret) {
atomic_inc(&spp->lockcount);
}
return(ret);
}
static inline void _spp_lock(struct stap_procfs_probe *spp)
{
mutex_lock(&spp->lock);
atomic_inc(&spp->lockcount);
}
static inline void _spp_unlock(struct stap_procfs_probe *spp)
{
atomic_dec(&spp->lockcount);
mutex_unlock(&spp->lock);
}
static inline void _spp_lock_shutdown(struct stap_procfs_probe *spp)
{
if (atomic_read(&spp->lockcount) != 0) {
_spp_unlock(spp);
}
mutex_destroy(&spp->lock);
}
#else /* LINUX_VERSION_CODE > KERNEL_VERSION(2,6,16) */
#define _spp_lock_init(spp) mutex_init(&(spp)->lock)
#define _spp_trylock(spp) mutex_trylock(&(spp)->lock)
#define _spp_lock(spp) mutex_lock(&(spp)->lock)
#define _spp_unlock(spp) mutex_unlock(&(spp)->lock)
static inline void _spp_lock_shutdown(struct stap_procfs_probe *spp)
{
if (mutex_is_locked(&spp->lock)) {
mutex_unlock(&spp->lock);
}
mutex_destroy(&spp->lock);
}
#endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2,6,16) */
static int _stp_proc_fill_read_buffer(struct stap_procfs_probe *spp);
static int _stp_process_write_buffer(struct stap_procfs_probe *spp,
const char __user *buf, size_t count);
static int
_stp_proc_open_file(struct inode *inode, struct file *filp)
{
struct stap_procfs_probe *spp;
int err;
spp = (struct stap_procfs_probe *)PDE(inode)->data;
if (spp == NULL) {
return -EINVAL;
}
err = generic_file_open(inode, filp);
if (err)
return err;
/* To avoid concurrency problems, we only allow 1 open at a
* time. (Grabbing a mutex here doesn't really work. The
* debug kernel can OOPS with "BUG: lock held when returning
* to user space!".)
*
* If open() was called with
* O_NONBLOCK, don't block, just return EAGAIN. */
if (filp->f_flags & O_NONBLOCK) {
if (_spp_trylock(spp) == 0) {
return -EAGAIN;
}
}
else {
_spp_lock(spp);
}
filp->private_data = spp;
if ((filp->f_flags & O_ACCMODE) == O_RDONLY) {
spp->buffer[0] = '\0';
spp->count = 0;
spp->needs_fill = 1;
}
return 0;
}
static int
_stp_proc_release_file(struct inode *inode, struct file *filp)
{
struct stap_procfs_probe *spp;
spp = (struct stap_procfs_probe *)filp->private_data;
if (spp != NULL) {
_spp_unlock(spp);
}
return 0;
}
static ssize_t
_stp_proc_read_file(struct file *file, char __user *buf, size_t count,
loff_t *ppos)
{
struct stap_procfs_probe *spp = file->private_data;
ssize_t retval = 0;
/* If we don't have a probe read function, just return 0 to
* indicate there isn't any data here. */
if (spp == NULL || spp->read_ph == NULL) {
goto out;
}
/* If needed, fill up the buffer.*/
if (spp->needs_fill) {
if ((retval = _stp_proc_fill_read_buffer(spp))) {
goto out;
}
}
/* Return bytes from the buffer. */
retval = simple_read_from_buffer(buf, count, ppos, spp->buffer,
spp->count);
out:
return retval;
}
static ssize_t
_stp_proc_write_file(struct file *file, const char __user *buf, size_t count,
loff_t *ppos)
{
struct stap_procfs_probe *spp = file->private_data;
struct _stp_procfs_data pdata;
ssize_t len;
/* If we don't have a write probe, return EIO. */
if (spp->write_ph == NULL) {
len = -EIO;
goto out;
}
/* Handle the input buffer. */
len = _stp_process_write_buffer(spp, buf, count);
if (len > 0) {
*ppos += len;
}
out:
return len;
}
static struct file_operations _stp_proc_fops = {
.owner = THIS_MODULE,
.open = _stp_proc_open_file,
.read = _stp_proc_read_file,
.write = _stp_proc_write_file,
.llseek = generic_file_llseek,
.release = _stp_proc_release_file
};
#endif /* _STP_PROCFS_PROBES_C_ */
|