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