2de4df1aeee9a3e0dd992e2843681cff070e0c9f
[lttng-ust.git] / liblttng-ust / lttng-ust-abi.c
1 /*
2 * lttng-ust-abi.c
3 *
4 * LTTng UST ABI
5 *
6 * Copyright (C) 2010-2012 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 * Mimic system calls for:
24 * - session creation, returns an object descriptor or failure.
25 * - channel creation, returns an object descriptor or failure.
26 * - Operates on a session object descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns an object descriptor or failure.
29 * - Operates on a channel object descriptor.
30 * - stream notifier get, returns an object descriptor or failure.
31 * - Operates on a channel object descriptor.
32 * - event creation, returns an object descriptor or failure.
33 * - Operates on a channel object descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
38 */
39
40 #define _LGPL_SOURCE
41 #include <lttng/ust-abi.h>
42 #include <lttng/ust-error.h>
43 #include <urcu/compiler.h>
44 #include <urcu/list.h>
45 #include <lttng/ust-events.h>
46 #include <lttng/ust-version.h>
47 #include <lttng/tracepoint.h>
48 #include <ust-fd.h>
49 #include "tracepoint-internal.h"
50 #include <usterr-signal-safe.h>
51 #include <helper.h>
52 #include "lttng-tracer.h"
53 #include "string-utils.h"
54 #include "../libringbuffer/shm.h"
55 #include "../libringbuffer/frontend_types.h"
56
57 #define OBJ_NAME_LEN 16
58
59 static int lttng_ust_abi_close_in_progress;
60
61 static
62 int lttng_abi_tracepoint_list(void *owner);
63 static
64 int lttng_abi_tracepoint_field_list(void *owner);
65
66 /*
67 * Object descriptor table. Should be protected from concurrent access
68 * by the caller.
69 */
70
71 struct lttng_ust_obj {
72 union {
73 struct {
74 void *private_data;
75 const struct lttng_ust_objd_ops *ops;
76 int f_count;
77 int owner_ref; /* has ref from owner */
78 void *owner;
79 char name[OBJ_NAME_LEN];
80 } s;
81 int freelist_next; /* offset freelist. end is -1. */
82 } u;
83 };
84
85 struct lttng_ust_objd_table {
86 struct lttng_ust_obj *array;
87 unsigned int len, allocated_len;
88 int freelist_head; /* offset freelist head. end is -1 */
89 };
90
91 static struct lttng_ust_objd_table objd_table = {
92 .freelist_head = -1,
93 };
94
95 static
96 int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops,
97 void *owner, const char *name)
98 {
99 struct lttng_ust_obj *obj;
100
101 if (objd_table.freelist_head != -1) {
102 obj = &objd_table.array[objd_table.freelist_head];
103 objd_table.freelist_head = obj->u.freelist_next;
104 goto end;
105 }
106
107 if (objd_table.len >= objd_table.allocated_len) {
108 unsigned int new_allocated_len, old_allocated_len;
109 struct lttng_ust_obj *new_table, *old_table;
110
111 old_allocated_len = objd_table.allocated_len;
112 old_table = objd_table.array;
113 if (!old_allocated_len)
114 new_allocated_len = 1;
115 else
116 new_allocated_len = old_allocated_len << 1;
117 new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len);
118 if (!new_table)
119 return -ENOMEM;
120 memcpy(new_table, old_table,
121 sizeof(struct lttng_ust_obj) * old_allocated_len);
122 free(old_table);
123 objd_table.array = new_table;
124 objd_table.allocated_len = new_allocated_len;
125 }
126 obj = &objd_table.array[objd_table.len];
127 objd_table.len++;
128 end:
129 obj->u.s.private_data = private_data;
130 obj->u.s.ops = ops;
131 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
132 /* count == 2 : allocated + hold ref */
133 obj->u.s.owner_ref = 1; /* One owner reference */
134 obj->u.s.owner = owner;
135 strncpy(obj->u.s.name, name, OBJ_NAME_LEN);
136 obj->u.s.name[OBJ_NAME_LEN - 1] = '\0';
137 return obj - objd_table.array;
138 }
139
140 static
141 struct lttng_ust_obj *_objd_get(int id)
142 {
143 if (id >= objd_table.len)
144 return NULL;
145 if (!objd_table.array[id].u.s.f_count)
146 return NULL;
147 return &objd_table.array[id];
148 }
149
150 static
151 void *objd_private(int id)
152 {
153 struct lttng_ust_obj *obj = _objd_get(id);
154 assert(obj);
155 return obj->u.s.private_data;
156 }
157
158 static
159 void objd_set_private(int id, void *private_data)
160 {
161 struct lttng_ust_obj *obj = _objd_get(id);
162 assert(obj);
163 obj->u.s.private_data = private_data;
164 }
165
166 const struct lttng_ust_objd_ops *objd_ops(int id)
167 {
168 struct lttng_ust_obj *obj = _objd_get(id);
169
170 if (!obj)
171 return NULL;
172 return obj->u.s.ops;
173 }
174
175 static
176 void objd_free(int id)
177 {
178 struct lttng_ust_obj *obj = _objd_get(id);
179
180 assert(obj);
181 obj->u.freelist_next = objd_table.freelist_head;
182 objd_table.freelist_head = obj - objd_table.array;
183 assert(obj->u.s.f_count == 1);
184 obj->u.s.f_count = 0; /* deallocated */
185 }
186
187 static
188 void objd_ref(int id)
189 {
190 struct lttng_ust_obj *obj = _objd_get(id);
191 assert(obj != NULL);
192 obj->u.s.f_count++;
193 }
194
195 int lttng_ust_objd_unref(int id, int is_owner)
196 {
197 struct lttng_ust_obj *obj = _objd_get(id);
198
199 if (!obj)
200 return -EINVAL;
201 if (obj->u.s.f_count == 1) {
202 ERR("Reference counting error\n");
203 return -EINVAL;
204 }
205 if (is_owner) {
206 if (!obj->u.s.owner_ref) {
207 ERR("Error decrementing owner reference");
208 return -EINVAL;
209 }
210 obj->u.s.owner_ref--;
211 }
212 if ((--obj->u.s.f_count) == 1) {
213 const struct lttng_ust_objd_ops *ops = objd_ops(id);
214
215 if (ops->release)
216 ops->release(id);
217 objd_free(id);
218 }
219 return 0;
220 }
221
222 static
223 void objd_table_destroy(void)
224 {
225 int i;
226
227 for (i = 0; i < objd_table.allocated_len; i++) {
228 struct lttng_ust_obj *obj;
229
230 obj = _objd_get(i);
231 if (!obj)
232 continue;
233 if (!obj->u.s.owner_ref)
234 continue; /* only unref owner ref. */
235 (void) lttng_ust_objd_unref(i, 1);
236 }
237 free(objd_table.array);
238 objd_table.array = NULL;
239 objd_table.len = 0;
240 objd_table.allocated_len = 0;
241 objd_table.freelist_head = -1;
242 }
243
244 const char *lttng_ust_obj_get_name(int id)
245 {
246 struct lttng_ust_obj *obj = _objd_get(id);
247
248 if (!obj)
249 return NULL;
250 return obj->u.s.name;
251 }
252
253 void lttng_ust_objd_table_owner_cleanup(void *owner)
254 {
255 int i;
256
257 for (i = 0; i < objd_table.allocated_len; i++) {
258 struct lttng_ust_obj *obj;
259
260 obj = _objd_get(i);
261 if (!obj)
262 continue;
263 if (!obj->u.s.owner)
264 continue; /* skip root handles */
265 if (!obj->u.s.owner_ref)
266 continue; /* only unref owner ref. */
267 if (obj->u.s.owner == owner)
268 (void) lttng_ust_objd_unref(i, 1);
269 }
270 }
271
272 /*
273 * This is LTTng's own personal way to create an ABI for sessiond.
274 * We send commands over a socket.
275 */
276
277 static const struct lttng_ust_objd_ops lttng_ops;
278 static const struct lttng_ust_objd_ops lttng_session_ops;
279 static const struct lttng_ust_objd_ops lttng_channel_ops;
280 static const struct lttng_ust_objd_ops lttng_enabler_ops;
281 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
282 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
283
284 int lttng_abi_create_root_handle(void)
285 {
286 int root_handle;
287
288 /* root handles have NULL owners */
289 root_handle = objd_alloc(NULL, &lttng_ops, NULL, "root");
290 return root_handle;
291 }
292
293 static
294 int lttng_is_channel_ready(struct lttng_channel *lttng_chan)
295 {
296 struct channel *chan;
297 unsigned int nr_streams, exp_streams;
298
299 chan = lttng_chan->chan;
300 nr_streams = channel_handle_get_nr_streams(lttng_chan->handle);
301 exp_streams = chan->nr_streams;
302 return nr_streams == exp_streams;
303 }
304
305 static
306 int lttng_abi_create_session(void *owner)
307 {
308 struct lttng_session *session;
309 int session_objd, ret;
310
311 session = lttng_session_create();
312 if (!session)
313 return -ENOMEM;
314 session_objd = objd_alloc(session, &lttng_session_ops, owner, "session");
315 if (session_objd < 0) {
316 ret = session_objd;
317 goto objd_error;
318 }
319 session->objd = session_objd;
320 session->owner = owner;
321 return session_objd;
322
323 objd_error:
324 lttng_session_destroy(session);
325 return ret;
326 }
327
328 static
329 long lttng_abi_tracer_version(int objd,
330 struct lttng_ust_tracer_version *v)
331 {
332 v->major = LTTNG_UST_MAJOR_VERSION;
333 v->minor = LTTNG_UST_MINOR_VERSION;
334 v->patchlevel = LTTNG_UST_PATCHLEVEL_VERSION;
335 return 0;
336 }
337
338 static
339 long lttng_abi_add_context(int objd,
340 struct lttng_ust_context *context_param,
341 union ust_args *uargs,
342 struct lttng_ctx **ctx, struct lttng_session *session)
343 {
344 return lttng_attach_context(context_param, uargs, ctx, session);
345 }
346
347 /**
348 * lttng_cmd - lttng control through socket commands
349 *
350 * @objd: the object descriptor
351 * @cmd: the command
352 * @arg: command arg
353 * @uargs: UST arguments (internal)
354 * @owner: objd owner
355 *
356 * This descriptor implements lttng commands:
357 * LTTNG_UST_SESSION
358 * Returns a LTTng trace session object descriptor
359 * LTTNG_UST_TRACER_VERSION
360 * Returns the LTTng kernel tracer version
361 * LTTNG_UST_TRACEPOINT_LIST
362 * Returns a file descriptor listing available tracepoints
363 * LTTNG_UST_TRACEPOINT_FIELD_LIST
364 * Returns a file descriptor listing available tracepoint fields
365 * LTTNG_UST_WAIT_QUIESCENT
366 * Returns after all previously running probes have completed
367 *
368 * The returned session will be deleted when its file descriptor is closed.
369 */
370 static
371 long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
372 union ust_args *uargs, void *owner)
373 {
374 switch (cmd) {
375 case LTTNG_UST_SESSION:
376 return lttng_abi_create_session(owner);
377 case LTTNG_UST_TRACER_VERSION:
378 return lttng_abi_tracer_version(objd,
379 (struct lttng_ust_tracer_version *) arg);
380 case LTTNG_UST_TRACEPOINT_LIST:
381 return lttng_abi_tracepoint_list(owner);
382 case LTTNG_UST_TRACEPOINT_FIELD_LIST:
383 return lttng_abi_tracepoint_field_list(owner);
384 case LTTNG_UST_WAIT_QUIESCENT:
385 synchronize_trace();
386 return 0;
387 default:
388 return -EINVAL;
389 }
390 }
391
392 static const struct lttng_ust_objd_ops lttng_ops = {
393 .cmd = lttng_cmd,
394 };
395
396 static
397 int lttng_abi_map_channel(int session_objd,
398 struct lttng_ust_channel *ust_chan,
399 union ust_args *uargs,
400 void *owner)
401 {
402 struct lttng_session *session = objd_private(session_objd);
403 const char *transport_name;
404 const struct lttng_transport *transport;
405 const char *chan_name;
406 int chan_objd;
407 struct lttng_ust_shm_handle *channel_handle;
408 struct lttng_channel *lttng_chan;
409 struct channel *chan;
410 struct lttng_ust_lib_ring_buffer_config *config;
411 void *chan_data;
412 int wakeup_fd;
413 uint64_t len;
414 int ret;
415 enum lttng_ust_chan_type type;
416
417 chan_data = uargs->channel.chan_data;
418 wakeup_fd = uargs->channel.wakeup_fd;
419 len = ust_chan->len;
420 type = ust_chan->type;
421
422 switch (type) {
423 case LTTNG_UST_CHAN_PER_CPU:
424 break;
425 default:
426 ret = -EINVAL;
427 goto invalid;
428 }
429
430 if (session->been_active) {
431 ret = -EBUSY;
432 goto active; /* Refuse to add channel to active session */
433 }
434
435 channel_handle = channel_handle_create(chan_data, len, wakeup_fd);
436 if (!channel_handle) {
437 ret = -EINVAL;
438 goto handle_error;
439 }
440
441 chan = shmp(channel_handle, channel_handle->chan);
442 assert(chan);
443 chan->handle = channel_handle;
444 config = &chan->backend.config;
445 lttng_chan = channel_get_private(chan);
446 if (!lttng_chan) {
447 ret = -EINVAL;
448 goto alloc_error;
449 }
450
451 /* Lookup transport name */
452 switch (type) {
453 case LTTNG_UST_CHAN_PER_CPU:
454 if (config->output == RING_BUFFER_MMAP) {
455 if (config->mode == RING_BUFFER_OVERWRITE) {
456 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER) {
457 transport_name = "relay-overwrite-mmap";
458 } else {
459 transport_name = "relay-overwrite-rt-mmap";
460 }
461 } else {
462 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER) {
463 transport_name = "relay-discard-mmap";
464 } else {
465 transport_name = "relay-discard-rt-mmap";
466 }
467 }
468 } else {
469 ret = -EINVAL;
470 goto notransport;
471 }
472 chan_name = "channel";
473 break;
474 default:
475 ret = -EINVAL;
476 goto notransport;
477 }
478 transport = lttng_transport_find(transport_name);
479 if (!transport) {
480 DBG("LTTng transport %s not found\n",
481 transport_name);
482 ret = -EINVAL;
483 goto notransport;
484 }
485
486 chan_objd = objd_alloc(NULL, &lttng_channel_ops, owner, chan_name);
487 if (chan_objd < 0) {
488 ret = chan_objd;
489 goto objd_error;
490 }
491
492 /* Initialize our lttng chan */
493 lttng_chan->chan = chan;
494 lttng_chan->tstate = 1;
495 lttng_chan->enabled = 1;
496 lttng_chan->ctx = NULL;
497 lttng_chan->session = session;
498 lttng_chan->ops = &transport->ops;
499 memcpy(&lttng_chan->chan->backend.config,
500 transport->client_config,
501 sizeof(lttng_chan->chan->backend.config));
502 cds_list_add(&lttng_chan->node, &session->chan_head);
503 lttng_chan->header_type = 0;
504 lttng_chan->handle = channel_handle;
505 lttng_chan->type = type;
506
507 /*
508 * We tolerate no failure path after channel creation. It will stay
509 * invariant for the rest of the session.
510 */
511 objd_set_private(chan_objd, lttng_chan);
512 lttng_chan->objd = chan_objd;
513 /* The channel created holds a reference on the session */
514 objd_ref(session_objd);
515 return chan_objd;
516
517 /* error path after channel was created */
518 objd_error:
519 notransport:
520 alloc_error:
521 channel_destroy(chan, channel_handle, 0);
522 return ret;
523
524 /*
525 * error path before channel creation (owning chan_data and
526 * wakeup_fd).
527 */
528 handle_error:
529 active:
530 invalid:
531 {
532 int close_ret;
533
534 lttng_ust_lock_fd_tracker();
535 close_ret = close(wakeup_fd);
536 lttng_ust_unlock_fd_tracker();
537 if (close_ret) {
538 PERROR("close");
539 }
540 }
541 free(chan_data);
542 return ret;
543 }
544
545 /**
546 * lttng_session_cmd - lttng session object command
547 *
548 * @obj: the object
549 * @cmd: the command
550 * @arg: command arg
551 * @uargs: UST arguments (internal)
552 * @owner: objd owner
553 *
554 * This descriptor implements lttng commands:
555 * LTTNG_UST_CHANNEL
556 * Returns a LTTng channel object descriptor
557 * LTTNG_UST_ENABLE
558 * Enables tracing for a session (weak enable)
559 * LTTNG_UST_DISABLE
560 * Disables tracing for a session (strong disable)
561 *
562 * The returned channel will be deleted when its file descriptor is closed.
563 */
564 static
565 long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
566 union ust_args *uargs, void *owner)
567 {
568 struct lttng_session *session = objd_private(objd);
569
570 switch (cmd) {
571 case LTTNG_UST_CHANNEL:
572 return lttng_abi_map_channel(objd,
573 (struct lttng_ust_channel *) arg,
574 uargs, owner);
575 case LTTNG_UST_SESSION_START:
576 case LTTNG_UST_ENABLE:
577 return lttng_session_enable(session);
578 case LTTNG_UST_SESSION_STOP:
579 case LTTNG_UST_DISABLE:
580 return lttng_session_disable(session);
581 case LTTNG_UST_SESSION_STATEDUMP:
582 return lttng_session_statedump(session);
583 default:
584 return -EINVAL;
585 }
586 }
587
588 /*
589 * Called when the last file reference is dropped.
590 *
591 * Big fat note: channels and events are invariant for the whole session after
592 * their creation. So this session destruction also destroys all channel and
593 * event structures specific to this session (they are not destroyed when their
594 * individual file is released).
595 */
596 static
597 int lttng_release_session(int objd)
598 {
599 struct lttng_session *session = objd_private(objd);
600
601 if (session) {
602 lttng_session_destroy(session);
603 return 0;
604 } else {
605 return -EINVAL;
606 }
607 }
608
609 static const struct lttng_ust_objd_ops lttng_session_ops = {
610 .release = lttng_release_session,
611 .cmd = lttng_session_cmd,
612 };
613
614 static
615 long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
616 union ust_args *uargs, void *owner)
617 {
618 struct lttng_ust_tracepoint_list *list = objd_private(objd);
619 struct lttng_ust_tracepoint_iter *tp =
620 (struct lttng_ust_tracepoint_iter *) arg;
621 struct lttng_ust_tracepoint_iter *iter;
622
623 switch (cmd) {
624 case LTTNG_UST_TRACEPOINT_LIST_GET:
625 {
626 iter = lttng_ust_tracepoint_list_get_iter_next(list);
627 if (!iter)
628 return -LTTNG_UST_ERR_NOENT;
629 memcpy(tp, iter, sizeof(*tp));
630 return 0;
631 }
632 default:
633 return -EINVAL;
634 }
635 }
636
637 static
638 int lttng_abi_tracepoint_list(void *owner)
639 {
640 int list_objd, ret;
641 struct lttng_ust_tracepoint_list *list;
642
643 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
644 if (list_objd < 0) {
645 ret = list_objd;
646 goto objd_error;
647 }
648 list = zmalloc(sizeof(*list));
649 if (!list) {
650 ret = -ENOMEM;
651 goto alloc_error;
652 }
653 objd_set_private(list_objd, list);
654
655 /* populate list by walking on all registered probes. */
656 ret = lttng_probes_get_event_list(list);
657 if (ret) {
658 goto list_error;
659 }
660 return list_objd;
661
662 list_error:
663 free(list);
664 alloc_error:
665 {
666 int err;
667
668 err = lttng_ust_objd_unref(list_objd, 1);
669 assert(!err);
670 }
671 objd_error:
672 return ret;
673 }
674
675 static
676 int lttng_release_tracepoint_list(int objd)
677 {
678 struct lttng_ust_tracepoint_list *list = objd_private(objd);
679
680 if (list) {
681 lttng_probes_prune_event_list(list);
682 free(list);
683 return 0;
684 } else {
685 return -EINVAL;
686 }
687 }
688
689 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
690 .release = lttng_release_tracepoint_list,
691 .cmd = lttng_tracepoint_list_cmd,
692 };
693
694 static
695 long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
696 unsigned long arg, union ust_args *uargs, void *owner)
697 {
698 struct lttng_ust_field_list *list = objd_private(objd);
699 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
700 struct lttng_ust_field_iter *iter;
701
702 switch (cmd) {
703 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
704 {
705 iter = lttng_ust_field_list_get_iter_next(list);
706 if (!iter)
707 return -LTTNG_UST_ERR_NOENT;
708 memcpy(tp, iter, sizeof(*tp));
709 return 0;
710 }
711 default:
712 return -EINVAL;
713 }
714 }
715
716 static
717 int lttng_abi_tracepoint_field_list(void *owner)
718 {
719 int list_objd, ret;
720 struct lttng_ust_field_list *list;
721
722 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
723 "tp_field_list");
724 if (list_objd < 0) {
725 ret = list_objd;
726 goto objd_error;
727 }
728 list = zmalloc(sizeof(*list));
729 if (!list) {
730 ret = -ENOMEM;
731 goto alloc_error;
732 }
733 objd_set_private(list_objd, list);
734
735 /* populate list by walking on all registered probes. */
736 ret = lttng_probes_get_field_list(list);
737 if (ret) {
738 goto list_error;
739 }
740 return list_objd;
741
742 list_error:
743 free(list);
744 alloc_error:
745 {
746 int err;
747
748 err = lttng_ust_objd_unref(list_objd, 1);
749 assert(!err);
750 }
751 objd_error:
752 return ret;
753 }
754
755 static
756 int lttng_release_tracepoint_field_list(int objd)
757 {
758 struct lttng_ust_field_list *list = objd_private(objd);
759
760 if (list) {
761 lttng_probes_prune_field_list(list);
762 free(list);
763 return 0;
764 } else {
765 return -EINVAL;
766 }
767 }
768
769 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
770 .release = lttng_release_tracepoint_field_list,
771 .cmd = lttng_tracepoint_field_list_cmd,
772 };
773
774 static
775 int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
776 union ust_args *uargs, void *owner)
777 {
778 struct lttng_channel *channel = objd_private(channel_objd);
779 int ret;
780
781 ret = channel_handle_add_stream(channel->handle,
782 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
783 info->stream_nr, info->len);
784 if (ret)
785 goto error_add_stream;
786
787 return 0;
788
789 error_add_stream:
790 return ret;
791 }
792
793 static
794 int lttng_abi_create_enabler(int channel_objd,
795 struct lttng_ust_event *event_param,
796 void *owner,
797 enum lttng_enabler_type type)
798 {
799 struct lttng_channel *channel = objd_private(channel_objd);
800 struct lttng_enabler *enabler;
801 int event_objd, ret;
802
803 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
804 event_objd = objd_alloc(NULL, &lttng_enabler_ops, owner, "enabler");
805 if (event_objd < 0) {
806 ret = event_objd;
807 goto objd_error;
808 }
809 /*
810 * We tolerate no failure path after event creation. It will stay
811 * invariant for the rest of the session.
812 */
813 enabler = lttng_enabler_create(type, event_param, channel);
814 if (!enabler) {
815 ret = -ENOMEM;
816 goto event_error;
817 }
818 objd_set_private(event_objd, enabler);
819 /* The event holds a reference on the channel */
820 objd_ref(channel_objd);
821 return event_objd;
822
823 event_error:
824 {
825 int err;
826
827 err = lttng_ust_objd_unref(event_objd, 1);
828 assert(!err);
829 }
830 objd_error:
831 return ret;
832 }
833
834 /**
835 * lttng_channel_cmd - lttng control through object descriptors
836 *
837 * @objd: the object descriptor
838 * @cmd: the command
839 * @arg: command arg
840 * @uargs: UST arguments (internal)
841 * @owner: objd owner
842 *
843 * This object descriptor implements lttng commands:
844 * LTTNG_UST_STREAM
845 * Returns an event stream object descriptor or failure.
846 * (typically, one event stream records events from one CPU)
847 * LTTNG_UST_EVENT
848 * Returns an event object descriptor or failure.
849 * LTTNG_UST_CONTEXT
850 * Prepend a context field to each event in the channel
851 * LTTNG_UST_ENABLE
852 * Enable recording for events in this channel (weak enable)
853 * LTTNG_UST_DISABLE
854 * Disable recording for events in this channel (strong disable)
855 *
856 * Channel and event file descriptors also hold a reference on the session.
857 */
858 static
859 long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
860 union ust_args *uargs, void *owner)
861 {
862 struct lttng_channel *channel = objd_private(objd);
863
864 if (cmd != LTTNG_UST_STREAM) {
865 /*
866 * Check if channel received all streams.
867 */
868 if (!lttng_is_channel_ready(channel))
869 return -EPERM;
870 }
871
872 switch (cmd) {
873 case LTTNG_UST_STREAM:
874 {
875 struct lttng_ust_stream *stream;
876
877 stream = (struct lttng_ust_stream *) arg;
878 /* stream used as output */
879 return lttng_abi_map_stream(objd, stream, uargs, owner);
880 }
881 case LTTNG_UST_EVENT:
882 {
883 struct lttng_ust_event *event_param =
884 (struct lttng_ust_event *) arg;
885
886 if (strutils_is_star_glob_pattern(event_param->name)) {
887 /*
888 * If the event name is a star globbing pattern,
889 * we create the special star globbing enabler.
890 */
891 return lttng_abi_create_enabler(objd, event_param,
892 owner, LTTNG_ENABLER_STAR_GLOB);
893 } else {
894 return lttng_abi_create_enabler(objd, event_param,
895 owner, LTTNG_ENABLER_EVENT);
896 }
897 }
898 case LTTNG_UST_CONTEXT:
899 return lttng_abi_add_context(objd,
900 (struct lttng_ust_context *) arg, uargs,
901 &channel->ctx, channel->session);
902 case LTTNG_UST_ENABLE:
903 return lttng_channel_enable(channel);
904 case LTTNG_UST_DISABLE:
905 return lttng_channel_disable(channel);
906 case LTTNG_UST_FLUSH_BUFFER:
907 return channel->ops->flush_buffer(channel->chan, channel->handle);
908 default:
909 return -EINVAL;
910 }
911 }
912
913 static
914 int lttng_channel_release(int objd)
915 {
916 struct lttng_channel *channel = objd_private(objd);
917
918 if (channel)
919 return lttng_ust_objd_unref(channel->session->objd, 0);
920 return 0;
921 }
922
923 static const struct lttng_ust_objd_ops lttng_channel_ops = {
924 .release = lttng_channel_release,
925 .cmd = lttng_channel_cmd,
926 };
927
928 /**
929 * lttng_enabler_cmd - lttng control through object descriptors
930 *
931 * @objd: the object descriptor
932 * @cmd: the command
933 * @arg: command arg
934 * @uargs: UST arguments (internal)
935 * @owner: objd owner
936 *
937 * This object descriptor implements lttng commands:
938 * LTTNG_UST_CONTEXT
939 * Prepend a context field to each record of events of this
940 * enabler.
941 * LTTNG_UST_ENABLE
942 * Enable recording for this enabler
943 * LTTNG_UST_DISABLE
944 * Disable recording for this enabler
945 * LTTNG_UST_FILTER
946 * Attach a filter to an enabler.
947 * LTTNG_UST_EXCLUSION
948 * Attach exclusions to an enabler.
949 */
950 static
951 long lttng_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
952 union ust_args *uargs, void *owner)
953 {
954 struct lttng_enabler *enabler = objd_private(objd);
955
956 switch (cmd) {
957 case LTTNG_UST_CONTEXT:
958 return lttng_enabler_attach_context(enabler,
959 (struct lttng_ust_context *) arg);
960 case LTTNG_UST_ENABLE:
961 return lttng_enabler_enable(enabler);
962 case LTTNG_UST_DISABLE:
963 return lttng_enabler_disable(enabler);
964 case LTTNG_UST_FILTER:
965 {
966 int ret;
967
968 ret = lttng_enabler_attach_bytecode(enabler,
969 (struct lttng_ust_filter_bytecode_node *) arg);
970 if (ret)
971 return ret;
972 return 0;
973 }
974 case LTTNG_UST_EXCLUSION:
975 {
976 return lttng_enabler_attach_exclusion(enabler,
977 (struct lttng_ust_excluder_node *) arg);
978 }
979 default:
980 return -EINVAL;
981 }
982 }
983
984 static
985 int lttng_enabler_release(int objd)
986 {
987 struct lttng_enabler *enabler = objd_private(objd);
988
989 if (enabler)
990 return lttng_ust_objd_unref(enabler->chan->objd, 0);
991 return 0;
992 }
993
994 static const struct lttng_ust_objd_ops lttng_enabler_ops = {
995 .release = lttng_enabler_release,
996 .cmd = lttng_enabler_cmd,
997 };
998
999 void lttng_ust_abi_exit(void)
1000 {
1001 lttng_ust_abi_close_in_progress = 1;
1002 ust_lock_nocheck();
1003 objd_table_destroy();
1004 ust_unlock();
1005 lttng_ust_abi_close_in_progress = 0;
1006 }
This page took 0.047337 seconds and 3 git commands to generate.