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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
/* -*- linux-c -*-
*
* debugfs control channel
* Copyright (C) 2007 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.
*/
#define STP_DEFAULT_BUFFERS 50
static int _stp_current_buffers = STP_DEFAULT_BUFFERS;
static struct list_head _stp_ctl_ready_q;
static struct list_head _stp_sym_ready_q;
static struct list_head _stp_pool_q;
spinlock_t _stp_pool_lock = SPIN_LOCK_UNLOCKED;
spinlock_t _stp_ctl_ready_lock = SPIN_LOCK_UNLOCKED;
spinlock_t _stp_sym_ready_lock = SPIN_LOCK_UNLOCKED;
static ssize_t _stp_sym_write_cmd (struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
static int saved_type = 0;
int type;
if (count < sizeof(int32_t))
return 0;
/* Allow sending of packet type followed by data in the next packet.*/
if (count == sizeof(int32_t)) {
if (get_user(saved_type, (int __user *)buf))
return -EFAULT;
return count;
} else if (saved_type) {
type = saved_type;
saved_type = 0;
} else {
if (get_user(type, (int __user *)buf))
return -EFAULT;
count -= sizeof(int);
buf += sizeof(int);
}
kbug ("count:%d type:%d\n", (int)count, type);
switch (type) {
case STP_SYMBOLS:
count = _stp_do_symbols(buf, count);
break;
case STP_MODULE:
if (count > 1)
count = _stp_do_module(buf, count);
else {
/* count == 1 indicates end of initial modules list */
_stp_ctl_send(STP_TRANSPORT, NULL, 0);
}
break;
case STP_EXIT:
_stp_exit_flag = 1;
break;
default:
errk ("invalid symbol command type %d\n", type);
return -EINVAL;
}
return count;
}
static ssize_t _stp_ctl_write_cmd (struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
int type;
static int started = 0;
if (count < sizeof(int))
return 0;
if (get_user(type, (int __user *)buf))
return -EFAULT;
kbug ("count:%d type:%d\n", (int)count, type);
count -= sizeof(int);
buf += sizeof(int);
switch (type) {
case STP_START:
if (started == 0) {
struct _stp_msg_start st;
if (count < sizeof(st))
return 0;
if (copy_from_user (&st, buf, sizeof(st)))
return -EFAULT;
_stp_handle_start (&st);
started = 1;
}
break;
case STP_EXIT:
_stp_exit_flag = 1;
break;
case STP_BULK:
#ifdef STP_BULKMODE
return count;
#else
return -1;
#endif
case STP_READY:
/* request symbolic information */
_stp_ask_for_symbols();
break;
default:
errk ("invalid command type %d\n", type);
return -EINVAL;
}
return count;
}
#define STP_CTL_BUFFER_SIZE 256
struct _stp_buffer {
struct list_head list;
int len;
int type;
char buf[STP_CTL_BUFFER_SIZE];
};
static DECLARE_WAIT_QUEUE_HEAD(_stp_ctl_wq);
static DECLARE_WAIT_QUEUE_HEAD(_stp_sym_wq);
#ifdef DEBUG
static void _stp_ctl_write_dbug (int type, void *data, int len)
{
char buf[64];
switch (type) {
case STP_START:
printk("_stp_ctl_write: sending STP_START\n");
break;
case STP_EXIT:
printk("_stp_ctl_write: sending STP_EXIT\n");
break;
case STP_OOB_DATA:
snprintf(buf, sizeof(buf), "%s", (char *)data);
printk("_stp_ctl_write: sending %d bytes of STP_OOB_DATA: %s\n", len, buf);
break;
case STP_SYSTEM:
snprintf(buf, sizeof(buf), "%s", (char *)data);
printk("_stp_ctl_write: sending STP_SYSTEM: %s\n", buf);
break;
case STP_TRANSPORT:
printk("_stp_ctl_write: sending STP_TRANSPORT\n");
break;
default:
printk("_stp_ctl_write: ERROR: unknown message type: %d\n", type);
break;
}
}
static void _stp_sym_write_dbug (int type, void *data, int len)
{
switch (type) {
case STP_SYMBOLS:
printk("_stp_sym_write: sending STP_SYMBOLS\n");
break;
case STP_MODULE:
printk("_stp_sym_write: sending STP_MODULE\n");
break;
default:
printk("_stp_sym_write: ERROR: unknown message type: %d\n", type);
break;
}
}
#endif
static int _stp_ctl_write (int type, void *data, unsigned len)
{
struct _stp_buffer *bptr;
unsigned long flags;
unsigned numtrylock;
#ifdef DEBUG
_stp_ctl_write_dbug(type, data, len);
#endif
/* make sure we won't overflow the buffer */
if (unlikely(len > STP_CTL_BUFFER_SIZE))
return 0;
numtrylock = 0;
while (!spin_trylock_irqsave (&_stp_pool_lock, flags) && (++numtrylock < MAXTRYLOCK))
ndelay (TRYLOCKDELAY);
if (unlikely (numtrylock >= MAXTRYLOCK))
return 0;
if (unlikely(list_empty(&_stp_pool_q))) {
spin_unlock_irqrestore(&_stp_pool_lock, flags);
dbug("_stp_pool_q empty\n");
return -1;
}
/* get the next buffer from the pool */
bptr = (struct _stp_buffer *)_stp_pool_q.next;
list_del_init(&bptr->list);
spin_unlock_irqrestore(&_stp_pool_lock, flags);
bptr->type = type;
memcpy(bptr->buf, data, len);
bptr->len = len;
/* put it on the pool of ready buffers */
numtrylock = 0;
while (!spin_trylock_irqsave (&_stp_ctl_ready_lock, flags) && (++numtrylock < MAXTRYLOCK))
ndelay (TRYLOCKDELAY);
if (unlikely (numtrylock >= MAXTRYLOCK))
return 0;
list_add_tail(&bptr->list, &_stp_ctl_ready_q);
spin_unlock_irqrestore(&_stp_ctl_ready_lock, flags);
return len;
}
static int _stp_sym_write (int type, void *data, unsigned len)
{
struct _stp_buffer *bptr;
unsigned long flags;
#ifdef DEBUG
_stp_sym_write_dbug(type, data, len);
#endif
/* make sure we won't overflow the buffer */
if (unlikely(len > STP_CTL_BUFFER_SIZE))
return 0;
spin_lock_irqsave (&_stp_pool_lock, flags);
if (unlikely(list_empty(&_stp_pool_q))) {
spin_unlock_irqrestore(&_stp_pool_lock, flags);
dbug("_stp_pool_q empty\n");
return -1;
}
/* get the next buffer from the pool */
bptr = (struct _stp_buffer *)_stp_pool_q.next;
list_del_init(&bptr->list);
spin_unlock_irqrestore(&_stp_pool_lock, flags);
bptr->type = type;
memcpy(bptr->buf, data, len);
bptr->len = len;
/* put it on the pool of ready buffers */
spin_lock_irqsave (&_stp_sym_ready_lock, flags);
list_add_tail(&bptr->list, &_stp_sym_ready_q);
spin_unlock_irqrestore(&_stp_sym_ready_lock, flags);
/* OK, it's queued. Now signal any waiters. */
wake_up_interruptible(&_stp_sym_wq);
return len;
}
/* send commands with timeout and retry */
static int _stp_ctl_send (int type, void *data, int len)
{
int err, trylimit = 50;
kbug("ctl_send: type=%d len=%d\n", type, len);
if (unlikely(type == STP_SYMBOLS || type == STP_MODULE)) {
while ((err = _stp_sym_write(type, data, len)) < 0 && trylimit--)
msleep (5);
} else {
while ((err = _stp_ctl_write(type, data, len)) < 0 && trylimit--)
msleep (5);
if (err > 0)
wake_up_interruptible(&_stp_ctl_wq);
}
kbug("returning %d\n", err);
return err;
}
static ssize_t
_stp_sym_read_cmd (struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
struct _stp_buffer *bptr;
int len;
unsigned long flags;
/* wait for nonempty ready queue */
spin_lock_irqsave(&_stp_sym_ready_lock, flags);
while (list_empty(&_stp_sym_ready_q)) {
spin_unlock_irqrestore(&_stp_sym_ready_lock, flags);
if (file->f_flags & O_NONBLOCK)
return -EAGAIN;
if (wait_event_interruptible(_stp_sym_wq, !list_empty(&_stp_sym_ready_q)))
return -ERESTARTSYS;
spin_lock_irqsave(&_stp_sym_ready_lock, flags);
}
/* get the next buffer off the ready list */
bptr = (struct _stp_buffer *)_stp_sym_ready_q.next;
list_del_init(&bptr->list);
spin_unlock_irqrestore(&_stp_sym_ready_lock, flags);
/* write it out */
len = bptr->len + 4;
if (len > count || copy_to_user(buf, &bptr->type, len)) {
/* now what? We took it off the queue then failed to send it */
/* we can't put it back on the queue because it will likely be out-of-order */
/* fortunately this should never happen */
/* FIXME need to mark this as a transport failure */
errk("Supplied buffer too small. count:%d len:%d\n", (int)count, len);
return -EFAULT;
}
/* put it on the pool of free buffers */
spin_lock_irqsave(&_stp_pool_lock, flags);
list_add_tail(&bptr->list, &_stp_pool_q);
spin_unlock_irqrestore(&_stp_pool_lock, flags);
return len;
}
static ssize_t
_stp_ctl_read_cmd (struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
struct _stp_buffer *bptr;
int len;
unsigned long flags;
/* wait for nonempty ready queue */
spin_lock_irqsave(&_stp_ctl_ready_lock, flags);
while (list_empty(&_stp_ctl_ready_q)) {
spin_unlock_irqrestore(&_stp_ctl_ready_lock, flags);
if (file->f_flags & O_NONBLOCK)
return -EAGAIN;
if (wait_event_interruptible(_stp_ctl_wq, !list_empty(&_stp_ctl_ready_q)))
return -ERESTARTSYS;
spin_lock_irqsave(&_stp_ctl_ready_lock, flags);
}
/* get the next buffer off the ready list */
bptr = (struct _stp_buffer *)_stp_ctl_ready_q.next;
list_del_init(&bptr->list);
spin_unlock_irqrestore(&_stp_ctl_ready_lock, flags);
/* write it out */
len = bptr->len + 4;
if (len > count || copy_to_user(buf, &bptr->type, len)) {
/* now what? We took it off the queue then failed to send it */
/* we can't put it back on the queue because it will likely be out-of-order */
/* fortunately this should never happen */
/* FIXME need to mark this as a transport failure */
errk("Supplied buffer too small. count:%d len:%d\n", (int)count, len);
return -EFAULT;
}
/* put it on the pool of free buffers */
spin_lock_irqsave(&_stp_pool_lock, flags);
list_add_tail(&bptr->list, &_stp_pool_q);
spin_unlock_irqrestore(&_stp_pool_lock, flags);
return len;
}
static int _stp_sym_opens = 0;
static int _stp_sym_open_cmd (struct inode *inode, struct file *file)
{
/* only allow one reader */
if (_stp_sym_opens)
return -1;
_stp_sym_opens++;
return 0;
}
static int _stp_sym_close_cmd (struct inode *inode, struct file *file)
{
if (_stp_sym_opens)
_stp_sym_opens--;
return 0;
}
static int _stp_ctl_open_cmd (struct inode *inode, struct file *file)
{
if (_stp_attached)
return -1;
_stp_attach();
return 0;
}
static int _stp_ctl_close_cmd (struct inode *inode, struct file *file)
{
if (_stp_attached)
_stp_detach();
return 0;
}
static struct file_operations _stp_ctl_fops_cmd = {
.owner = THIS_MODULE,
.read = _stp_ctl_read_cmd,
.write = _stp_ctl_write_cmd,
.open = _stp_ctl_open_cmd,
.release = _stp_ctl_close_cmd,
};
static struct file_operations _stp_sym_fops_cmd = {
.owner = THIS_MODULE,
.read = _stp_sym_read_cmd,
.write = _stp_sym_write_cmd,
.open = _stp_sym_open_cmd,
.release = _stp_sym_close_cmd,
};
static struct dentry *_stp_cmd_file = NULL;
static struct dentry *_stp_sym_file = NULL;
static int _stp_register_ctl_channel (void)
{
int i;
struct list_head *p, *tmp;
char buf[32];
if (_stp_utt == NULL) {
errk("_expected _stp_utt to be set.\n");
return -1;
}
INIT_LIST_HEAD(&_stp_ctl_ready_q);
INIT_LIST_HEAD(&_stp_sym_ready_q);
INIT_LIST_HEAD(&_stp_pool_q);
/* allocate buffers */
for (i = 0; i < STP_DEFAULT_BUFFERS; i++) {
p = (struct list_head *)_stp_kmalloc(sizeof(struct _stp_buffer));
// printk("allocated buffer at %lx\n", (long)p);
if (!p)
goto err0;
_stp_allocated_net_memory += sizeof(struct _stp_buffer);
list_add (p, &_stp_pool_q);
}
/* create [debugfs]/systemtap/module_name/.cmd */
_stp_cmd_file = debugfs_create_file(".cmd", 0600, _stp_utt->dir, NULL, &_stp_ctl_fops_cmd);
if (_stp_cmd_file == NULL)
goto err0;
_stp_cmd_file->d_inode->i_uid = _stp_uid;
_stp_cmd_file->d_inode->i_gid = _stp_gid;
/* create [debugfs]/systemtap/module_name/.symbols */
_stp_sym_file = debugfs_create_file(".symbols", 0600, _stp_utt->dir, NULL, &_stp_sym_fops_cmd);
if (_stp_sym_file == NULL)
goto err0;
return 0;
err0:
if (_stp_cmd_file) debugfs_remove(_stp_cmd_file);
list_for_each_safe(p, tmp, &_stp_pool_q) {
list_del(p);
_stp_kfree(p);
}
errk ("Error creating systemtap debugfs entries.\n");
return -1;
}
static void _stp_unregister_ctl_channel (void)
{
struct list_head *p, *tmp;
if (_stp_sym_file) debugfs_remove(_stp_sym_file);
if (_stp_cmd_file) debugfs_remove(_stp_cmd_file);
/* free memory pools */
list_for_each_safe(p, tmp, &_stp_pool_q) {
list_del(p);
_stp_kfree(p);
}
list_for_each_safe(p, tmp, &_stp_sym_ready_q) {
list_del(p);
_stp_kfree(p);
}
list_for_each_safe(p, tmp, &_stp_ctl_ready_q) {
list_del(p);
_stp_kfree(p);
}
}
|