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