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