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