2 * Copyright (C) 2007 Mathieu Desnoyers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 * LTT marker control module over /proc
21 //ust// #include <linux/proc_fs.h>
22 //ust// #include <linux/module.h>
23 //ust// #include <linux/stat.h>
24 //ust// #include <linux/vmalloc.h>
25 //ust// #include <linux/marker.h>
26 //ust// #include <linux/ltt-tracer.h>
27 //ust// #include <linux/uaccess.h>
28 //ust// #include <linux/string.h>
29 //ust// #include <linux/ctype.h>
30 //ust// #include <linux/list.h>
31 //ust// #include <linux/mutex.h>
32 //ust// #include <linux/seq_file.h>
33 //ust// #include <linux/slab.h>
36 #include <ust/kernelcompat.h>
38 #include <ust/tracer.h>
41 #define DEFAULT_CHANNEL "cpu"
42 #define DEFAULT_PROBE "default"
44 LIST_HEAD(probes_list
);
47 * Mutex protecting the probe slab cache.
48 * Nests inside the traces mutex.
50 DEFINE_MUTEX(probes_mutex
);
52 struct ltt_available_probe default_probe
= {
55 .probe_func
= ltt_vtrace
,
56 .callbacks
[0] = ltt_serialize_data
,
59 //ust//static struct kmem_cache *markers_loaded_cachep;
60 static LIST_HEAD(markers_loaded_list
);
62 * List sorted by name strcmp order.
64 static LIST_HEAD(probes_registered_list
);
66 //ust// static struct proc_dir_entry *pentry;
68 //ust// static struct file_operations ltt_fops;
70 static struct ltt_available_probe
*get_probe_from_name(const char *pname
)
72 struct ltt_available_probe
*iter
;
73 int comparison
, found
= 0;
76 pname
= DEFAULT_PROBE
;
77 list_for_each_entry(iter
, &probes_registered_list
, node
) {
78 comparison
= strcmp(pname
, iter
->name
);
91 static char *skip_spaces(char *buf)
93 while (*buf != '\0' && isspace(*buf))
98 static char *skip_nonspaces(char *buf)
100 while (*buf != '\0' && !isspace(*buf))
105 static void get_marker_string(char *buf, char **start,
108 *start = skip_spaces(buf);
109 *end = skip_nonspaces(*start);
114 int ltt_probe_register(struct ltt_available_probe
*pdata
)
118 struct ltt_available_probe
*iter
;
120 mutex_lock(&probes_mutex
);
121 list_for_each_entry_reverse(iter
, &probes_registered_list
, node
) {
122 comparison
= strcmp(pdata
->name
, iter
->name
);
126 } else if (comparison
> 0) {
127 /* We belong to the location right after iter. */
128 list_add(&pdata
->node
, &iter
->node
);
132 /* Should be added at the head of the list */
133 list_add(&pdata
->node
, &probes_registered_list
);
135 mutex_unlock(&probes_mutex
);
140 * Called when a probe does not want to be called anymore.
142 int ltt_probe_unregister(struct ltt_available_probe
*pdata
)
145 struct ltt_active_marker
*amark
, *tmp
;
147 mutex_lock(&probes_mutex
);
148 list_for_each_entry_safe(amark
, tmp
, &markers_loaded_list
, node
) {
149 if (amark
->probe
== pdata
) {
150 ret
= marker_probe_unregister_private_data(
151 pdata
->probe_func
, amark
);
154 list_del(&amark
->node
);
158 list_del(&pdata
->node
);
160 mutex_unlock(&probes_mutex
);
165 * Connect marker "mname" to probe "pname".
166 * Only allow _only_ probe instance to be connected to a marker.
168 int ltt_marker_connect(const char *channel
, const char *mname
,
173 struct ltt_active_marker
*pdata
;
174 struct ltt_available_probe
*probe
;
177 mutex_lock(&probes_mutex
);
178 probe
= get_probe_from_name(pname
);
183 pdata
= marker_get_private_data(channel
, mname
, probe
->probe_func
, 0);
184 if (pdata
&& !IS_ERR(pdata
)) {
188 pdata
= zmalloc(sizeof(struct ltt_active_marker
));
193 pdata
->probe
= probe
;
195 * ID has priority over channel in case of conflict.
197 ret
= marker_probe_register(channel
, mname
, NULL
,
198 probe
->probe_func
, pdata
);
202 list_add(&pdata
->node
, &markers_loaded_list
);
204 mutex_unlock(&probes_mutex
);
210 * Disconnect marker "mname", probe "pname".
212 int ltt_marker_disconnect(const char *channel
, const char *mname
,
215 struct ltt_active_marker
*pdata
;
216 struct ltt_available_probe
*probe
;
219 mutex_lock(&probes_mutex
);
220 probe
= get_probe_from_name(pname
);
225 pdata
= marker_get_private_data(channel
, mname
, probe
->probe_func
, 0);
227 ret
= PTR_ERR(pdata
);
231 * Not registered by us.
236 ret
= marker_probe_unregister(channel
, mname
, probe
->probe_func
, pdata
);
240 list_del(&pdata
->node
);
244 mutex_unlock(&probes_mutex
);
249 * function handling proc entry write.
251 * connect <channel name> <marker name> [<probe name>]]
252 * disconnect <channel name> <marker name> [<probe name>]
254 //ust// static ssize_t ltt_write(struct file *file, const char __user *buffer,
255 //ust// size_t count, loff_t *offset)
258 //ust// char *iter, *marker_action, *arg[4];
263 //ust// return -EINVAL;
265 //ust// kbuf = vmalloc(count + 1);
266 //ust// kbuf[count] = '\0'; /* Transform into a string */
267 //ust// ret = copy_from_user(kbuf, buffer, count);
269 //ust// ret = -EINVAL;
272 //ust// get_marker_string(kbuf, &marker_action, &iter);
273 //ust// if (!marker_action || marker_action == iter) {
274 //ust// ret = -EINVAL;
277 //ust// for (i = 0; i < 4; i++) {
278 //ust// arg[i] = NULL;
279 //ust// if (iter < kbuf + count) {
280 //ust// iter++; /* skip the added '\0' */
281 //ust// get_marker_string(iter, &arg[i], &iter);
282 //ust// if (arg[i] == iter)
283 //ust// arg[i] = NULL;
287 //ust// if (!arg[0] || !arg[1]) {
288 //ust// ret = -EINVAL;
292 //ust// if (!strcmp(marker_action, "connect")) {
293 //ust// ret = ltt_marker_connect(arg[0], arg[1], arg[2]);
296 //ust// } else if (!strcmp(marker_action, "disconnect")) {
297 //ust// ret = ltt_marker_disconnect(arg[0], arg[1], arg[2]);
307 //ust// static void *s_next(struct seq_file *m, void *p, loff_t *pos)
309 //ust// struct marker_iter *iter = m->private;
311 //ust// marker_iter_next(iter);
312 //ust// if (!iter->marker) {
314 //ust// * Setting the iter module to -1UL will make sure
315 //ust// * that no module can possibly hold the current marker.
317 //ust// iter->module = (void *)-1UL;
320 //ust// return iter->marker;
323 //ust// static void *s_start(struct seq_file *m, loff_t *pos)
325 //ust// struct marker_iter *iter = m->private;
328 //ust// marker_iter_reset(iter);
329 //ust// marker_iter_start(iter);
330 //ust// if (!iter->marker) {
332 //ust// * Setting the iter module to -1UL will make sure
333 //ust// * that no module can possibly hold the current marker.
335 //ust// iter->module = (void *)-1UL;
338 //ust// return iter->marker;
341 //ust// static void s_stop(struct seq_file *m, void *p)
343 //ust// marker_iter_stop(m->private);
346 //ust// static int s_show(struct seq_file *m, void *p)
348 //ust// struct marker_iter *iter = m->private;
350 //ust// seq_printf(m, "channel: %s marker: %s format: \"%s\" state: %d "
351 //ust// "event_id: %hu call: 0x%p probe %s : 0x%p\n",
352 //ust// iter->marker->channel,
353 //ust// iter->marker->name, iter->marker->format,
354 //ust// _imv_read(iter->marker->state),
355 //ust// iter->marker->event_id,
356 //ust// iter->marker->call,
357 //ust// iter->marker->ptype ? "multi" : "single",
358 //ust// iter->marker->ptype ?
359 //ust// (void*)iter->marker->multi : (void*)iter->marker->single.func);
363 //ust// static const struct seq_operations ltt_seq_op = {
364 //ust// .start = s_start,
365 //ust// .next = s_next,
366 //ust// .stop = s_stop,
367 //ust// .show = s_show,
370 //ust// static int ltt_open(struct inode *inode, struct file *file)
373 //ust// * Iterator kept in m->private.
374 //ust// * Restart iteration on all modules between reads because we do not lock
375 //ust// * the module mutex between those.
378 //ust// struct marker_iter *iter;
380 //ust// iter = kzalloc(sizeof(*iter), GFP_KERNEL);
382 //ust// return -ENOMEM;
384 //ust// ret = seq_open(file, <t_seq_op);
385 //ust// if (ret == 0)
386 //ust// ((struct seq_file *)file->private_data)->private = iter;
392 //ust// static struct file_operations ltt_fops = {
393 //ust// .write = ltt_write,
394 //ust// .open = ltt_open,
395 //ust// .read = seq_read,
396 //ust// .llseek = seq_lseek,
397 //ust// .release = seq_release_private,
400 static void disconnect_all_markers(void)
402 struct ltt_active_marker
*pdata
, *tmp
;
404 list_for_each_entry_safe(pdata
, tmp
, &markers_loaded_list
, node
) {
405 marker_probe_unregister_private_data(pdata
->probe
->probe_func
,
407 list_del(&pdata
->node
);
412 static char initialized
= 0;
414 void __attribute__((constructor
)) init_marker_control(void)
419 //ust// pentry = create_proc_entry("ltt", S_IRUSR|S_IWUSR, NULL);
421 //ust// return -EBUSY;
422 //ust// markers_loaded_cachep = KMEM_CACHE(ltt_active_marker, 0);
424 ret
= ltt_probe_register(&default_probe
);
426 ret
= ltt_marker_connect("metadata", "core_marker_format",
429 ret
= ltt_marker_connect("metadata", "core_marker_id", DEFAULT_PROBE
);
431 //ust// pentry->proc_fops = <t_fops;
436 //ust// module_init(marker_control_init);
438 static void __attribute__((destructor
)) marker_control_exit(void)
442 //ust// remove_proc_entry("ltt", NULL);
443 ret
= ltt_marker_disconnect("metadata", "core_marker_format",
446 ret
= ltt_marker_disconnect("metadata", "core_marker_id",
449 ret
= ltt_probe_unregister(&default_probe
);
451 disconnect_all_markers();
452 //ust// kmem_cache_destroy(markers_loaded_cachep);
453 //ust// marker_synchronize_unregister();
455 //ust// module_exit(marker_control_exit);
457 //ust// MODULE_LICENSE("GPL");
458 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
459 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Marker Control");