Fix: refcount issue in lttng-ust-abi.c
[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 uint64_t len;
407 int ret;
408 enum lttng_ust_chan_type type;
409
410 chan_data = uargs->channel.chan_data;
411 len = ust_chan->len;
412 type = ust_chan->type;
413
414 switch (type) {
415 case LTTNG_UST_CHAN_PER_CPU:
416 break;
417 default:
418 return -EINVAL;
419 }
420
421 if (session->been_active) {
422 ret = -EBUSY;
423 goto active; /* Refuse to add channel to active session */
424 }
425
426 channel_handle = channel_handle_create(chan_data, len);
427 if (!channel_handle) {
428 ret = -EINVAL;
429 goto handle_error;
430 }
431
432 chan = shmp(channel_handle, channel_handle->chan);
433 assert(chan);
434 chan->handle = channel_handle;
435 config = &chan->backend.config;
436 lttng_chan = channel_get_private(chan);
437 if (!lttng_chan) {
438 ret = -EINVAL;
439 goto alloc_error;
440 }
441
442 /* Lookup transport name */
443 switch (type) {
444 case LTTNG_UST_CHAN_PER_CPU:
445 if (config->output == RING_BUFFER_MMAP) {
446 transport_name = config->mode == RING_BUFFER_OVERWRITE ?
447 "relay-overwrite-mmap" : "relay-discard-mmap";
448 } else {
449 ret = -EINVAL;
450 goto notransport;
451 }
452 chan_name = "channel";
453 break;
454 default:
455 transport_name = "<unknown>";
456 chan_name = "<unknown>";
457 ret = -EINVAL;
458 goto notransport;
459 }
460 transport = lttng_transport_find(transport_name);
461 if (!transport) {
462 DBG("LTTng transport %s not found\n",
463 transport_name);
464 ret = -EINVAL;
465 goto notransport;
466 }
467
468 chan_objd = objd_alloc(NULL, &lttng_channel_ops, owner, chan_name);
469 if (chan_objd < 0) {
470 ret = chan_objd;
471 goto objd_error;
472 }
473
474 /* Initialize our lttng chan */
475 lttng_chan->chan = chan;
476 lttng_chan->tstate = 1;
477 lttng_chan->enabled = 1;
478 lttng_chan->ctx = NULL;
479 lttng_chan->session = session;
480 lttng_chan->ops = &transport->ops;
481 memcpy(&lttng_chan->chan->backend.config,
482 transport->client_config,
483 sizeof(lttng_chan->chan->backend.config));
484 cds_list_add(&lttng_chan->node, &session->chan_head);
485 lttng_chan->header_type = 0;
486 lttng_chan->handle = channel_handle;
487 lttng_chan->type = type;
488
489 /*
490 * We tolerate no failure path after channel creation. It will stay
491 * invariant for the rest of the session.
492 */
493 objd_set_private(chan_objd, lttng_chan);
494 lttng_chan->objd = chan_objd;
495 /* The channel created holds a reference on the session */
496 objd_ref(session_objd);
497 return chan_objd;
498
499 objd_error:
500 notransport:
501 free(lttng_chan);
502 alloc_error:
503 channel_destroy(chan, channel_handle, 0);
504 handle_error:
505 active:
506 return ret;
507 }
508
509 /**
510 * lttng_session_cmd - lttng session object command
511 *
512 * @obj: the object
513 * @cmd: the command
514 * @arg: command arg
515 * @uargs: UST arguments (internal)
516 * @owner: objd owner
517 *
518 * This descriptor implements lttng commands:
519 * LTTNG_UST_CHANNEL
520 * Returns a LTTng channel object descriptor
521 * LTTNG_UST_ENABLE
522 * Enables tracing for a session (weak enable)
523 * LTTNG_UST_DISABLE
524 * Disables tracing for a session (strong disable)
525 *
526 * The returned channel will be deleted when its file descriptor is closed.
527 */
528 static
529 long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
530 union ust_args *uargs, void *owner)
531 {
532 struct lttng_session *session = objd_private(objd);
533
534 switch (cmd) {
535 case LTTNG_UST_CHANNEL:
536 return lttng_abi_map_channel(objd,
537 (struct lttng_ust_channel *) arg,
538 uargs, owner);
539 case LTTNG_UST_SESSION_START:
540 case LTTNG_UST_ENABLE:
541 return lttng_session_enable(session);
542 case LTTNG_UST_SESSION_STOP:
543 case LTTNG_UST_DISABLE:
544 return lttng_session_disable(session);
545 default:
546 return -EINVAL;
547 }
548 }
549
550 /*
551 * Called when the last file reference is dropped.
552 *
553 * Big fat note: channels and events are invariant for the whole session after
554 * their creation. So this session destruction also destroys all channel and
555 * event structures specific to this session (they are not destroyed when their
556 * individual file is released).
557 */
558 static
559 int lttng_release_session(int objd)
560 {
561 struct lttng_session *session = objd_private(objd);
562
563 if (session) {
564 lttng_session_destroy(session);
565 return 0;
566 } else {
567 return -EINVAL;
568 }
569 }
570
571 static const struct lttng_ust_objd_ops lttng_session_ops = {
572 .release = lttng_release_session,
573 .cmd = lttng_session_cmd,
574 };
575
576 static
577 long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
578 union ust_args *uargs, void *owner)
579 {
580 struct lttng_ust_tracepoint_list *list = objd_private(objd);
581 struct lttng_ust_tracepoint_iter *tp =
582 (struct lttng_ust_tracepoint_iter *) arg;
583 struct lttng_ust_tracepoint_iter *iter;
584
585 switch (cmd) {
586 case LTTNG_UST_TRACEPOINT_LIST_GET:
587 {
588 iter = lttng_ust_tracepoint_list_get_iter_next(list);
589 if (!iter)
590 return -LTTNG_UST_ERR_NOENT;
591 memcpy(tp, iter, sizeof(*tp));
592 return 0;
593 }
594 default:
595 return -EINVAL;
596 }
597 }
598
599 static
600 int lttng_abi_tracepoint_list(void *owner)
601 {
602 int list_objd, ret;
603 struct lttng_ust_tracepoint_list *list;
604
605 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
606 if (list_objd < 0) {
607 ret = list_objd;
608 goto objd_error;
609 }
610 list = zmalloc(sizeof(*list));
611 if (!list) {
612 ret = -ENOMEM;
613 goto alloc_error;
614 }
615 objd_set_private(list_objd, list);
616
617 /* populate list by walking on all registered probes. */
618 ret = lttng_probes_get_event_list(list);
619 if (ret) {
620 goto list_error;
621 }
622 return list_objd;
623
624 list_error:
625 free(list);
626 alloc_error:
627 {
628 int err;
629
630 err = lttng_ust_objd_unref(list_objd, 1);
631 assert(!err);
632 }
633 objd_error:
634 return ret;
635 }
636
637 static
638 int lttng_release_tracepoint_list(int objd)
639 {
640 struct lttng_ust_tracepoint_list *list = objd_private(objd);
641
642 if (list) {
643 lttng_probes_prune_event_list(list);
644 free(list);
645 return 0;
646 } else {
647 return -EINVAL;
648 }
649 }
650
651 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
652 .release = lttng_release_tracepoint_list,
653 .cmd = lttng_tracepoint_list_cmd,
654 };
655
656 static
657 long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
658 unsigned long arg, union ust_args *uargs, void *owner)
659 {
660 struct lttng_ust_field_list *list = objd_private(objd);
661 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
662 struct lttng_ust_field_iter *iter;
663
664 switch (cmd) {
665 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
666 {
667 iter = lttng_ust_field_list_get_iter_next(list);
668 if (!iter)
669 return -LTTNG_UST_ERR_NOENT;
670 memcpy(tp, iter, sizeof(*tp));
671 return 0;
672 }
673 default:
674 return -EINVAL;
675 }
676 }
677
678 static
679 int lttng_abi_tracepoint_field_list(void *owner)
680 {
681 int list_objd, ret;
682 struct lttng_ust_field_list *list;
683
684 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
685 "tp_field_list");
686 if (list_objd < 0) {
687 ret = list_objd;
688 goto objd_error;
689 }
690 list = zmalloc(sizeof(*list));
691 if (!list) {
692 ret = -ENOMEM;
693 goto alloc_error;
694 }
695 objd_set_private(list_objd, list);
696
697 /* populate list by walking on all registered probes. */
698 ret = lttng_probes_get_field_list(list);
699 if (ret) {
700 goto list_error;
701 }
702 return list_objd;
703
704 list_error:
705 free(list);
706 alloc_error:
707 {
708 int err;
709
710 err = lttng_ust_objd_unref(list_objd, 1);
711 assert(!err);
712 }
713 objd_error:
714 return ret;
715 }
716
717 static
718 int lttng_release_tracepoint_field_list(int objd)
719 {
720 struct lttng_ust_field_list *list = objd_private(objd);
721
722 if (list) {
723 lttng_probes_prune_field_list(list);
724 free(list);
725 return 0;
726 } else {
727 return -EINVAL;
728 }
729 }
730
731 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
732 .release = lttng_release_tracepoint_field_list,
733 .cmd = lttng_tracepoint_field_list_cmd,
734 };
735
736 static
737 int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
738 union ust_args *uargs, void *owner)
739 {
740 struct lttng_channel *channel = objd_private(channel_objd);
741 int ret;
742
743 ret = channel_handle_add_stream(channel->handle,
744 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
745 info->stream_nr, info->len);
746 if (ret)
747 goto error_add_stream;
748
749 return 0;
750
751 error_add_stream:
752 return ret;
753 }
754
755 static
756 int lttng_abi_create_enabler(int channel_objd,
757 struct lttng_ust_event *event_param,
758 void *owner,
759 enum lttng_enabler_type type)
760 {
761 struct lttng_channel *channel = objd_private(channel_objd);
762 struct lttng_enabler *enabler;
763 int event_objd, ret;
764
765 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
766 event_objd = objd_alloc(NULL, &lttng_enabler_ops, owner, "enabler");
767 if (event_objd < 0) {
768 ret = event_objd;
769 goto objd_error;
770 }
771 /*
772 * We tolerate no failure path after event creation. It will stay
773 * invariant for the rest of the session.
774 */
775 enabler = lttng_enabler_create(type, event_param, channel);
776 if (!enabler) {
777 ret = -ENOMEM;
778 goto event_error;
779 }
780 objd_set_private(event_objd, enabler);
781 /* The event holds a reference on the channel */
782 objd_ref(channel_objd);
783 return event_objd;
784
785 event_error:
786 {
787 int err;
788
789 err = lttng_ust_objd_unref(event_objd, 1);
790 assert(!err);
791 }
792 objd_error:
793 return ret;
794 }
795
796 /**
797 * lttng_channel_cmd - lttng control through object descriptors
798 *
799 * @objd: the object descriptor
800 * @cmd: the command
801 * @arg: command arg
802 * @uargs: UST arguments (internal)
803 * @owner: objd owner
804 *
805 * This object descriptor implements lttng commands:
806 * LTTNG_UST_STREAM
807 * Returns an event stream object descriptor or failure.
808 * (typically, one event stream records events from one CPU)
809 * LTTNG_UST_EVENT
810 * Returns an event object descriptor or failure.
811 * LTTNG_UST_CONTEXT
812 * Prepend a context field to each event in the channel
813 * LTTNG_UST_ENABLE
814 * Enable recording for events in this channel (weak enable)
815 * LTTNG_UST_DISABLE
816 * Disable recording for events in this channel (strong disable)
817 *
818 * Channel and event file descriptors also hold a reference on the session.
819 */
820 static
821 long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
822 union ust_args *uargs, void *owner)
823 {
824 struct lttng_channel *channel = objd_private(objd);
825
826 if (cmd != LTTNG_UST_STREAM) {
827 /*
828 * Check if channel received all streams.
829 */
830 if (!lttng_is_channel_ready(channel))
831 return -EPERM;
832 }
833
834 switch (cmd) {
835 case LTTNG_UST_STREAM:
836 {
837 struct lttng_ust_stream *stream;
838
839 stream = (struct lttng_ust_stream *) arg;
840 /* stream used as output */
841 return lttng_abi_map_stream(objd, stream, uargs, owner);
842 }
843 case LTTNG_UST_EVENT:
844 {
845 struct lttng_ust_event *event_param =
846 (struct lttng_ust_event *) arg;
847 if (event_param->name[strlen(event_param->name) - 1] == '*') {
848 /* If ends with wildcard, create wildcard. */
849 return lttng_abi_create_enabler(objd, event_param,
850 owner, LTTNG_ENABLER_WILDCARD);
851 } else {
852 return lttng_abi_create_enabler(objd, event_param,
853 owner, LTTNG_ENABLER_EVENT);
854 }
855 }
856 case LTTNG_UST_CONTEXT:
857 return lttng_abi_add_context(objd,
858 (struct lttng_ust_context *) arg,
859 &channel->ctx, channel->session);
860 case LTTNG_UST_ENABLE:
861 return lttng_channel_enable(channel);
862 case LTTNG_UST_DISABLE:
863 return lttng_channel_disable(channel);
864 case LTTNG_UST_FLUSH_BUFFER:
865 return channel->ops->flush_buffer(channel->chan, channel->handle);
866 default:
867 return -EINVAL;
868 }
869 }
870
871 static
872 int lttng_channel_release(int objd)
873 {
874 struct lttng_channel *channel = objd_private(objd);
875
876 if (channel)
877 return lttng_ust_objd_unref(channel->session->objd, 0);
878 return 0;
879 }
880
881 static const struct lttng_ust_objd_ops lttng_channel_ops = {
882 .release = lttng_channel_release,
883 .cmd = lttng_channel_cmd,
884 };
885
886 /**
887 * lttng_enabler_cmd - lttng control through object descriptors
888 *
889 * @objd: the object descriptor
890 * @cmd: the command
891 * @arg: command arg
892 * @uargs: UST arguments (internal)
893 * @owner: objd owner
894 *
895 * This object descriptor implements lttng commands:
896 * LTTNG_UST_CONTEXT
897 * Prepend a context field to each record of events of this
898 * enabler.
899 * LTTNG_UST_ENABLE
900 * Enable recording for this enabler
901 * LTTNG_UST_DISABLE
902 * Disable recording for this enabler
903 * LTTNG_UST_FILTER
904 * Attach a filter to an enabler.
905 */
906 static
907 long lttng_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
908 union ust_args *uargs, void *owner)
909 {
910 struct lttng_enabler *enabler = objd_private(objd);
911
912 switch (cmd) {
913 case LTTNG_UST_CONTEXT:
914 return lttng_enabler_attach_context(enabler,
915 (struct lttng_ust_context *) arg);
916 case LTTNG_UST_ENABLE:
917 return lttng_enabler_enable(enabler);
918 case LTTNG_UST_DISABLE:
919 return lttng_enabler_disable(enabler);
920 case LTTNG_UST_FILTER:
921 {
922 int ret;
923
924 ret = lttng_enabler_attach_bytecode(enabler,
925 (struct lttng_ust_filter_bytecode_node *) arg);
926 if (ret)
927 return ret;
928 return 0;
929 }
930 default:
931 return -EINVAL;
932 }
933 }
934
935 static
936 int lttng_enabler_release(int objd)
937 {
938 struct lttng_enabler *enabler = objd_private(objd);
939
940 if (enabler)
941 return lttng_ust_objd_unref(enabler->chan->objd, 0);
942 return 0;
943 }
944
945 static const struct lttng_ust_objd_ops lttng_enabler_ops = {
946 .release = lttng_enabler_release,
947 .cmd = lttng_enabler_cmd,
948 };
949
950 void lttng_ust_abi_exit(void)
951 {
952 lttng_ust_abi_close_in_progress = 1;
953 objd_table_destroy();
954 lttng_ust_abi_close_in_progress = 0;
955 }
This page took 0.047944 seconds and 5 git commands to generate.