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