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