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