-
Notifications
You must be signed in to change notification settings - Fork 0
/
lttng-probes.c
324 lines (285 loc) · 7.17 KB
/
lttng-probes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
*
* lttng-probes.c
*
* Holds LTTng probes registry.
*
* Copyright (C) 2010-2012 Mathieu Desnoyers <[email protected]>
*/
#include <linux/module.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/seq_file.h>
#include <lttng-events.h>
/*
* probe list is protected by sessions lock.
*/
static LIST_HEAD(_probe_list);
/*
* List of probes registered by not yet processed.
*/
static LIST_HEAD(lazy_probe_init);
/*
* lazy_nesting counter ensures we don't trigger lazy probe registration
* fixup while we are performing the fixup. It is protected by the
* sessions lock.
*/
static int lazy_nesting;
DEFINE_PER_CPU(struct lttng_dynamic_len_stack, lttng_dynamic_len_stack);
EXPORT_PER_CPU_SYMBOL_GPL(lttng_dynamic_len_stack);
/*
* Called under sessions lock.
*/
static
int check_event_provider(struct lttng_probe_desc *desc)
{
int i;
size_t provider_name_len;
provider_name_len = strnlen(desc->provider,
LTTNG_KERNEL_SYM_NAME_LEN - 1);
for (i = 0; i < desc->nr_events; i++) {
if (strncmp(desc->event_desc[i]->name,
desc->provider,
provider_name_len))
return 0; /* provider mismatch */
/*
* The event needs to contain at least provider name + _ +
* one or more letter.
*/
if (strlen(desc->event_desc[i]->name) <= provider_name_len + 1)
return 0; /* provider mismatch */
if (desc->event_desc[i]->name[provider_name_len] != '_')
return 0; /* provider mismatch */
}
return 1;
}
/*
* Called under sessions lock.
*/
static
void lttng_lazy_probe_register(struct lttng_probe_desc *desc)
{
struct lttng_probe_desc *iter;
struct list_head *probe_list;
/*
* Each provider enforce that every event name begins with the
* provider name. Check this in an assertion for extra
* carefulness. This ensures we cannot have duplicate event
* names across providers.
*/
WARN_ON_ONCE(!check_event_provider(desc));
/*
* The provider ensures there are no duplicate event names.
* Duplicated TRACEPOINT_EVENT event names would generate a
* compile-time error due to duplicated symbol names.
*/
/*
* We sort the providers by struct lttng_probe_desc pointer
* address.
*/
probe_list = &_probe_list;
list_for_each_entry_reverse(iter, probe_list, head) {
BUG_ON(iter == desc); /* Should never be in the list twice */
if (iter < desc) {
/* We belong to the location right after iter. */
list_add(&desc->head, &iter->head);
goto desc_added;
}
}
/* We should be added at the head of the list */
list_add(&desc->head, probe_list);
desc_added:
pr_debug("LTTng: just registered probe %s containing %u events\n",
desc->provider, desc->nr_events);
}
/*
* Called under sessions lock.
*/
static
void fixup_lazy_probes(void)
{
struct lttng_probe_desc *iter, *tmp;
int ret;
lazy_nesting++;
list_for_each_entry_safe(iter, tmp,
&lazy_probe_init, lazy_init_head) {
lttng_lazy_probe_register(iter);
iter->lazy = 0;
list_del(&iter->lazy_init_head);
}
ret = lttng_fix_pending_events();
WARN_ON_ONCE(ret);
lazy_nesting--;
}
/*
* Called under sessions lock.
*/
struct list_head *lttng_get_probe_list_head(void)
{
if (!lazy_nesting && !list_empty(&lazy_probe_init))
fixup_lazy_probes();
return &_probe_list;
}
static
const struct lttng_probe_desc *find_provider(const char *provider)
{
struct lttng_probe_desc *iter;
struct list_head *probe_list;
probe_list = lttng_get_probe_list_head();
list_for_each_entry(iter, probe_list, head) {
if (!strcmp(iter->provider, provider))
return iter;
}
return NULL;
}
int lttng_probe_register(struct lttng_probe_desc *desc)
{
int ret = 0;
lttng_lock_sessions();
/*
* Check if the provider has already been registered.
*/
if (find_provider(desc->provider)) {
ret = -EEXIST;
goto end;
}
list_add(&desc->lazy_init_head, &lazy_probe_init);
desc->lazy = 1;
pr_debug("LTTng: adding probe %s containing %u events to lazy registration list\n",
desc->provider, desc->nr_events);
/*
* If there is at least one active session, we need to register
* the probe immediately, since we cannot delay event
* registration because they are needed ASAP.
*/
if (lttng_session_active())
fixup_lazy_probes();
end:
lttng_unlock_sessions();
return ret;
}
EXPORT_SYMBOL_GPL(lttng_probe_register);
void lttng_probe_unregister(struct lttng_probe_desc *desc)
{
lttng_lock_sessions();
if (!desc->lazy)
list_del(&desc->head);
else
list_del(&desc->lazy_init_head);
pr_debug("LTTng: just unregistered probe %s\n", desc->provider);
lttng_unlock_sessions();
}
EXPORT_SYMBOL_GPL(lttng_probe_unregister);
/*
* TODO: this is O(nr_probes * nb_events), could be faster.
* Called with sessions lock held.
*/
static
const struct lttng_event_desc *find_event(const char *name)
{
struct lttng_probe_desc *probe_desc;
int i;
list_for_each_entry(probe_desc, &_probe_list, head) {
for (i = 0; i < probe_desc->nr_events; i++) {
if (!strcmp(probe_desc->event_desc[i]->name, name))
return probe_desc->event_desc[i];
}
}
return NULL;
}
/*
* Called with sessions lock held.
*/
const struct lttng_event_desc *lttng_event_get(const char *name)
{
const struct lttng_event_desc *event;
int ret;
event = find_event(name);
if (!event)
return NULL;
ret = try_module_get(event->owner);
WARN_ON_ONCE(!ret);
return event;
}
EXPORT_SYMBOL_GPL(lttng_event_get);
/*
* Called with sessions lock held.
*/
void lttng_event_put(const struct lttng_event_desc *event)
{
module_put(event->owner);
}
EXPORT_SYMBOL_GPL(lttng_event_put);
static
void *tp_list_start(struct seq_file *m, loff_t *pos)
{
struct lttng_probe_desc *probe_desc;
struct list_head *probe_list;
int iter = 0, i;
lttng_lock_sessions();
probe_list = lttng_get_probe_list_head();
list_for_each_entry(probe_desc, probe_list, head) {
for (i = 0; i < probe_desc->nr_events; i++) {
if (iter++ >= *pos)
return (void *) probe_desc->event_desc[i];
}
}
/* End of list */
return NULL;
}
static
void *tp_list_next(struct seq_file *m, void *p, loff_t *ppos)
{
struct lttng_probe_desc *probe_desc;
struct list_head *probe_list;
int iter = 0, i;
(*ppos)++;
probe_list = lttng_get_probe_list_head();
list_for_each_entry(probe_desc, probe_list, head) {
for (i = 0; i < probe_desc->nr_events; i++) {
if (iter++ >= *ppos)
return (void *) probe_desc->event_desc[i];
}
}
/* End of list */
return NULL;
}
static
void tp_list_stop(struct seq_file *m, void *p)
{
lttng_unlock_sessions();
}
static
int tp_list_show(struct seq_file *m, void *p)
{
const struct lttng_event_desc *probe_desc = p;
seq_printf(m, "event { name = %s; };\n",
probe_desc->name);
return 0;
}
static
const struct seq_operations lttng_tracepoint_list_seq_ops = {
.start = tp_list_start,
.next = tp_list_next,
.stop = tp_list_stop,
.show = tp_list_show,
};
static
int lttng_tracepoint_list_open(struct inode *inode, struct file *file)
{
return seq_open(file, <tng_tracepoint_list_seq_ops);
}
const struct file_operations lttng_tracepoint_list_fops = {
.owner = THIS_MODULE,
.open = lttng_tracepoint_list_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
int lttng_probes_init(void)
{
int cpu;
for_each_possible_cpu(cpu)
per_cpu_ptr(<tng_dynamic_len_stack, cpu)->offset = 0;
return 0;
}