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