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