Implement ioctl-alike communication
[lttng-ust.git] / libust / 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 <ust/lttng-ust-abi.h>
28 #include <urcu/compiler.h>
29 #include <urcu/list.h>
30 #include <ust/lttng-events.h>
31 #include <ust/usterr-signal-safe.h>
32 #include "ust/core.h"
33 #include "ltt-tracer.h"
34
35 /*
36 * Object descriptor table. Should be protected from concurrent access
37 * by the caller.
38 */
39
40 struct obj {
41 union {
42 struct {
43 void *private_data;
44 const struct objd_ops *ops;
45 int f_count;
46 } s;
47 int freelist_next; /* offset freelist. end is -1. */
48 } u;
49 };
50
51 struct objd_table {
52 struct obj *array;
53 unsigned int len, allocated_len;
54 int freelist_head; /* offset freelist head. end is -1 */
55 };
56
57 static struct objd_table objd_table = {
58 .freelist_head = -1,
59 };
60
61 static
62 int objd_alloc(void *private_data, const struct objd_ops *ops)
63 {
64 struct obj *obj;
65
66 if (objd_table.freelist_head != -1) {
67 obj = &objd_table.array[objd_table.freelist_head];
68 objd_table.freelist_head = obj->u.freelist_next;
69 goto end;
70 }
71
72 if (objd_table.len >= objd_table.allocated_len) {
73 unsigned int new_allocated_len, old_allocated_len;
74 struct obj *new_table, *old_table;
75
76 old_allocated_len = objd_table.allocated_len;
77 old_table = objd_table.array;
78 if (!old_allocated_len)
79 new_allocated_len = 1;
80 else
81 new_allocated_len = old_allocated_len << 1;
82 new_table = zmalloc(sizeof(struct obj) * new_allocated_len);
83 if (!new_table)
84 return -ENOMEM;
85 memcpy(new_table, old_table,
86 sizeof(struct obj) * old_allocated_len);
87 free(old_table);
88 objd_table.array = new_table;
89 objd_table.allocated_len = new_allocated_len;
90 }
91 obj = &objd_table.array[objd_table.len];
92 objd_table.len++;
93 end:
94 obj->u.s.private_data = private_data;
95 obj->u.s.ops = ops;
96 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
97 /* count == 2 : allocated + hold ref */
98 return obj - objd_table.array;
99 }
100
101 static
102 struct obj *_objd_get(int id)
103 {
104 if (id >= objd_table.len)
105 return NULL;
106 if (!objd_table.array[id].u.s.f_count)
107 return NULL;
108 return &objd_table.array[id];
109 }
110
111 static
112 void *objd_private(int id)
113 {
114 struct obj *obj = _objd_get(id);
115 assert(obj);
116 return obj->u.s.private_data;
117 }
118
119 static
120 void objd_set_private(int id, void *private_data)
121 {
122 struct obj *obj = _objd_get(id);
123 assert(obj);
124 obj->u.s.private_data = private_data;
125 }
126
127 const struct objd_ops *objd_ops(int id)
128 {
129 struct obj *obj = _objd_get(id);
130
131 if (!obj)
132 return NULL;
133 return obj->u.s.ops;
134 }
135
136 static
137 void objd_free(int id)
138 {
139 struct obj *obj = _objd_get(id);
140
141 assert(obj);
142 obj->u.freelist_next = objd_table.freelist_head;
143 objd_table.freelist_head = obj - objd_table.array;
144 assert(obj->u.s.f_count == 1);
145 obj->u.s.f_count = 0; /* deallocated */
146 }
147
148 static
149 void objd_ref(int id)
150 {
151 struct obj *obj = _objd_get(id);
152 obj->u.s.f_count++;
153 }
154
155 int objd_unref(int id)
156 {
157 struct obj *obj = _objd_get(id);
158
159 if (!obj)
160 return -EINVAL;
161 if (obj->u.s.f_count == 1) {
162 ERR("Reference counting error\n");
163 return -EINVAL;
164 }
165 if ((--obj->u.s.f_count) == 1) {
166 const struct objd_ops *ops = objd_ops(id);
167
168 if (ops->release)
169 ops->release(id);
170 objd_free(id);
171 }
172 return 0;
173 }
174
175 static
176 void objd_table_destroy(void)
177 {
178 int i;
179
180 for (i = 0; i < objd_table.allocated_len; i++) {
181 struct obj *obj = _objd_get(i);
182 const struct objd_ops *ops;
183
184 if (!obj)
185 continue;
186 ops = obj->u.s.ops;
187 if (ops->release)
188 ops->release(i);
189 }
190 free(objd_table.array);
191 }
192
193 /*
194 * This is LTTng's own personal way to create an ABI for sessiond.
195 * We send commands over a socket.
196 */
197
198 static const struct objd_ops lttng_ops;
199 static const struct objd_ops lttng_session_ops;
200 static const struct objd_ops lttng_channel_ops;
201 static const struct objd_ops lttng_metadata_ops;
202 static const struct objd_ops lttng_event_ops;
203
204 enum channel_type {
205 PER_CPU_CHANNEL,
206 METADATA_CHANNEL,
207 };
208
209 int lttng_abi_create_root_handle(void)
210 {
211 int root_handle;
212
213 root_handle = objd_alloc(NULL, &lttng_ops);
214 assert(root_handle == 0);
215 return root_handle;
216 }
217
218 int lttng_abi_create_session(void)
219 {
220 struct ltt_session *session;
221 int session_objd, ret;
222
223 session = ltt_session_create();
224 if (!session)
225 return -ENOMEM;
226 session_objd = objd_alloc(session, &lttng_session_ops);
227 if (session_objd < 0) {
228 ret = session_objd;
229 goto objd_error;
230 }
231 session->objd = session_objd;
232 return session_objd;
233
234 objd_error:
235 ltt_session_destroy(session);
236 return ret;
237 }
238
239 #if 0
240 static
241 int lttng_abi_tracepoint_list(void)
242 {
243 int list_objd, ret;
244
245 /* TODO: Create list private data */
246 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops);
247 if (list_objd < 0) {
248 ret = list_objd;
249 goto objd_error;
250 }
251
252 return list_objd;
253
254 objd_error:
255 return ret;
256 }
257 #endif //0
258
259 static
260 long lttng_abi_tracer_version(int objd,
261 struct lttng_ust_tracer_version *v)
262 {
263 v->version = LTTNG_UST_VERSION;
264 v->patchlevel = LTTNG_UST_PATCHLEVEL;
265 v->sublevel = LTTNG_UST_SUBLEVEL;
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_VTID:
279 //TODO return lttng_add_vtid_to_ctx(ctx);
280 default:
281 return -EINVAL;
282 }
283 }
284
285 /**
286 * lttng_cmd - lttng control through socket commands
287 *
288 * @objd: the object descriptor
289 * @cmd: the command
290 * @arg: command arg
291 *
292 * This descriptor implements lttng commands:
293 * LTTNG_UST_SESSION
294 * Returns a LTTng trace session object descriptor
295 * LTTNG_UST_TRACER_VERSION
296 * Returns the LTTng kernel tracer version
297 * LTTNG_UST_TRACEPOINT_LIST
298 * Returns a file descriptor listing available tracepoints
299 * LTTNG_UST_WAIT_QUIESCENT
300 * Returns after all previously running probes have completed
301 *
302 * The returned session will be deleted when its file descriptor is closed.
303 */
304 static
305 long lttng_cmd(int objd, unsigned int cmd, unsigned long arg)
306 {
307 switch (cmd) {
308 case LTTNG_UST_SESSION:
309 return lttng_abi_create_session();
310 case LTTNG_UST_TRACER_VERSION:
311 return lttng_abi_tracer_version(objd,
312 (struct lttng_ust_tracer_version *) arg);
313 case LTTNG_UST_TRACEPOINT_LIST:
314 return -ENOSYS; //TODO
315 //return lttng_abi_tracepoint_list();
316 case LTTNG_UST_WAIT_QUIESCENT:
317 synchronize_trace();
318 return 0;
319 default:
320 return -EINVAL;
321 }
322 }
323
324 static const struct objd_ops lttng_ops = {
325 .cmd = lttng_cmd,
326 };
327
328 /*
329 * We tolerate no failure in this function (if one happens, we print a dmesg
330 * error, but cannot return any error, because the channel information is
331 * invariant.
332 */
333 static
334 void lttng_metadata_create_events(int channel_objd)
335 {
336 struct ltt_channel *channel = objd_private(channel_objd);
337 static struct lttng_ust_event metadata_params = {
338 .instrumentation = LTTNG_UST_TRACEPOINT,
339 .name = "lttng_metadata",
340 };
341 struct ltt_event *event;
342 int ret;
343
344 /*
345 * We tolerate no failure path after event creation. It will stay
346 * invariant for the rest of the session.
347 */
348 event = ltt_event_create(channel, &metadata_params, NULL);
349 if (!event) {
350 ret = -EINVAL;
351 goto create_error;
352 }
353 return;
354
355 create_error:
356 WARN_ON(1);
357 return; /* not allowed to return error */
358 }
359
360 int lttng_abi_create_channel(int session_objd,
361 struct lttng_ust_channel *chan_param,
362 enum channel_type channel_type)
363 {
364 struct ltt_session *session = objd_private(session_objd);
365 const struct objd_ops *ops;
366 const char *transport_name;
367 struct ltt_channel *chan;
368 int chan_objd;
369 int ret = 0;
370
371 chan_objd = objd_alloc(NULL, &lttng_channel_ops);
372 if (chan_objd < 0) {
373 ret = chan_objd;
374 goto objd_error;
375 }
376 switch (channel_type) {
377 case PER_CPU_CHANNEL:
378 if (chan_param->output == LTTNG_UST_MMAP) {
379 transport_name = chan_param->overwrite ?
380 "relay-overwrite-mmap" : "relay-discard-mmap";
381 } else {
382 return -EINVAL;
383 }
384 ops = &lttng_channel_ops;
385 break;
386 case METADATA_CHANNEL:
387 if (chan_param->output == LTTNG_UST_MMAP)
388 transport_name = "relay-metadata-mmap";
389 else
390 return -EINVAL;
391 ops = &lttng_metadata_ops;
392 break;
393 default:
394 transport_name = "<unknown>";
395 break;
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 if (!chan) {
407 ret = -EINVAL;
408 goto chan_error;
409 }
410 objd_set_private(chan_objd, chan);
411 chan->objd = chan_objd;
412 if (channel_type == METADATA_CHANNEL) {
413 session->metadata = chan;
414 lttng_metadata_create_events(chan_objd);
415 }
416
417 /* The channel created holds a reference on the session */
418 objd_ref(session_objd);
419
420 return chan_objd;
421
422 chan_error:
423 {
424 int err;
425
426 err = objd_unref(chan_objd);
427 assert(!err);
428 }
429 objd_error:
430 return ret;
431 }
432
433 /**
434 * lttng_session_cmd - lttng session object command
435 *
436 * @obj: the object
437 * @cmd: the command
438 * @arg: command arg
439 *
440 * This descriptor implements lttng commands:
441 * LTTNG_UST_CHANNEL
442 * Returns a LTTng channel object descriptor
443 * LTTNG_UST_ENABLE
444 * Enables tracing for a session (weak enable)
445 * LTTNG_UST_DISABLE
446 * Disables tracing for a session (strong disable)
447 * LTTNG_UST_METADATA
448 * Returns a LTTng metadata object descriptor
449 *
450 * The returned channel will be deleted when its file descriptor is closed.
451 */
452 static
453 long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg)
454 {
455 struct ltt_session *session = objd_private(objd);
456
457 switch (cmd) {
458 case LTTNG_UST_CHANNEL:
459 return lttng_abi_create_channel(objd,
460 (struct lttng_ust_channel *) arg,
461 PER_CPU_CHANNEL);
462 case LTTNG_UST_SESSION_START:
463 case LTTNG_UST_ENABLE:
464 return ltt_session_enable(session);
465 case LTTNG_UST_SESSION_STOP:
466 case LTTNG_UST_DISABLE:
467 return ltt_session_disable(session);
468 case LTTNG_UST_METADATA:
469 return lttng_abi_create_channel(objd,
470 (struct lttng_ust_channel *) arg,
471 METADATA_CHANNEL);
472 default:
473 return -EINVAL;
474 }
475 }
476
477 /*
478 * Called when the last file reference is dropped.
479 *
480 * Big fat note: channels and events are invariant for the whole session after
481 * their creation. So this session destruction also destroys all channel and
482 * event structures specific to this session (they are not destroyed when their
483 * individual file is released).
484 */
485 static
486 int lttng_release_session(int objd)
487 {
488 struct ltt_session *session = objd_private(objd);
489
490 if (session) {
491 ltt_session_destroy(session);
492 return 0;
493 } else {
494 return -EINVAL;
495 }
496 }
497
498 static const struct objd_ops lttng_session_ops = {
499 .release = lttng_release_session,
500 .cmd = lttng_session_cmd,
501 };
502
503 #if 0
504 static
505 int lttng_abi_open_stream(int channel_objd)
506 {
507 struct ltt_channel *channel = objd_private(channel_objd);
508 struct lib_ring_buffer *buf;
509 int stream_objd, ret;
510
511 buf = channel->ops->buffer_read_open(channel->chan);
512 if (!buf)
513 return -ENOENT;
514
515 stream_objd = objd_alloc(buf, &lib_ring_buffer_objd_ops);
516 if (stream_objd < 0) {
517 ret = stream_objd;
518 goto objd_error;
519 }
520 /*
521 * The stream holds a reference to the channel within the generic ring
522 * buffer library, so no need to hold a refcount on the channel and
523 * session files here.
524 */
525 return stream_objd;
526
527 objd_error:
528 channel->ops->buffer_read_close(buf);
529 return ret;
530 }
531 #endif //0
532
533 static
534 int lttng_abi_create_event(int channel_objd,
535 struct lttng_ust_event *event_param)
536 {
537 struct ltt_channel *channel = objd_private(channel_objd);
538 struct ltt_event *event;
539 int event_objd, ret;
540
541 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
542 event_objd = objd_alloc(NULL, &lttng_event_ops);
543 if (event_objd < 0) {
544 ret = event_objd;
545 goto objd_error;
546 }
547 /*
548 * We tolerate no failure path after event creation. It will stay
549 * invariant for the rest of the session.
550 */
551 event = ltt_event_create(channel, event_param, NULL);
552 if (!event) {
553 ret = -EINVAL;
554 goto event_error;
555 }
556 objd_set_private(event_objd, event);
557 /* The event holds a reference on the channel */
558 objd_ref(channel_objd);
559 return event_objd;
560
561 event_error:
562 {
563 int err;
564
565 err = objd_unref(event_objd);
566 assert(!err);
567 }
568 objd_error:
569 return ret;
570 }
571
572 /**
573 * lttng_channel_cmd - lttng control through object descriptors
574 *
575 * @objd: the object descriptor
576 * @cmd: the command
577 * @arg: command arg
578 *
579 * This object descriptor implements lttng commands:
580 * LTTNG_UST_STREAM
581 * Returns an event stream object descriptor or failure.
582 * (typically, one event stream records events from one CPU)
583 * LTTNG_UST_EVENT
584 * Returns an event object descriptor or failure.
585 * LTTNG_UST_CONTEXT
586 * Prepend a context field to each event in the channel
587 * LTTNG_UST_ENABLE
588 * Enable recording for events in this channel (weak enable)
589 * LTTNG_UST_DISABLE
590 * Disable recording for events in this channel (strong disable)
591 *
592 * Channel and event file descriptors also hold a reference on the session.
593 */
594 static
595 long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg)
596 {
597 struct ltt_channel *channel = objd_private(objd);
598
599 switch (cmd) {
600 case LTTNG_UST_STREAM:
601 return -ENOSYS; //TODO
602 //return lttng_abi_open_stream(objd);
603 case LTTNG_UST_EVENT:
604 return lttng_abi_create_event(objd, (struct lttng_ust_event *) arg);
605 case LTTNG_UST_CONTEXT:
606 return lttng_abi_add_context(objd,
607 (struct lttng_ust_context *) arg,
608 &channel->ctx, channel->session);
609 case LTTNG_UST_ENABLE:
610 return ltt_channel_enable(channel);
611 case LTTNG_UST_DISABLE:
612 return ltt_channel_disable(channel);
613 default:
614 return -EINVAL;
615 }
616 }
617
618 /**
619 * lttng_metadata_cmd - lttng control through object descriptors
620 *
621 * @objd: the object descriptor
622 * @cmd: the command
623 * @arg: command arg
624 *
625 * This object descriptor implements lttng commands:
626 * LTTNG_UST_STREAM
627 * Returns an event stream file descriptor or failure.
628 *
629 * Channel and event file descriptors also hold a reference on the session.
630 */
631 static
632 long lttng_metadata_cmd(int objd, unsigned int cmd, unsigned long arg)
633 {
634 switch (cmd) {
635 case LTTNG_UST_STREAM:
636 return -ENOSYS; //TODO
637 //return lttng_abi_open_stream(objd);
638 default:
639 return -EINVAL;
640 }
641 }
642
643 #if 0
644 /**
645 * lttng_channel_poll - lttng stream addition/removal monitoring
646 *
647 * @file: the file
648 * @wait: poll table
649 */
650 unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
651 {
652 struct ltt_channel *channel = file->private_data;
653 unsigned int mask = 0;
654
655 if (file->f_mode & FMODE_READ) {
656 poll_wait_set_exclusive(wait);
657 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
658 wait);
659
660 if (channel->ops->is_disabled(channel->chan))
661 return POLLERR;
662 if (channel->ops->is_finalized(channel->chan))
663 return POLLHUP;
664 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
665 return POLLIN | POLLRDNORM;
666 return 0;
667 }
668 return mask;
669
670 }
671 #endif //0
672
673 static
674 int lttng_channel_release(int objd)
675 {
676 struct ltt_channel *channel = objd_private(objd);
677
678 if (channel)
679 return objd_unref(channel->session->objd);
680 return 0;
681 }
682
683 static const struct objd_ops lttng_channel_ops = {
684 .release = lttng_channel_release,
685 //.poll = lttng_channel_poll,
686 .cmd = lttng_channel_cmd,
687 };
688
689 static const struct objd_ops lttng_metadata_ops = {
690 .release = lttng_channel_release,
691 .cmd = lttng_metadata_cmd,
692 };
693
694 /**
695 * lttng_event_cmd - lttng control through object descriptors
696 *
697 * @objd: the object descriptor
698 * @cmd: the command
699 * @arg: command arg
700 *
701 * This object descriptor implements lttng commands:
702 * LTTNG_UST_CONTEXT
703 * Prepend a context field to each record of this event
704 * LTTNG_UST_ENABLE
705 * Enable recording for this event (weak enable)
706 * LTTNG_UST_DISABLE
707 * Disable recording for this event (strong disable)
708 */
709 static
710 long lttng_event_cmd(int objd, unsigned int cmd, unsigned long arg)
711 {
712 struct ltt_event *event = objd_private(objd);
713
714 switch (cmd) {
715 case LTTNG_UST_CONTEXT:
716 return lttng_abi_add_context(objd,
717 (struct lttng_ust_context *) arg,
718 &event->ctx, event->chan->session);
719 case LTTNG_UST_ENABLE:
720 return ltt_event_enable(event);
721 case LTTNG_UST_DISABLE:
722 return ltt_event_disable(event);
723 default:
724 return -EINVAL;
725 }
726 }
727
728 static
729 int lttng_event_release(int objd)
730 {
731 struct ltt_event *event = objd_private(objd);
732
733 if (event)
734 return objd_unref(event->chan->objd);
735 return 0;
736 }
737
738 /* TODO: filter control ioctl */
739 static const struct objd_ops lttng_event_ops = {
740 .release = lttng_event_release,
741 .cmd = lttng_event_cmd,
742 };
743
744 void lttng_ust_abi_exit(void)
745 {
746 objd_table_destroy();
747 }
This page took 0.04409 seconds and 5 git commands to generate.