Cleanup: add lttng_/lttng-/LTTNG_ prefixes
[lttng-ust.git] / liblttng-ust / lttng-probes.c
1 /*
2 * lttng-probes.c
3 *
4 * Holds LTTng probes registry.
5 *
6 * Copyright 2010-2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include <string.h>
24 #include <errno.h>
25 #include <urcu/list.h>
26 #include <urcu/hlist.h>
27 #include <lttng/ust-events.h>
28 #include <lttng/tracepoint.h>
29 #include "tracepoint-internal.h"
30 #include <assert.h>
31 #include <helper.h>
32 #include <ctype.h>
33
34 #include "lttng-tracer-core.h"
35 #include "jhash.h"
36 #include "error.h"
37
38 /*
39 * probe list is protected by ust_lock()/ust_unlock().
40 */
41 static CDS_LIST_HEAD(probe_list);
42
43 static
44 const struct lttng_probe_desc *find_provider(const char *provider)
45 {
46 struct lttng_probe_desc *iter;
47
48 cds_list_for_each_entry(iter, &probe_list, head) {
49 if (!strcmp(iter->provider, provider))
50 return iter;
51 }
52 return NULL;
53 }
54
55 static
56 const struct lttng_event_desc *find_event(const char *name)
57 {
58 struct lttng_probe_desc *probe_desc;
59 int i;
60
61 cds_list_for_each_entry(probe_desc, &probe_list, head) {
62 for (i = 0; i < probe_desc->nr_events; i++) {
63 if (!strncmp(probe_desc->event_desc[i]->name, name,
64 LTTNG_UST_SYM_NAME_LEN - 1))
65 return probe_desc->event_desc[i];
66 }
67 }
68 return NULL;
69 }
70
71 int lttng_probe_register(struct lttng_probe_desc *desc)
72 {
73 struct lttng_probe_desc *iter;
74 int ret = 0;
75 int i;
76
77 ust_lock();
78 if (find_provider(desc->provider)) {
79 ret = -EEXIST;
80 goto end;
81 }
82 /*
83 * TODO: This is O(N^2). Turn into a hash table when probe registration
84 * overhead becomes an issue.
85 */
86 for (i = 0; i < desc->nr_events; i++) {
87 if (find_event(desc->event_desc[i]->name)) {
88 ret = -EEXIST;
89 goto end;
90 }
91 }
92
93 /*
94 * We sort the providers by struct lttng_probe_desc pointer
95 * address.
96 */
97 cds_list_for_each_entry_reverse(iter, &probe_list, head) {
98 BUG_ON(iter == desc); /* Should never be in the list twice */
99 if (iter < desc) {
100 /* We belong to the location right after iter. */
101 cds_list_add(&desc->head, &iter->head);
102 goto desc_added;
103 }
104 }
105 /* We should be added at the head of the list */
106 cds_list_add(&desc->head, &probe_list);
107 desc_added:
108 DBG("just registered probe %s containing %u events",
109 desc->provider, desc->nr_events);
110 /*
111 * fix the events awaiting probe load.
112 */
113 for (i = 0; i < desc->nr_events; i++) {
114 const struct lttng_event_desc *ed;
115
116 ed = desc->event_desc[i];
117 DBG("Registered event probe \"%s\" with signature \"%s\"",
118 ed->name, ed->signature);
119 ret = pending_probe_fix_events(ed);
120 assert(!ret);
121 }
122 end:
123 ust_unlock();
124 return ret;
125 }
126
127 /* Backward compatibility with UST 2.0 */
128 int ltt_probe_register(struct lttng_probe_desc *desc)
129 {
130 return lttng_probe_register(desc);
131 }
132
133 void lttng_probe_unregister(struct lttng_probe_desc *desc)
134 {
135 ust_lock();
136 cds_list_del(&desc->head);
137 DBG("just unregistered probe %s", desc->provider);
138 ust_unlock();
139 }
140
141 /* Backward compatibility with UST 2.0 */
142 void ltt_probe_unregister(struct lttng_probe_desc *desc)
143 {
144 lttng_probe_unregister(desc);
145 }
146
147 /*
148 * called with UST lock held.
149 */
150 const struct lttng_event_desc *lttng_event_get(const char *name)
151 {
152 const struct lttng_event_desc *event;
153
154 event = find_event(name);
155 if (!event)
156 return NULL;
157 return event;
158 }
159
160 void lttng_event_put(const struct lttng_event_desc *event)
161 {
162 }
163
164 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
165 {
166 struct tp_list_entry *list_entry, *tmp;
167
168 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
169 cds_list_del(&list_entry->head);
170 free(list_entry);
171 }
172 }
173
174 /*
175 * called with UST lock held.
176 */
177 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
178 {
179 struct lttng_probe_desc *probe_desc;
180 int i;
181
182 CDS_INIT_LIST_HEAD(&list->head);
183 cds_list_for_each_entry(probe_desc, &probe_list, head) {
184 for (i = 0; i < probe_desc->nr_events; i++) {
185 struct tp_list_entry *list_entry;
186
187 list_entry = zmalloc(sizeof(*list_entry));
188 if (!list_entry)
189 goto err_nomem;
190 cds_list_add(&list_entry->head, &list->head);
191 strncpy(list_entry->tp.name,
192 probe_desc->event_desc[i]->name,
193 LTTNG_UST_SYM_NAME_LEN);
194 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
195 if (!probe_desc->event_desc[i]->loglevel) {
196 list_entry->tp.loglevel = TRACE_DEFAULT;
197 } else {
198 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
199 }
200 }
201 }
202 if (cds_list_empty(&list->head))
203 list->iter = NULL;
204 else
205 list->iter =
206 cds_list_first_entry(&list->head, struct tp_list_entry, head);
207 return 0;
208
209 err_nomem:
210 lttng_probes_prune_event_list(list);
211 return -ENOMEM;
212 }
213
214 /*
215 * Return current iteration position, advance internal iterator to next.
216 * Return NULL if end of list.
217 */
218 struct lttng_ust_tracepoint_iter *
219 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
220 {
221 struct tp_list_entry *entry;
222
223 if (!list->iter)
224 return NULL;
225 entry = list->iter;
226 if (entry->head.next == &list->head)
227 list->iter = NULL;
228 else
229 list->iter = cds_list_entry(entry->head.next,
230 struct tp_list_entry, head);
231 return &entry->tp;
232 }
233
234 void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
235 {
236 struct tp_field_list_entry *list_entry, *tmp;
237
238 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
239 cds_list_del(&list_entry->head);
240 free(list_entry);
241 }
242 }
243
244 /*
245 * called with UST lock held.
246 */
247 int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
248 {
249 struct lttng_probe_desc *probe_desc;
250 int i;
251
252 CDS_INIT_LIST_HEAD(&list->head);
253 cds_list_for_each_entry(probe_desc, &probe_list, head) {
254 for (i = 0; i < probe_desc->nr_events; i++) {
255 const struct lttng_event_desc *event_desc =
256 probe_desc->event_desc[i];
257 int j;
258
259 if (event_desc->nr_fields == 0) {
260 /* Events without fields. */
261 struct tp_field_list_entry *list_entry;
262
263 list_entry = zmalloc(sizeof(*list_entry));
264 if (!list_entry)
265 goto err_nomem;
266 cds_list_add(&list_entry->head, &list->head);
267 strncpy(list_entry->field.event_name,
268 event_desc->name,
269 LTTNG_UST_SYM_NAME_LEN);
270 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
271 list_entry->field.field_name[0] = '\0';
272 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
273 if (!event_desc->loglevel) {
274 list_entry->field.loglevel = TRACE_DEFAULT;
275 } else {
276 list_entry->field.loglevel = *(*event_desc->loglevel);
277 }
278 list_entry->field.nowrite = 1;
279 }
280
281 for (j = 0; j < event_desc->nr_fields; j++) {
282 const struct lttng_event_field *event_field =
283 &event_desc->fields[j];
284 struct tp_field_list_entry *list_entry;
285
286 list_entry = zmalloc(sizeof(*list_entry));
287 if (!list_entry)
288 goto err_nomem;
289 cds_list_add(&list_entry->head, &list->head);
290 strncpy(list_entry->field.event_name,
291 event_desc->name,
292 LTTNG_UST_SYM_NAME_LEN);
293 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
294 strncpy(list_entry->field.field_name,
295 event_field->name,
296 LTTNG_UST_SYM_NAME_LEN);
297 list_entry->field.field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
298 switch (event_field->type.atype) {
299 case atype_integer:
300 list_entry->field.type = LTTNG_UST_FIELD_INTEGER;
301 break;
302 case atype_string:
303 list_entry->field.type = LTTNG_UST_FIELD_STRING;
304 break;
305 case atype_array:
306 if (event_field->type.u.array.elem_type.atype != atype_integer
307 || event_field->type.u.array.elem_type.u.basic.integer.encoding == lttng_encode_none)
308 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
309 else
310 list_entry->field.type = LTTNG_UST_FIELD_STRING;
311 break;
312 case atype_sequence:
313 if (event_field->type.u.sequence.elem_type.atype != atype_integer
314 || event_field->type.u.sequence.elem_type.u.basic.integer.encoding == lttng_encode_none)
315 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
316 else
317 list_entry->field.type = LTTNG_UST_FIELD_STRING;
318 break;
319 case atype_float:
320 list_entry->field.type = LTTNG_UST_FIELD_FLOAT;
321 break;
322 case atype_enum:
323 list_entry->field.type = LTTNG_UST_FIELD_ENUM;
324 break;
325 default:
326 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
327 }
328 if (!event_desc->loglevel) {
329 list_entry->field.loglevel = TRACE_DEFAULT;
330 } else {
331 list_entry->field.loglevel = *(*event_desc->loglevel);
332 }
333 list_entry->field.nowrite = event_field->nowrite;
334 }
335 }
336 }
337 if (cds_list_empty(&list->head))
338 list->iter = NULL;
339 else
340 list->iter =
341 cds_list_first_entry(&list->head,
342 struct tp_field_list_entry, head);
343 return 0;
344
345 err_nomem:
346 lttng_probes_prune_field_list(list);
347 return -ENOMEM;
348 }
349
350 /*
351 * Return current iteration position, advance internal iterator to next.
352 * Return NULL if end of list.
353 */
354 struct lttng_ust_field_iter *
355 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list)
356 {
357 struct tp_field_list_entry *entry;
358
359 if (!list->iter)
360 return NULL;
361 entry = list->iter;
362 if (entry->head.next == &list->head)
363 list->iter = NULL;
364 else
365 list->iter = cds_list_entry(entry->head.next,
366 struct tp_field_list_entry, head);
367 return &entry->field;
368 }
369
370 /*
371 * marshall all probes/all events and create those that fit the
372 * wildcard. Add them to the events list as created.
373 */
374 void lttng_probes_create_wildcard_events(struct wildcard_entry *entry,
375 struct session_wildcard *wildcard)
376 {
377 struct lttng_probe_desc *probe_desc;
378 struct lttng_ust_event event_param;
379 int i;
380
381 cds_list_for_each_entry(probe_desc, &probe_list, head) {
382 for (i = 0; i < probe_desc->nr_events; i++) {
383 const struct lttng_event_desc *event_desc;
384 int match = 0;
385
386 event_desc = probe_desc->event_desc[i];
387 /* compare excluding final '*' */
388 assert(strlen(entry->name) > 0);
389 if (strcmp(event_desc->name, "lttng_ust:metadata")
390 && (strlen(entry->name) == 1
391 || !strncmp(event_desc->name, entry->name,
392 strlen(entry->name) - 1))) {
393 if (lttng_loglevel_match(event_desc,
394 entry->loglevel_type,
395 entry->loglevel)) {
396 match = 1;
397 }
398 }
399 if (match) {
400 struct lttng_event *ev;
401 int ret;
402
403 memcpy(&event_param, &wildcard->event_param,
404 sizeof(event_param));
405 strncpy(event_param.name,
406 event_desc->name,
407 sizeof(event_param.name));
408 event_param.name[sizeof(event_param.name) - 1] = '\0';
409 /* create event */
410 ret = lttng_event_create(wildcard->chan,
411 &event_param, &ev);
412 if (ret) {
413 DBG("Error creating event");
414 continue;
415 }
416 cds_list_add(&ev->wildcard_list,
417 &wildcard->events);
418 }
419 }
420 }
421 lttng_filter_wildcard_link_bytecode(wildcard);
422 }
423
This page took 0.043854 seconds and 5 git commands to generate.