Tracepoint and TRACEPOINT_EVENT API cleanup
[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 17 *
93e5ce29
PMF
18 */
19
b521931e
MD
20/* This file contains a high-level API for activating and deactivating ust_markers,
21 * and making sure ust_markers in a given library can be released when the library
93e5ce29 22 * is unloaded.
ba6459ba
PMF
23 */
24
772030fe 25#include <ctype.h>
909bc43f 26#include <stdlib.h>
772030fe 27
c93858f1 28#include "tracer.h"
30ffe279 29#include "usterr_signal_safe.h"
ba6459ba
PMF
30
31#define DEFAULT_CHANNEL "cpu"
32#define DEFAULT_PROBE "default"
33
0222e121 34CDS_LIST_HEAD(probes_list);
ba6459ba
PMF
35
36/*
37 * Mutex protecting the probe slab cache.
38 * Nests inside the traces mutex.
39 */
40DEFINE_MUTEX(probes_mutex);
41
42struct ltt_available_probe default_probe = {
43 .name = "default",
44 .format = NULL,
45 .probe_func = ltt_vtrace,
46 .callbacks[0] = ltt_serialize_data,
47};
48
b521931e
MD
49//ust//static struct kmem_cache *ust_markers_loaded_cachep;
50static CDS_LIST_HEAD(ust_markers_loaded_list);
ba6459ba
PMF
51/*
52 * List sorted by name strcmp order.
53 */
0222e121 54static CDS_LIST_HEAD(probes_registered_list);
ba6459ba
PMF
55
56//ust// static struct proc_dir_entry *pentry;
57
58//ust// static struct file_operations ltt_fops;
59
60static 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;
0222e121 67 cds_list_for_each_entry(iter, &probes_registered_list, node) {
ba6459ba
PMF
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
772030fe 80/* (unused)
ba6459ba
PMF
81static char *skip_spaces(char *buf)
82{
83 while (*buf != '\0' && isspace(*buf))
84 buf++;
85 return buf;
86}
87
88static char *skip_nonspaces(char *buf)
89{
90 while (*buf != '\0' && !isspace(*buf))
91 buf++;
92 return buf;
93}
94
b521931e 95static void get_ust_marker_string(char *buf, char **start,
ba6459ba
PMF
96 char **end)
97{
98 *start = skip_spaces(buf);
99 *end = skip_nonspaces(*start);
100 **end = '\0';
101}
772030fe 102*/
ba6459ba
PMF
103
104int ltt_probe_register(struct ltt_available_probe *pdata)
105{
106 int ret = 0;
107 int comparison;
108 struct ltt_available_probe *iter;
109
f7b16408 110 pthread_mutex_lock(&probes_mutex);
0222e121 111 cds_list_for_each_entry_reverse(iter, &probes_registered_list, node) {
ba6459ba
PMF
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. */
0222e121 118 cds_list_add(&pdata->node, &iter->node);
ba6459ba
PMF
119 goto end;
120 }
121 }
122 /* Should be added at the head of the list */
0222e121 123 cds_list_add(&pdata->node, &probes_registered_list);
ba6459ba 124end:
f7b16408 125 pthread_mutex_unlock(&probes_mutex);
ba6459ba
PMF
126 return ret;
127}
ba6459ba
PMF
128
129/*
130 * Called when a probe does not want to be called anymore.
131 */
132int ltt_probe_unregister(struct ltt_available_probe *pdata)
133{
134 int ret = 0;
b521931e 135 struct ltt_active_ust_marker *amark, *tmp;
ba6459ba 136
f7b16408 137 pthread_mutex_lock(&probes_mutex);
b521931e 138 cds_list_for_each_entry_safe(amark, tmp, &ust_markers_loaded_list, node) {
ba6459ba 139 if (amark->probe == pdata) {
b521931e 140 ret = ust_marker_probe_unregister_private_data(
ba6459ba
PMF
141 pdata->probe_func, amark);
142 if (ret)
143 goto end;
0222e121 144 cds_list_del(&amark->node);
ba6459ba
PMF
145 free(amark);
146 }
147 }
0222e121 148 cds_list_del(&pdata->node);
ba6459ba 149end:
f7b16408 150 pthread_mutex_unlock(&probes_mutex);
ba6459ba
PMF
151 return ret;
152}
ba6459ba
PMF
153
154/*
b521931e
MD
155 * Connect ust_marker "mname" to probe "pname".
156 * Only allow _only_ probe instance to be connected to a ust_marker.
ba6459ba 157 */
b521931e 158int ltt_ust_marker_connect(const char *channel, const char *mname,
ba6459ba
PMF
159 const char *pname)
160
161{
162 int ret;
b521931e 163 struct ltt_active_ust_marker *pdata;
ba6459ba
PMF
164 struct ltt_available_probe *probe;
165
166 ltt_lock_traces();
f7b16408 167 pthread_mutex_lock(&probes_mutex);
ba6459ba
PMF
168 probe = get_probe_from_name(pname);
169 if (!probe) {
170 ret = -ENOENT;
171 goto end;
172 }
b521931e 173 pdata = ust_marker_get_private_data(channel, mname, probe->probe_func, 0);
ba6459ba
PMF
174 if (pdata && !IS_ERR(pdata)) {
175 ret = -EEXIST;
176 goto end;
177 }
b521931e 178 pdata = zmalloc(sizeof(struct ltt_active_ust_marker));
ba6459ba
PMF
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 */
b521931e 187 ret = ust_marker_probe_register(channel, mname, NULL,
ba6459ba
PMF
188 probe->probe_func, pdata);
189 if (ret)
190 free(pdata);
191 else
b521931e 192 cds_list_add(&pdata->node, &ust_markers_loaded_list);
ba6459ba 193end:
f7b16408 194 pthread_mutex_unlock(&probes_mutex);
ba6459ba
PMF
195 ltt_unlock_traces();
196 return ret;
197}
ba6459ba
PMF
198
199/*
b521931e 200 * Disconnect ust_marker "mname", probe "pname".
ba6459ba 201 */
b521931e 202int ltt_ust_marker_disconnect(const char *channel, const char *mname,
ba6459ba
PMF
203 const char *pname)
204{
b521931e 205 struct ltt_active_ust_marker *pdata;
ba6459ba
PMF
206 struct ltt_available_probe *probe;
207 int ret = 0;
208
f7b16408 209 pthread_mutex_lock(&probes_mutex);
ba6459ba
PMF
210 probe = get_probe_from_name(pname);
211 if (!probe) {
212 ret = -ENOENT;
213 goto end;
214 }
b521931e 215 pdata = ust_marker_get_private_data(channel, mname, probe->probe_func, 0);
ba6459ba
PMF
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 }
b521931e 226 ret = ust_marker_probe_unregister(channel, mname, probe->probe_func, pdata);
ba6459ba
PMF
227 if (ret)
228 goto end;
229 else {
0222e121 230 cds_list_del(&pdata->node);
ba6459ba
PMF
231 free(pdata);
232 }
233end:
f7b16408 234 pthread_mutex_unlock(&probes_mutex);
ba6459ba
PMF
235 return ret;
236}
ba6459ba
PMF
237
238/*
239 * function handling proc entry write.
240 *
b521931e
MD
241 * connect <channel name> <ust_marker name> [<probe name>]]
242 * disconnect <channel name> <ust_marker name> [<probe name>]
ba6459ba
PMF
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;
b521931e 248//ust// char *iter, *ust_marker_action, *arg[4];
ba6459ba
PMF
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// }
b521931e
MD
262//ust// get_ust_marker_string(kbuf, &ust_marker_action, &iter);
263//ust// if (!ust_marker_action || ust_marker_action == iter) {
ba6459ba
PMF
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' */
b521931e 271//ust// get_ust_marker_string(iter, &arg[i], &iter);
ba6459ba
PMF
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//
b521931e
MD
282//ust// if (!strcmp(ust_marker_action, "connect")) {
283//ust// ret = ltt_ust_marker_connect(arg[0], arg[1], arg[2]);
ba6459ba
PMF
284//ust// if (ret)
285//ust// goto end;
b521931e
MD
286//ust// } else if (!strcmp(ust_marker_action, "disconnect")) {
287//ust// ret = ltt_ust_marker_disconnect(arg[0], arg[1], arg[2]);
ba6459ba
PMF
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// {
b521931e 299//ust// struct ust_marker_iter *iter = m->private;
ba6459ba 300//ust//
b521931e
MD
301//ust// ust_marker_iter_next(iter);
302//ust// if (!iter->ust_marker) {
ba6459ba
PMF
303//ust// /*
304//ust// * Setting the iter module to -1UL will make sure
b521931e 305//ust// * that no module can possibly hold the current ust_marker.
ba6459ba
PMF
306//ust// */
307//ust// iter->module = (void *)-1UL;
308//ust// return NULL;
309//ust// }
b521931e 310//ust// return iter->ust_marker;
ba6459ba
PMF
311//ust// }
312//ust//
313//ust// static void *s_start(struct seq_file *m, loff_t *pos)
314//ust// {
b521931e 315//ust// struct ust_marker_iter *iter = m->private;
ba6459ba
PMF
316//ust//
317//ust// if (!*pos)
b521931e
MD
318//ust// ust_marker_iter_reset(iter);
319//ust// ust_marker_iter_start(iter);
320//ust// if (!iter->ust_marker) {
ba6459ba
PMF
321//ust// /*
322//ust// * Setting the iter module to -1UL will make sure
b521931e 323//ust// * that no module can possibly hold the current ust_marker.
ba6459ba
PMF
324//ust// */
325//ust// iter->module = (void *)-1UL;
326//ust// return NULL;
327//ust// }
b521931e 328//ust// return iter->ust_marker;
ba6459ba
PMF
329//ust// }
330//ust//
331//ust// static void s_stop(struct seq_file *m, void *p)
332//ust// {
b521931e 333//ust// ust_marker_iter_stop(m->private);
ba6459ba
PMF
334//ust// }
335//ust//
336//ust// static int s_show(struct seq_file *m, void *p)
337//ust// {
b521931e 338//ust// struct ust_marker_iter *iter = m->private;
ba6459ba 339//ust//
b521931e 340//ust// seq_printf(m, "channel: %s ust_marker: %s format: \"%s\" state: %d "
ba6459ba 341//ust// "event_id: %hu call: 0x%p probe %s : 0x%p\n",
b521931e
MD
342//ust// iter->ust_marker->channel,
343//ust// iter->ust_marker->name, iter->ust_marker->format,
344//ust// _imv_read(iter->ust_marker->state),
345//ust// iter->ust_marker->event_id,
346//ust// iter->ust_marker->call,
347//ust// iter->ust_marker->ptype ? "multi" : "single",
348//ust// iter->ust_marker->ptype ?
349//ust// (void*)iter->ust_marker->multi : (void*)iter->ust_marker->single.func);
ba6459ba
PMF
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;
b521931e 368//ust// struct ust_marker_iter *iter;
ba6459ba
PMF
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
b521931e 390static void disconnect_all_ust_markers(void)
ba6459ba 391{
b521931e 392 struct ltt_active_ust_marker *pdata, *tmp;
ba6459ba 393
b521931e
MD
394 cds_list_for_each_entry_safe(pdata, tmp, &ust_markers_loaded_list, node) {
395 ust_marker_probe_unregister_private_data(pdata->probe->probe_func,
ba6459ba 396 pdata);
0222e121 397 cds_list_del(&pdata->node);
ba6459ba
PMF
398 free(pdata);
399 }
400}
401
4db647c5
PMF
402static char initialized = 0;
403
b521931e 404void __attribute__((constructor)) init_ust_marker_control(void)
ba6459ba 405{
4db647c5
PMF
406 if(!initialized) {
407 int ret;
ba6459ba
PMF
408
409//ust// pentry = create_proc_entry("ltt", S_IRUSR|S_IWUSR, NULL);
410//ust// if (!pentry)
411//ust// return -EBUSY;
b521931e 412//ust// ust_markers_loaded_cachep = KMEM_CACHE(ltt_active_ust_marker, 0);
ba6459ba 413
4db647c5
PMF
414 ret = ltt_probe_register(&default_probe);
415 BUG_ON(ret);
b521931e 416 ret = ltt_ust_marker_connect("metadata", "core_marker_format",
4db647c5
PMF
417 DEFAULT_PROBE);
418 BUG_ON(ret);
b521931e 419 ret = ltt_ust_marker_connect("metadata", "core_marker_id", DEFAULT_PROBE);
4db647c5 420 BUG_ON(ret);
ba6459ba
PMF
421//ust// pentry->proc_fops = &ltt_fops;
422
4db647c5
PMF
423 initialized = 1;
424 }
ba6459ba 425}
b521931e 426//ust// module_init(ust_marker_control_init);
ba6459ba 427
b521931e 428static void __attribute__((destructor)) ust_marker_control_exit(void)
ba6459ba
PMF
429{
430 int ret;
431
432//ust// remove_proc_entry("ltt", NULL);
b521931e 433 ret = ltt_ust_marker_disconnect("metadata", "core_marker_format",
ba6459ba
PMF
434 DEFAULT_PROBE);
435 BUG_ON(ret);
b521931e 436 ret = ltt_ust_marker_disconnect("metadata", "core_marker_id",
ba6459ba
PMF
437 DEFAULT_PROBE);
438 BUG_ON(ret);
439 ret = ltt_probe_unregister(&default_probe);
440 BUG_ON(ret);
b521931e
MD
441 disconnect_all_ust_markers();
442//ust// kmem_cache_destroy(ust_markers_loaded_cachep);
443//ust// ust_marker_synchronize_unregister();
ba6459ba 444}
b521931e 445//ust// module_exit(ust_marker_control_exit);
ba6459ba
PMF
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.055489 seconds and 4 git commands to generate.