Fix: liblttng-ust-fork Makefile flags mismatch
[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
52 #define OBJ_NAME_LEN 16
53
54 static int lttng_ust_abi_close_in_progress;
55
56 static
57 int lttng_abi_tracepoint_list(void *owner);
58 static
59 int lttng_abi_tracepoint_field_list(void *owner);
60
61 /*
62 * Object descriptor table. Should be protected from concurrent access
63 * by the caller.
64 */
65
66 struct lttng_ust_obj {
67 union {
68 struct {
69 void *private_data;
70 const struct lttng_ust_objd_ops *ops;
71 int f_count;
72 int owner_ref; /* has ref from owner */
73 void *owner;
74 char name[OBJ_NAME_LEN];
75 } s;
76 int freelist_next; /* offset freelist. end is -1. */
77 } u;
78 };
79
80 struct lttng_ust_objd_table {
81 struct lttng_ust_obj *array;
82 unsigned int len, allocated_len;
83 int freelist_head; /* offset freelist head. end is -1 */
84 };
85
86 static struct lttng_ust_objd_table objd_table = {
87 .freelist_head = -1,
88 };
89
90 static
91 int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops,
92 void *owner, const char *name)
93 {
94 struct lttng_ust_obj *obj;
95
96 if (objd_table.freelist_head != -1) {
97 obj = &objd_table.array[objd_table.freelist_head];
98 objd_table.freelist_head = obj->u.freelist_next;
99 goto end;
100 }
101
102 if (objd_table.len >= objd_table.allocated_len) {
103 unsigned int new_allocated_len, old_allocated_len;
104 struct lttng_ust_obj *new_table, *old_table;
105
106 old_allocated_len = objd_table.allocated_len;
107 old_table = objd_table.array;
108 if (!old_allocated_len)
109 new_allocated_len = 1;
110 else
111 new_allocated_len = old_allocated_len << 1;
112 new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len);
113 if (!new_table)
114 return -ENOMEM;
115 memcpy(new_table, old_table,
116 sizeof(struct lttng_ust_obj) * old_allocated_len);
117 free(old_table);
118 objd_table.array = new_table;
119 objd_table.allocated_len = new_allocated_len;
120 }
121 obj = &objd_table.array[objd_table.len];
122 objd_table.len++;
123 end:
124 obj->u.s.private_data = private_data;
125 obj->u.s.ops = ops;
126 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
127 /* count == 2 : allocated + hold ref */
128 obj->u.s.owner_ref = 1; /* One owner reference */
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, int is_owner)
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 (is_owner) {
200 if (!obj->u.s.owner_ref) {
201 ERR("Error decrementing owner reference");
202 return -EINVAL;
203 }
204 obj->u.s.owner_ref--;
205 }
206 if ((--obj->u.s.f_count) == 1) {
207 const struct lttng_ust_objd_ops *ops = objd_ops(id);
208
209 if (ops->release)
210 ops->release(id);
211 objd_free(id);
212 }
213 return 0;
214 }
215
216 static
217 void objd_table_destroy(void)
218 {
219 int i;
220
221 for (i = 0; i < objd_table.allocated_len; i++) {
222 struct lttng_ust_obj *obj;
223
224 obj = _objd_get(i);
225 if (!obj)
226 continue;
227 if (!obj->u.s.owner_ref)
228 continue; /* only unref owner ref. */
229 (void) lttng_ust_objd_unref(i, 1);
230 }
231 free(objd_table.array);
232 objd_table.array = NULL;
233 objd_table.len = 0;
234 objd_table.allocated_len = 0;
235 objd_table.freelist_head = -1;
236 }
237
238 void lttng_ust_objd_table_owner_cleanup(void *owner)
239 {
240 int i;
241
242 for (i = 0; i < objd_table.allocated_len; i++) {
243 struct lttng_ust_obj *obj;
244
245 obj = _objd_get(i);
246 if (!obj)
247 continue;
248 if (!obj->u.s.owner)
249 continue; /* skip root handles */
250 if (!obj->u.s.owner_ref)
251 continue; /* only unref owner ref. */
252 if (obj->u.s.owner == owner)
253 (void) lttng_ust_objd_unref(i, 1);
254 }
255 }
256
257 /*
258 * This is LTTng's own personal way to create an ABI for sessiond.
259 * We send commands over a socket.
260 */
261
262 static const struct lttng_ust_objd_ops lttng_ops;
263 static const struct lttng_ust_objd_ops lttng_session_ops;
264 static const struct lttng_ust_objd_ops lttng_channel_ops;
265 static const struct lttng_ust_objd_ops lttng_metadata_ops;
266 static const struct lttng_ust_objd_ops lttng_enabler_ops;
267 static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops;
268 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
269 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
270
271 enum channel_type {
272 PER_CPU_CHANNEL,
273 METADATA_CHANNEL,
274 };
275
276 int lttng_abi_create_root_handle(void)
277 {
278 int root_handle;
279
280 /* root handles have NULL owners */
281 root_handle = objd_alloc(NULL, &lttng_ops, NULL, "root");
282 return root_handle;
283 }
284
285 static
286 int lttng_abi_create_session(void *owner)
287 {
288 struct lttng_session *session;
289 int session_objd, ret;
290
291 session = lttng_session_create();
292 if (!session)
293 return -ENOMEM;
294 session_objd = objd_alloc(session, &lttng_session_ops, owner, "session");
295 if (session_objd < 0) {
296 ret = session_objd;
297 goto objd_error;
298 }
299 session->objd = session_objd;
300 return session_objd;
301
302 objd_error:
303 lttng_session_destroy(session);
304 return ret;
305 }
306
307 static
308 long lttng_abi_tracer_version(int objd,
309 struct lttng_ust_tracer_version *v)
310 {
311 v->major = LTTNG_UST_INTERNAL_MAJOR_VERSION;
312 v->minor = LTTNG_UST_INTERNAL_MINOR_VERSION;
313 v->patchlevel = LTTNG_UST_INTERNAL_PATCHLEVEL_VERSION;
314 return 0;
315 }
316
317 static
318 long lttng_abi_add_context(int objd,
319 struct lttng_ust_context *context_param,
320 struct lttng_ctx **ctx, struct lttng_session *session)
321 {
322 return lttng_attach_context(context_param, ctx, session);
323 }
324
325 /**
326 * lttng_cmd - lttng control through socket commands
327 *
328 * @objd: the object descriptor
329 * @cmd: the command
330 * @arg: command arg
331 * @uargs: UST arguments (internal)
332 * @owner: objd owner
333 *
334 * This descriptor implements lttng commands:
335 * LTTNG_UST_SESSION
336 * Returns a LTTng trace session object descriptor
337 * LTTNG_UST_TRACER_VERSION
338 * Returns the LTTng kernel tracer version
339 * LTTNG_UST_TRACEPOINT_LIST
340 * Returns a file descriptor listing available tracepoints
341 * LTTNG_UST_TRACEPOINT_FIELD_LIST
342 * Returns a file descriptor listing available tracepoint fields
343 * LTTNG_UST_WAIT_QUIESCENT
344 * Returns after all previously running probes have completed
345 *
346 * The returned session will be deleted when its file descriptor is closed.
347 */
348 static
349 long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
350 union ust_args *uargs, void *owner)
351 {
352 switch (cmd) {
353 case LTTNG_UST_SESSION:
354 return lttng_abi_create_session(owner);
355 case LTTNG_UST_TRACER_VERSION:
356 return lttng_abi_tracer_version(objd,
357 (struct lttng_ust_tracer_version *) arg);
358 case LTTNG_UST_TRACEPOINT_LIST:
359 return lttng_abi_tracepoint_list(owner);
360 case LTTNG_UST_TRACEPOINT_FIELD_LIST:
361 return lttng_abi_tracepoint_field_list(owner);
362 case LTTNG_UST_WAIT_QUIESCENT:
363 synchronize_trace();
364 return 0;
365 default:
366 return -EINVAL;
367 }
368 }
369
370 static const struct lttng_ust_objd_ops lttng_ops = {
371 .cmd = lttng_cmd,
372 };
373
374 /*
375 * We tolerate no failure in this function (if one happens, we print a dmesg
376 * error, but cannot return any error, because the channel information is
377 * invariant.
378 */
379 static
380 void lttng_metadata_create_events(int channel_objd)
381 {
382 struct lttng_channel *chan = objd_private(channel_objd);
383 struct lttng_enabler *enabler;
384 static struct lttng_ust_event metadata_params = {
385 .instrumentation = LTTNG_UST_TRACEPOINT,
386 .name = "lttng_ust:metadata",
387 .loglevel_type = LTTNG_UST_LOGLEVEL_ALL,
388 .loglevel = TRACE_DEFAULT,
389 };
390
391 /*
392 * We tolerate no failure path after event creation. It will stay
393 * invariant for the rest of the session.
394 */
395 enabler = lttng_enabler_create(LTTNG_ENABLER_EVENT,
396 &metadata_params, chan);
397 if (!enabler) {
398 goto create_error;
399 }
400 return;
401
402 create_error:
403 WARN_ON(1);
404 return; /* not allowed to return error */
405 }
406
407 int lttng_abi_create_channel(int session_objd,
408 struct lttng_ust_channel *chan_param,
409 enum channel_type channel_type,
410 union ust_args *uargs,
411 void *owner)
412 {
413 struct lttng_session *session = objd_private(session_objd);
414 const struct lttng_ust_objd_ops *ops;
415 const char *transport_name;
416 struct lttng_channel *chan;
417 int chan_objd;
418 int ret = 0;
419 struct lttng_channel chan_priv_init;
420
421 switch (channel_type) {
422 case PER_CPU_CHANNEL:
423 if (chan_param->output == LTTNG_UST_MMAP) {
424 transport_name = chan_param->overwrite ?
425 "relay-overwrite-mmap" : "relay-discard-mmap";
426 } else {
427 return -EINVAL;
428 }
429 ops = &lttng_channel_ops;
430 break;
431 case METADATA_CHANNEL:
432 if (chan_param->output == LTTNG_UST_MMAP)
433 transport_name = "relay-metadata-mmap";
434 else
435 return -EINVAL;
436 ops = &lttng_metadata_ops;
437 break;
438 default:
439 transport_name = "<unknown>";
440 return -EINVAL;
441 }
442 chan_objd = objd_alloc(NULL, ops, owner, "channel");
443 if (chan_objd < 0) {
444 ret = chan_objd;
445 goto objd_error;
446 }
447 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
448 /* Copy of session UUID for consumer (availability through shm) */
449 memcpy(chan_priv_init.uuid, session->uuid, sizeof(session->uuid));
450
451 /*
452 * We tolerate no failure path after channel creation. It will stay
453 * invariant for the rest of the session.
454 */
455 chan = lttng_channel_create(session, transport_name, NULL,
456 chan_param->subbuf_size,
457 chan_param->num_subbuf,
458 chan_param->switch_timer_interval,
459 chan_param->read_timer_interval,
460 &uargs->channel.shm_fd,
461 &uargs->channel.wait_fd,
462 &uargs->channel.memory_map_size,
463 &chan_priv_init);
464 if (!chan) {
465 ret = -EINVAL;
466 goto chan_error;
467 }
468 objd_set_private(chan_objd, chan);
469 chan->objd = chan_objd;
470 if (channel_type == METADATA_CHANNEL) {
471 session->metadata = chan;
472 lttng_metadata_create_events(chan_objd);
473 }
474 /* The channel created holds a reference on the session */
475 objd_ref(session_objd);
476
477 return chan_objd;
478
479 chan_error:
480 {
481 int err;
482
483 err = lttng_ust_objd_unref(chan_objd, 1);
484 assert(!err);
485 }
486 objd_error:
487 return ret;
488 }
489
490 /**
491 * lttng_session_cmd - lttng session object command
492 *
493 * @obj: the object
494 * @cmd: the command
495 * @arg: command arg
496 * @uargs: UST arguments (internal)
497 * @owner: objd owner
498 *
499 * This descriptor implements lttng commands:
500 * LTTNG_UST_CHANNEL
501 * Returns a LTTng channel object descriptor
502 * LTTNG_UST_ENABLE
503 * Enables tracing for a session (weak enable)
504 * LTTNG_UST_DISABLE
505 * Disables tracing for a session (strong disable)
506 * LTTNG_UST_METADATA
507 * Returns a LTTng metadata object descriptor
508 *
509 * The returned channel will be deleted when its file descriptor is closed.
510 */
511 static
512 long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
513 union ust_args *uargs, void *owner)
514 {
515 struct lttng_session *session = objd_private(objd);
516
517 switch (cmd) {
518 case LTTNG_UST_CHANNEL:
519 return lttng_abi_create_channel(objd,
520 (struct lttng_ust_channel *) arg,
521 PER_CPU_CHANNEL, uargs, owner);
522 case LTTNG_UST_SESSION_START:
523 case LTTNG_UST_ENABLE:
524 return lttng_session_enable(session);
525 case LTTNG_UST_SESSION_STOP:
526 case LTTNG_UST_DISABLE:
527 return lttng_session_disable(session);
528 case LTTNG_UST_METADATA:
529 return lttng_abi_create_channel(objd,
530 (struct lttng_ust_channel *) arg,
531 METADATA_CHANNEL, uargs, owner);
532 default:
533 return -EINVAL;
534 }
535 }
536
537 /*
538 * Called when the last file reference is dropped.
539 *
540 * Big fat note: channels and events are invariant for the whole session after
541 * their creation. So this session destruction also destroys all channel and
542 * event structures specific to this session (they are not destroyed when their
543 * individual file is released).
544 */
545 static
546 int lttng_release_session(int objd)
547 {
548 struct lttng_session *session = objd_private(objd);
549
550 if (session) {
551 lttng_session_destroy(session);
552 return 0;
553 } else {
554 return -EINVAL;
555 }
556 }
557
558 static const struct lttng_ust_objd_ops lttng_session_ops = {
559 .release = lttng_release_session,
560 .cmd = lttng_session_cmd,
561 };
562
563 static
564 long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
565 union ust_args *uargs, void *owner)
566 {
567 struct lttng_ust_tracepoint_list *list = objd_private(objd);
568 struct lttng_ust_tracepoint_iter *tp =
569 (struct lttng_ust_tracepoint_iter *) arg;
570 struct lttng_ust_tracepoint_iter *iter;
571
572 switch (cmd) {
573 case LTTNG_UST_TRACEPOINT_LIST_GET:
574 {
575 retry:
576 iter = lttng_ust_tracepoint_list_get_iter_next(list);
577 if (!iter)
578 return -LTTNG_UST_ERR_NOENT;
579 if (!strcmp(iter->name, "lttng_ust:metadata"))
580 goto retry;
581 memcpy(tp, iter, sizeof(*tp));
582 return 0;
583 }
584 default:
585 return -EINVAL;
586 }
587 }
588
589 static
590 int lttng_abi_tracepoint_list(void *owner)
591 {
592 int list_objd, ret;
593 struct lttng_ust_tracepoint_list *list;
594
595 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
596 if (list_objd < 0) {
597 ret = list_objd;
598 goto objd_error;
599 }
600 list = zmalloc(sizeof(*list));
601 if (!list) {
602 ret = -ENOMEM;
603 goto alloc_error;
604 }
605 objd_set_private(list_objd, list);
606
607 /* populate list by walking on all registered probes. */
608 ret = lttng_probes_get_event_list(list);
609 if (ret) {
610 goto list_error;
611 }
612 return list_objd;
613
614 list_error:
615 free(list);
616 alloc_error:
617 {
618 int err;
619
620 err = lttng_ust_objd_unref(list_objd, 1);
621 assert(!err);
622 }
623 objd_error:
624 return ret;
625 }
626
627 static
628 int lttng_release_tracepoint_list(int objd)
629 {
630 struct lttng_ust_tracepoint_list *list = objd_private(objd);
631
632 if (list) {
633 lttng_probes_prune_event_list(list);
634 free(list);
635 return 0;
636 } else {
637 return -EINVAL;
638 }
639 }
640
641 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
642 .release = lttng_release_tracepoint_list,
643 .cmd = lttng_tracepoint_list_cmd,
644 };
645
646 static
647 long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
648 unsigned long arg, union ust_args *uargs, void *owner)
649 {
650 struct lttng_ust_field_list *list = objd_private(objd);
651 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
652 struct lttng_ust_field_iter *iter;
653
654 switch (cmd) {
655 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
656 {
657 retry:
658 iter = lttng_ust_field_list_get_iter_next(list);
659 if (!iter)
660 return -LTTNG_UST_ERR_NOENT;
661 if (!strcmp(iter->event_name, "lttng_ust:metadata"))
662 goto retry;
663 memcpy(tp, iter, sizeof(*tp));
664 return 0;
665 }
666 default:
667 return -EINVAL;
668 }
669 }
670
671 static
672 int lttng_abi_tracepoint_field_list(void *owner)
673 {
674 int list_objd, ret;
675 struct lttng_ust_field_list *list;
676
677 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
678 "tp_field_list");
679 if (list_objd < 0) {
680 ret = list_objd;
681 goto objd_error;
682 }
683 list = zmalloc(sizeof(*list));
684 if (!list) {
685 ret = -ENOMEM;
686 goto alloc_error;
687 }
688 objd_set_private(list_objd, list);
689
690 /* populate list by walking on all registered probes. */
691 ret = lttng_probes_get_field_list(list);
692 if (ret) {
693 goto list_error;
694 }
695 return list_objd;
696
697 list_error:
698 free(list);
699 alloc_error:
700 {
701 int err;
702
703 err = lttng_ust_objd_unref(list_objd, 1);
704 assert(!err);
705 }
706 objd_error:
707 return ret;
708 }
709
710 static
711 int lttng_release_tracepoint_field_list(int objd)
712 {
713 struct lttng_ust_field_list *list = objd_private(objd);
714
715 if (list) {
716 lttng_probes_prune_field_list(list);
717 free(list);
718 return 0;
719 } else {
720 return -EINVAL;
721 }
722 }
723
724 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
725 .release = lttng_release_tracepoint_field_list,
726 .cmd = lttng_tracepoint_field_list_cmd,
727 };
728
729 struct stream_priv_data {
730 struct lttng_ust_lib_ring_buffer *buf;
731 struct lttng_channel *lttng_chan;
732 };
733
734 static
735 int lttng_abi_open_stream(int channel_objd, struct lttng_ust_stream *info,
736 union ust_args *uargs, void *owner)
737 {
738 struct lttng_channel *channel = objd_private(channel_objd);
739 struct lttng_ust_lib_ring_buffer *buf;
740 struct stream_priv_data *priv;
741 int stream_objd, ret;
742
743 buf = channel->ops->buffer_read_open(channel->chan, channel->handle,
744 &uargs->stream.shm_fd,
745 &uargs->stream.wait_fd,
746 &uargs->stream.memory_map_size);
747 if (!buf)
748 return -ENOENT;
749
750 priv = zmalloc(sizeof(*priv));
751 if (!priv) {
752 ret = -ENOMEM;
753 goto alloc_error;
754 }
755 priv->buf = buf;
756 priv->lttng_chan = channel;
757 stream_objd = objd_alloc(priv, &lib_ring_buffer_objd_ops, owner, "open_stream");
758 if (stream_objd < 0) {
759 ret = stream_objd;
760 goto objd_error;
761 }
762 /* Hold a reference on the channel object descriptor */
763 objd_ref(channel_objd);
764 return stream_objd;
765
766 objd_error:
767 free(priv);
768 alloc_error:
769 channel->ops->buffer_read_close(buf, channel->handle);
770 return ret;
771 }
772
773 static
774 int lttng_abi_create_enabler(int channel_objd,
775 struct lttng_ust_event *event_param,
776 void *owner,
777 enum lttng_enabler_type type)
778 {
779 struct lttng_channel *channel = objd_private(channel_objd);
780 struct lttng_enabler *enabler;
781 int event_objd, ret;
782
783 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
784 event_objd = objd_alloc(NULL, &lttng_enabler_ops, owner, "enabler");
785 if (event_objd < 0) {
786 ret = event_objd;
787 goto objd_error;
788 }
789 /*
790 * We tolerate no failure path after event creation. It will stay
791 * invariant for the rest of the session.
792 */
793 enabler = lttng_enabler_create(type, event_param, channel);
794 if (!enabler) {
795 ret = -ENOMEM;
796 goto event_error;
797 }
798 objd_set_private(event_objd, enabler);
799 /* The event holds a reference on the channel */
800 objd_ref(channel_objd);
801 return event_objd;
802
803 event_error:
804 {
805 int err;
806
807 err = lttng_ust_objd_unref(event_objd, 1);
808 assert(!err);
809 }
810 objd_error:
811 return ret;
812 }
813
814 /**
815 * lttng_channel_cmd - lttng control through object descriptors
816 *
817 * @objd: the object descriptor
818 * @cmd: the command
819 * @arg: command arg
820 * @uargs: UST arguments (internal)
821 * @owner: objd owner
822 *
823 * This object descriptor implements lttng commands:
824 * LTTNG_UST_STREAM
825 * Returns an event stream object descriptor or failure.
826 * (typically, one event stream records events from one CPU)
827 * LTTNG_UST_EVENT
828 * Returns an event object descriptor or failure.
829 * LTTNG_UST_CONTEXT
830 * Prepend a context field to each event in the channel
831 * LTTNG_UST_ENABLE
832 * Enable recording for events in this channel (weak enable)
833 * LTTNG_UST_DISABLE
834 * Disable recording for events in this channel (strong disable)
835 *
836 * Channel and event file descriptors also hold a reference on the session.
837 */
838 static
839 long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
840 union ust_args *uargs, void *owner)
841 {
842 struct lttng_channel *channel = objd_private(objd);
843
844 switch (cmd) {
845 case LTTNG_UST_STREAM:
846 {
847 struct lttng_ust_stream *stream;
848
849 stream = (struct lttng_ust_stream *) arg;
850 /* stream used as output */
851 return lttng_abi_open_stream(objd, stream, uargs, owner);
852 }
853 case LTTNG_UST_EVENT:
854 {
855 struct lttng_ust_event *event_param =
856 (struct lttng_ust_event *) arg;
857 if (event_param->name[strlen(event_param->name) - 1] == '*') {
858 /* If ends with wildcard, create wildcard. */
859 return lttng_abi_create_enabler(objd, event_param,
860 owner, LTTNG_ENABLER_WILDCARD);
861 } else {
862 return lttng_abi_create_enabler(objd, event_param,
863 owner, LTTNG_ENABLER_EVENT);
864 }
865 }
866 case LTTNG_UST_CONTEXT:
867 return lttng_abi_add_context(objd,
868 (struct lttng_ust_context *) arg,
869 &channel->ctx, channel->session);
870 case LTTNG_UST_ENABLE:
871 return lttng_channel_enable(channel);
872 case LTTNG_UST_DISABLE:
873 return lttng_channel_disable(channel);
874 case LTTNG_UST_FLUSH_BUFFER:
875 return channel->ops->flush_buffer(channel->chan, channel->handle);
876 default:
877 return -EINVAL;
878 }
879 }
880
881 /**
882 * lttng_metadata_cmd - lttng control through object descriptors
883 *
884 * @objd: the object descriptor
885 * @cmd: the command
886 * @arg: command arg
887 * @uargs: UST arguments (internal)
888 * @owner: objd owner
889 *
890 * This object descriptor implements lttng commands:
891 * LTTNG_UST_STREAM
892 * Returns an event stream file descriptor or failure.
893 *
894 * Channel and event file descriptors also hold a reference on the session.
895 */
896 static
897 long lttng_metadata_cmd(int objd, unsigned int cmd, unsigned long arg,
898 union ust_args *uargs, void *owner)
899 {
900 struct lttng_channel *channel = objd_private(objd);
901
902 switch (cmd) {
903 case LTTNG_UST_STREAM:
904 {
905 struct lttng_ust_stream *stream;
906
907 stream = (struct lttng_ust_stream *) arg;
908 /* stream used as output */
909 return lttng_abi_open_stream(objd, stream, uargs, owner);
910 }
911 case LTTNG_UST_FLUSH_BUFFER:
912 return channel->ops->flush_buffer(channel->chan, channel->handle);
913 default:
914 return -EINVAL;
915 }
916 }
917
918 static
919 int lttng_channel_release(int objd)
920 {
921 struct lttng_channel *channel = objd_private(objd);
922
923 if (channel)
924 return lttng_ust_objd_unref(channel->session->objd, 0);
925 return 0;
926 }
927
928 static const struct lttng_ust_objd_ops lttng_channel_ops = {
929 .release = lttng_channel_release,
930 .cmd = lttng_channel_cmd,
931 };
932
933 static const struct lttng_ust_objd_ops lttng_metadata_ops = {
934 .release = lttng_channel_release,
935 .cmd = lttng_metadata_cmd,
936 };
937
938 /**
939 * lttng_rb_cmd - lttng ring buffer control through object descriptors
940 *
941 * @objd: the object descriptor
942 * @cmd: the command
943 * @arg: command arg
944 * @uargs: UST arguments (internal)
945 * @owner: objd owner
946 *
947 * This object descriptor implements lttng commands:
948 * (None for now. Access is done directly though shm.)
949 */
950 static
951 long lttng_rb_cmd(int objd, unsigned int cmd, unsigned long arg,
952 union ust_args *uargs, void *owner)
953 {
954 switch (cmd) {
955 default:
956 return -EINVAL;
957 }
958 }
959
960 static
961 int lttng_rb_release(int objd)
962 {
963 struct stream_priv_data *priv = objd_private(objd);
964 struct lttng_ust_lib_ring_buffer *buf;
965 struct lttng_channel *channel;
966
967 if (priv) {
968 buf = priv->buf;
969 channel = priv->lttng_chan;
970 free(priv);
971 /*
972 * If we are at ABI exit, we don't want to close the
973 * buffer opened for read: it is being shared between
974 * the parent and child (right after fork), and we don't
975 * want the child to close it for the parent. For a real
976 * exit, we don't care about marking it as closed, as
977 * the consumer daemon (if there is one) will do fine
978 * even if we don't mark it as "closed" for reading on
979 * our side.
980 * We only mark it as closed if it is being explicitely
981 * released by the session daemon with an explicit
982 * release command.
983 */
984 if (!lttng_ust_abi_close_in_progress)
985 channel->ops->buffer_read_close(buf, channel->handle);
986
987 return lttng_ust_objd_unref(channel->objd, 0);
988 }
989 return 0;
990 }
991
992 static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops = {
993 .release = lttng_rb_release,
994 .cmd = lttng_rb_cmd,
995 };
996
997 /**
998 * lttng_enabler_cmd - lttng control through object descriptors
999 *
1000 * @objd: the object descriptor
1001 * @cmd: the command
1002 * @arg: command arg
1003 * @uargs: UST arguments (internal)
1004 * @owner: objd owner
1005 *
1006 * This object descriptor implements lttng commands:
1007 * LTTNG_UST_CONTEXT
1008 * Prepend a context field to each record of events of this
1009 * enabler.
1010 * LTTNG_UST_ENABLE
1011 * Enable recording for this enabler
1012 * LTTNG_UST_DISABLE
1013 * Disable recording for this enabler
1014 * LTTNG_UST_FILTER
1015 * Attach a filter to an enabler.
1016 */
1017 static
1018 long lttng_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
1019 union ust_args *uargs, void *owner)
1020 {
1021 struct lttng_enabler *enabler = objd_private(objd);
1022
1023 switch (cmd) {
1024 case LTTNG_UST_CONTEXT:
1025 return lttng_enabler_attach_context(enabler,
1026 (struct lttng_ust_context *) arg);
1027 case LTTNG_UST_ENABLE:
1028 return lttng_enabler_enable(enabler);
1029 case LTTNG_UST_DISABLE:
1030 return lttng_enabler_disable(enabler);
1031 case LTTNG_UST_FILTER:
1032 {
1033 int ret;
1034
1035 ret = lttng_enabler_attach_bytecode(enabler,
1036 (struct lttng_ust_filter_bytecode_node *) arg);
1037 if (ret)
1038 return ret;
1039 return 0;
1040 }
1041 default:
1042 return -EINVAL;
1043 }
1044 }
1045
1046 static
1047 int lttng_enabler_release(int objd)
1048 {
1049 struct lttng_enabler *enabler = objd_private(objd);
1050
1051 if (enabler)
1052 return lttng_ust_objd_unref(enabler->chan->objd, 0);
1053 return 0;
1054 }
1055
1056 static const struct lttng_ust_objd_ops lttng_enabler_ops = {
1057 .release = lttng_enabler_release,
1058 .cmd = lttng_enabler_cmd,
1059 };
1060
1061 void lttng_ust_abi_exit(void)
1062 {
1063 lttng_ust_abi_close_in_progress = 1;
1064 objd_table_destroy();
1065 lttng_ust_abi_close_in_progress = 0;
1066 }
This page took 0.051258 seconds and 4 git commands to generate.