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