kmalloc, kfree, etc => malloc, free, etc
[ust.git] / libust / marker-control.c
CommitLineData
ba6459ba
PMF
1/*
2 * Copyright (C) 2007 Mathieu Desnoyers
3 *
34e4b7db
PMF
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.
ba6459ba 8 *
34e4b7db 9 * This library is distributed in the hope that it will be useful,
ba6459ba 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34e4b7db
PMF
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
ba6459ba 13 *
34e4b7db
PMF
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
ba6459ba
PMF
17 *
18 * LTT marker control module over /proc
19 */
20
772030fe 21#include <ctype.h>
909bc43f 22#include <stdlib.h>
772030fe 23
fbca6b62 24#include <ust/kernelcompat.h>
c93858f1 25#include "tracer.h"
6af64c43 26#include "usterr.h"
ba6459ba
PMF
27
28#define DEFAULT_CHANNEL "cpu"
29#define DEFAULT_PROBE "default"
30
31LIST_HEAD(probes_list);
32
33/*
34 * Mutex protecting the probe slab cache.
35 * Nests inside the traces mutex.
36 */
37DEFINE_MUTEX(probes_mutex);
38
39struct ltt_available_probe default_probe = {
40 .name = "default",
41 .format = NULL,
42 .probe_func = ltt_vtrace,
43 .callbacks[0] = ltt_serialize_data,
44};
45
46//ust//static struct kmem_cache *markers_loaded_cachep;
47static LIST_HEAD(markers_loaded_list);
48/*
49 * List sorted by name strcmp order.
50 */
51static LIST_HEAD(probes_registered_list);
52
53//ust// static struct proc_dir_entry *pentry;
54
55//ust// static struct file_operations ltt_fops;
56
57static struct ltt_available_probe *get_probe_from_name(const char *pname)
58{
59 struct ltt_available_probe *iter;
60 int comparison, found = 0;
61
62 if (!pname)
63 pname = DEFAULT_PROBE;
64 list_for_each_entry(iter, &probes_registered_list, node) {
65 comparison = strcmp(pname, iter->name);
66 if (!comparison)
67 found = 1;
68 if (comparison <= 0)
69 break;
70 }
71 if (found)
72 return iter;
73 else
74 return NULL;
75}
76
772030fe 77/* (unused)
ba6459ba
PMF
78static char *skip_spaces(char *buf)
79{
80 while (*buf != '\0' && isspace(*buf))
81 buf++;
82 return buf;
83}
84
85static char *skip_nonspaces(char *buf)
86{
87 while (*buf != '\0' && !isspace(*buf))
88 buf++;
89 return buf;
90}
91
92static void get_marker_string(char *buf, char **start,
93 char **end)
94{
95 *start = skip_spaces(buf);
96 *end = skip_nonspaces(*start);
97 **end = '\0';
98}
772030fe 99*/
ba6459ba
PMF
100
101int ltt_probe_register(struct ltt_available_probe *pdata)
102{
103 int ret = 0;
104 int comparison;
105 struct ltt_available_probe *iter;
106
107 mutex_lock(&probes_mutex);
108 list_for_each_entry_reverse(iter, &probes_registered_list, node) {
109 comparison = strcmp(pdata->name, iter->name);
110 if (!comparison) {
111 ret = -EBUSY;
112 goto end;
113 } else if (comparison > 0) {
114 /* We belong to the location right after iter. */
115 list_add(&pdata->node, &iter->node);
116 goto end;
117 }
118 }
119 /* Should be added at the head of the list */
120 list_add(&pdata->node, &probes_registered_list);
121end:
122 mutex_unlock(&probes_mutex);
123 return ret;
124}
ba6459ba
PMF
125
126/*
127 * Called when a probe does not want to be called anymore.
128 */
129int ltt_probe_unregister(struct ltt_available_probe *pdata)
130{
131 int ret = 0;
132 struct ltt_active_marker *amark, *tmp;
133
134 mutex_lock(&probes_mutex);
135 list_for_each_entry_safe(amark, tmp, &markers_loaded_list, node) {
136 if (amark->probe == pdata) {
137 ret = marker_probe_unregister_private_data(
138 pdata->probe_func, amark);
139 if (ret)
140 goto end;
141 list_del(&amark->node);
142 free(amark);
143 }
144 }
145 list_del(&pdata->node);
146end:
147 mutex_unlock(&probes_mutex);
148 return ret;
149}
ba6459ba
PMF
150
151/*
152 * Connect marker "mname" to probe "pname".
153 * Only allow _only_ probe instance to be connected to a marker.
154 */
155int ltt_marker_connect(const char *channel, const char *mname,
156 const char *pname)
157
158{
159 int ret;
160 struct ltt_active_marker *pdata;
161 struct ltt_available_probe *probe;
162
163 ltt_lock_traces();
164 mutex_lock(&probes_mutex);
165 probe = get_probe_from_name(pname);
166 if (!probe) {
167 ret = -ENOENT;
168 goto end;
169 }
170 pdata = marker_get_private_data(channel, mname, probe->probe_func, 0);
171 if (pdata && !IS_ERR(pdata)) {
172 ret = -EEXIST;
173 goto end;
174 }
175 pdata = zmalloc(sizeof(struct ltt_active_marker));
176 if (!pdata) {
177 ret = -ENOMEM;
178 goto end;
179 }
180 pdata->probe = probe;
181 /*
182 * ID has priority over channel in case of conflict.
183 */
184 ret = marker_probe_register(channel, mname, NULL,
185 probe->probe_func, pdata);
186 if (ret)
187 free(pdata);
188 else
189 list_add(&pdata->node, &markers_loaded_list);
190end:
191 mutex_unlock(&probes_mutex);
192 ltt_unlock_traces();
193 return ret;
194}
ba6459ba
PMF
195
196/*
197 * Disconnect marker "mname", probe "pname".
198 */
199int ltt_marker_disconnect(const char *channel, const char *mname,
200 const char *pname)
201{
202 struct ltt_active_marker *pdata;
203 struct ltt_available_probe *probe;
204 int ret = 0;
205
206 mutex_lock(&probes_mutex);
207 probe = get_probe_from_name(pname);
208 if (!probe) {
209 ret = -ENOENT;
210 goto end;
211 }
212 pdata = marker_get_private_data(channel, mname, probe->probe_func, 0);
213 if (IS_ERR(pdata)) {
214 ret = PTR_ERR(pdata);
215 goto end;
216 } else if (!pdata) {
217 /*
218 * Not registered by us.
219 */
220 ret = -EPERM;
221 goto end;
222 }
223 ret = marker_probe_unregister(channel, mname, probe->probe_func, pdata);
224 if (ret)
225 goto end;
226 else {
227 list_del(&pdata->node);
228 free(pdata);
229 }
230end:
231 mutex_unlock(&probes_mutex);
232 return ret;
233}
ba6459ba
PMF
234
235/*
236 * function handling proc entry write.
237 *
238 * connect <channel name> <marker name> [<probe name>]]
239 * disconnect <channel name> <marker name> [<probe name>]
240 */
241//ust// static ssize_t ltt_write(struct file *file, const char __user *buffer,
242//ust// size_t count, loff_t *offset)
243//ust// {
244//ust// char *kbuf;
245//ust// char *iter, *marker_action, *arg[4];
246//ust// ssize_t ret;
247//ust// int i;
248//ust//
249//ust// if (!count)
250//ust// return -EINVAL;
251//ust//
252//ust// kbuf = vmalloc(count + 1);
253//ust// kbuf[count] = '\0'; /* Transform into a string */
254//ust// ret = copy_from_user(kbuf, buffer, count);
255//ust// if (ret) {
256//ust// ret = -EINVAL;
257//ust// goto end;
258//ust// }
259//ust// get_marker_string(kbuf, &marker_action, &iter);
260//ust// if (!marker_action || marker_action == iter) {
261//ust// ret = -EINVAL;
262//ust// goto end;
263//ust// }
264//ust// for (i = 0; i < 4; i++) {
265//ust// arg[i] = NULL;
266//ust// if (iter < kbuf + count) {
267//ust// iter++; /* skip the added '\0' */
268//ust// get_marker_string(iter, &arg[i], &iter);
269//ust// if (arg[i] == iter)
270//ust// arg[i] = NULL;
271//ust// }
272//ust// }
273//ust//
274//ust// if (!arg[0] || !arg[1]) {
275//ust// ret = -EINVAL;
276//ust// goto end;
277//ust// }
278//ust//
279//ust// if (!strcmp(marker_action, "connect")) {
280//ust// ret = ltt_marker_connect(arg[0], arg[1], arg[2]);
281//ust// if (ret)
282//ust// goto end;
283//ust// } else if (!strcmp(marker_action, "disconnect")) {
284//ust// ret = ltt_marker_disconnect(arg[0], arg[1], arg[2]);
285//ust// if (ret)
286//ust// goto end;
287//ust// }
288//ust// ret = count;
289//ust// end:
290//ust// vfree(kbuf);
291//ust// return ret;
292//ust// }
293//ust//
294//ust// static void *s_next(struct seq_file *m, void *p, loff_t *pos)
295//ust// {
296//ust// struct marker_iter *iter = m->private;
297//ust//
298//ust// marker_iter_next(iter);
299//ust// if (!iter->marker) {
300//ust// /*
301//ust// * Setting the iter module to -1UL will make sure
302//ust// * that no module can possibly hold the current marker.
303//ust// */
304//ust// iter->module = (void *)-1UL;
305//ust// return NULL;
306//ust// }
307//ust// return iter->marker;
308//ust// }
309//ust//
310//ust// static void *s_start(struct seq_file *m, loff_t *pos)
311//ust// {
312//ust// struct marker_iter *iter = m->private;
313//ust//
314//ust// if (!*pos)
315//ust// marker_iter_reset(iter);
316//ust// marker_iter_start(iter);
317//ust// if (!iter->marker) {
318//ust// /*
319//ust// * Setting the iter module to -1UL will make sure
320//ust// * that no module can possibly hold the current marker.
321//ust// */
322//ust// iter->module = (void *)-1UL;
323//ust// return NULL;
324//ust// }
325//ust// return iter->marker;
326//ust// }
327//ust//
328//ust// static void s_stop(struct seq_file *m, void *p)
329//ust// {
330//ust// marker_iter_stop(m->private);
331//ust// }
332//ust//
333//ust// static int s_show(struct seq_file *m, void *p)
334//ust// {
335//ust// struct marker_iter *iter = m->private;
336//ust//
337//ust// seq_printf(m, "channel: %s marker: %s format: \"%s\" state: %d "
338//ust// "event_id: %hu call: 0x%p probe %s : 0x%p\n",
339//ust// iter->marker->channel,
340//ust// iter->marker->name, iter->marker->format,
341//ust// _imv_read(iter->marker->state),
342//ust// iter->marker->event_id,
343//ust// iter->marker->call,
344//ust// iter->marker->ptype ? "multi" : "single",
345//ust// iter->marker->ptype ?
346//ust// (void*)iter->marker->multi : (void*)iter->marker->single.func);
347//ust// return 0;
348//ust// }
349//ust//
350//ust// static const struct seq_operations ltt_seq_op = {
351//ust// .start = s_start,
352//ust// .next = s_next,
353//ust// .stop = s_stop,
354//ust// .show = s_show,
355//ust// };
356//ust//
357//ust// static int ltt_open(struct inode *inode, struct file *file)
358//ust// {
359//ust// /*
360//ust// * Iterator kept in m->private.
361//ust// * Restart iteration on all modules between reads because we do not lock
362//ust// * the module mutex between those.
363//ust// */
364//ust// int ret;
365//ust// struct marker_iter *iter;
366//ust//
367//ust// iter = kzalloc(sizeof(*iter), GFP_KERNEL);
368//ust// if (!iter)
369//ust// return -ENOMEM;
370//ust//
371//ust// ret = seq_open(file, &ltt_seq_op);
372//ust// if (ret == 0)
373//ust// ((struct seq_file *)file->private_data)->private = iter;
374//ust// else
375//ust// kfree(iter);
376//ust// return ret;
377//ust// }
378//ust//
379//ust// static struct file_operations ltt_fops = {
380//ust// .write = ltt_write,
381//ust// .open = ltt_open,
382//ust// .read = seq_read,
383//ust// .llseek = seq_lseek,
384//ust// .release = seq_release_private,
385//ust// };
386
387static void disconnect_all_markers(void)
388{
389 struct ltt_active_marker *pdata, *tmp;
390
391 list_for_each_entry_safe(pdata, tmp, &markers_loaded_list, node) {
392 marker_probe_unregister_private_data(pdata->probe->probe_func,
393 pdata);
394 list_del(&pdata->node);
395 free(pdata);
396 }
397}
398
4db647c5
PMF
399static char initialized = 0;
400
401void __attribute__((constructor)) init_marker_control(void)
ba6459ba 402{
4db647c5
PMF
403 if(!initialized) {
404 int ret;
ba6459ba
PMF
405
406//ust// pentry = create_proc_entry("ltt", S_IRUSR|S_IWUSR, NULL);
407//ust// if (!pentry)
408//ust// return -EBUSY;
409//ust// markers_loaded_cachep = KMEM_CACHE(ltt_active_marker, 0);
410
4db647c5
PMF
411 ret = ltt_probe_register(&default_probe);
412 BUG_ON(ret);
413 ret = ltt_marker_connect("metadata", "core_marker_format",
414 DEFAULT_PROBE);
415 BUG_ON(ret);
416 ret = ltt_marker_connect("metadata", "core_marker_id", DEFAULT_PROBE);
417 BUG_ON(ret);
ba6459ba
PMF
418//ust// pentry->proc_fops = &ltt_fops;
419
4db647c5
PMF
420 initialized = 1;
421 }
ba6459ba
PMF
422}
423//ust// module_init(marker_control_init);
424
772030fe 425static void __attribute__((destructor)) marker_control_exit(void)
ba6459ba
PMF
426{
427 int ret;
428
429//ust// remove_proc_entry("ltt", NULL);
430 ret = ltt_marker_disconnect("metadata", "core_marker_format",
431 DEFAULT_PROBE);
432 BUG_ON(ret);
433 ret = ltt_marker_disconnect("metadata", "core_marker_id",
434 DEFAULT_PROBE);
435 BUG_ON(ret);
436 ret = ltt_probe_unregister(&default_probe);
437 BUG_ON(ret);
438 disconnect_all_markers();
439//ust// kmem_cache_destroy(markers_loaded_cachep);
440//ust// marker_synchronize_unregister();
441}
442//ust// module_exit(marker_control_exit);
443
444//ust// MODULE_LICENSE("GPL");
445//ust// MODULE_AUTHOR("Mathieu Desnoyers");
446//ust// MODULE_DESCRIPTION("Linux Trace Toolkit Marker Control");
This page took 0.044871 seconds and 4 git commands to generate.