Update loglevel ABI: only loglevel value/enum is known by 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 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;
e6c12e3d 206static const struct lttng_ust_objd_ops lttng_wildcard_ops;
b61ce3b2 207static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops;
51489cad 208static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
f4681817
MD
209
210enum channel_type {
211 PER_CPU_CHANNEL,
212 METADATA_CHANNEL,
213};
214
46050b1a
MD
215int lttng_abi_create_root_handle(void)
216{
217 int root_handle;
218
219 root_handle = objd_alloc(NULL, &lttng_ops);
46050b1a
MD
220 return root_handle;
221}
222
818173b9 223static
f4681817
MD
224int lttng_abi_create_session(void)
225{
226 struct ltt_session *session;
227 int session_objd, ret;
228
229 session = ltt_session_create();
230 if (!session)
231 return -ENOMEM;
232 session_objd = objd_alloc(session, &lttng_session_ops);
233 if (session_objd < 0) {
234 ret = session_objd;
235 goto objd_error;
236 }
237 session->objd = session_objd;
238 return session_objd;
239
240objd_error:
241 ltt_session_destroy(session);
242 return ret;
243}
244
f4681817
MD
245static
246long lttng_abi_tracer_version(int objd,
247 struct lttng_ust_tracer_version *v)
248{
23c8854a
MD
249 v->major = LTTNG_UST_MAJOR_VERSION;
250 v->minor = LTTNG_UST_MINOR_VERSION;
251 v->patchlevel = LTTNG_UST_PATCHLEVEL_VERSION;
f4681817
MD
252 return 0;
253}
254
255static
256long lttng_abi_add_context(int objd,
257 struct lttng_ust_context *context_param,
258 struct lttng_ctx **ctx, struct ltt_session *session)
259{
260 if (session->been_active)
261 return -EPERM;
262
263 switch (context_param->ctx) {
8de38cf7
MD
264 case LTTNG_UST_CONTEXT_PTHREAD_ID:
265 return lttng_add_pthread_id_to_ctx(ctx);
3b402b40
MD
266 case LTTNG_UST_CONTEXT_VTID:
267 return lttng_add_vtid_to_ctx(ctx);
c1ef86f0
MD
268 case LTTNG_UST_CONTEXT_VPID:
269 return lttng_add_vpid_to_ctx(ctx);
4847e9bb
MD
270 case LTTNG_UST_CONTEXT_PROCNAME:
271 return lttng_add_procname_to_ctx(ctx);
f4681817
MD
272 default:
273 return -EINVAL;
274 }
275}
276
277/**
278 * lttng_cmd - lttng control through socket commands
279 *
280 * @objd: the object descriptor
281 * @cmd: the command
282 * @arg: command arg
ef9ff354 283 * @uargs: UST arguments (internal)
f4681817
MD
284 *
285 * This descriptor implements lttng commands:
286 * LTTNG_UST_SESSION
287 * Returns a LTTng trace session object descriptor
288 * LTTNG_UST_TRACER_VERSION
289 * Returns the LTTng kernel tracer version
290 * LTTNG_UST_TRACEPOINT_LIST
291 * Returns a file descriptor listing available tracepoints
292 * LTTNG_UST_WAIT_QUIESCENT
293 * Returns after all previously running probes have completed
294 *
295 * The returned session will be deleted when its file descriptor is closed.
296 */
297static
ef9ff354
MD
298long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
299 union ust_args *uargs)
f4681817
MD
300{
301 switch (cmd) {
302 case LTTNG_UST_SESSION:
303 return lttng_abi_create_session();
304 case LTTNG_UST_TRACER_VERSION:
305 return lttng_abi_tracer_version(objd,
306 (struct lttng_ust_tracer_version *) arg);
307 case LTTNG_UST_TRACEPOINT_LIST:
51489cad 308 return lttng_abi_tracepoint_list();
f4681817
MD
309 case LTTNG_UST_WAIT_QUIESCENT:
310 synchronize_trace();
311 return 0;
312 default:
313 return -EINVAL;
314 }
315}
316
b61ce3b2 317static const struct lttng_ust_objd_ops lttng_ops = {
f4681817
MD
318 .cmd = lttng_cmd,
319};
320
321/*
322 * We tolerate no failure in this function (if one happens, we print a dmesg
323 * error, but cannot return any error, because the channel information is
324 * invariant.
325 */
326static
327void lttng_metadata_create_events(int channel_objd)
328{
329 struct ltt_channel *channel = objd_private(channel_objd);
330 static struct lttng_ust_event metadata_params = {
331 .instrumentation = LTTNG_UST_TRACEPOINT,
a4ada9b8 332 .name = "lttng_ust:metadata",
f4681817
MD
333 };
334 struct ltt_event *event;
576599a0 335 int ret;
f4681817
MD
336
337 /*
338 * We tolerate no failure path after event creation. It will stay
339 * invariant for the rest of the session.
340 */
576599a0
MD
341 ret = ltt_event_create(channel, &metadata_params, NULL, &event);
342 if (ret < 0) {
f4681817
MD
343 goto create_error;
344 }
345 return;
346
347create_error:
348 WARN_ON(1);
349 return; /* not allowed to return error */
350}
351
f4681817
MD
352int lttng_abi_create_channel(int session_objd,
353 struct lttng_ust_channel *chan_param,
ef9ff354
MD
354 enum channel_type channel_type,
355 union ust_args *uargs)
f4681817
MD
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 403 chan_param->read_timer_interval,
ef9ff354
MD
404 &uargs->channel.shm_fd,
405 &uargs->channel.wait_fd,
406 &uargs->channel.memory_map_size,
d028eddb 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
ef9ff354 440 * @uargs: UST arguments (internal)
f4681817
MD
441 *
442 * This descriptor implements lttng commands:
443 * LTTNG_UST_CHANNEL
444 * Returns a LTTng channel object descriptor
445 * LTTNG_UST_ENABLE
446 * Enables tracing for a session (weak enable)
447 * LTTNG_UST_DISABLE
448 * Disables tracing for a session (strong disable)
449 * LTTNG_UST_METADATA
450 * Returns a LTTng metadata object descriptor
451 *
452 * The returned channel will be deleted when its file descriptor is closed.
453 */
454static
ef9ff354
MD
455long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
456 union ust_args *uargs)
f4681817
MD
457{
458 struct ltt_session *session = objd_private(objd);
459
460 switch (cmd) {
461 case LTTNG_UST_CHANNEL:
462 return lttng_abi_create_channel(objd,
463 (struct lttng_ust_channel *) arg,
ef9ff354 464 PER_CPU_CHANNEL, uargs);
f4681817
MD
465 case LTTNG_UST_SESSION_START:
466 case LTTNG_UST_ENABLE:
467 return ltt_session_enable(session);
468 case LTTNG_UST_SESSION_STOP:
469 case LTTNG_UST_DISABLE:
470 return ltt_session_disable(session);
471 case LTTNG_UST_METADATA:
472 return lttng_abi_create_channel(objd,
473 (struct lttng_ust_channel *) arg,
ef9ff354 474 METADATA_CHANNEL, uargs);
f4681817
MD
475 default:
476 return -EINVAL;
477 }
478}
479
480/*
481 * Called when the last file reference is dropped.
482 *
483 * Big fat note: channels and events are invariant for the whole session after
484 * their creation. So this session destruction also destroys all channel and
485 * event structures specific to this session (they are not destroyed when their
486 * individual file is released).
487 */
488static
1ea11eab 489int lttng_release_session(int objd)
f4681817
MD
490{
491 struct ltt_session *session = objd_private(objd);
492
1ea11eab 493 if (session) {
f4681817 494 ltt_session_destroy(session);
1ea11eab
MD
495 return 0;
496 } else {
497 return -EINVAL;
498 }
f4681817
MD
499}
500
b61ce3b2 501static const struct lttng_ust_objd_ops lttng_session_ops = {
1ea11eab 502 .release = lttng_release_session,
f4681817
MD
503 .cmd = lttng_session_cmd,
504};
505
51489cad 506static
ef9ff354
MD
507long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
508 union ust_args *uargs)
51489cad 509{
c8fcf224 510 struct lttng_ust_tracepoint_list *list = objd_private(objd);
cbef6901
MD
511 struct lttng_ust_tracepoint_iter *tp =
512 (struct lttng_ust_tracepoint_iter *) arg;
c8fcf224 513 struct lttng_ust_tracepoint_iter *iter;
51489cad
MD
514
515 switch (cmd) {
516 case LTTNG_UST_TRACEPOINT_LIST_GET:
c8fcf224
MD
517 {
518 retry:
519 iter = lttng_ust_tracepoint_list_get_iter_next(list);
520 if (!iter)
b115631f 521 return -ENOENT;
c8fcf224
MD
522 if (!strcmp(iter->name, "lttng_ust:metadata"))
523 goto retry;
524 memcpy(tp, iter, sizeof(*tp));
51489cad 525 return 0;
c8fcf224 526 }
51489cad
MD
527 default:
528 return -EINVAL;
529 }
530}
531
532static
533int lttng_abi_tracepoint_list(void)
534{
535 int list_objd, ret;
c8fcf224 536 struct lttng_ust_tracepoint_list *list;
51489cad
MD
537
538 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops);
539 if (list_objd < 0) {
540 ret = list_objd;
541 goto objd_error;
542 }
543 list = zmalloc(sizeof(*list));
544 if (!list) {
545 ret = -ENOMEM;
546 goto alloc_error;
547 }
548 objd_set_private(list_objd, list);
549
c8fcf224
MD
550 /* populate list by walking on all registered probes. */
551 ret = ltt_probes_get_event_list(list);
552 if (ret) {
553 goto list_error;
554 }
51489cad
MD
555 return list_objd;
556
c8fcf224
MD
557list_error:
558 free(list);
51489cad
MD
559alloc_error:
560 {
561 int err;
562
563 err = lttng_ust_objd_unref(list_objd);
564 assert(!err);
565 }
566objd_error:
567 return ret;
568}
569
570static
571int lttng_release_tracepoint_list(int objd)
572{
c8fcf224 573 struct lttng_ust_tracepoint_list *list = objd_private(objd);
51489cad
MD
574
575 if (list) {
c8fcf224 576 ltt_probes_prune_event_list(list);
51489cad
MD
577 free(list);
578 return 0;
579 } else {
580 return -EINVAL;
581 }
582}
583
584static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
585 .release = lttng_release_tracepoint_list,
586 .cmd = lttng_tracepoint_list_cmd,
587};
588
381c0f1e 589struct stream_priv_data {
4cfec15c 590 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e
MD
591 struct ltt_channel *ltt_chan;
592};
593
f4681817 594static
ef9ff354
MD
595int lttng_abi_open_stream(int channel_objd, struct lttng_ust_stream *info,
596 union ust_args *uargs)
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 603 buf = channel->ops->buffer_read_open(channel->chan, channel->handle,
ef9ff354
MD
604 &uargs->stream.shm_fd,
605 &uargs->stream.wait_fd,
606 &uargs->stream.memory_map_size);
f4681817
MD
607 if (!buf)
608 return -ENOENT;
609
381c0f1e
MD
610 priv = zmalloc(sizeof(*priv));
611 if (!priv) {
612 ret = -ENOMEM;
613 goto alloc_error;
614 }
615 priv->buf = buf;
616 priv->ltt_chan = channel;
617 stream_objd = objd_alloc(priv, &lib_ring_buffer_objd_ops);
f4681817
MD
618 if (stream_objd < 0) {
619 ret = stream_objd;
620 goto objd_error;
621 }
381c0f1e
MD
622 /* Hold a reference on the channel object descriptor */
623 objd_ref(channel_objd);
f4681817
MD
624 return stream_objd;
625
626objd_error:
381c0f1e
MD
627 free(priv);
628alloc_error:
629 channel->ops->buffer_read_close(buf, channel->handle);
f4681817
MD
630 return ret;
631}
f4681817
MD
632
633static
634int lttng_abi_create_event(int channel_objd,
635 struct lttng_ust_event *event_param)
636{
637 struct ltt_channel *channel = objd_private(channel_objd);
638 struct ltt_event *event;
639 int event_objd, ret;
640
641 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
642 event_objd = objd_alloc(NULL, &lttng_event_ops);
643 if (event_objd < 0) {
644 ret = event_objd;
645 goto objd_error;
646 }
647 /*
648 * We tolerate no failure path after event creation. It will stay
649 * invariant for the rest of the session.
650 */
576599a0
MD
651 ret = ltt_event_create(channel, event_param, NULL, &event);
652 if (ret < 0) {
f4681817
MD
653 goto event_error;
654 }
655 objd_set_private(event_objd, event);
656 /* The event holds a reference on the channel */
657 objd_ref(channel_objd);
658 return event_objd;
659
660event_error:
1ea11eab
MD
661 {
662 int err;
663
d4419b81 664 err = lttng_ust_objd_unref(event_objd);
1ea11eab
MD
665 assert(!err);
666 }
f4681817
MD
667objd_error:
668 return ret;
669}
670
e6c12e3d
MD
671static
672int lttng_abi_create_wildcard(int channel_objd,
673 struct lttng_ust_event *event_param)
674{
675 struct ltt_channel *channel = objd_private(channel_objd);
676 struct session_wildcard *wildcard;
677 int wildcard_objd, ret;
678
679 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
680 wildcard_objd = objd_alloc(NULL, &lttng_wildcard_ops);
681 if (wildcard_objd < 0) {
682 ret = wildcard_objd;
683 goto objd_error;
684 }
685 /*
686 * We tolerate no failure path after wildcard creation. It will
687 * stay invariant for the rest of the session.
688 */
689 ret = ltt_wildcard_create(channel, event_param, &wildcard);
690 if (ret < 0) {
691 goto wildcard_error;
692 }
693 objd_set_private(wildcard_objd, wildcard);
694 /* The wildcard holds a reference on the channel */
695 objd_ref(channel_objd);
696 return wildcard_objd;
697
698wildcard_error:
699 {
700 int err;
701
702 err = lttng_ust_objd_unref(wildcard_objd);
703 assert(!err);
704 }
705objd_error:
706 return ret;
707}
708
f4681817
MD
709/**
710 * lttng_channel_cmd - lttng control through object descriptors
711 *
712 * @objd: the object descriptor
713 * @cmd: the command
714 * @arg: command arg
ef9ff354 715 * @uargs: UST arguments (internal)
f4681817
MD
716 *
717 * This object descriptor implements lttng commands:
718 * LTTNG_UST_STREAM
719 * Returns an event stream object descriptor or failure.
720 * (typically, one event stream records events from one CPU)
721 * LTTNG_UST_EVENT
722 * Returns an event object descriptor or failure.
723 * LTTNG_UST_CONTEXT
724 * Prepend a context field to each event in the channel
725 * LTTNG_UST_ENABLE
726 * Enable recording for events in this channel (weak enable)
727 * LTTNG_UST_DISABLE
728 * Disable recording for events in this channel (strong disable)
729 *
730 * Channel and event file descriptors also hold a reference on the session.
731 */
732static
ef9ff354
MD
733long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
734 union ust_args *uargs)
f4681817
MD
735{
736 struct ltt_channel *channel = objd_private(objd);
737
738 switch (cmd) {
739 case LTTNG_UST_STREAM:
381c0f1e
MD
740 {
741 struct lttng_ust_stream *stream;
742
743 stream = (struct lttng_ust_stream *) arg;
744 /* stream used as output */
ef9ff354 745 return lttng_abi_open_stream(objd, stream, uargs);
381c0f1e 746 }
f4681817 747 case LTTNG_UST_EVENT:
1f18504e
MD
748 {
749 struct lttng_ust_event *event_param =
750 (struct lttng_ust_event *) arg;
6b0e60f1
MD
751 if (event_param->name[strlen(event_param->name) - 1] == '*') {
752 /* If ends with wildcard, create wildcard. */
753 return lttng_abi_create_wildcard(objd, event_param);
1f18504e 754 } else {
6b0e60f1 755 return lttng_abi_create_event(objd, event_param);
1f18504e
MD
756 }
757 }
f4681817
MD
758 case LTTNG_UST_CONTEXT:
759 return lttng_abi_add_context(objd,
760 (struct lttng_ust_context *) arg,
761 &channel->ctx, channel->session);
762 case LTTNG_UST_ENABLE:
763 return ltt_channel_enable(channel);
764 case LTTNG_UST_DISABLE:
765 return ltt_channel_disable(channel);
43d330a4
MD
766 case LTTNG_UST_FLUSH_BUFFER:
767 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
768 default:
769 return -EINVAL;
770 }
771}
772
773/**
774 * lttng_metadata_cmd - lttng control through object descriptors
775 *
776 * @objd: the object descriptor
777 * @cmd: the command
778 * @arg: command arg
ef9ff354 779 * @uargs: UST arguments (internal)
f4681817
MD
780 *
781 * This object descriptor implements lttng commands:
782 * LTTNG_UST_STREAM
783 * Returns an event stream file descriptor or failure.
784 *
785 * Channel and event file descriptors also hold a reference on the session.
786 */
787static
ef9ff354
MD
788long lttng_metadata_cmd(int objd, unsigned int cmd, unsigned long arg,
789 union ust_args *uargs)
f4681817 790{
43861eab
MD
791 struct ltt_channel *channel = objd_private(objd);
792
f4681817
MD
793 switch (cmd) {
794 case LTTNG_UST_STREAM:
381c0f1e
MD
795 {
796 struct lttng_ust_stream *stream;
797
798 stream = (struct lttng_ust_stream *) arg;
799 /* stream used as output */
ef9ff354 800 return lttng_abi_open_stream(objd, stream, uargs);
381c0f1e 801 }
43d330a4
MD
802 case LTTNG_UST_FLUSH_BUFFER:
803 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
804 default:
805 return -EINVAL;
806 }
807}
808
809#if 0
810/**
811 * lttng_channel_poll - lttng stream addition/removal monitoring
812 *
813 * @file: the file
814 * @wait: poll table
815 */
816unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
817{
818 struct ltt_channel *channel = file->private_data;
819 unsigned int mask = 0;
820
821 if (file->f_mode & FMODE_READ) {
822 poll_wait_set_exclusive(wait);
823 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
824 wait);
825
826 if (channel->ops->is_disabled(channel->chan))
827 return POLLERR;
828 if (channel->ops->is_finalized(channel->chan))
829 return POLLHUP;
830 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
831 return POLLIN | POLLRDNORM;
832 return 0;
833 }
834 return mask;
835
836}
837#endif //0
838
839static
840int lttng_channel_release(int objd)
841{
842 struct ltt_channel *channel = objd_private(objd);
843
844 if (channel)
d4419b81 845 return lttng_ust_objd_unref(channel->session->objd);
f4681817
MD
846 return 0;
847}
848
b61ce3b2 849static const struct lttng_ust_objd_ops lttng_channel_ops = {
f4681817
MD
850 .release = lttng_channel_release,
851 //.poll = lttng_channel_poll,
852 .cmd = lttng_channel_cmd,
853};
854
b61ce3b2 855static const struct lttng_ust_objd_ops lttng_metadata_ops = {
f4681817
MD
856 .release = lttng_channel_release,
857 .cmd = lttng_metadata_cmd,
858};
859
381c0f1e
MD
860/**
861 * lttng_rb_cmd - lttng ring buffer control through object descriptors
862 *
863 * @objd: the object descriptor
864 * @cmd: the command
865 * @arg: command arg
ef9ff354 866 * @uargs: UST arguments (internal)
381c0f1e
MD
867 *
868 * This object descriptor implements lttng commands:
869 * (None for now. Access is done directly though shm.)
381c0f1e
MD
870 */
871static
ef9ff354
MD
872long lttng_rb_cmd(int objd, unsigned int cmd, unsigned long arg,
873 union ust_args *uargs)
381c0f1e 874{
381c0f1e
MD
875 switch (cmd) {
876 default:
877 return -EINVAL;
878 }
879}
880
881static
882int lttng_rb_release(int objd)
883{
884 struct stream_priv_data *priv = objd_private(objd);
4cfec15c 885 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e
MD
886 struct ltt_channel *channel;
887
888 if (priv) {
889 buf = priv->buf;
890 channel = priv->ltt_chan;
891 free(priv);
45e9e699
MD
892 /*
893 * If we are at ABI exit, we don't want to close the
894 * buffer opened for read: it is being shared between
895 * the parent and child (right after fork), and we don't
896 * want the child to close it for the parent. For a real
897 * exit, we don't care about marking it as closed, as
898 * the consumer daemon (if there is one) will do fine
899 * even if we don't mark it as "closed" for reading on
900 * our side.
901 * We only mark it as closed if it is being explicitely
902 * released by the session daemon with an explicit
903 * release command.
904 */
905 if (!lttng_ust_abi_close_in_progress)
906 channel->ops->buffer_read_close(buf, channel->handle);
381c0f1e 907
d4419b81 908 return lttng_ust_objd_unref(channel->objd);
381c0f1e
MD
909 }
910 return 0;
911}
912
b61ce3b2 913static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops = {
381c0f1e
MD
914 .release = lttng_rb_release,
915 .cmd = lttng_rb_cmd,
916};
917
f4681817
MD
918/**
919 * lttng_event_cmd - lttng control through object descriptors
920 *
921 * @objd: the object descriptor
922 * @cmd: the command
923 * @arg: command arg
ef9ff354 924 * @uargs: UST arguments (internal)
f4681817
MD
925 *
926 * This object descriptor implements lttng commands:
927 * LTTNG_UST_CONTEXT
928 * Prepend a context field to each record of this event
929 * LTTNG_UST_ENABLE
930 * Enable recording for this event (weak enable)
931 * LTTNG_UST_DISABLE
932 * Disable recording for this event (strong disable)
933 */
934static
ef9ff354
MD
935long lttng_event_cmd(int objd, unsigned int cmd, unsigned long arg,
936 union ust_args *uargs)
f4681817
MD
937{
938 struct ltt_event *event = objd_private(objd);
939
940 switch (cmd) {
941 case LTTNG_UST_CONTEXT:
942 return lttng_abi_add_context(objd,
943 (struct lttng_ust_context *) arg,
944 &event->ctx, event->chan->session);
945 case LTTNG_UST_ENABLE:
946 return ltt_event_enable(event);
947 case LTTNG_UST_DISABLE:
948 return ltt_event_disable(event);
949 default:
950 return -EINVAL;
951 }
952}
953
954static
955int lttng_event_release(int objd)
956{
957 struct ltt_event *event = objd_private(objd);
958
959 if (event)
d4419b81 960 return lttng_ust_objd_unref(event->chan->objd);
f4681817
MD
961 return 0;
962}
963
964/* TODO: filter control ioctl */
b61ce3b2 965static const struct lttng_ust_objd_ops lttng_event_ops = {
f4681817
MD
966 .release = lttng_event_release,
967 .cmd = lttng_event_cmd,
968};
969
e6c12e3d
MD
970/**
971 * lttng_wildcard_cmd - lttng control through object descriptors
972 *
973 * @objd: the object descriptor
974 * @cmd: the command
975 * @arg: command arg
ef9ff354 976 * @uargs: UST arguments (internal)
e6c12e3d
MD
977 *
978 * This object descriptor implements lttng commands:
979 * LTTNG_UST_CONTEXT
980 * Prepend a context field to each record of events of this
981 * wildcard.
982 * LTTNG_UST_ENABLE
983 * Enable recording for these wildcard events (weak enable)
984 * LTTNG_UST_DISABLE
985 * Disable recording for these wildcard events (strong disable)
986 */
987static
ef9ff354
MD
988long lttng_wildcard_cmd(int objd, unsigned int cmd, unsigned long arg,
989 union ust_args *uargs)
e6c12e3d
MD
990{
991 struct session_wildcard *wildcard = objd_private(objd);
992
993 switch (cmd) {
994 case LTTNG_UST_CONTEXT:
995 return -ENOSYS; /* not implemented yet */
996#if 0
997 return lttng_abi_add_context(objd,
998 (struct lttng_ust_context *) arg,
999 &wildcard->ctx, wildcard->chan->session);
1000#endif
1001 case LTTNG_UST_ENABLE:
1002 return ltt_wildcard_enable(wildcard);
1003 case LTTNG_UST_DISABLE:
1004 return ltt_wildcard_disable(wildcard);
1005 default:
1006 return -EINVAL;
1007 }
1008}
1009
1010static
1011int lttng_wildcard_release(int objd)
1012{
1013 struct session_wildcard *wildcard = objd_private(objd);
1014
1015 if (wildcard)
1016 return lttng_ust_objd_unref(wildcard->chan->objd);
1017 return 0;
1018}
1019
1020/* TODO: filter control ioctl */
1021static const struct lttng_ust_objd_ops lttng_wildcard_ops = {
1022 .release = lttng_wildcard_release,
1023 .cmd = lttng_wildcard_cmd,
1024};
1025
b35d179d 1026void lttng_ust_abi_exit(void)
f4681817 1027{
45e9e699 1028 lttng_ust_abi_close_in_progress = 1;
f4681817 1029 objd_table_destroy();
45e9e699 1030 lttng_ust_abi_close_in_progress = 0;
f4681817 1031}
This page took 0.069664 seconds and 4 git commands to generate.