4 * Holds LTTng probes registry.
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <linux/module.h>
24 #include <linux/list.h>
25 #include <linux/mutex.h>
26 #include <linux/seq_file.h>
28 #include <lttng-events.h>
31 * probe list is protected by sessions lock.
33 static LIST_HEAD(_probe_list
);
36 * List of probes registered by not yet processed.
38 static LIST_HEAD(lazy_probe_init
);
41 * lazy_nesting counter ensures we don't trigger lazy probe registration
42 * fixup while we are performing the fixup. It is protected by the
45 static int lazy_nesting
;
48 * Called under sessions lock.
51 int check_event_provider(struct lttng_probe_desc
*desc
)
54 size_t provider_name_len
;
56 provider_name_len
= strnlen(desc
->provider
,
57 LTTNG_KERNEL_SYM_NAME_LEN
- 1);
58 for (i
= 0; i
< desc
->nr_events
; i
++) {
59 if (strncmp(desc
->event_desc
[i
]->name
,
62 return 0; /* provider mismatch */
64 * The event needs to contain at least provider name + _ +
67 if (strlen(desc
->event_desc
[i
]->name
) <= provider_name_len
+ 1)
68 return 0; /* provider mismatch */
69 if (desc
->event_desc
[i
]->name
[provider_name_len
] != '_')
70 return 0; /* provider mismatch */
76 * Called under sessions lock.
79 void lttng_lazy_probe_register(struct lttng_probe_desc
*desc
)
81 struct lttng_probe_desc
*iter
;
82 struct list_head
*probe_list
;
85 * Each provider enforce that every event name begins with the
86 * provider name. Check this in an assertion for extra
87 * carefulness. This ensures we cannot have duplicate event
88 * names across providers.
90 WARN_ON_ONCE(!check_event_provider(desc
));
93 * The provider ensures there are no duplicate event names.
94 * Duplicated TRACEPOINT_EVENT event names would generate a
95 * compile-time error due to duplicated symbol names.
99 * We sort the providers by struct lttng_probe_desc pointer
102 probe_list
= &_probe_list
;
103 list_for_each_entry_reverse(iter
, probe_list
, head
) {
104 BUG_ON(iter
== desc
); /* Should never be in the list twice */
106 /* We belong to the location right after iter. */
107 list_add(&desc
->head
, &iter
->head
);
111 /* We should be added at the head of the list */
112 list_add(&desc
->head
, probe_list
);
114 pr_debug("LTTng: just registered probe %s containing %u events\n",
115 desc
->provider
, desc
->nr_events
);
119 * Called under sessions lock.
122 void fixup_lazy_probes(void)
124 struct lttng_probe_desc
*iter
, *tmp
;
128 list_for_each_entry_safe(iter
, tmp
,
129 &lazy_probe_init
, lazy_init_head
) {
130 lttng_lazy_probe_register(iter
);
132 list_del(&iter
->lazy_init_head
);
134 ret
= lttng_fix_pending_events();
140 * Called under sessions lock.
142 struct list_head
*lttng_get_probe_list_head(void)
144 if (!lazy_nesting
&& !list_empty(&lazy_probe_init
))
150 const struct lttng_probe_desc
*find_provider(const char *provider
)
152 struct lttng_probe_desc
*iter
;
153 struct list_head
*probe_list
;
155 probe_list
= lttng_get_probe_list_head();
156 list_for_each_entry(iter
, probe_list
, head
) {
157 if (!strcmp(iter
->provider
, provider
))
163 int lttng_probe_register(struct lttng_probe_desc
*desc
)
167 lttng_lock_sessions();
170 * Check if the provider has already been registered.
172 if (find_provider(desc
->provider
)) {
176 list_add(&desc
->lazy_init_head
, &lazy_probe_init
);
178 pr_debug("LTTng: adding probe %s containing %u events to lazy registration list\n",
179 desc
->provider
, desc
->nr_events
);
181 * If there is at least one active session, we need to register
182 * the probe immediately, since we cannot delay event
183 * registration because they are needed ASAP.
185 if (lttng_session_active())
188 lttng_unlock_sessions();
191 EXPORT_SYMBOL_GPL(lttng_probe_register
);
193 void lttng_probe_unregister(struct lttng_probe_desc
*desc
)
195 lttng_lock_sessions();
197 list_del(&desc
->head
);
199 list_del(&desc
->lazy_init_head
);
200 pr_debug("LTTng: just unregistered probe %s\n", desc
->provider
);
201 lttng_unlock_sessions();
203 EXPORT_SYMBOL_GPL(lttng_probe_unregister
);
206 * TODO: this is O(nr_probes * nb_events), could be faster.
207 * Called with sessions lock held.
210 const struct lttng_event_desc
*find_event(const char *name
)
212 struct lttng_probe_desc
*probe_desc
;
215 list_for_each_entry(probe_desc
, &_probe_list
, head
) {
216 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
217 if (!strcmp(probe_desc
->event_desc
[i
]->name
, name
))
218 return probe_desc
->event_desc
[i
];
225 * Called with sessions lock held.
227 const struct lttng_event_desc
*lttng_event_get(const char *name
)
229 const struct lttng_event_desc
*event
;
232 event
= find_event(name
);
235 ret
= try_module_get(event
->owner
);
239 EXPORT_SYMBOL_GPL(lttng_event_get
);
242 * Called with sessions lock held.
244 void lttng_event_put(const struct lttng_event_desc
*event
)
246 module_put(event
->owner
);
248 EXPORT_SYMBOL_GPL(lttng_event_put
);
251 void *tp_list_start(struct seq_file
*m
, loff_t
*pos
)
253 struct lttng_probe_desc
*probe_desc
;
254 struct list_head
*probe_list
;
257 lttng_lock_sessions();
258 probe_list
= lttng_get_probe_list_head();
259 list_for_each_entry(probe_desc
, probe_list
, head
) {
260 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
262 return (void *) probe_desc
->event_desc
[i
];
270 void *tp_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
272 struct lttng_probe_desc
*probe_desc
;
273 struct list_head
*probe_list
;
277 probe_list
= lttng_get_probe_list_head();
278 list_for_each_entry(probe_desc
, probe_list
, head
) {
279 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
281 return (void *) probe_desc
->event_desc
[i
];
289 void tp_list_stop(struct seq_file
*m
, void *p
)
291 lttng_unlock_sessions();
295 int tp_list_show(struct seq_file
*m
, void *p
)
297 const struct lttng_event_desc
*probe_desc
= p
;
299 seq_printf(m
, "event { name = %s; };\n",
305 const struct seq_operations lttng_tracepoint_list_seq_ops
= {
306 .start
= tp_list_start
,
307 .next
= tp_list_next
,
308 .stop
= tp_list_stop
,
309 .show
= tp_list_show
,
313 int lttng_tracepoint_list_open(struct inode
*inode
, struct file
*file
)
315 return seq_open(file
, <tng_tracepoint_list_seq_ops
);
318 const struct file_operations lttng_tracepoint_list_fops
= {
319 .owner
= THIS_MODULE
,
320 .open
= lttng_tracepoint_list_open
,
323 .release
= seq_release
,