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