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