Fix: Fix self-assign warning on struct ustfork_clone_info init
[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_INTERNAL_MAJOR_VERSION;
267 v->minor = LTTNG_UST_INTERNAL_MINOR_VERSION;
268 v->patchlevel = LTTNG_UST_INTERNAL_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 case LTTNG_UST_CONTEXT_IP:
290 return lttng_add_ip_to_ctx(ctx);
291 default:
292 return -EINVAL;
293 }
294 }
295
296 /**
297 * lttng_cmd - lttng control through socket commands
298 *
299 * @objd: the object descriptor
300 * @cmd: the command
301 * @arg: command arg
302 * @uargs: UST arguments (internal)
303 *
304 * This descriptor implements lttng commands:
305 * LTTNG_UST_SESSION
306 * Returns a LTTng trace session object descriptor
307 * LTTNG_UST_TRACER_VERSION
308 * Returns the LTTng kernel tracer version
309 * LTTNG_UST_TRACEPOINT_LIST
310 * Returns a file descriptor listing available tracepoints
311 * LTTNG_UST_TRACEPOINT_FIELD_LIST
312 * Returns a file descriptor listing available tracepoint fields
313 * LTTNG_UST_WAIT_QUIESCENT
314 * Returns after all previously running probes have completed
315 *
316 * The returned session will be deleted when its file descriptor is closed.
317 */
318 static
319 long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
320 union ust_args *uargs)
321 {
322 switch (cmd) {
323 case LTTNG_UST_SESSION:
324 return lttng_abi_create_session();
325 case LTTNG_UST_TRACER_VERSION:
326 return lttng_abi_tracer_version(objd,
327 (struct lttng_ust_tracer_version *) arg);
328 case LTTNG_UST_TRACEPOINT_LIST:
329 return lttng_abi_tracepoint_list();
330 case LTTNG_UST_TRACEPOINT_FIELD_LIST:
331 return lttng_abi_tracepoint_field_list();
332 case LTTNG_UST_WAIT_QUIESCENT:
333 synchronize_trace();
334 return 0;
335 default:
336 return -EINVAL;
337 }
338 }
339
340 static const struct lttng_ust_objd_ops lttng_ops = {
341 .cmd = lttng_cmd,
342 };
343
344 /*
345 * We tolerate no failure in this function (if one happens, we print a dmesg
346 * error, but cannot return any error, because the channel information is
347 * invariant.
348 */
349 static
350 void lttng_metadata_create_events(int channel_objd)
351 {
352 struct ltt_channel *channel = objd_private(channel_objd);
353 static struct lttng_ust_event metadata_params = {
354 .instrumentation = LTTNG_UST_TRACEPOINT,
355 .name = "lttng_ust:metadata",
356 .loglevel_type = LTTNG_UST_LOGLEVEL_ALL,
357 .loglevel = TRACE_DEFAULT,
358 };
359 struct ltt_event *event;
360 int ret;
361
362 /*
363 * We tolerate no failure path after event creation. It will stay
364 * invariant for the rest of the session.
365 */
366 ret = ltt_event_create(channel, &metadata_params, &event);
367 if (ret < 0) {
368 goto create_error;
369 }
370 return;
371
372 create_error:
373 WARN_ON(1);
374 return; /* not allowed to return error */
375 }
376
377 int lttng_abi_create_channel(int session_objd,
378 struct lttng_ust_channel *chan_param,
379 enum channel_type channel_type,
380 union ust_args *uargs)
381 {
382 struct ltt_session *session = objd_private(session_objd);
383 const struct lttng_ust_objd_ops *ops;
384 const char *transport_name;
385 struct ltt_channel *chan;
386 int chan_objd;
387 int ret = 0;
388 struct ltt_channel chan_priv_init;
389
390 switch (channel_type) {
391 case PER_CPU_CHANNEL:
392 if (chan_param->output == LTTNG_UST_MMAP) {
393 transport_name = chan_param->overwrite ?
394 "relay-overwrite-mmap" : "relay-discard-mmap";
395 } else {
396 return -EINVAL;
397 }
398 ops = &lttng_channel_ops;
399 break;
400 case METADATA_CHANNEL:
401 if (chan_param->output == LTTNG_UST_MMAP)
402 transport_name = "relay-metadata-mmap";
403 else
404 return -EINVAL;
405 ops = &lttng_metadata_ops;
406 break;
407 default:
408 transport_name = "<unknown>";
409 return -EINVAL;
410 }
411 chan_objd = objd_alloc(NULL, ops);
412 if (chan_objd < 0) {
413 ret = chan_objd;
414 goto objd_error;
415 }
416 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
417 /* Copy of session UUID for consumer (availability through shm) */
418 memcpy(chan_priv_init.uuid, session->uuid, sizeof(session->uuid));
419
420 /*
421 * We tolerate no failure path after channel creation. It will stay
422 * invariant for the rest of the session.
423 */
424 chan = ltt_channel_create(session, transport_name, NULL,
425 chan_param->subbuf_size,
426 chan_param->num_subbuf,
427 chan_param->switch_timer_interval,
428 chan_param->read_timer_interval,
429 &uargs->channel.shm_fd,
430 &uargs->channel.wait_fd,
431 &uargs->channel.memory_map_size,
432 &chan_priv_init);
433 if (!chan) {
434 ret = -EINVAL;
435 goto chan_error;
436 }
437 objd_set_private(chan_objd, chan);
438 chan->objd = chan_objd;
439 if (channel_type == METADATA_CHANNEL) {
440 session->metadata = chan;
441 lttng_metadata_create_events(chan_objd);
442 }
443 /* The channel created holds a reference on the session */
444 objd_ref(session_objd);
445
446 return chan_objd;
447
448 chan_error:
449 {
450 int err;
451
452 err = lttng_ust_objd_unref(chan_objd);
453 assert(!err);
454 }
455 objd_error:
456 return ret;
457 }
458
459 /**
460 * lttng_session_cmd - lttng session object command
461 *
462 * @obj: the object
463 * @cmd: the command
464 * @arg: command arg
465 * @uargs: UST arguments (internal)
466 *
467 * This descriptor implements lttng commands:
468 * LTTNG_UST_CHANNEL
469 * Returns a LTTng channel object descriptor
470 * LTTNG_UST_ENABLE
471 * Enables tracing for a session (weak enable)
472 * LTTNG_UST_DISABLE
473 * Disables tracing for a session (strong disable)
474 * LTTNG_UST_METADATA
475 * Returns a LTTng metadata object descriptor
476 *
477 * The returned channel will be deleted when its file descriptor is closed.
478 */
479 static
480 long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
481 union ust_args *uargs)
482 {
483 struct ltt_session *session = objd_private(objd);
484
485 switch (cmd) {
486 case LTTNG_UST_CHANNEL:
487 return lttng_abi_create_channel(objd,
488 (struct lttng_ust_channel *) arg,
489 PER_CPU_CHANNEL, uargs);
490 case LTTNG_UST_SESSION_START:
491 case LTTNG_UST_ENABLE:
492 return ltt_session_enable(session);
493 case LTTNG_UST_SESSION_STOP:
494 case LTTNG_UST_DISABLE:
495 return ltt_session_disable(session);
496 case LTTNG_UST_METADATA:
497 return lttng_abi_create_channel(objd,
498 (struct lttng_ust_channel *) arg,
499 METADATA_CHANNEL, uargs);
500 default:
501 return -EINVAL;
502 }
503 }
504
505 /*
506 * Called when the last file reference is dropped.
507 *
508 * Big fat note: channels and events are invariant for the whole session after
509 * their creation. So this session destruction also destroys all channel and
510 * event structures specific to this session (they are not destroyed when their
511 * individual file is released).
512 */
513 static
514 int lttng_release_session(int objd)
515 {
516 struct ltt_session *session = objd_private(objd);
517
518 if (session) {
519 ltt_session_destroy(session);
520 return 0;
521 } else {
522 return -EINVAL;
523 }
524 }
525
526 static const struct lttng_ust_objd_ops lttng_session_ops = {
527 .release = lttng_release_session,
528 .cmd = lttng_session_cmd,
529 };
530
531 static
532 long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
533 union ust_args *uargs)
534 {
535 struct lttng_ust_tracepoint_list *list = objd_private(objd);
536 struct lttng_ust_tracepoint_iter *tp =
537 (struct lttng_ust_tracepoint_iter *) arg;
538 struct lttng_ust_tracepoint_iter *iter;
539
540 switch (cmd) {
541 case LTTNG_UST_TRACEPOINT_LIST_GET:
542 {
543 retry:
544 iter = lttng_ust_tracepoint_list_get_iter_next(list);
545 if (!iter)
546 return -ENOENT;
547 if (!strcmp(iter->name, "lttng_ust:metadata"))
548 goto retry;
549 memcpy(tp, iter, sizeof(*tp));
550 return 0;
551 }
552 default:
553 return -EINVAL;
554 }
555 }
556
557 static
558 int lttng_abi_tracepoint_list(void)
559 {
560 int list_objd, ret;
561 struct lttng_ust_tracepoint_list *list;
562
563 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops);
564 if (list_objd < 0) {
565 ret = list_objd;
566 goto objd_error;
567 }
568 list = zmalloc(sizeof(*list));
569 if (!list) {
570 ret = -ENOMEM;
571 goto alloc_error;
572 }
573 objd_set_private(list_objd, list);
574
575 /* populate list by walking on all registered probes. */
576 ret = ltt_probes_get_event_list(list);
577 if (ret) {
578 goto list_error;
579 }
580 return list_objd;
581
582 list_error:
583 free(list);
584 alloc_error:
585 {
586 int err;
587
588 err = lttng_ust_objd_unref(list_objd);
589 assert(!err);
590 }
591 objd_error:
592 return ret;
593 }
594
595 static
596 int lttng_release_tracepoint_list(int objd)
597 {
598 struct lttng_ust_tracepoint_list *list = objd_private(objd);
599
600 if (list) {
601 ltt_probes_prune_event_list(list);
602 free(list);
603 return 0;
604 } else {
605 return -EINVAL;
606 }
607 }
608
609 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
610 .release = lttng_release_tracepoint_list,
611 .cmd = lttng_tracepoint_list_cmd,
612 };
613
614 static
615 long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
616 unsigned long arg, union ust_args *uargs)
617 {
618 struct lttng_ust_field_list *list = objd_private(objd);
619 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
620 struct lttng_ust_field_iter *iter;
621
622 switch (cmd) {
623 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
624 {
625 retry:
626 iter = lttng_ust_field_list_get_iter_next(list);
627 if (!iter)
628 return -ENOENT;
629 if (!strcmp(iter->event_name, "lttng_ust:metadata"))
630 goto retry;
631 memcpy(tp, iter, sizeof(*tp));
632 return 0;
633 }
634 default:
635 return -EINVAL;
636 }
637 }
638
639 static
640 int lttng_abi_tracepoint_field_list(void)
641 {
642 int list_objd, ret;
643 struct lttng_ust_field_list *list;
644
645 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops);
646 if (list_objd < 0) {
647 ret = list_objd;
648 goto objd_error;
649 }
650 list = zmalloc(sizeof(*list));
651 if (!list) {
652 ret = -ENOMEM;
653 goto alloc_error;
654 }
655 objd_set_private(list_objd, list);
656
657 /* populate list by walking on all registered probes. */
658 ret = ltt_probes_get_field_list(list);
659 if (ret) {
660 goto list_error;
661 }
662 return list_objd;
663
664 list_error:
665 free(list);
666 alloc_error:
667 {
668 int err;
669
670 err = lttng_ust_objd_unref(list_objd);
671 assert(!err);
672 }
673 objd_error:
674 return ret;
675 }
676
677 static
678 int lttng_release_tracepoint_field_list(int objd)
679 {
680 struct lttng_ust_field_list *list = objd_private(objd);
681
682 if (list) {
683 ltt_probes_prune_field_list(list);
684 free(list);
685 return 0;
686 } else {
687 return -EINVAL;
688 }
689 }
690
691 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
692 .release = lttng_release_tracepoint_field_list,
693 .cmd = lttng_tracepoint_field_list_cmd,
694 };
695
696 struct stream_priv_data {
697 struct lttng_ust_lib_ring_buffer *buf;
698 struct ltt_channel *ltt_chan;
699 };
700
701 static
702 int lttng_abi_open_stream(int channel_objd, struct lttng_ust_stream *info,
703 union ust_args *uargs)
704 {
705 struct ltt_channel *channel = objd_private(channel_objd);
706 struct lttng_ust_lib_ring_buffer *buf;
707 struct stream_priv_data *priv;
708 int stream_objd, ret;
709
710 buf = channel->ops->buffer_read_open(channel->chan, channel->handle,
711 &uargs->stream.shm_fd,
712 &uargs->stream.wait_fd,
713 &uargs->stream.memory_map_size);
714 if (!buf)
715 return -ENOENT;
716
717 priv = zmalloc(sizeof(*priv));
718 if (!priv) {
719 ret = -ENOMEM;
720 goto alloc_error;
721 }
722 priv->buf = buf;
723 priv->ltt_chan = channel;
724 stream_objd = objd_alloc(priv, &lib_ring_buffer_objd_ops);
725 if (stream_objd < 0) {
726 ret = stream_objd;
727 goto objd_error;
728 }
729 /* Hold a reference on the channel object descriptor */
730 objd_ref(channel_objd);
731 return stream_objd;
732
733 objd_error:
734 free(priv);
735 alloc_error:
736 channel->ops->buffer_read_close(buf, channel->handle);
737 return ret;
738 }
739
740 static
741 int lttng_abi_create_event(int channel_objd,
742 struct lttng_ust_event *event_param)
743 {
744 struct ltt_channel *channel = objd_private(channel_objd);
745 struct ltt_event *event;
746 int event_objd, ret;
747
748 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
749 event_objd = objd_alloc(NULL, &lttng_event_ops);
750 if (event_objd < 0) {
751 ret = event_objd;
752 goto objd_error;
753 }
754 /*
755 * We tolerate no failure path after event creation. It will stay
756 * invariant for the rest of the session.
757 */
758 ret = ltt_event_create(channel, event_param, &event);
759 if (ret < 0) {
760 goto event_error;
761 }
762 objd_set_private(event_objd, event);
763 /* The event holds a reference on the channel */
764 objd_ref(channel_objd);
765 return event_objd;
766
767 event_error:
768 {
769 int err;
770
771 err = lttng_ust_objd_unref(event_objd);
772 assert(!err);
773 }
774 objd_error:
775 return ret;
776 }
777
778 static
779 int lttng_abi_create_wildcard(int channel_objd,
780 struct lttng_ust_event *event_param)
781 {
782 struct ltt_channel *channel = objd_private(channel_objd);
783 struct session_wildcard *wildcard;
784 int wildcard_objd, ret;
785
786 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
787 wildcard_objd = objd_alloc(NULL, &lttng_wildcard_ops);
788 if (wildcard_objd < 0) {
789 ret = wildcard_objd;
790 goto objd_error;
791 }
792 /*
793 * We tolerate no failure path after wildcard creation. It will
794 * stay invariant for the rest of the session.
795 */
796 ret = ltt_wildcard_create(channel, event_param, &wildcard);
797 if (ret < 0) {
798 goto wildcard_error;
799 }
800 objd_set_private(wildcard_objd, wildcard);
801 /* The wildcard holds a reference on the channel */
802 objd_ref(channel_objd);
803 return wildcard_objd;
804
805 wildcard_error:
806 {
807 int err;
808
809 err = lttng_ust_objd_unref(wildcard_objd);
810 assert(!err);
811 }
812 objd_error:
813 return ret;
814 }
815
816 /**
817 * lttng_channel_cmd - lttng control through object descriptors
818 *
819 * @objd: the object descriptor
820 * @cmd: the command
821 * @arg: command arg
822 * @uargs: UST arguments (internal)
823 *
824 * This object descriptor implements lttng commands:
825 * LTTNG_UST_STREAM
826 * Returns an event stream object descriptor or failure.
827 * (typically, one event stream records events from one CPU)
828 * LTTNG_UST_EVENT
829 * Returns an event object descriptor or failure.
830 * LTTNG_UST_CONTEXT
831 * Prepend a context field to each event in the channel
832 * LTTNG_UST_ENABLE
833 * Enable recording for events in this channel (weak enable)
834 * LTTNG_UST_DISABLE
835 * Disable recording for events in this channel (strong disable)
836 *
837 * Channel and event file descriptors also hold a reference on the session.
838 */
839 static
840 long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
841 union ust_args *uargs)
842 {
843 struct ltt_channel *channel = objd_private(objd);
844
845 switch (cmd) {
846 case LTTNG_UST_STREAM:
847 {
848 struct lttng_ust_stream *stream;
849
850 stream = (struct lttng_ust_stream *) arg;
851 /* stream used as output */
852 return lttng_abi_open_stream(objd, stream, uargs);
853 }
854 case LTTNG_UST_EVENT:
855 {
856 struct lttng_ust_event *event_param =
857 (struct lttng_ust_event *) arg;
858 if (event_param->name[strlen(event_param->name) - 1] == '*') {
859 /* If ends with wildcard, create wildcard. */
860 return lttng_abi_create_wildcard(objd, event_param);
861 } else {
862 return lttng_abi_create_event(objd, event_param);
863 }
864 }
865 case LTTNG_UST_CONTEXT:
866 return lttng_abi_add_context(objd,
867 (struct lttng_ust_context *) arg,
868 &channel->ctx, channel->session);
869 case LTTNG_UST_ENABLE:
870 return ltt_channel_enable(channel);
871 case LTTNG_UST_DISABLE:
872 return ltt_channel_disable(channel);
873 case LTTNG_UST_FLUSH_BUFFER:
874 return channel->ops->flush_buffer(channel->chan, channel->handle);
875 default:
876 return -EINVAL;
877 }
878 }
879
880 /**
881 * lttng_metadata_cmd - lttng control through object descriptors
882 *
883 * @objd: the object descriptor
884 * @cmd: the command
885 * @arg: command arg
886 * @uargs: UST arguments (internal)
887 *
888 * This object descriptor implements lttng commands:
889 * LTTNG_UST_STREAM
890 * Returns an event stream file descriptor or failure.
891 *
892 * Channel and event file descriptors also hold a reference on the session.
893 */
894 static
895 long lttng_metadata_cmd(int objd, unsigned int cmd, unsigned long arg,
896 union ust_args *uargs)
897 {
898 struct ltt_channel *channel = objd_private(objd);
899
900 switch (cmd) {
901 case LTTNG_UST_STREAM:
902 {
903 struct lttng_ust_stream *stream;
904
905 stream = (struct lttng_ust_stream *) arg;
906 /* stream used as output */
907 return lttng_abi_open_stream(objd, stream, uargs);
908 }
909 case LTTNG_UST_FLUSH_BUFFER:
910 return channel->ops->flush_buffer(channel->chan, channel->handle);
911 default:
912 return -EINVAL;
913 }
914 }
915
916 #if 0
917 /**
918 * lttng_channel_poll - lttng stream addition/removal monitoring
919 *
920 * @file: the file
921 * @wait: poll table
922 */
923 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
924 {
925 struct ltt_channel *channel = file->private_data;
926 unsigned int mask = 0;
927
928 if (file->f_mode & FMODE_READ) {
929 poll_wait_set_exclusive(wait);
930 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
931 wait);
932
933 if (channel->ops->is_disabled(channel->chan))
934 return POLLERR;
935 if (channel->ops->is_finalized(channel->chan))
936 return POLLHUP;
937 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
938 return POLLIN | POLLRDNORM;
939 return 0;
940 }
941 return mask;
942
943 }
944 #endif //0
945
946 static
947 int lttng_channel_release(int objd)
948 {
949 struct ltt_channel *channel = objd_private(objd);
950
951 if (channel)
952 return lttng_ust_objd_unref(channel->session->objd);
953 return 0;
954 }
955
956 static const struct lttng_ust_objd_ops lttng_channel_ops = {
957 .release = lttng_channel_release,
958 //.poll = lttng_channel_poll,
959 .cmd = lttng_channel_cmd,
960 };
961
962 static const struct lttng_ust_objd_ops lttng_metadata_ops = {
963 .release = lttng_channel_release,
964 .cmd = lttng_metadata_cmd,
965 };
966
967 /**
968 * lttng_rb_cmd - lttng ring buffer control through object descriptors
969 *
970 * @objd: the object descriptor
971 * @cmd: the command
972 * @arg: command arg
973 * @uargs: UST arguments (internal)
974 *
975 * This object descriptor implements lttng commands:
976 * (None for now. Access is done directly though shm.)
977 */
978 static
979 long lttng_rb_cmd(int objd, unsigned int cmd, unsigned long arg,
980 union ust_args *uargs)
981 {
982 switch (cmd) {
983 default:
984 return -EINVAL;
985 }
986 }
987
988 static
989 int lttng_rb_release(int objd)
990 {
991 struct stream_priv_data *priv = objd_private(objd);
992 struct lttng_ust_lib_ring_buffer *buf;
993 struct ltt_channel *channel;
994
995 if (priv) {
996 buf = priv->buf;
997 channel = priv->ltt_chan;
998 free(priv);
999 /*
1000 * If we are at ABI exit, we don't want to close the
1001 * buffer opened for read: it is being shared between
1002 * the parent and child (right after fork), and we don't
1003 * want the child to close it for the parent. For a real
1004 * exit, we don't care about marking it as closed, as
1005 * the consumer daemon (if there is one) will do fine
1006 * even if we don't mark it as "closed" for reading on
1007 * our side.
1008 * We only mark it as closed if it is being explicitely
1009 * released by the session daemon with an explicit
1010 * release command.
1011 */
1012 if (!lttng_ust_abi_close_in_progress)
1013 channel->ops->buffer_read_close(buf, channel->handle);
1014
1015 return lttng_ust_objd_unref(channel->objd);
1016 }
1017 return 0;
1018 }
1019
1020 static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops = {
1021 .release = lttng_rb_release,
1022 .cmd = lttng_rb_cmd,
1023 };
1024
1025 /**
1026 * lttng_event_cmd - lttng control through object descriptors
1027 *
1028 * @objd: the object descriptor
1029 * @cmd: the command
1030 * @arg: command arg
1031 * @uargs: UST arguments (internal)
1032 *
1033 * This object descriptor implements lttng commands:
1034 * LTTNG_UST_CONTEXT
1035 * Prepend a context field to each record of this event
1036 * LTTNG_UST_ENABLE
1037 * Enable recording for this event (weak enable)
1038 * LTTNG_UST_DISABLE
1039 * Disable recording for this event (strong disable)
1040 * LTTNG_UST_FILTER
1041 * Attach a filter to an event.
1042 */
1043 static
1044 long lttng_event_cmd(int objd, unsigned int cmd, unsigned long arg,
1045 union ust_args *uargs)
1046 {
1047 struct ltt_event *event = objd_private(objd);
1048
1049 switch (cmd) {
1050 case LTTNG_UST_CONTEXT:
1051 return lttng_abi_add_context(objd,
1052 (struct lttng_ust_context *) arg,
1053 &event->ctx, event->chan->session);
1054 case LTTNG_UST_ENABLE:
1055 return ltt_event_enable(event);
1056 case LTTNG_UST_DISABLE:
1057 return ltt_event_disable(event);
1058 case LTTNG_UST_FILTER:
1059 {
1060 int ret;
1061 ret = lttng_filter_event_attach_bytecode(event,
1062 (struct lttng_ust_filter_bytecode *) arg);
1063 if (ret)
1064 return ret;
1065 lttng_filter_event_link_bytecode(event,
1066 event->filter_bytecode);
1067 return 0;
1068 }
1069 default:
1070 return -EINVAL;
1071 }
1072 }
1073
1074 static
1075 int lttng_event_release(int objd)
1076 {
1077 struct ltt_event *event = objd_private(objd);
1078
1079 if (event)
1080 return lttng_ust_objd_unref(event->chan->objd);
1081 return 0;
1082 }
1083
1084 /* TODO: filter control ioctl */
1085 static const struct lttng_ust_objd_ops lttng_event_ops = {
1086 .release = lttng_event_release,
1087 .cmd = lttng_event_cmd,
1088 };
1089
1090 /**
1091 * lttng_wildcard_cmd - lttng control through object descriptors
1092 *
1093 * @objd: the object descriptor
1094 * @cmd: the command
1095 * @arg: command arg
1096 * @uargs: UST arguments (internal)
1097 *
1098 * This object descriptor implements lttng commands:
1099 * LTTNG_UST_CONTEXT
1100 * Prepend a context field to each record of events of this
1101 * wildcard.
1102 * LTTNG_UST_ENABLE
1103 * Enable recording for these wildcard events (weak enable)
1104 * LTTNG_UST_DISABLE
1105 * Disable recording for these wildcard events (strong disable)
1106 * LTTNG_UST_FILTER
1107 * Attach a filter to a wildcard.
1108 */
1109 static
1110 long lttng_wildcard_cmd(int objd, unsigned int cmd, unsigned long arg,
1111 union ust_args *uargs)
1112 {
1113 struct session_wildcard *wildcard = objd_private(objd);
1114
1115 switch (cmd) {
1116 case LTTNG_UST_CONTEXT:
1117 return -ENOSYS; /* not implemented yet */
1118 #if 0
1119 return lttng_abi_add_context(objd,
1120 (struct lttng_ust_context *) arg,
1121 &wildcard->ctx, wildcard->chan->session);
1122 #endif
1123 case LTTNG_UST_ENABLE:
1124 return ltt_wildcard_enable(wildcard);
1125 case LTTNG_UST_DISABLE:
1126 return ltt_wildcard_disable(wildcard);
1127 case LTTNG_UST_FILTER:
1128 {
1129 int ret;
1130
1131 ret = lttng_filter_wildcard_attach_bytecode(wildcard,
1132 (struct lttng_ust_filter_bytecode *) arg);
1133 if (ret)
1134 return ret;
1135 lttng_filter_wildcard_link_bytecode(wildcard);
1136 return 0;
1137 }
1138 default:
1139 return -EINVAL;
1140 }
1141 }
1142
1143 static
1144 int lttng_wildcard_release(int objd)
1145 {
1146 struct session_wildcard *wildcard = objd_private(objd);
1147
1148 if (wildcard)
1149 return lttng_ust_objd_unref(wildcard->chan->objd);
1150 return 0;
1151 }
1152
1153 /* TODO: filter control ioctl */
1154 static const struct lttng_ust_objd_ops lttng_wildcard_ops = {
1155 .release = lttng_wildcard_release,
1156 .cmd = lttng_wildcard_cmd,
1157 };
1158
1159 void lttng_ust_abi_exit(void)
1160 {
1161 lttng_ust_abi_close_in_progress = 1;
1162 objd_table_destroy();
1163 lttng_ust_abi_close_in_progress = 0;
1164 }
This page took 0.053277 seconds and 4 git commands to generate.