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