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