Always use lttng_get_probe_list_head to get probe list
[lttng-ust.git] / liblttng-ust / lttng-probes.c
CommitLineData
ce5aef0b 1/*
7dd08bec 2 * lttng-probes.c
ce5aef0b 3 *
ce5aef0b
MD
4 * Holds LTTng probes registry.
5 *
e92f3e28
MD
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
ce5aef0b
MD
21 */
22
8d8a24c8
MD
23#include <string.h>
24#include <errno.h>
25#include <urcu/list.h>
1f18504e 26#include <urcu/hlist.h>
4318ae1b 27#include <lttng/ust-events.h>
902e1379 28#include <lttng/tracepoint.h>
d8de1354 29#include "tracepoint-internal.h"
a3bb4b27 30#include <assert.h>
c8fcf224 31#include <helper.h>
48740cab 32#include <ctype.h>
ce5aef0b 33
7dd08bec 34#include "lttng-tracer-core.h"
1f18504e
MD
35#include "jhash.h"
36#include "error.h"
8165c8da
MD
37
38/*
17dfb34b 39 * probe list is protected by ust_lock()/ust_unlock().
8165c8da 40 */
ac69b35b 41static CDS_LIST_HEAD(_probe_list);
e58095ef
MD
42
43struct cds_list_head *lttng_get_probe_list_head(void)
44{
ac69b35b 45 return &_probe_list;
e58095ef 46}
ce5aef0b 47
df854e41
MD
48static
49const struct lttng_probe_desc *find_provider(const char *provider)
50{
51 struct lttng_probe_desc *iter;
ac69b35b 52 struct cds_list_head *probe_list;
df854e41 53
ac69b35b
MD
54 probe_list = lttng_get_probe_list_head();
55 cds_list_for_each_entry(iter, probe_list, head) {
df854e41
MD
56 if (!strcmp(iter->provider, provider))
57 return iter;
58 }
59 return NULL;
60}
61
ce5aef0b 62static
48205d60 63int check_event_provider(struct lttng_probe_desc *desc)
ce5aef0b 64{
ce5aef0b 65 int i;
48205d60 66 size_t provider_name_len;
ce5aef0b 67
48205d60
MD
68 provider_name_len = strnlen(desc->provider,
69 LTTNG_UST_SYM_NAME_LEN - 1);
70 for (i = 0; i < desc->nr_events; i++) {
71 if (strncmp(desc->event_desc[i]->name,
72 desc->provider,
73 provider_name_len))
74 return 0; /* provider mismatch */
ce5aef0b 75 }
48205d60 76 return 1;
ce5aef0b
MD
77}
78
7dd08bec 79int lttng_probe_register(struct lttng_probe_desc *desc)
ce5aef0b 80{
df854e41 81 struct lttng_probe_desc *iter;
ce5aef0b
MD
82 int ret = 0;
83 int i;
ac69b35b 84 struct cds_list_head *probe_list;
ce5aef0b 85
17dfb34b 86 ust_lock();
48205d60
MD
87
88 /*
89 * Check if the provider has already been registered.
90 */
df854e41
MD
91 if (find_provider(desc->provider)) {
92 ret = -EEXIST;
93 goto end;
94 }
48205d60 95
ce5aef0b 96 /*
48205d60
MD
97 * Each provider enforce that every event name begins with the
98 * provider name. Check this in an assertion for extra
99 * carefulness. This ensures we cannot have duplicate event
100 * names across providers.
101 */
102 assert(check_event_provider(desc));
103
104 /*
105 * The provider ensures there are no duplicate event names.
106 * Duplicated TRACEPOINT_EVENT event names would generate a
107 * compile-time error due to duplicated symbol names.
ce5aef0b 108 */
df854e41
MD
109
110 /*
111 * We sort the providers by struct lttng_probe_desc pointer
112 * address.
113 */
ac69b35b
MD
114 probe_list = lttng_get_probe_list_head();
115 cds_list_for_each_entry_reverse(iter, probe_list, head) {
df854e41
MD
116 BUG_ON(iter == desc); /* Should never be in the list twice */
117 if (iter < desc) {
118 /* We belong to the location right after iter. */
119 cds_list_add(&desc->head, &iter->head);
120 goto desc_added;
121 }
122 }
123 /* We should be added at the head of the list */
ac69b35b 124 cds_list_add(&desc->head, probe_list);
df854e41 125desc_added:
e81a53af
MD
126 DBG("just registered probe %s containing %u events",
127 desc->provider, desc->nr_events);
8165c8da
MD
128 /*
129 * fix the events awaiting probe load.
130 */
131 for (i = 0; i < desc->nr_events; i++) {
68755429
MD
132 const struct lttng_event_desc *ed;
133
134 ed = desc->event_desc[i];
135 DBG("Registered event probe \"%s\" with signature \"%s\"",
136 ed->name, ed->signature);
e58095ef 137 ret = lttng_fix_pending_event_desc(ed);
8165c8da
MD
138 assert(!ret);
139 }
ce5aef0b 140end:
17dfb34b 141 ust_unlock();
ce5aef0b
MD
142 return ret;
143}
ce5aef0b 144
7dd08bec
MD
145/* Backward compatibility with UST 2.0 */
146int ltt_probe_register(struct lttng_probe_desc *desc)
147{
148 return lttng_probe_register(desc);
149}
150
151void lttng_probe_unregister(struct lttng_probe_desc *desc)
ce5aef0b 152{
17dfb34b 153 ust_lock();
8d8a24c8 154 cds_list_del(&desc->head);
e81a53af 155 DBG("just unregistered probe %s", desc->provider);
17dfb34b 156 ust_unlock();
ce5aef0b 157}
ce5aef0b 158
7dd08bec
MD
159/* Backward compatibility with UST 2.0 */
160void ltt_probe_unregister(struct lttng_probe_desc *desc)
161{
162 lttng_probe_unregister(desc);
163}
164
7dd08bec 165void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
c8fcf224
MD
166{
167 struct tp_list_entry *list_entry, *tmp;
168
169 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
170 cds_list_del(&list_entry->head);
171 free(list_entry);
172 }
173}
174
175/*
176 * called with UST lock held.
177 */
7dd08bec 178int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
c8fcf224
MD
179{
180 struct lttng_probe_desc *probe_desc;
181 int i;
ac69b35b 182 struct cds_list_head *probe_list;
c8fcf224 183
ac69b35b 184 probe_list = lttng_get_probe_list_head();
c8fcf224 185 CDS_INIT_LIST_HEAD(&list->head);
ac69b35b 186 cds_list_for_each_entry(probe_desc, probe_list, head) {
c8fcf224
MD
187 for (i = 0; i < probe_desc->nr_events; i++) {
188 struct tp_list_entry *list_entry;
189
190 list_entry = zmalloc(sizeof(*list_entry));
191 if (!list_entry)
192 goto err_nomem;
193 cds_list_add(&list_entry->head, &list->head);
194 strncpy(list_entry->tp.name,
195 probe_desc->event_desc[i]->name,
196 LTTNG_UST_SYM_NAME_LEN);
197 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
198 if (!probe_desc->event_desc[i]->loglevel) {
882a56d7 199 list_entry->tp.loglevel = TRACE_DEFAULT;
c8fcf224 200 } else {
882a56d7 201 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
c8fcf224
MD
202 }
203 }
204 }
205 if (cds_list_empty(&list->head))
206 list->iter = NULL;
207 else
208 list->iter =
209 cds_list_first_entry(&list->head, struct tp_list_entry, head);
210 return 0;
211
212err_nomem:
7dd08bec 213 lttng_probes_prune_event_list(list);
c8fcf224
MD
214 return -ENOMEM;
215}
216
217/*
218 * Return current iteration position, advance internal iterator to next.
219 * Return NULL if end of list.
220 */
221struct lttng_ust_tracepoint_iter *
222 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
223{
224 struct tp_list_entry *entry;
225
226 if (!list->iter)
227 return NULL;
228 entry = list->iter;
229 if (entry->head.next == &list->head)
230 list->iter = NULL;
231 else
232 list->iter = cds_list_entry(entry->head.next,
233 struct tp_list_entry, head);
234 return &entry->tp;
235}
1f18504e 236
7dd08bec 237void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
06d4f27e
MD
238{
239 struct tp_field_list_entry *list_entry, *tmp;
240
241 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
242 cds_list_del(&list_entry->head);
243 free(list_entry);
244 }
245}
246
247/*
248 * called with UST lock held.
249 */
7dd08bec 250int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
06d4f27e
MD
251{
252 struct lttng_probe_desc *probe_desc;
253 int i;
ac69b35b 254 struct cds_list_head *probe_list;
06d4f27e 255
ac69b35b 256 probe_list = lttng_get_probe_list_head();
06d4f27e 257 CDS_INIT_LIST_HEAD(&list->head);
ac69b35b 258 cds_list_for_each_entry(probe_desc, probe_list, head) {
06d4f27e
MD
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
26329f26
MD
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
06d4f27e
MD
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';
40003310
MD
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 }
06d4f27e
MD
333 if (!event_desc->loglevel) {
334 list_entry->field.loglevel = TRACE_DEFAULT;
335 } else {
336 list_entry->field.loglevel = *(*event_desc->loglevel);
337 }
180901e6 338 list_entry->field.nowrite = event_field->nowrite;
06d4f27e
MD
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
350err_nomem:
7dd08bec 351 lttng_probes_prune_field_list(list);
06d4f27e
MD
352 return -ENOMEM;
353}
354
355/*
356 * Return current iteration position, advance internal iterator to next.
357 * Return NULL if end of list.
358 */
359struct 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.045063 seconds and 4 git commands to generate.