ust: continue moving communication stuff to libustcomm and cleanup
[ust.git] / libtracectl / marker-control.c
1 /*
2 * Copyright (C) 2007 Mathieu Desnoyers
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * LTT marker control module over /proc
19 */
20
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>
34 #include "kernelcompat.h"
35 #include "list.h"
36 #include "tracer.h"
37 #include "localerr.h"
38
39 #define DEFAULT_CHANNEL "cpu"
40 #define DEFAULT_PROBE "default"
41
42 LIST_HEAD(probes_list);
43
44 /*
45 * Mutex protecting the probe slab cache.
46 * Nests inside the traces mutex.
47 */
48 DEFINE_MUTEX(probes_mutex);
49
50 struct ltt_available_probe default_probe = {
51 .name = "default",
52 .format = NULL,
53 .probe_func = ltt_vtrace,
54 .callbacks[0] = ltt_serialize_data,
55 };
56
57 //ust//static struct kmem_cache *markers_loaded_cachep;
58 static LIST_HEAD(markers_loaded_list);
59 /*
60 * List sorted by name strcmp order.
61 */
62 static LIST_HEAD(probes_registered_list);
63
64 //ust// static struct proc_dir_entry *pentry;
65
66 //ust// static struct file_operations ltt_fops;
67
68 static struct ltt_available_probe *get_probe_from_name(const char *pname)
69 {
70 struct ltt_available_probe *iter;
71 int comparison, found = 0;
72
73 if (!pname)
74 pname = DEFAULT_PROBE;
75 list_for_each_entry(iter, &probes_registered_list, node) {
76 comparison = strcmp(pname, iter->name);
77 if (!comparison)
78 found = 1;
79 if (comparison <= 0)
80 break;
81 }
82 if (found)
83 return iter;
84 else
85 return NULL;
86 }
87
88 static char *skip_spaces(char *buf)
89 {
90 while (*buf != '\0' && isspace(*buf))
91 buf++;
92 return buf;
93 }
94
95 static char *skip_nonspaces(char *buf)
96 {
97 while (*buf != '\0' && !isspace(*buf))
98 buf++;
99 return buf;
100 }
101
102 static void get_marker_string(char *buf, char **start,
103 char **end)
104 {
105 *start = skip_spaces(buf);
106 *end = skip_nonspaces(*start);
107 **end = '\0';
108 }
109
110 int ltt_probe_register(struct ltt_available_probe *pdata)
111 {
112 int ret = 0;
113 int comparison;
114 struct ltt_available_probe *iter;
115
116 mutex_lock(&probes_mutex);
117 list_for_each_entry_reverse(iter, &probes_registered_list, node) {
118 comparison = strcmp(pdata->name, iter->name);
119 if (!comparison) {
120 ret = -EBUSY;
121 goto end;
122 } else if (comparison > 0) {
123 /* We belong to the location right after iter. */
124 list_add(&pdata->node, &iter->node);
125 goto end;
126 }
127 }
128 /* Should be added at the head of the list */
129 list_add(&pdata->node, &probes_registered_list);
130 end:
131 mutex_unlock(&probes_mutex);
132 return ret;
133 }
134 EXPORT_SYMBOL_GPL(ltt_probe_register);
135
136 /*
137 * Called when a probe does not want to be called anymore.
138 */
139 int ltt_probe_unregister(struct ltt_available_probe *pdata)
140 {
141 int ret = 0;
142 struct ltt_active_marker *amark, *tmp;
143
144 mutex_lock(&probes_mutex);
145 list_for_each_entry_safe(amark, tmp, &markers_loaded_list, node) {
146 if (amark->probe == pdata) {
147 ret = marker_probe_unregister_private_data(
148 pdata->probe_func, amark);
149 if (ret)
150 goto end;
151 list_del(&amark->node);
152 free(amark);
153 }
154 }
155 list_del(&pdata->node);
156 end:
157 mutex_unlock(&probes_mutex);
158 return ret;
159 }
160 EXPORT_SYMBOL_GPL(ltt_probe_unregister);
161
162 /*
163 * Connect marker "mname" to probe "pname".
164 * Only allow _only_ probe instance to be connected to a marker.
165 */
166 int ltt_marker_connect(const char *channel, const char *mname,
167 const char *pname)
168
169 {
170 int ret;
171 struct ltt_active_marker *pdata;
172 struct ltt_available_probe *probe;
173
174 ltt_lock_traces();
175 mutex_lock(&probes_mutex);
176 probe = get_probe_from_name(pname);
177 if (!probe) {
178 ret = -ENOENT;
179 goto end;
180 }
181 pdata = marker_get_private_data(channel, mname, probe->probe_func, 0);
182 if (pdata && !IS_ERR(pdata)) {
183 ret = -EEXIST;
184 goto end;
185 }
186 pdata = zmalloc(sizeof(struct ltt_active_marker));
187 if (!pdata) {
188 ret = -ENOMEM;
189 goto end;
190 }
191 pdata->probe = probe;
192 /*
193 * ID has priority over channel in case of conflict.
194 */
195 ret = marker_probe_register(channel, mname, NULL,
196 probe->probe_func, pdata);
197 if (ret)
198 free(pdata);
199 else
200 list_add(&pdata->node, &markers_loaded_list);
201 end:
202 mutex_unlock(&probes_mutex);
203 ltt_unlock_traces();
204 return ret;
205 }
206 EXPORT_SYMBOL_GPL(ltt_marker_connect);
207
208 /*
209 * Disconnect marker "mname", probe "pname".
210 */
211 int ltt_marker_disconnect(const char *channel, const char *mname,
212 const char *pname)
213 {
214 struct ltt_active_marker *pdata;
215 struct ltt_available_probe *probe;
216 int ret = 0;
217
218 mutex_lock(&probes_mutex);
219 probe = get_probe_from_name(pname);
220 if (!probe) {
221 ret = -ENOENT;
222 goto end;
223 }
224 pdata = marker_get_private_data(channel, mname, probe->probe_func, 0);
225 if (IS_ERR(pdata)) {
226 ret = PTR_ERR(pdata);
227 goto end;
228 } else if (!pdata) {
229 /*
230 * Not registered by us.
231 */
232 ret = -EPERM;
233 goto end;
234 }
235 ret = marker_probe_unregister(channel, mname, probe->probe_func, pdata);
236 if (ret)
237 goto end;
238 else {
239 list_del(&pdata->node);
240 free(pdata);
241 }
242 end:
243 mutex_unlock(&probes_mutex);
244 return ret;
245 }
246 EXPORT_SYMBOL_GPL(ltt_marker_disconnect);
247
248 /*
249 * function handling proc entry write.
250 *
251 * connect <channel name> <marker name> [<probe name>]]
252 * disconnect <channel name> <marker name> [<probe name>]
253 */
254 //ust// static ssize_t ltt_write(struct file *file, const char __user *buffer,
255 //ust// size_t count, loff_t *offset)
256 //ust// {
257 //ust// char *kbuf;
258 //ust// char *iter, *marker_action, *arg[4];
259 //ust// ssize_t ret;
260 //ust// int i;
261 //ust//
262 //ust// if (!count)
263 //ust// return -EINVAL;
264 //ust//
265 //ust// kbuf = vmalloc(count + 1);
266 //ust// kbuf[count] = '\0'; /* Transform into a string */
267 //ust// ret = copy_from_user(kbuf, buffer, count);
268 //ust// if (ret) {
269 //ust// ret = -EINVAL;
270 //ust// goto end;
271 //ust// }
272 //ust// get_marker_string(kbuf, &marker_action, &iter);
273 //ust// if (!marker_action || marker_action == iter) {
274 //ust// ret = -EINVAL;
275 //ust// goto end;
276 //ust// }
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;
284 //ust// }
285 //ust// }
286 //ust//
287 //ust// if (!arg[0] || !arg[1]) {
288 //ust// ret = -EINVAL;
289 //ust// goto end;
290 //ust// }
291 //ust//
292 //ust// if (!strcmp(marker_action, "connect")) {
293 //ust// ret = ltt_marker_connect(arg[0], arg[1], arg[2]);
294 //ust// if (ret)
295 //ust// goto end;
296 //ust// } else if (!strcmp(marker_action, "disconnect")) {
297 //ust// ret = ltt_marker_disconnect(arg[0], arg[1], arg[2]);
298 //ust// if (ret)
299 //ust// goto end;
300 //ust// }
301 //ust// ret = count;
302 //ust// end:
303 //ust// vfree(kbuf);
304 //ust// return ret;
305 //ust// }
306 //ust//
307 //ust// static void *s_next(struct seq_file *m, void *p, loff_t *pos)
308 //ust// {
309 //ust// struct marker_iter *iter = m->private;
310 //ust//
311 //ust// marker_iter_next(iter);
312 //ust// if (!iter->marker) {
313 //ust// /*
314 //ust// * Setting the iter module to -1UL will make sure
315 //ust// * that no module can possibly hold the current marker.
316 //ust// */
317 //ust// iter->module = (void *)-1UL;
318 //ust// return NULL;
319 //ust// }
320 //ust// return iter->marker;
321 //ust// }
322 //ust//
323 //ust// static void *s_start(struct seq_file *m, loff_t *pos)
324 //ust// {
325 //ust// struct marker_iter *iter = m->private;
326 //ust//
327 //ust// if (!*pos)
328 //ust// marker_iter_reset(iter);
329 //ust// marker_iter_start(iter);
330 //ust// if (!iter->marker) {
331 //ust// /*
332 //ust// * Setting the iter module to -1UL will make sure
333 //ust// * that no module can possibly hold the current marker.
334 //ust// */
335 //ust// iter->module = (void *)-1UL;
336 //ust// return NULL;
337 //ust// }
338 //ust// return iter->marker;
339 //ust// }
340 //ust//
341 //ust// static void s_stop(struct seq_file *m, void *p)
342 //ust// {
343 //ust// marker_iter_stop(m->private);
344 //ust// }
345 //ust//
346 //ust// static int s_show(struct seq_file *m, void *p)
347 //ust// {
348 //ust// struct marker_iter *iter = m->private;
349 //ust//
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);
360 //ust// return 0;
361 //ust// }
362 //ust//
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,
368 //ust// };
369 //ust//
370 //ust// static int ltt_open(struct inode *inode, struct file *file)
371 //ust// {
372 //ust// /*
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.
376 //ust// */
377 //ust// int ret;
378 //ust// struct marker_iter *iter;
379 //ust//
380 //ust// iter = kzalloc(sizeof(*iter), GFP_KERNEL);
381 //ust// if (!iter)
382 //ust// return -ENOMEM;
383 //ust//
384 //ust// ret = seq_open(file, &ltt_seq_op);
385 //ust// if (ret == 0)
386 //ust// ((struct seq_file *)file->private_data)->private = iter;
387 //ust// else
388 //ust// kfree(iter);
389 //ust// return ret;
390 //ust// }
391 //ust//
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,
398 //ust// };
399
400 static void disconnect_all_markers(void)
401 {
402 struct ltt_active_marker *pdata, *tmp;
403
404 list_for_each_entry_safe(pdata, tmp, &markers_loaded_list, node) {
405 marker_probe_unregister_private_data(pdata->probe->probe_func,
406 pdata);
407 list_del(&pdata->node);
408 free(pdata);
409 }
410 }
411
412 static char initialized = 0;
413
414 void __attribute__((constructor)) init_marker_control(void)
415 {
416 if(!initialized) {
417 int ret;
418
419 //ust// pentry = create_proc_entry("ltt", S_IRUSR|S_IWUSR, NULL);
420 //ust// if (!pentry)
421 //ust// return -EBUSY;
422 //ust// markers_loaded_cachep = KMEM_CACHE(ltt_active_marker, 0);
423
424 ret = ltt_probe_register(&default_probe);
425 BUG_ON(ret);
426 ret = ltt_marker_connect("metadata", "core_marker_format",
427 DEFAULT_PROBE);
428 BUG_ON(ret);
429 ret = ltt_marker_connect("metadata", "core_marker_id", DEFAULT_PROBE);
430 BUG_ON(ret);
431 //ust// pentry->proc_fops = &ltt_fops;
432
433 initialized = 1;
434 }
435 }
436 //ust// module_init(marker_control_init);
437
438 static void __exit marker_control_exit(void)
439 {
440 int ret;
441
442 //ust// remove_proc_entry("ltt", NULL);
443 ret = ltt_marker_disconnect("metadata", "core_marker_format",
444 DEFAULT_PROBE);
445 BUG_ON(ret);
446 ret = ltt_marker_disconnect("metadata", "core_marker_id",
447 DEFAULT_PROBE);
448 BUG_ON(ret);
449 ret = ltt_probe_unregister(&default_probe);
450 BUG_ON(ret);
451 disconnect_all_markers();
452 //ust// kmem_cache_destroy(markers_loaded_cachep);
453 //ust// marker_synchronize_unregister();
454 }
455 //ust// module_exit(marker_control_exit);
456
457 //ust// MODULE_LICENSE("GPL");
458 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
459 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Marker Control");
This page took 0.038262 seconds and 4 git commands to generate.