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>
16 #include <lttng/events-internal.h>
19 * probe list is protected by sessions lock.
21 static LIST_HEAD(_probe_list
);
24 * List of probes registered by not yet processed.
26 static LIST_HEAD(lazy_probe_init
);
29 * lazy_nesting counter ensures we don't notify lazy probe registration
30 * fixup while we are performing the fixup. It is protected by the
33 static int lazy_nesting
;
35 DEFINE_PER_CPU(struct lttng_dynamic_len_stack
, lttng_dynamic_len_stack
);
37 EXPORT_PER_CPU_SYMBOL_GPL(lttng_dynamic_len_stack
);
40 * Called under sessions lock.
43 int check_event_provider(struct lttng_kernel_probe_desc
*desc
)
46 size_t provider_name_len
;
48 provider_name_len
= strnlen(desc
->provider_name
,
49 LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1);
50 for (i
= 0; i
< desc
->nr_events
; i
++) {
52 * The event name needs to start with provider name + _ +
55 if (strncmp(desc
->event_desc
[i
]->event_name
, desc
->provider_name
, provider_name_len
))
57 else if (strlen(desc
->event_desc
[i
]->event_name
) <= provider_name_len
+ 1)
59 else if (desc
->event_desc
[i
]->event_name
[provider_name_len
] != '_')
63 printk(KERN_WARNING
"LTTng: event provider mismatch: "
64 "The event name needs to start with provider "
65 "name + _ + one or more letter, "
66 "provider: %s, event name: %s\n",
67 desc
->provider_name
, desc
->event_desc
[i
]->event_name
);
75 * Called under sessions lock.
78 void lttng_lazy_probe_register(struct lttng_kernel_probe_desc
*desc
)
80 struct lttng_kernel_probe_desc
*iter
;
81 struct list_head
*probe_list
;
84 * Each provider enforce that every event name begins with the
85 * provider name. Check this in an assertion for extra
86 * carefulness. This ensures we cannot have duplicate event
87 * names across providers.
89 WARN_ON_ONCE(!check_event_provider(desc
));
92 * The provider ensures there are no duplicate event names.
93 * Duplicated TRACEPOINT_EVENT event names would generate a
94 * compile-time error due to duplicated symbol names.
98 * We sort the providers by struct lttng_kernel_probe_desc pointer
101 probe_list
= &_probe_list
;
102 list_for_each_entry_reverse(iter
, probe_list
, head
) {
103 BUG_ON(iter
== desc
); /* Should never be in the list twice */
105 /* We belong to the location right after iter. */
106 list_add(&desc
->head
, &iter
->head
);
110 /* We should be added at the head of the list */
111 list_add(&desc
->head
, probe_list
);
113 pr_debug("LTTng: just registered probe %s containing %u events\n",
114 desc
->provider_name
, desc
->nr_events
);
118 * Called under sessions lock.
121 void fixup_lazy_probes(void)
123 struct lttng_kernel_probe_desc
*iter
, *tmp
;
127 list_for_each_entry_safe(iter
, tmp
,
128 &lazy_probe_init
, lazy_init_head
) {
129 lttng_lazy_probe_register(iter
);
131 list_del(&iter
->lazy_init_head
);
133 ret
= lttng_fix_pending_events();
135 ret
= lttng_fix_pending_event_notifiers();
141 * Called under sessions lock.
143 struct list_head
*lttng_get_probe_list_head(void)
145 if (!lazy_nesting
&& !list_empty(&lazy_probe_init
))
151 const struct lttng_kernel_probe_desc
*find_provider(const char *provider
)
153 struct lttng_kernel_probe_desc
*iter
;
154 struct list_head
*probe_list
;
156 probe_list
= lttng_get_probe_list_head();
157 list_for_each_entry(iter
, probe_list
, head
) {
158 if (!strcmp(iter
->provider_name
, provider
))
164 int lttng_kernel_probe_register(struct lttng_kernel_probe_desc
*desc
)
168 lttng_lock_sessions();
171 * Check if the provider has already been registered.
173 if (find_provider(desc
->provider_name
)) {
177 list_add(&desc
->lazy_init_head
, &lazy_probe_init
);
179 pr_debug("LTTng: adding probe %s containing %u events to lazy registration list\n",
180 desc
->provider_name
, desc
->nr_events
);
182 * If there is at least one active session, we need to register
183 * the probe immediately, since we cannot delay event
184 * registration because they are needed ASAP.
186 if (lttng_session_active() || lttng_event_notifier_active())
189 lttng_unlock_sessions();
192 EXPORT_SYMBOL_GPL(lttng_kernel_probe_register
);
194 void lttng_kernel_probe_unregister(struct lttng_kernel_probe_desc
*desc
)
196 lttng_lock_sessions();
198 list_del(&desc
->head
);
200 list_del(&desc
->lazy_init_head
);
201 pr_debug("LTTng: just unregistered probe %s\n", desc
->provider_name
);
202 lttng_unlock_sessions();
204 EXPORT_SYMBOL_GPL(lttng_kernel_probe_unregister
);
207 * TODO: this is O(nr_probes * nb_events), could be faster.
208 * Called with sessions lock held.
211 const struct lttng_kernel_event_desc
*find_event_desc(const char *name
)
213 struct lttng_kernel_probe_desc
*probe_desc
;
216 list_for_each_entry(probe_desc
, &_probe_list
, head
) {
217 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
218 if (!strcmp(probe_desc
->event_desc
[i
]->event_name
, name
))
219 return probe_desc
->event_desc
[i
];
226 * Called with sessions lock held.
228 const struct lttng_kernel_event_desc
*lttng_event_desc_get(const char *name
)
230 const struct lttng_kernel_event_desc
*event_desc
;
233 event_desc
= find_event_desc(name
);
236 ret
= try_module_get(event_desc
->owner
);
240 EXPORT_SYMBOL_GPL(lttng_event_desc_get
);
243 * Called with sessions lock held.
245 void lttng_event_desc_put(const struct lttng_kernel_event_desc
*event_desc
)
247 module_put(event_desc
->owner
);
249 EXPORT_SYMBOL_GPL(lttng_event_desc_put
);
252 void *tp_list_start(struct seq_file
*m
, loff_t
*pos
)
254 struct lttng_kernel_probe_desc
*probe_desc
;
255 struct list_head
*probe_list
;
258 lttng_lock_sessions();
259 probe_list
= lttng_get_probe_list_head();
260 list_for_each_entry(probe_desc
, probe_list
, head
) {
261 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
263 return (void *) probe_desc
->event_desc
[i
];
271 void *tp_list_next(struct seq_file
*m
, void *p
, loff_t
*ppos
)
273 struct lttng_kernel_probe_desc
*probe_desc
;
274 struct list_head
*probe_list
;
278 probe_list
= lttng_get_probe_list_head();
279 list_for_each_entry(probe_desc
, probe_list
, head
) {
280 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
282 return (void *) probe_desc
->event_desc
[i
];
290 void tp_list_stop(struct seq_file
*m
, void *p
)
292 lttng_unlock_sessions();
296 int tp_list_show(struct seq_file
*m
, void *p
)
298 const struct lttng_kernel_event_desc
*probe_desc
= p
;
300 seq_printf(m
, "event { name = %s; };\n",
301 probe_desc
->event_name
);
306 const struct seq_operations lttng_tracepoint_list_seq_ops
= {
307 .start
= tp_list_start
,
308 .next
= tp_list_next
,
309 .stop
= tp_list_stop
,
310 .show
= tp_list_show
,
314 int lttng_tracepoint_list_open(struct inode
*inode
, struct file
*file
)
316 return seq_open(file
, <tng_tracepoint_list_seq_ops
);
319 const struct file_operations lttng_tracepoint_list_fops
= {
320 .owner
= THIS_MODULE
,
321 .open
= lttng_tracepoint_list_open
,
324 .release
= seq_release
,
327 int lttng_probes_init(void)
331 for_each_possible_cpu(cpu
)
332 per_cpu_ptr(<tng_dynamic_len_stack
, cpu
)->offset
= 0;