Implement support for overlapping wildcard/events
[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 CDS_LIST_HEAD(probe_list);
42
43 struct cds_list_head *lttng_get_probe_list_head(void)
44 {
45 return &probe_list;
46 }
47
48 static
49 const struct lttng_probe_desc *find_provider(const char *provider)
50 {
51 struct lttng_probe_desc *iter;
52
53 cds_list_for_each_entry(iter, &probe_list, head) {
54 if (!strcmp(iter->provider, provider))
55 return iter;
56 }
57 return NULL;
58 }
59
60 static
61 const struct lttng_event_desc *find_event(const char *name)
62 {
63 struct lttng_probe_desc *probe_desc;
64 int i;
65
66 cds_list_for_each_entry(probe_desc, &probe_list, head) {
67 for (i = 0; i < probe_desc->nr_events; i++) {
68 if (!strncmp(probe_desc->event_desc[i]->name, name,
69 LTTNG_UST_SYM_NAME_LEN - 1))
70 return probe_desc->event_desc[i];
71 }
72 }
73 return NULL;
74 }
75
76 int lttng_probe_register(struct lttng_probe_desc *desc)
77 {
78 struct lttng_probe_desc *iter;
79 int ret = 0;
80 int i;
81
82 ust_lock();
83 if (find_provider(desc->provider)) {
84 ret = -EEXIST;
85 goto end;
86 }
87 /*
88 * TODO: This is O(N^2). Turn into a hash table when probe registration
89 * overhead becomes an issue.
90 */
91 for (i = 0; i < desc->nr_events; i++) {
92 if (find_event(desc->event_desc[i]->name)) {
93 ret = -EEXIST;
94 goto end;
95 }
96 }
97
98 /*
99 * We sort the providers by struct lttng_probe_desc pointer
100 * address.
101 */
102 cds_list_for_each_entry_reverse(iter, &probe_list, head) {
103 BUG_ON(iter == desc); /* Should never be in the list twice */
104 if (iter < desc) {
105 /* We belong to the location right after iter. */
106 cds_list_add(&desc->head, &iter->head);
107 goto desc_added;
108 }
109 }
110 /* We should be added at the head of the list */
111 cds_list_add(&desc->head, &probe_list);
112 desc_added:
113 DBG("just registered probe %s containing %u events",
114 desc->provider, desc->nr_events);
115 /*
116 * fix the events awaiting probe load.
117 */
118 for (i = 0; i < desc->nr_events; i++) {
119 const struct lttng_event_desc *ed;
120
121 ed = desc->event_desc[i];
122 DBG("Registered event probe \"%s\" with signature \"%s\"",
123 ed->name, ed->signature);
124 ret = lttng_fix_pending_event_desc(ed);
125 assert(!ret);
126 }
127 end:
128 ust_unlock();
129 return ret;
130 }
131
132 /* Backward compatibility with UST 2.0 */
133 int ltt_probe_register(struct lttng_probe_desc *desc)
134 {
135 return lttng_probe_register(desc);
136 }
137
138 void lttng_probe_unregister(struct lttng_probe_desc *desc)
139 {
140 ust_lock();
141 cds_list_del(&desc->head);
142 DBG("just unregistered probe %s", desc->provider);
143 ust_unlock();
144 }
145
146 /* Backward compatibility with UST 2.0 */
147 void ltt_probe_unregister(struct lttng_probe_desc *desc)
148 {
149 lttng_probe_unregister(desc);
150 }
151
152 /*
153 * called with UST lock held.
154 */
155 const struct lttng_event_desc *lttng_event_get(const char *name)
156 {
157 const struct lttng_event_desc *event;
158
159 event = find_event(name);
160 if (!event)
161 return NULL;
162 return event;
163 }
164
165 void lttng_event_put(const struct lttng_event_desc *event)
166 {
167 }
168
169 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
170 {
171 struct tp_list_entry *list_entry, *tmp;
172
173 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
174 cds_list_del(&list_entry->head);
175 free(list_entry);
176 }
177 }
178
179 /*
180 * called with UST lock held.
181 */
182 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
183 {
184 struct lttng_probe_desc *probe_desc;
185 int i;
186
187 CDS_INIT_LIST_HEAD(&list->head);
188 cds_list_for_each_entry(probe_desc, &probe_list, head) {
189 for (i = 0; i < probe_desc->nr_events; i++) {
190 struct tp_list_entry *list_entry;
191
192 list_entry = zmalloc(sizeof(*list_entry));
193 if (!list_entry)
194 goto err_nomem;
195 cds_list_add(&list_entry->head, &list->head);
196 strncpy(list_entry->tp.name,
197 probe_desc->event_desc[i]->name,
198 LTTNG_UST_SYM_NAME_LEN);
199 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
200 if (!probe_desc->event_desc[i]->loglevel) {
201 list_entry->tp.loglevel = TRACE_DEFAULT;
202 } else {
203 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
204 }
205 }
206 }
207 if (cds_list_empty(&list->head))
208 list->iter = NULL;
209 else
210 list->iter =
211 cds_list_first_entry(&list->head, struct tp_list_entry, head);
212 return 0;
213
214 err_nomem:
215 lttng_probes_prune_event_list(list);
216 return -ENOMEM;
217 }
218
219 /*
220 * Return current iteration position, advance internal iterator to next.
221 * Return NULL if end of list.
222 */
223 struct lttng_ust_tracepoint_iter *
224 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
225 {
226 struct tp_list_entry *entry;
227
228 if (!list->iter)
229 return NULL;
230 entry = list->iter;
231 if (entry->head.next == &list->head)
232 list->iter = NULL;
233 else
234 list->iter = cds_list_entry(entry->head.next,
235 struct tp_list_entry, head);
236 return &entry->tp;
237 }
238
239 void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
240 {
241 struct tp_field_list_entry *list_entry, *tmp;
242
243 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
244 cds_list_del(&list_entry->head);
245 free(list_entry);
246 }
247 }
248
249 /*
250 * called with UST lock held.
251 */
252 int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
253 {
254 struct lttng_probe_desc *probe_desc;
255 int i;
256
257 CDS_INIT_LIST_HEAD(&list->head);
258 cds_list_for_each_entry(probe_desc, &probe_list, head) {
259 for (i = 0; i < probe_desc->nr_events; i++) {
260 const struct lttng_event_desc *event_desc =
261 probe_desc->event_desc[i];
262 int j;
263
264 if (event_desc->nr_fields == 0) {
265 /* Events without fields. */
266 struct tp_field_list_entry *list_entry;
267
268 list_entry = zmalloc(sizeof(*list_entry));
269 if (!list_entry)
270 goto err_nomem;
271 cds_list_add(&list_entry->head, &list->head);
272 strncpy(list_entry->field.event_name,
273 event_desc->name,
274 LTTNG_UST_SYM_NAME_LEN);
275 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
276 list_entry->field.field_name[0] = '\0';
277 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
278 if (!event_desc->loglevel) {
279 list_entry->field.loglevel = TRACE_DEFAULT;
280 } else {
281 list_entry->field.loglevel = *(*event_desc->loglevel);
282 }
283 list_entry->field.nowrite = 1;
284 }
285
286 for (j = 0; j < event_desc->nr_fields; j++) {
287 const struct lttng_event_field *event_field =
288 &event_desc->fields[j];
289 struct tp_field_list_entry *list_entry;
290
291 list_entry = zmalloc(sizeof(*list_entry));
292 if (!list_entry)
293 goto err_nomem;
294 cds_list_add(&list_entry->head, &list->head);
295 strncpy(list_entry->field.event_name,
296 event_desc->name,
297 LTTNG_UST_SYM_NAME_LEN);
298 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
299 strncpy(list_entry->field.field_name,
300 event_field->name,
301 LTTNG_UST_SYM_NAME_LEN);
302 list_entry->field.field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
303 switch (event_field->type.atype) {
304 case atype_integer:
305 list_entry->field.type = LTTNG_UST_FIELD_INTEGER;
306 break;
307 case atype_string:
308 list_entry->field.type = LTTNG_UST_FIELD_STRING;
309 break;
310 case atype_array:
311 if (event_field->type.u.array.elem_type.atype != atype_integer
312 || event_field->type.u.array.elem_type.u.basic.integer.encoding == lttng_encode_none)
313 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
314 else
315 list_entry->field.type = LTTNG_UST_FIELD_STRING;
316 break;
317 case atype_sequence:
318 if (event_field->type.u.sequence.elem_type.atype != atype_integer
319 || event_field->type.u.sequence.elem_type.u.basic.integer.encoding == lttng_encode_none)
320 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
321 else
322 list_entry->field.type = LTTNG_UST_FIELD_STRING;
323 break;
324 case atype_float:
325 list_entry->field.type = LTTNG_UST_FIELD_FLOAT;
326 break;
327 case atype_enum:
328 list_entry->field.type = LTTNG_UST_FIELD_ENUM;
329 break;
330 default:
331 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
332 }
333 if (!event_desc->loglevel) {
334 list_entry->field.loglevel = TRACE_DEFAULT;
335 } else {
336 list_entry->field.loglevel = *(*event_desc->loglevel);
337 }
338 list_entry->field.nowrite = event_field->nowrite;
339 }
340 }
341 }
342 if (cds_list_empty(&list->head))
343 list->iter = NULL;
344 else
345 list->iter =
346 cds_list_first_entry(&list->head,
347 struct tp_field_list_entry, head);
348 return 0;
349
350 err_nomem:
351 lttng_probes_prune_field_list(list);
352 return -ENOMEM;
353 }
354
355 /*
356 * Return current iteration position, advance internal iterator to next.
357 * Return NULL if end of list.
358 */
359 struct lttng_ust_field_iter *
360 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list)
361 {
362 struct tp_field_list_entry *entry;
363
364 if (!list->iter)
365 return NULL;
366 entry = list->iter;
367 if (entry->head.next == &list->head)
368 list->iter = NULL;
369 else
370 list->iter = cds_list_entry(entry->head.next,
371 struct tp_field_list_entry, head);
372 return &entry->field;
373 }
This page took 0.037775 seconds and 5 git commands to generate.