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