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