1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
5 * Holds LTTng probes registry.
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/module.h>
11 #include <linux/list.h>
12 #include <linux/mutex.h>
13 #include <linux/seq_file.h>
15 #include <lttng/events.h>
18 * probe list is protected by sessions lock.
20 static LIST_HEAD(_probe_list
);
23 * List of probes registered by not yet processed.
25 static LIST_HEAD(lazy_probe_init
);
28 * lazy_nesting counter ensures we don't trigger lazy probe registration
29 * fixup while we are performing the fixup. It is protected by the
32 static int lazy_nesting
;
34 DEFINE_PER_CPU(struct lttng_dynamic_len_stack
, lttng_dynamic_len_stack
);
36 EXPORT_PER_CPU_SYMBOL_GPL(lttng_dynamic_len_stack
);
39 * Called under sessions lock.
42 int check_event_provider(struct lttng_probe_desc
*desc
)
45 size_t provider_name_len
;
47 provider_name_len
= strnlen(desc
->provider
,
48 LTTNG_KERNEL_SYM_NAME_LEN
- 1);
49 for (i
= 0; i
< desc
->nr_events
; i
++) {
50 if (strncmp(desc
->event_desc
[i
]->name
,
53 return 0; /* provider mismatch */
55 * The event needs to contain at least provider name + _ +
58 if (strlen(desc
->event_desc
[i
]->name
) <= provider_name_len
+ 1)
59 return 0; /* provider mismatch */
60 if (desc
->event_desc
[i
]->name
[provider_name_len
] != '_')
61 return 0; /* provider mismatch */
67 * Called under sessions lock.
70 void lttng_lazy_probe_register(struct lttng_probe_desc
*desc
)
72 struct lttng_probe_desc
*iter
;
73 struct list_head
*probe_list
;
76 * Each provider enforce that every event name begins with the
77 * provider name. Check this in an assertion for extra
78 * carefulness. This ensures we cannot have duplicate event
79 * names across providers.
81 WARN_ON_ONCE(!check_event_provider(desc
));
84 * The provider ensures there are no duplicate event names.
85 * Duplicated TRACEPOINT_EVENT event names would generate a
86 * compile-time error due to duplicated symbol names.
90 * We sort the providers by struct lttng_probe_desc pointer
93 probe_list
= &_probe_list
;
94 list_for_each_entry_reverse(iter
, probe_list
, head
) {
95 BUG_ON(iter
== desc
); /* Should never be in the list twice */
97 /* We belong to the location right after iter. */
98 list_add(&desc
->head
, &iter
->head
);
102 /* We should be added at the head of the list */
103 list_add(&desc
->head
, probe_list
);
105 pr_debug("LTTng: just registered probe %s containing %u events\n",
106 desc
->provider
, desc
->nr_events
);
110 * Called under sessions lock.
113 void fixup_lazy_probes(void)
115 struct lttng_probe_desc
*iter
, *tmp
;
119 list_for_each_entry_safe(iter
, tmp
,
120 &lazy_probe_init
, lazy_init_head
) {
121 lttng_lazy_probe_register(iter
);
123 list_del(&iter
->lazy_init_head
);
125 ret
= lttng_fix_pending_events();
131 * Called under sessions lock.
133 struct list_head
*lttng_get_probe_list_head(void)
135 if (!lazy_nesting
&& !list_empty(&lazy_probe_init
))
141 const struct lttng_probe_desc
*find_provider(const char *provider
)
143 struct lttng_probe_desc
*iter
;
144 struct list_head
*probe_list
;
146 probe_list
= lttng_get_probe_list_head();
147 list_for_each_entry(iter
, probe_list
, head
) {
148 if (!strcmp(iter
->provider
, provider
))
154 int lttng_probe_register(struct lttng_probe_desc
*desc
)
158 lttng_lock_sessions();
161 * Check if the provider has already been registered.
163 if (find_provider(desc
->provider
)) {
167 list_add(&desc
->lazy_init_head
, &lazy_probe_init
);
169 pr_debug("LTTng: adding probe %s containing %u events to lazy registration list\n",
170 desc
->provider
, desc
->nr_events
);
172 * If there is at least one active session, we need to register
173 * the probe immediately, since we cannot delay event
174 * registration because they are needed ASAP.
176 if (lttng_session_active())
179 lttng_unlock_sessions();
182 EXPORT_SYMBOL_GPL(lttng_probe_register
);
184 void lttng_probe_unregister(struct lttng_probe_desc
*desc
)
186 lttng_lock_sessions();
188 list_del(&desc
->head
);
190 list_del(&desc
->lazy_init_head
);
191 pr_debug("LTTng: just unregistered probe %s\n", desc
->provider
);
192 lttng_unlock_sessions();
194 EXPORT_SYMBOL_GPL(lttng_probe_unregister
);
197 * TODO: this is O(nr_probes * nb_events), could be faster.
198 * Called with sessions lock held.
201 const struct lttng_event_desc
*find_event(const char *name
)
203 struct lttng_probe_desc
*probe_desc
;
206 list_for_each_entry(probe_desc
, &_probe_list
, head
) {
207 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
208 if (!strcmp(probe_desc
->event_desc
[i
]->name
, name
))
209 return probe_desc
->event_desc
[i
];
216 * Called with sessions lock held.
218 const struct lttng_event_desc
*lttng_event_get(const char *name
)
220 const struct lttng_event_desc
*event
;
223 event
= find_event(name
);
226 ret
= try_module_get(event
->owner
);
230 EXPORT_SYMBOL_GPL(lttng_event_get
);
233 * Called with sessions lock held.
235 void lttng_event_put(const struct lttng_event_desc
*event
)
237 module_put(event
->owner
);
239 EXPORT_SYMBOL_GPL(lttng_event_put
);
242 void *tp_list_start(struct seq_file
*m
, loff_t
*pos
)
244 struct lttng_probe_desc
*probe_desc
;
245 struct list_head
*probe_list
;
248 lttng_lock_sessions();
249 probe_list
= lttng_get_probe_list_head();
250 list_for_each_entry(probe_desc
, probe_list
, head
) {
251 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
253 return (void *) probe_desc
->event_desc
[i
];
261 void *tp_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
263 struct lttng_probe_desc
*probe_desc
;
264 struct list_head
*probe_list
;
268 probe_list
= lttng_get_probe_list_head();
269 list_for_each_entry(probe_desc
, probe_list
, head
) {
270 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
272 return (void *) probe_desc
->event_desc
[i
];
280 void tp_list_stop(struct seq_file
*m
, void *p
)
282 lttng_unlock_sessions();
286 int tp_list_show(struct seq_file
*m
, void *p
)
288 const struct lttng_event_desc
*probe_desc
= p
;
290 seq_printf(m
, "event { name = %s; };\n",
296 const struct seq_operations lttng_tracepoint_list_seq_ops
= {
297 .start
= tp_list_start
,
298 .next
= tp_list_next
,
299 .stop
= tp_list_stop
,
300 .show
= tp_list_show
,
304 int lttng_tracepoint_list_open(struct inode
*inode
, struct file
*file
)
306 return seq_open(file
, <tng_tracepoint_list_seq_ops
);
309 const struct file_operations lttng_tracepoint_list_fops
= {
310 .owner
= THIS_MODULE
,
311 .open
= lttng_tracepoint_list_open
,
314 .release
= seq_release
,
317 int lttng_probes_init(void)
321 for_each_possible_cpu(cpu
)
322 per_cpu_ptr(<tng_dynamic_len_stack
, cpu
)->offset
= 0;