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
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
29 #include "usterr_signal_safe.h"
31 #define DEFAULT_CHANNEL "cpu"
32 #define DEFAULT_PROBE "default"
34 CDS_LIST_HEAD(probes_list
);
37 * Mutex protecting the probe slab cache.
38 * Nests inside the traces mutex.
40 DEFINE_MUTEX(probes_mutex
);
42 struct ltt_available_probe default_probe
= {
45 .probe_func
= ltt_vtrace
,
46 .callbacks
[0] = ltt_serialize_data
,
49 //ust//static struct kmem_cache *ust_markers_loaded_cachep;
50 static CDS_LIST_HEAD(ust_markers_loaded_list
);
52 * List sorted by name strcmp order.
54 static CDS_LIST_HEAD(probes_registered_list
);
56 //ust// static struct proc_dir_entry *pentry;
58 //ust// static struct file_operations ltt_fops;
60 static struct ltt_available_probe
*get_probe_from_name(const char *pname
)
62 struct ltt_available_probe
*iter
;
63 int comparison
, found
= 0;
66 pname
= DEFAULT_PROBE
;
67 cds_list_for_each_entry(iter
, &probes_registered_list
, node
) {
68 comparison
= strcmp(pname
, iter
->name
);
81 static char *skip_spaces(char *buf)
83 while (*buf != '\0' && isspace(*buf))
88 static char *skip_nonspaces(char *buf)
90 while (*buf != '\0' && !isspace(*buf))
95 static void get_ust_marker_string(char *buf, char **start,
98 *start = skip_spaces(buf);
99 *end = skip_nonspaces(*start);
104 int ltt_probe_register(struct ltt_available_probe
*pdata
)
108 struct ltt_available_probe
*iter
;
110 pthread_mutex_lock(&probes_mutex
);
111 cds_list_for_each_entry_reverse(iter
, &probes_registered_list
, node
) {
112 comparison
= strcmp(pdata
->name
, iter
->name
);
116 } else if (comparison
> 0) {
117 /* We belong to the location right after iter. */
118 cds_list_add(&pdata
->node
, &iter
->node
);
122 /* Should be added at the head of the list */
123 cds_list_add(&pdata
->node
, &probes_registered_list
);
125 pthread_mutex_unlock(&probes_mutex
);
130 * Called when a probe does not want to be called anymore.
132 int ltt_probe_unregister(struct ltt_available_probe
*pdata
)
135 struct ltt_active_ust_marker
*amark
, *tmp
;
137 pthread_mutex_lock(&probes_mutex
);
138 cds_list_for_each_entry_safe(amark
, tmp
, &ust_markers_loaded_list
, node
) {
139 if (amark
->probe
== pdata
) {
140 ret
= ust_marker_probe_unregister_private_data(
141 pdata
->probe_func
, amark
);
144 cds_list_del(&amark
->node
);
148 cds_list_del(&pdata
->node
);
150 pthread_mutex_unlock(&probes_mutex
);
155 * Connect ust_marker "mname" to probe "pname".
156 * Only allow _only_ probe instance to be connected to a ust_marker.
158 int ltt_ust_marker_connect(const char *channel
, const char *mname
,
163 struct ltt_active_ust_marker
*pdata
;
164 struct ltt_available_probe
*probe
;
167 pthread_mutex_lock(&probes_mutex
);
168 probe
= get_probe_from_name(pname
);
173 pdata
= ust_marker_get_private_data(channel
, mname
, probe
->probe_func
, 0);
174 if (pdata
&& !IS_ERR(pdata
)) {
178 pdata
= zmalloc(sizeof(struct ltt_active_ust_marker
));
183 pdata
->probe
= probe
;
185 * ID has priority over channel in case of conflict.
187 ret
= ust_marker_probe_register(channel
, mname
, NULL
,
188 probe
->probe_func
, pdata
);
192 cds_list_add(&pdata
->node
, &ust_markers_loaded_list
);
194 pthread_mutex_unlock(&probes_mutex
);
200 * Disconnect ust_marker "mname", probe "pname".
202 int ltt_ust_marker_disconnect(const char *channel
, const char *mname
,
205 struct ltt_active_ust_marker
*pdata
;
206 struct ltt_available_probe
*probe
;
209 pthread_mutex_lock(&probes_mutex
);
210 probe
= get_probe_from_name(pname
);
215 pdata
= ust_marker_get_private_data(channel
, mname
, probe
->probe_func
, 0);
217 ret
= PTR_ERR(pdata
);
221 * Not registered by us.
226 ret
= ust_marker_probe_unregister(channel
, mname
, probe
->probe_func
, pdata
);
230 cds_list_del(&pdata
->node
);
234 pthread_mutex_unlock(&probes_mutex
);
239 * function handling proc entry write.
241 * connect <channel name> <ust_marker name> [<probe name>]]
242 * disconnect <channel name> <ust_marker name> [<probe name>]
244 //ust// static ssize_t ltt_write(struct file *file, const char __user *buffer,
245 //ust// size_t count, loff_t *offset)
248 //ust// char *iter, *ust_marker_action, *arg[4];
253 //ust// return -EINVAL;
255 //ust// kbuf = vmalloc(count + 1);
256 //ust// kbuf[count] = '\0'; /* Transform into a string */
257 //ust// ret = copy_from_user(kbuf, buffer, count);
259 //ust// ret = -EINVAL;
262 //ust// get_ust_marker_string(kbuf, &ust_marker_action, &iter);
263 //ust// if (!ust_marker_action || ust_marker_action == iter) {
264 //ust// ret = -EINVAL;
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_ust_marker_string(iter, &arg[i], &iter);
272 //ust// if (arg[i] == iter)
273 //ust// arg[i] = NULL;
277 //ust// if (!arg[0] || !arg[1]) {
278 //ust// ret = -EINVAL;
282 //ust// if (!strcmp(ust_marker_action, "connect")) {
283 //ust// ret = ltt_ust_marker_connect(arg[0], arg[1], arg[2]);
286 //ust// } else if (!strcmp(ust_marker_action, "disconnect")) {
287 //ust// ret = ltt_ust_marker_disconnect(arg[0], arg[1], arg[2]);
297 //ust// static void *s_next(struct seq_file *m, void *p, loff_t *pos)
299 //ust// struct ust_marker_iter *iter = m->private;
301 //ust// ust_marker_iter_next(iter);
302 //ust// if (!iter->ust_marker) {
304 //ust// * Setting the iter module to -1UL will make sure
305 //ust// * that no module can possibly hold the current ust_marker.
307 //ust// iter->module = (void *)-1UL;
310 //ust// return iter->ust_marker;
313 //ust// static void *s_start(struct seq_file *m, loff_t *pos)
315 //ust// struct ust_marker_iter *iter = m->private;
318 //ust// ust_marker_iter_reset(iter);
319 //ust// ust_marker_iter_start(iter);
320 //ust// if (!iter->ust_marker) {
322 //ust// * Setting the iter module to -1UL will make sure
323 //ust// * that no module can possibly hold the current ust_marker.
325 //ust// iter->module = (void *)-1UL;
328 //ust// return iter->ust_marker;
331 //ust// static void s_stop(struct seq_file *m, void *p)
333 //ust// ust_marker_iter_stop(m->private);
336 //ust// static int s_show(struct seq_file *m, void *p)
338 //ust// struct ust_marker_iter *iter = m->private;
340 //ust// seq_printf(m, "channel: %s ust_marker: %s format: \"%s\" state: %d "
341 //ust// "event_id: %hu call: 0x%p probe %s : 0x%p\n",
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);
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,
360 //ust// static int ltt_open(struct inode *inode, struct file *file)
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.
368 //ust// struct ust_marker_iter *iter;
370 //ust// iter = kzalloc(sizeof(*iter), GFP_KERNEL);
372 //ust// return -ENOMEM;
374 //ust// ret = seq_open(file, <t_seq_op);
375 //ust// if (ret == 0)
376 //ust// ((struct seq_file *)file->private_data)->private = iter;
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,
390 static void disconnect_all_ust_markers(void)
392 struct ltt_active_ust_marker
*pdata
, *tmp
;
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
,
397 cds_list_del(&pdata
->node
);
402 static char initialized
= 0;
404 void __attribute__((constructor
)) init_ust_marker_control(void)
409 //ust// pentry = create_proc_entry("ltt", S_IRUSR|S_IWUSR, NULL);
411 //ust// return -EBUSY;
412 //ust// ust_markers_loaded_cachep = KMEM_CACHE(ltt_active_ust_marker, 0);
414 ret
= ltt_probe_register(&default_probe
);
416 ret
= ltt_ust_marker_connect("metadata", "core_marker_format",
419 ret
= ltt_ust_marker_connect("metadata", "core_marker_id", DEFAULT_PROBE
);
421 //ust// pentry->proc_fops = <t_fops;
426 //ust// module_init(ust_marker_control_init);
428 static void __attribute__((destructor
)) ust_marker_control_exit(void)
432 //ust// remove_proc_entry("ltt", NULL);
433 ret
= ltt_ust_marker_disconnect("metadata", "core_marker_format",
436 ret
= ltt_ust_marker_disconnect("metadata", "core_marker_id",
439 ret
= ltt_probe_unregister(&default_probe
);
441 disconnect_all_ust_markers();
442 //ust// kmem_cache_destroy(ust_markers_loaded_cachep);
443 //ust// ust_marker_synchronize_unregister();
445 //ust// module_exit(ust_marker_control_exit);
447 //ust// MODULE_LICENSE("GPL");
448 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
449 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Marker Control");