Add missing demo-trace shell script to dist tarball
[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 */
e58095ef
MD
41CDS_LIST_HEAD(probe_list);
42
43struct cds_list_head *lttng_get_probe_list_head(void)
44{
45 return &probe_list;
46}
ce5aef0b 47
df854e41
MD
48static
49const 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
ce5aef0b
MD
60static
61const struct lttng_event_desc *find_event(const char *name)
62{
63 struct lttng_probe_desc *probe_desc;
64 int i;
65
8d8a24c8 66 cds_list_for_each_entry(probe_desc, &probe_list, head) {
ce5aef0b 67 for (i = 0; i < probe_desc->nr_events; i++) {
ff412fb5
MD
68 if (!strncmp(probe_desc->event_desc[i]->name, name,
69 LTTNG_UST_SYM_NAME_LEN - 1))
df854e41 70 return probe_desc->event_desc[i];
ce5aef0b
MD
71 }
72 }
73 return NULL;
74}
75
7dd08bec 76int lttng_probe_register(struct lttng_probe_desc *desc)
ce5aef0b 77{
df854e41 78 struct lttng_probe_desc *iter;
ce5aef0b
MD
79 int ret = 0;
80 int i;
81
17dfb34b 82 ust_lock();
df854e41
MD
83 if (find_provider(desc->provider)) {
84 ret = -EEXIST;
85 goto end;
86 }
ce5aef0b
MD
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++) {
df854e41 92 if (find_event(desc->event_desc[i]->name)) {
ce5aef0b
MD
93 ret = -EEXIST;
94 goto end;
95 }
96 }
df854e41
MD
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 */
8d8a24c8 111 cds_list_add(&desc->head, &probe_list);
df854e41 112desc_added:
e81a53af
MD
113 DBG("just registered probe %s containing %u events",
114 desc->provider, desc->nr_events);
8165c8da
MD
115 /*
116 * fix the events awaiting probe load.
117 */
118 for (i = 0; i < desc->nr_events; i++) {
68755429
MD
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);
e58095ef 124 ret = lttng_fix_pending_event_desc(ed);
8165c8da
MD
125 assert(!ret);
126 }
ce5aef0b 127end:
17dfb34b 128 ust_unlock();
ce5aef0b
MD
129 return ret;
130}
ce5aef0b 131
7dd08bec
MD
132/* Backward compatibility with UST 2.0 */
133int ltt_probe_register(struct lttng_probe_desc *desc)
134{
135 return lttng_probe_register(desc);
136}
137
138void lttng_probe_unregister(struct lttng_probe_desc *desc)
ce5aef0b 139{
17dfb34b 140 ust_lock();
8d8a24c8 141 cds_list_del(&desc->head);
e81a53af 142 DBG("just unregistered probe %s", desc->provider);
17dfb34b 143 ust_unlock();
ce5aef0b 144}
ce5aef0b 145
7dd08bec
MD
146/* Backward compatibility with UST 2.0 */
147void ltt_probe_unregister(struct lttng_probe_desc *desc)
148{
149 lttng_probe_unregister(desc);
150}
151
8165c8da
MD
152/*
153 * called with UST lock held.
154 */
7dd08bec 155const struct lttng_event_desc *lttng_event_get(const char *name)
ce5aef0b
MD
156{
157 const struct lttng_event_desc *event;
ce5aef0b 158
ce5aef0b 159 event = find_event(name);
ce5aef0b
MD
160 if (!event)
161 return NULL;
ce5aef0b
MD
162 return event;
163}
ce5aef0b 164
7dd08bec 165void lttng_event_put(const struct lttng_event_desc *event)
ce5aef0b 166{
ce5aef0b 167}
c8fcf224 168
7dd08bec 169void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
c8fcf224
MD
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 */
7dd08bec 182int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
c8fcf224
MD
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) {
882a56d7 201 list_entry->tp.loglevel = TRACE_DEFAULT;
c8fcf224 202 } else {
882a56d7 203 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
c8fcf224
MD
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
214err_nomem:
7dd08bec 215 lttng_probes_prune_event_list(list);
c8fcf224
MD
216 return -ENOMEM;
217}
218
219/*
220 * Return current iteration position, advance internal iterator to next.
221 * Return NULL if end of list.
222 */
223struct 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}
1f18504e 238
7dd08bec 239void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
06d4f27e
MD
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 */
7dd08bec 252int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
06d4f27e
MD
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
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.043846 seconds and 4 git commands to generate.