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