Fix loglevel value enable: use isdigit rather than isalpha
[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;
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
283 *
284 * This descriptor implements lttng commands:
285 * LTTNG_UST_SESSION
286 * Returns a LTTng trace session object descriptor
287 * LTTNG_UST_TRACER_VERSION
288 * Returns the LTTng kernel tracer version
289 * LTTNG_UST_TRACEPOINT_LIST
290 * Returns a file descriptor listing available tracepoints
291 * LTTNG_UST_WAIT_QUIESCENT
292 * Returns after all previously running probes have completed
293 *
294 * The returned session will be deleted when its file descriptor is closed.
295 */
296static
297long lttng_cmd(int objd, unsigned int cmd, unsigned long arg)
298{
299 switch (cmd) {
300 case LTTNG_UST_SESSION:
301 return lttng_abi_create_session();
302 case LTTNG_UST_TRACER_VERSION:
303 return lttng_abi_tracer_version(objd,
304 (struct lttng_ust_tracer_version *) arg);
305 case LTTNG_UST_TRACEPOINT_LIST:
51489cad 306 return lttng_abi_tracepoint_list();
f4681817
MD
307 case LTTNG_UST_WAIT_QUIESCENT:
308 synchronize_trace();
309 return 0;
310 default:
311 return -EINVAL;
312 }
313}
314
b61ce3b2 315static const struct lttng_ust_objd_ops lttng_ops = {
f4681817
MD
316 .cmd = lttng_cmd,
317};
318
319/*
320 * We tolerate no failure in this function (if one happens, we print a dmesg
321 * error, but cannot return any error, because the channel information is
322 * invariant.
323 */
324static
325void lttng_metadata_create_events(int channel_objd)
326{
327 struct ltt_channel *channel = objd_private(channel_objd);
328 static struct lttng_ust_event metadata_params = {
329 .instrumentation = LTTNG_UST_TRACEPOINT,
a4ada9b8 330 .name = "lttng_ust:metadata",
f4681817
MD
331 };
332 struct ltt_event *event;
576599a0 333 int ret;
f4681817
MD
334
335 /*
336 * We tolerate no failure path after event creation. It will stay
337 * invariant for the rest of the session.
338 */
576599a0
MD
339 ret = ltt_event_create(channel, &metadata_params, NULL, &event);
340 if (ret < 0) {
f4681817
MD
341 goto create_error;
342 }
343 return;
344
345create_error:
346 WARN_ON(1);
347 return; /* not allowed to return error */
348}
349
f4681817
MD
350int lttng_abi_create_channel(int session_objd,
351 struct lttng_ust_channel *chan_param,
352 enum channel_type channel_type)
353{
354 struct ltt_session *session = objd_private(session_objd);
b61ce3b2 355 const struct lttng_ust_objd_ops *ops;
f4681817
MD
356 const char *transport_name;
357 struct ltt_channel *chan;
358 int chan_objd;
359 int ret = 0;
d028eddb 360 struct ltt_channel chan_priv_init;
f4681817 361
f4681817
MD
362 switch (channel_type) {
363 case PER_CPU_CHANNEL:
364 if (chan_param->output == LTTNG_UST_MMAP) {
365 transport_name = chan_param->overwrite ?
366 "relay-overwrite-mmap" : "relay-discard-mmap";
367 } else {
368 return -EINVAL;
369 }
370 ops = &lttng_channel_ops;
371 break;
372 case METADATA_CHANNEL:
373 if (chan_param->output == LTTNG_UST_MMAP)
374 transport_name = "relay-metadata-mmap";
375 else
376 return -EINVAL;
377 ops = &lttng_metadata_ops;
378 break;
379 default:
380 transport_name = "<unknown>";
fe38d4af
MD
381 return -EINVAL;
382 }
383 chan_objd = objd_alloc(NULL, ops);
384 if (chan_objd < 0) {
385 ret = chan_objd;
386 goto objd_error;
f4681817 387 }
d028eddb
MD
388 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
389 /* Copy of session UUID for consumer (availability through shm) */
390 memcpy(chan_priv_init.uuid, session->uuid, sizeof(session->uuid));
391
f4681817
MD
392 /*
393 * We tolerate no failure path after channel creation. It will stay
394 * invariant for the rest of the session.
395 */
396 chan = ltt_channel_create(session, transport_name, NULL,
397 chan_param->subbuf_size,
398 chan_param->num_subbuf,
399 chan_param->switch_timer_interval,
193183fb
MD
400 chan_param->read_timer_interval,
401 &chan_param->shm_fd,
402 &chan_param->wait_fd,
d028eddb
MD
403 &chan_param->memory_map_size,
404 &chan_priv_init);
f4681817
MD
405 if (!chan) {
406 ret = -EINVAL;
407 goto chan_error;
408 }
409 objd_set_private(chan_objd, chan);
410 chan->objd = chan_objd;
411 if (channel_type == METADATA_CHANNEL) {
412 session->metadata = chan;
413 lttng_metadata_create_events(chan_objd);
414 }
f4681817
MD
415 /* The channel created holds a reference on the session */
416 objd_ref(session_objd);
417
418 return chan_objd;
419
420chan_error:
1ea11eab
MD
421 {
422 int err;
423
d4419b81 424 err = lttng_ust_objd_unref(chan_objd);
1ea11eab
MD
425 assert(!err);
426 }
f4681817
MD
427objd_error:
428 return ret;
429}
430
431/**
432 * lttng_session_cmd - lttng session object command
433 *
434 * @obj: the object
435 * @cmd: the command
436 * @arg: command arg
437 *
438 * This descriptor implements lttng commands:
439 * LTTNG_UST_CHANNEL
440 * Returns a LTTng channel object descriptor
441 * LTTNG_UST_ENABLE
442 * Enables tracing for a session (weak enable)
443 * LTTNG_UST_DISABLE
444 * Disables tracing for a session (strong disable)
445 * LTTNG_UST_METADATA
446 * Returns a LTTng metadata object descriptor
447 *
448 * The returned channel will be deleted when its file descriptor is closed.
449 */
450static
451long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg)
452{
453 struct ltt_session *session = objd_private(objd);
454
455 switch (cmd) {
456 case LTTNG_UST_CHANNEL:
457 return lttng_abi_create_channel(objd,
458 (struct lttng_ust_channel *) arg,
459 PER_CPU_CHANNEL);
460 case LTTNG_UST_SESSION_START:
461 case LTTNG_UST_ENABLE:
462 return ltt_session_enable(session);
463 case LTTNG_UST_SESSION_STOP:
464 case LTTNG_UST_DISABLE:
465 return ltt_session_disable(session);
466 case LTTNG_UST_METADATA:
467 return lttng_abi_create_channel(objd,
468 (struct lttng_ust_channel *) arg,
469 METADATA_CHANNEL);
470 default:
471 return -EINVAL;
472 }
473}
474
475/*
476 * Called when the last file reference is dropped.
477 *
478 * Big fat note: channels and events are invariant for the whole session after
479 * their creation. So this session destruction also destroys all channel and
480 * event structures specific to this session (they are not destroyed when their
481 * individual file is released).
482 */
483static
1ea11eab 484int lttng_release_session(int objd)
f4681817
MD
485{
486 struct ltt_session *session = objd_private(objd);
487
1ea11eab 488 if (session) {
f4681817 489 ltt_session_destroy(session);
1ea11eab
MD
490 return 0;
491 } else {
492 return -EINVAL;
493 }
f4681817
MD
494}
495
b61ce3b2 496static const struct lttng_ust_objd_ops lttng_session_ops = {
1ea11eab 497 .release = lttng_release_session,
f4681817
MD
498 .cmd = lttng_session_cmd,
499};
500
51489cad
MD
501static
502long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg)
503{
c8fcf224 504 struct lttng_ust_tracepoint_list *list = objd_private(objd);
cbef6901
MD
505 struct lttng_ust_tracepoint_iter *tp =
506 (struct lttng_ust_tracepoint_iter *) arg;
c8fcf224 507 struct lttng_ust_tracepoint_iter *iter;
51489cad
MD
508
509 switch (cmd) {
510 case LTTNG_UST_TRACEPOINT_LIST_GET:
c8fcf224
MD
511 {
512 retry:
513 iter = lttng_ust_tracepoint_list_get_iter_next(list);
514 if (!iter)
b115631f 515 return -ENOENT;
c8fcf224
MD
516 if (!strcmp(iter->name, "lttng_ust:metadata"))
517 goto retry;
518 memcpy(tp, iter, sizeof(*tp));
51489cad 519 return 0;
c8fcf224 520 }
51489cad
MD
521 default:
522 return -EINVAL;
523 }
524}
525
526static
527int lttng_abi_tracepoint_list(void)
528{
529 int list_objd, ret;
c8fcf224 530 struct lttng_ust_tracepoint_list *list;
51489cad
MD
531
532 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops);
533 if (list_objd < 0) {
534 ret = list_objd;
535 goto objd_error;
536 }
537 list = zmalloc(sizeof(*list));
538 if (!list) {
539 ret = -ENOMEM;
540 goto alloc_error;
541 }
542 objd_set_private(list_objd, list);
543
c8fcf224
MD
544 /* populate list by walking on all registered probes. */
545 ret = ltt_probes_get_event_list(list);
546 if (ret) {
547 goto list_error;
548 }
51489cad
MD
549 return list_objd;
550
c8fcf224
MD
551list_error:
552 free(list);
51489cad
MD
553alloc_error:
554 {
555 int err;
556
557 err = lttng_ust_objd_unref(list_objd);
558 assert(!err);
559 }
560objd_error:
561 return ret;
562}
563
564static
565int lttng_release_tracepoint_list(int objd)
566{
c8fcf224 567 struct lttng_ust_tracepoint_list *list = objd_private(objd);
51489cad
MD
568
569 if (list) {
c8fcf224 570 ltt_probes_prune_event_list(list);
51489cad
MD
571 free(list);
572 return 0;
573 } else {
574 return -EINVAL;
575 }
576}
577
578static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
579 .release = lttng_release_tracepoint_list,
580 .cmd = lttng_tracepoint_list_cmd,
581};
582
381c0f1e 583struct stream_priv_data {
4cfec15c 584 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e
MD
585 struct ltt_channel *ltt_chan;
586};
587
f4681817 588static
381c0f1e 589int lttng_abi_open_stream(int channel_objd, struct lttng_ust_stream *info)
f4681817
MD
590{
591 struct ltt_channel *channel = objd_private(channel_objd);
4cfec15c 592 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e 593 struct stream_priv_data *priv;
f4681817
MD
594 int stream_objd, ret;
595
381c0f1e
MD
596 buf = channel->ops->buffer_read_open(channel->chan, channel->handle,
597 &info->shm_fd, &info->wait_fd, &info->memory_map_size);
f4681817
MD
598 if (!buf)
599 return -ENOENT;
600
381c0f1e
MD
601 priv = zmalloc(sizeof(*priv));
602 if (!priv) {
603 ret = -ENOMEM;
604 goto alloc_error;
605 }
606 priv->buf = buf;
607 priv->ltt_chan = channel;
608 stream_objd = objd_alloc(priv, &lib_ring_buffer_objd_ops);
f4681817
MD
609 if (stream_objd < 0) {
610 ret = stream_objd;
611 goto objd_error;
612 }
381c0f1e
MD
613 /* Hold a reference on the channel object descriptor */
614 objd_ref(channel_objd);
f4681817
MD
615 return stream_objd;
616
617objd_error:
381c0f1e
MD
618 free(priv);
619alloc_error:
620 channel->ops->buffer_read_close(buf, channel->handle);
f4681817
MD
621 return ret;
622}
f4681817
MD
623
624static
625int lttng_abi_create_event(int channel_objd,
626 struct lttng_ust_event *event_param)
627{
628 struct ltt_channel *channel = objd_private(channel_objd);
629 struct ltt_event *event;
630 int event_objd, ret;
631
632 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
633 event_objd = objd_alloc(NULL, &lttng_event_ops);
634 if (event_objd < 0) {
635 ret = event_objd;
636 goto objd_error;
637 }
638 /*
639 * We tolerate no failure path after event creation. It will stay
640 * invariant for the rest of the session.
641 */
576599a0
MD
642 ret = ltt_event_create(channel, event_param, NULL, &event);
643 if (ret < 0) {
f4681817
MD
644 goto event_error;
645 }
646 objd_set_private(event_objd, event);
647 /* The event holds a reference on the channel */
648 objd_ref(channel_objd);
649 return event_objd;
650
651event_error:
1ea11eab
MD
652 {
653 int err;
654
d4419b81 655 err = lttng_ust_objd_unref(event_objd);
1ea11eab
MD
656 assert(!err);
657 }
f4681817
MD
658objd_error:
659 return ret;
660}
661
1f18504e
MD
662static
663int lttng_abi_create_loglevel(int channel_objd,
664 struct lttng_ust_event *event_param)
665{
666 struct ltt_channel *channel = objd_private(channel_objd);
574a6217 667 struct session_loglevel *loglevel;
1f18504e
MD
668 int loglevel_objd, ret;
669
670 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
671 loglevel_objd = objd_alloc(NULL, &lttng_loglevel_ops);
672 if (loglevel_objd < 0) {
673 ret = loglevel_objd;
674 goto objd_error;
675 }
676 /*
677 * We tolerate no failure path after loglevel creation. It will
678 * stay invariant for the rest of the session.
679 */
680 ret = ltt_loglevel_create(channel, event_param, &loglevel);
681 if (ret < 0) {
682 goto loglevel_error;
683 }
684 objd_set_private(loglevel_objd, loglevel);
685 /* The loglevel holds a reference on the channel */
686 objd_ref(channel_objd);
687 return loglevel_objd;
688
689loglevel_error:
690 {
691 int err;
692
693 err = lttng_ust_objd_unref(loglevel_objd);
694 assert(!err);
695 }
696objd_error:
697 return ret;
698}
699
f4681817
MD
700/**
701 * lttng_channel_cmd - lttng control through object descriptors
702 *
703 * @objd: the object descriptor
704 * @cmd: the command
705 * @arg: command arg
706 *
707 * This object descriptor implements lttng commands:
708 * LTTNG_UST_STREAM
709 * Returns an event stream object descriptor or failure.
710 * (typically, one event stream records events from one CPU)
711 * LTTNG_UST_EVENT
712 * Returns an event object descriptor or failure.
713 * LTTNG_UST_CONTEXT
714 * Prepend a context field to each event in the channel
715 * LTTNG_UST_ENABLE
716 * Enable recording for events in this channel (weak enable)
717 * LTTNG_UST_DISABLE
718 * Disable recording for events in this channel (strong disable)
719 *
720 * Channel and event file descriptors also hold a reference on the session.
721 */
722static
723long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg)
724{
725 struct ltt_channel *channel = objd_private(objd);
726
727 switch (cmd) {
728 case LTTNG_UST_STREAM:
381c0f1e
MD
729 {
730 struct lttng_ust_stream *stream;
731
732 stream = (struct lttng_ust_stream *) arg;
733 /* stream used as output */
734 return lttng_abi_open_stream(objd, stream);
735 }
f4681817 736 case LTTNG_UST_EVENT:
1f18504e
MD
737 {
738 struct lttng_ust_event *event_param =
739 (struct lttng_ust_event *) arg;
740 if (event_param->instrumentation == LTTNG_UST_TRACEPOINT_LOGLEVEL) {
741 return lttng_abi_create_loglevel(objd, event_param);
742 } else {
743 return lttng_abi_create_event(objd, event_param);
744 }
745 }
f4681817
MD
746 case LTTNG_UST_CONTEXT:
747 return lttng_abi_add_context(objd,
748 (struct lttng_ust_context *) arg,
749 &channel->ctx, channel->session);
750 case LTTNG_UST_ENABLE:
751 return ltt_channel_enable(channel);
752 case LTTNG_UST_DISABLE:
753 return ltt_channel_disable(channel);
43d330a4
MD
754 case LTTNG_UST_FLUSH_BUFFER:
755 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
756 default:
757 return -EINVAL;
758 }
759}
760
761/**
762 * lttng_metadata_cmd - lttng control through object descriptors
763 *
764 * @objd: the object descriptor
765 * @cmd: the command
766 * @arg: command arg
767 *
768 * This object descriptor implements lttng commands:
769 * LTTNG_UST_STREAM
770 * Returns an event stream file descriptor or failure.
771 *
772 * Channel and event file descriptors also hold a reference on the session.
773 */
774static
775long lttng_metadata_cmd(int objd, unsigned int cmd, unsigned long arg)
776{
43861eab
MD
777 struct ltt_channel *channel = objd_private(objd);
778
f4681817
MD
779 switch (cmd) {
780 case LTTNG_UST_STREAM:
381c0f1e
MD
781 {
782 struct lttng_ust_stream *stream;
783
784 stream = (struct lttng_ust_stream *) arg;
785 /* stream used as output */
786 return lttng_abi_open_stream(objd, stream);
787 }
43d330a4
MD
788 case LTTNG_UST_FLUSH_BUFFER:
789 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
790 default:
791 return -EINVAL;
792 }
793}
794
795#if 0
796/**
797 * lttng_channel_poll - lttng stream addition/removal monitoring
798 *
799 * @file: the file
800 * @wait: poll table
801 */
802unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
803{
804 struct ltt_channel *channel = file->private_data;
805 unsigned int mask = 0;
806
807 if (file->f_mode & FMODE_READ) {
808 poll_wait_set_exclusive(wait);
809 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
810 wait);
811
812 if (channel->ops->is_disabled(channel->chan))
813 return POLLERR;
814 if (channel->ops->is_finalized(channel->chan))
815 return POLLHUP;
816 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
817 return POLLIN | POLLRDNORM;
818 return 0;
819 }
820 return mask;
821
822}
823#endif //0
824
825static
826int lttng_channel_release(int objd)
827{
828 struct ltt_channel *channel = objd_private(objd);
829
830 if (channel)
d4419b81 831 return lttng_ust_objd_unref(channel->session->objd);
f4681817
MD
832 return 0;
833}
834
b61ce3b2 835static const struct lttng_ust_objd_ops lttng_channel_ops = {
f4681817
MD
836 .release = lttng_channel_release,
837 //.poll = lttng_channel_poll,
838 .cmd = lttng_channel_cmd,
839};
840
b61ce3b2 841static const struct lttng_ust_objd_ops lttng_metadata_ops = {
f4681817
MD
842 .release = lttng_channel_release,
843 .cmd = lttng_metadata_cmd,
844};
845
381c0f1e
MD
846/**
847 * lttng_rb_cmd - lttng ring buffer control through object descriptors
848 *
849 * @objd: the object descriptor
850 * @cmd: the command
851 * @arg: command arg
852 *
853 * This object descriptor implements lttng commands:
854 * (None for now. Access is done directly though shm.)
381c0f1e
MD
855 */
856static
857long lttng_rb_cmd(int objd, unsigned int cmd, unsigned long arg)
858{
381c0f1e
MD
859 switch (cmd) {
860 default:
861 return -EINVAL;
862 }
863}
864
865static
866int lttng_rb_release(int objd)
867{
868 struct stream_priv_data *priv = objd_private(objd);
4cfec15c 869 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e
MD
870 struct ltt_channel *channel;
871
872 if (priv) {
873 buf = priv->buf;
874 channel = priv->ltt_chan;
875 free(priv);
45e9e699
MD
876 /*
877 * If we are at ABI exit, we don't want to close the
878 * buffer opened for read: it is being shared between
879 * the parent and child (right after fork), and we don't
880 * want the child to close it for the parent. For a real
881 * exit, we don't care about marking it as closed, as
882 * the consumer daemon (if there is one) will do fine
883 * even if we don't mark it as "closed" for reading on
884 * our side.
885 * We only mark it as closed if it is being explicitely
886 * released by the session daemon with an explicit
887 * release command.
888 */
889 if (!lttng_ust_abi_close_in_progress)
890 channel->ops->buffer_read_close(buf, channel->handle);
381c0f1e 891
d4419b81 892 return lttng_ust_objd_unref(channel->objd);
381c0f1e
MD
893 }
894 return 0;
895}
896
b61ce3b2 897static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops = {
381c0f1e
MD
898 .release = lttng_rb_release,
899 .cmd = lttng_rb_cmd,
900};
901
f4681817
MD
902/**
903 * lttng_event_cmd - lttng control through object descriptors
904 *
905 * @objd: the object descriptor
906 * @cmd: the command
907 * @arg: command arg
908 *
909 * This object descriptor implements lttng commands:
910 * LTTNG_UST_CONTEXT
911 * Prepend a context field to each record of this event
912 * LTTNG_UST_ENABLE
913 * Enable recording for this event (weak enable)
914 * LTTNG_UST_DISABLE
915 * Disable recording for this event (strong disable)
916 */
917static
918long lttng_event_cmd(int objd, unsigned int cmd, unsigned long arg)
919{
920 struct ltt_event *event = objd_private(objd);
921
922 switch (cmd) {
923 case LTTNG_UST_CONTEXT:
924 return lttng_abi_add_context(objd,
925 (struct lttng_ust_context *) arg,
926 &event->ctx, event->chan->session);
927 case LTTNG_UST_ENABLE:
928 return ltt_event_enable(event);
929 case LTTNG_UST_DISABLE:
930 return ltt_event_disable(event);
931 default:
932 return -EINVAL;
933 }
934}
935
936static
937int lttng_event_release(int objd)
938{
939 struct ltt_event *event = objd_private(objd);
940
941 if (event)
d4419b81 942 return lttng_ust_objd_unref(event->chan->objd);
f4681817
MD
943 return 0;
944}
945
946/* TODO: filter control ioctl */
b61ce3b2 947static const struct lttng_ust_objd_ops lttng_event_ops = {
f4681817
MD
948 .release = lttng_event_release,
949 .cmd = lttng_event_cmd,
950};
951
1f18504e
MD
952/**
953 * lttng_loglevel_cmd - lttng control through object descriptors
954 *
955 * @objd: the object descriptor
956 * @cmd: the command
957 * @arg: command arg
958 *
959 * This object descriptor implements lttng commands:
960 * LTTNG_UST_CONTEXT
961 * Prepend a context field to each record of events of this
962 * loglevel.
963 * LTTNG_UST_ENABLE
964 * Enable recording for these loglevel events (weak enable)
965 * LTTNG_UST_DISABLE
966 * Disable recording for these loglevel events (strong disable)
967 */
968static
969long lttng_loglevel_cmd(int objd, unsigned int cmd, unsigned long arg)
970{
574a6217 971 struct session_loglevel *loglevel = objd_private(objd);
1f18504e
MD
972
973 switch (cmd) {
974 case LTTNG_UST_CONTEXT:
975 return -ENOSYS; /* not implemented yet */
976#if 0
977 return lttng_abi_add_context(objd,
978 (struct lttng_ust_context *) arg,
979 &loglevel->ctx, loglevel->chan->session);
980#endif
981 case LTTNG_UST_ENABLE:
982 return ltt_loglevel_enable(loglevel);
983 case LTTNG_UST_DISABLE:
984 return ltt_loglevel_disable(loglevel);
985 default:
986 return -EINVAL;
987 }
988}
989
990static
991int lttng_loglevel_release(int objd)
992{
574a6217 993 struct session_loglevel *loglevel = objd_private(objd);
1f18504e
MD
994
995 if (loglevel)
996 return lttng_ust_objd_unref(loglevel->chan->objd);
997 return 0;
998}
999
1000/* TODO: filter control ioctl */
1001static const struct lttng_ust_objd_ops lttng_loglevel_ops = {
1002 .release = lttng_loglevel_release,
1003 .cmd = lttng_loglevel_cmd,
1004};
1005
b35d179d 1006void lttng_ust_abi_exit(void)
f4681817 1007{
45e9e699 1008 lttng_ust_abi_close_in_progress = 1;
f4681817 1009 objd_table_destroy();
45e9e699 1010 lttng_ust_abi_close_in_progress = 0;
f4681817 1011}
This page took 0.068462 seconds and 4 git commands to generate.