callsite: add "ip" context
[lttng-ust.git] / liblttng-ust / lttng-ust-abi.c
CommitLineData
f4681817
MD
1/*
2 * lttng-ust-abi.c
3 *
f4681817
MD
4 * LTTng UST ABI
5 *
e92f3e28
MD
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 *
f4681817
MD
23 * Mimic system calls for:
24 * - session creation, returns an object descriptor or failure.
25 * - channel creation, returns an object descriptor or failure.
26 * - Operates on a session object descriptor
27 * - Takes all channel options as parameters.
28 * - stream get, returns an object descriptor or failure.
29 * - Operates on a channel object descriptor.
30 * - stream notifier get, returns an object descriptor or failure.
31 * - Operates on a channel object descriptor.
32 * - event creation, returns an object descriptor or failure.
33 * - Operates on a channel object descriptor
34 * - Takes an event name as parameter
35 * - Takes an instrumentation source as parameter
36 * - e.g. tracepoints, dynamic_probes...
37 * - Takes instrumentation source specific arguments.
f4681817
MD
38 */
39
4318ae1b 40#include <lttng/ust-abi.h>
f4681817
MD
41#include <urcu/compiler.h>
42#include <urcu/list.h>
4318ae1b 43#include <lttng/ust-events.h>
23c8854a 44#include <lttng/ust-version.h>
902e1379 45#include <lttng/tracepoint.h>
d8de1354 46#include "tracepoint-internal.h"
44c72f10
MD
47#include <usterr-signal-safe.h>
48#include <helper.h>
f4681817 49#include "ltt-tracer.h"
4ab44fbe 50
45e9e699
MD
51static int lttng_ust_abi_close_in_progress;
52
51489cad
MD
53static
54int lttng_abi_tracepoint_list(void);
06d4f27e
MD
55static
56int lttng_abi_tracepoint_field_list(void);
51489cad 57
f4681817
MD
58/*
59 * Object descriptor table. Should be protected from concurrent access
60 * by the caller.
61 */
62
b61ce3b2 63struct lttng_ust_obj {
f4681817
MD
64 union {
65 struct {
66 void *private_data;
b61ce3b2 67 const struct lttng_ust_objd_ops *ops;
f4681817
MD
68 int f_count;
69 } s;
70 int freelist_next; /* offset freelist. end is -1. */
71 } u;
72};
73
b61ce3b2
MD
74struct lttng_ust_objd_table {
75 struct lttng_ust_obj *array;
f4681817
MD
76 unsigned int len, allocated_len;
77 int freelist_head; /* offset freelist head. end is -1 */
78};
79
b61ce3b2 80static struct lttng_ust_objd_table objd_table = {
f4681817
MD
81 .freelist_head = -1,
82};
83
84static
b61ce3b2 85int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops)
f4681817 86{
b61ce3b2 87 struct lttng_ust_obj *obj;
f4681817
MD
88
89 if (objd_table.freelist_head != -1) {
90 obj = &objd_table.array[objd_table.freelist_head];
91 objd_table.freelist_head = obj->u.freelist_next;
92 goto end;
93 }
94
95 if (objd_table.len >= objd_table.allocated_len) {
96 unsigned int new_allocated_len, old_allocated_len;
b61ce3b2 97 struct lttng_ust_obj *new_table, *old_table;
f4681817
MD
98
99 old_allocated_len = objd_table.allocated_len;
100 old_table = objd_table.array;
101 if (!old_allocated_len)
102 new_allocated_len = 1;
103 else
104 new_allocated_len = old_allocated_len << 1;
b61ce3b2 105 new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len);
f4681817
MD
106 if (!new_table)
107 return -ENOMEM;
108 memcpy(new_table, old_table,
b61ce3b2 109 sizeof(struct lttng_ust_obj) * old_allocated_len);
f4681817
MD
110 free(old_table);
111 objd_table.array = new_table;
112 objd_table.allocated_len = new_allocated_len;
113 }
114 obj = &objd_table.array[objd_table.len];
115 objd_table.len++;
116end:
117 obj->u.s.private_data = private_data;
118 obj->u.s.ops = ops;
a4be8962
MD
119 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
120 /* count == 2 : allocated + hold ref */
f4681817
MD
121 return obj - objd_table.array;
122}
123
124static
b61ce3b2 125struct lttng_ust_obj *_objd_get(int id)
f4681817
MD
126{
127 if (id >= objd_table.len)
128 return NULL;
a4be8962
MD
129 if (!objd_table.array[id].u.s.f_count)
130 return NULL;
f4681817
MD
131 return &objd_table.array[id];
132}
133
134static
135void *objd_private(int id)
136{
b61ce3b2 137 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
138 assert(obj);
139 return obj->u.s.private_data;
140}
141
142static
143void objd_set_private(int id, void *private_data)
144{
b61ce3b2 145 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
146 assert(obj);
147 obj->u.s.private_data = private_data;
148}
149
b61ce3b2 150const struct lttng_ust_objd_ops *objd_ops(int id)
f4681817 151{
b61ce3b2 152 struct lttng_ust_obj *obj = _objd_get(id);
46050b1a 153
a4be8962
MD
154 if (!obj)
155 return NULL;
f4681817
MD
156 return obj->u.s.ops;
157}
158
159static
160void objd_free(int id)
161{
b61ce3b2 162 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
163
164 assert(obj);
165 obj->u.freelist_next = objd_table.freelist_head;
166 objd_table.freelist_head = obj - objd_table.array;
a4be8962
MD
167 assert(obj->u.s.f_count == 1);
168 obj->u.s.f_count = 0; /* deallocated */
f4681817
MD
169}
170
171static
172void objd_ref(int id)
173{
b61ce3b2 174 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
175 obj->u.s.f_count++;
176}
177
d4419b81 178int lttng_ust_objd_unref(int id)
f4681817 179{
b61ce3b2 180 struct lttng_ust_obj *obj = _objd_get(id);
f4681817 181
1ea11eab
MD
182 if (!obj)
183 return -EINVAL;
a4be8962
MD
184 if (obj->u.s.f_count == 1) {
185 ERR("Reference counting error\n");
186 return -EINVAL;
187 }
188 if ((--obj->u.s.f_count) == 1) {
b61ce3b2 189 const struct lttng_ust_objd_ops *ops = objd_ops(id);
a4be8962 190
f4681817
MD
191 if (ops->release)
192 ops->release(id);
193 objd_free(id);
194 }
1ea11eab 195 return 0;
f4681817
MD
196}
197
198static
199void objd_table_destroy(void)
200{
a4be8962
MD
201 int i;
202
fb50c39d 203 for (i = 0; i < objd_table.allocated_len; i++)
d4419b81 204 (void) lttng_ust_objd_unref(i);
f4681817 205 free(objd_table.array);
02fb3381
MD
206 objd_table.array = NULL;
207 objd_table.len = 0;
208 objd_table.allocated_len = 0;
17dfb34b 209 objd_table.freelist_head = -1;
f4681817
MD
210}
211
212/*
213 * This is LTTng's own personal way to create an ABI for sessiond.
214 * We send commands over a socket.
215 */
216
b61ce3b2
MD
217static const struct lttng_ust_objd_ops lttng_ops;
218static const struct lttng_ust_objd_ops lttng_session_ops;
219static const struct lttng_ust_objd_ops lttng_channel_ops;
220static const struct lttng_ust_objd_ops lttng_metadata_ops;
221static const struct lttng_ust_objd_ops lttng_event_ops;
e6c12e3d 222static const struct lttng_ust_objd_ops lttng_wildcard_ops;
b61ce3b2 223static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops;
51489cad 224static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
06d4f27e 225static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
f4681817
MD
226
227enum channel_type {
228 PER_CPU_CHANNEL,
229 METADATA_CHANNEL,
230};
231
46050b1a
MD
232int lttng_abi_create_root_handle(void)
233{
234 int root_handle;
235
236 root_handle = objd_alloc(NULL, &lttng_ops);
46050b1a
MD
237 return root_handle;
238}
239
818173b9 240static
f4681817
MD
241int lttng_abi_create_session(void)
242{
243 struct ltt_session *session;
244 int session_objd, ret;
245
246 session = ltt_session_create();
247 if (!session)
248 return -ENOMEM;
249 session_objd = objd_alloc(session, &lttng_session_ops);
250 if (session_objd < 0) {
251 ret = session_objd;
252 goto objd_error;
253 }
254 session->objd = session_objd;
255 return session_objd;
256
257objd_error:
258 ltt_session_destroy(session);
259 return ret;
260}
261
f4681817
MD
262static
263long lttng_abi_tracer_version(int objd,
264 struct lttng_ust_tracer_version *v)
265{
0f4eaec3
MD
266 v->major = LTTNG_UST_INTERNAL_MAJOR_VERSION;
267 v->minor = LTTNG_UST_INTERNAL_MINOR_VERSION;
268 v->patchlevel = LTTNG_UST_INTERNAL_PATCHLEVEL_VERSION;
f4681817
MD
269 return 0;
270}
271
272static
273long lttng_abi_add_context(int objd,
274 struct lttng_ust_context *context_param,
275 struct lttng_ctx **ctx, struct ltt_session *session)
276{
277 if (session->been_active)
278 return -EPERM;
279
280 switch (context_param->ctx) {
8de38cf7
MD
281 case LTTNG_UST_CONTEXT_PTHREAD_ID:
282 return lttng_add_pthread_id_to_ctx(ctx);
3b402b40
MD
283 case LTTNG_UST_CONTEXT_VTID:
284 return lttng_add_vtid_to_ctx(ctx);
c1ef86f0
MD
285 case LTTNG_UST_CONTEXT_VPID:
286 return lttng_add_vpid_to_ctx(ctx);
4847e9bb
MD
287 case LTTNG_UST_CONTEXT_PROCNAME:
288 return lttng_add_procname_to_ctx(ctx);
e8e05404
MD
289 case LTTNG_UST_CONTEXT_IP:
290 return lttng_add_ip_to_ctx(ctx);
f4681817
MD
291 default:
292 return -EINVAL;
293 }
294}
295
296/**
297 * lttng_cmd - lttng control through socket commands
298 *
299 * @objd: the object descriptor
300 * @cmd: the command
301 * @arg: command arg
ef9ff354 302 * @uargs: UST arguments (internal)
f4681817
MD
303 *
304 * This descriptor implements lttng commands:
305 * LTTNG_UST_SESSION
306 * Returns a LTTng trace session object descriptor
307 * LTTNG_UST_TRACER_VERSION
308 * Returns the LTTng kernel tracer version
309 * LTTNG_UST_TRACEPOINT_LIST
310 * Returns a file descriptor listing available tracepoints
06d4f27e
MD
311 * LTTNG_UST_TRACEPOINT_FIELD_LIST
312 * Returns a file descriptor listing available tracepoint fields
f4681817
MD
313 * LTTNG_UST_WAIT_QUIESCENT
314 * Returns after all previously running probes have completed
315 *
316 * The returned session will be deleted when its file descriptor is closed.
317 */
318static
ef9ff354
MD
319long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
320 union ust_args *uargs)
f4681817
MD
321{
322 switch (cmd) {
323 case LTTNG_UST_SESSION:
324 return lttng_abi_create_session();
325 case LTTNG_UST_TRACER_VERSION:
326 return lttng_abi_tracer_version(objd,
327 (struct lttng_ust_tracer_version *) arg);
328 case LTTNG_UST_TRACEPOINT_LIST:
51489cad 329 return lttng_abi_tracepoint_list();
06d4f27e
MD
330 case LTTNG_UST_TRACEPOINT_FIELD_LIST:
331 return lttng_abi_tracepoint_field_list();
f4681817
MD
332 case LTTNG_UST_WAIT_QUIESCENT:
333 synchronize_trace();
334 return 0;
335 default:
336 return -EINVAL;
337 }
338}
339
b61ce3b2 340static const struct lttng_ust_objd_ops lttng_ops = {
f4681817
MD
341 .cmd = lttng_cmd,
342};
343
344/*
345 * We tolerate no failure in this function (if one happens, we print a dmesg
346 * error, but cannot return any error, because the channel information is
347 * invariant.
348 */
349static
350void lttng_metadata_create_events(int channel_objd)
351{
352 struct ltt_channel *channel = objd_private(channel_objd);
353 static struct lttng_ust_event metadata_params = {
354 .instrumentation = LTTNG_UST_TRACEPOINT,
a4ada9b8 355 .name = "lttng_ust:metadata",
457a6b58
MD
356 .loglevel_type = LTTNG_UST_LOGLEVEL_ALL,
357 .loglevel = TRACE_DEFAULT,
f4681817
MD
358 };
359 struct ltt_event *event;
576599a0 360 int ret;
f4681817
MD
361
362 /*
363 * We tolerate no failure path after event creation. It will stay
364 * invariant for the rest of the session.
365 */
2d78951a 366 ret = ltt_event_create(channel, &metadata_params, &event);
576599a0 367 if (ret < 0) {
f4681817
MD
368 goto create_error;
369 }
370 return;
371
372create_error:
373 WARN_ON(1);
374 return; /* not allowed to return error */
375}
376
f4681817
MD
377int lttng_abi_create_channel(int session_objd,
378 struct lttng_ust_channel *chan_param,
ef9ff354
MD
379 enum channel_type channel_type,
380 union ust_args *uargs)
f4681817
MD
381{
382 struct ltt_session *session = objd_private(session_objd);
b61ce3b2 383 const struct lttng_ust_objd_ops *ops;
f4681817
MD
384 const char *transport_name;
385 struct ltt_channel *chan;
386 int chan_objd;
387 int ret = 0;
d028eddb 388 struct ltt_channel chan_priv_init;
f4681817 389
f4681817
MD
390 switch (channel_type) {
391 case PER_CPU_CHANNEL:
392 if (chan_param->output == LTTNG_UST_MMAP) {
393 transport_name = chan_param->overwrite ?
394 "relay-overwrite-mmap" : "relay-discard-mmap";
395 } else {
396 return -EINVAL;
397 }
398 ops = &lttng_channel_ops;
399 break;
400 case METADATA_CHANNEL:
401 if (chan_param->output == LTTNG_UST_MMAP)
402 transport_name = "relay-metadata-mmap";
403 else
404 return -EINVAL;
405 ops = &lttng_metadata_ops;
406 break;
407 default:
408 transport_name = "<unknown>";
fe38d4af
MD
409 return -EINVAL;
410 }
411 chan_objd = objd_alloc(NULL, ops);
412 if (chan_objd < 0) {
413 ret = chan_objd;
414 goto objd_error;
f4681817 415 }
d028eddb
MD
416 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
417 /* Copy of session UUID for consumer (availability through shm) */
418 memcpy(chan_priv_init.uuid, session->uuid, sizeof(session->uuid));
419
f4681817
MD
420 /*
421 * We tolerate no failure path after channel creation. It will stay
422 * invariant for the rest of the session.
423 */
424 chan = ltt_channel_create(session, transport_name, NULL,
425 chan_param->subbuf_size,
426 chan_param->num_subbuf,
427 chan_param->switch_timer_interval,
193183fb 428 chan_param->read_timer_interval,
ef9ff354
MD
429 &uargs->channel.shm_fd,
430 &uargs->channel.wait_fd,
431 &uargs->channel.memory_map_size,
d028eddb 432 &chan_priv_init);
f4681817
MD
433 if (!chan) {
434 ret = -EINVAL;
435 goto chan_error;
436 }
437 objd_set_private(chan_objd, chan);
438 chan->objd = chan_objd;
439 if (channel_type == METADATA_CHANNEL) {
440 session->metadata = chan;
441 lttng_metadata_create_events(chan_objd);
442 }
f4681817
MD
443 /* The channel created holds a reference on the session */
444 objd_ref(session_objd);
445
446 return chan_objd;
447
448chan_error:
1ea11eab
MD
449 {
450 int err;
451
d4419b81 452 err = lttng_ust_objd_unref(chan_objd);
1ea11eab
MD
453 assert(!err);
454 }
f4681817
MD
455objd_error:
456 return ret;
457}
458
459/**
460 * lttng_session_cmd - lttng session object command
461 *
462 * @obj: the object
463 * @cmd: the command
464 * @arg: command arg
ef9ff354 465 * @uargs: UST arguments (internal)
f4681817
MD
466 *
467 * This descriptor implements lttng commands:
468 * LTTNG_UST_CHANNEL
469 * Returns a LTTng channel object descriptor
470 * LTTNG_UST_ENABLE
471 * Enables tracing for a session (weak enable)
472 * LTTNG_UST_DISABLE
473 * Disables tracing for a session (strong disable)
474 * LTTNG_UST_METADATA
475 * Returns a LTTng metadata object descriptor
476 *
477 * The returned channel will be deleted when its file descriptor is closed.
478 */
479static
ef9ff354
MD
480long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
481 union ust_args *uargs)
f4681817
MD
482{
483 struct ltt_session *session = objd_private(objd);
484
485 switch (cmd) {
486 case LTTNG_UST_CHANNEL:
487 return lttng_abi_create_channel(objd,
488 (struct lttng_ust_channel *) arg,
ef9ff354 489 PER_CPU_CHANNEL, uargs);
f4681817
MD
490 case LTTNG_UST_SESSION_START:
491 case LTTNG_UST_ENABLE:
492 return ltt_session_enable(session);
493 case LTTNG_UST_SESSION_STOP:
494 case LTTNG_UST_DISABLE:
495 return ltt_session_disable(session);
496 case LTTNG_UST_METADATA:
497 return lttng_abi_create_channel(objd,
498 (struct lttng_ust_channel *) arg,
ef9ff354 499 METADATA_CHANNEL, uargs);
f4681817
MD
500 default:
501 return -EINVAL;
502 }
503}
504
505/*
506 * Called when the last file reference is dropped.
507 *
508 * Big fat note: channels and events are invariant for the whole session after
509 * their creation. So this session destruction also destroys all channel and
510 * event structures specific to this session (they are not destroyed when their
511 * individual file is released).
512 */
513static
1ea11eab 514int lttng_release_session(int objd)
f4681817
MD
515{
516 struct ltt_session *session = objd_private(objd);
517
1ea11eab 518 if (session) {
f4681817 519 ltt_session_destroy(session);
1ea11eab
MD
520 return 0;
521 } else {
522 return -EINVAL;
523 }
f4681817
MD
524}
525
b61ce3b2 526static const struct lttng_ust_objd_ops lttng_session_ops = {
1ea11eab 527 .release = lttng_release_session,
f4681817
MD
528 .cmd = lttng_session_cmd,
529};
530
51489cad 531static
ef9ff354
MD
532long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
533 union ust_args *uargs)
51489cad 534{
c8fcf224 535 struct lttng_ust_tracepoint_list *list = objd_private(objd);
cbef6901
MD
536 struct lttng_ust_tracepoint_iter *tp =
537 (struct lttng_ust_tracepoint_iter *) arg;
c8fcf224 538 struct lttng_ust_tracepoint_iter *iter;
51489cad
MD
539
540 switch (cmd) {
541 case LTTNG_UST_TRACEPOINT_LIST_GET:
c8fcf224
MD
542 {
543 retry:
544 iter = lttng_ust_tracepoint_list_get_iter_next(list);
545 if (!iter)
b115631f 546 return -ENOENT;
c8fcf224
MD
547 if (!strcmp(iter->name, "lttng_ust:metadata"))
548 goto retry;
549 memcpy(tp, iter, sizeof(*tp));
51489cad 550 return 0;
c8fcf224 551 }
51489cad
MD
552 default:
553 return -EINVAL;
554 }
555}
556
557static
558int lttng_abi_tracepoint_list(void)
559{
560 int list_objd, ret;
c8fcf224 561 struct lttng_ust_tracepoint_list *list;
51489cad
MD
562
563 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops);
564 if (list_objd < 0) {
565 ret = list_objd;
566 goto objd_error;
567 }
568 list = zmalloc(sizeof(*list));
569 if (!list) {
570 ret = -ENOMEM;
571 goto alloc_error;
572 }
573 objd_set_private(list_objd, list);
574
c8fcf224
MD
575 /* populate list by walking on all registered probes. */
576 ret = ltt_probes_get_event_list(list);
577 if (ret) {
578 goto list_error;
579 }
51489cad
MD
580 return list_objd;
581
c8fcf224
MD
582list_error:
583 free(list);
51489cad
MD
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{
c8fcf224 598 struct lttng_ust_tracepoint_list *list = objd_private(objd);
51489cad
MD
599
600 if (list) {
c8fcf224 601 ltt_probes_prune_event_list(list);
51489cad
MD
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
06d4f27e
MD
614static
615long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
616 unsigned long arg, union ust_args *uargs)
617{
618 struct lttng_ust_field_list *list = objd_private(objd);
40003310 619 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
06d4f27e
MD
620 struct lttng_ust_field_iter *iter;
621
622 switch (cmd) {
623 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
624 {
625 retry:
626 iter = lttng_ust_field_list_get_iter_next(list);
627 if (!iter)
628 return -ENOENT;
629 if (!strcmp(iter->event_name, "lttng_ust:metadata"))
630 goto retry;
631 memcpy(tp, iter, sizeof(*tp));
632 return 0;
633 }
634 default:
635 return -EINVAL;
636 }
637}
638
639static
640int lttng_abi_tracepoint_field_list(void)
641{
642 int list_objd, ret;
643 struct lttng_ust_field_list *list;
644
645 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops);
646 if (list_objd < 0) {
647 ret = list_objd;
648 goto objd_error;
649 }
650 list = zmalloc(sizeof(*list));
651 if (!list) {
652 ret = -ENOMEM;
653 goto alloc_error;
654 }
655 objd_set_private(list_objd, list);
656
657 /* populate list by walking on all registered probes. */
658 ret = ltt_probes_get_field_list(list);
659 if (ret) {
660 goto list_error;
661 }
662 return list_objd;
663
664list_error:
665 free(list);
666alloc_error:
667 {
668 int err;
669
670 err = lttng_ust_objd_unref(list_objd);
671 assert(!err);
672 }
673objd_error:
674 return ret;
675}
676
677static
678int lttng_release_tracepoint_field_list(int objd)
679{
680 struct lttng_ust_field_list *list = objd_private(objd);
681
682 if (list) {
683 ltt_probes_prune_field_list(list);
684 free(list);
685 return 0;
686 } else {
687 return -EINVAL;
688 }
689}
690
691static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
692 .release = lttng_release_tracepoint_field_list,
693 .cmd = lttng_tracepoint_field_list_cmd,
694};
695
381c0f1e 696struct stream_priv_data {
4cfec15c 697 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e
MD
698 struct ltt_channel *ltt_chan;
699};
700
f4681817 701static
ef9ff354
MD
702int lttng_abi_open_stream(int channel_objd, struct lttng_ust_stream *info,
703 union ust_args *uargs)
f4681817
MD
704{
705 struct ltt_channel *channel = objd_private(channel_objd);
4cfec15c 706 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e 707 struct stream_priv_data *priv;
f4681817
MD
708 int stream_objd, ret;
709
381c0f1e 710 buf = channel->ops->buffer_read_open(channel->chan, channel->handle,
ef9ff354
MD
711 &uargs->stream.shm_fd,
712 &uargs->stream.wait_fd,
713 &uargs->stream.memory_map_size);
f4681817
MD
714 if (!buf)
715 return -ENOENT;
716
381c0f1e
MD
717 priv = zmalloc(sizeof(*priv));
718 if (!priv) {
719 ret = -ENOMEM;
720 goto alloc_error;
721 }
722 priv->buf = buf;
723 priv->ltt_chan = channel;
724 stream_objd = objd_alloc(priv, &lib_ring_buffer_objd_ops);
f4681817
MD
725 if (stream_objd < 0) {
726 ret = stream_objd;
727 goto objd_error;
728 }
381c0f1e
MD
729 /* Hold a reference on the channel object descriptor */
730 objd_ref(channel_objd);
f4681817
MD
731 return stream_objd;
732
733objd_error:
381c0f1e
MD
734 free(priv);
735alloc_error:
736 channel->ops->buffer_read_close(buf, channel->handle);
f4681817
MD
737 return ret;
738}
f4681817
MD
739
740static
741int lttng_abi_create_event(int channel_objd,
742 struct lttng_ust_event *event_param)
743{
744 struct ltt_channel *channel = objd_private(channel_objd);
745 struct ltt_event *event;
746 int event_objd, ret;
747
748 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
749 event_objd = objd_alloc(NULL, &lttng_event_ops);
750 if (event_objd < 0) {
751 ret = event_objd;
752 goto objd_error;
753 }
754 /*
755 * We tolerate no failure path after event creation. It will stay
756 * invariant for the rest of the session.
757 */
2d78951a 758 ret = ltt_event_create(channel, event_param, &event);
576599a0 759 if (ret < 0) {
f4681817
MD
760 goto event_error;
761 }
762 objd_set_private(event_objd, event);
763 /* The event holds a reference on the channel */
764 objd_ref(channel_objd);
765 return event_objd;
766
767event_error:
1ea11eab
MD
768 {
769 int err;
770
d4419b81 771 err = lttng_ust_objd_unref(event_objd);
1ea11eab
MD
772 assert(!err);
773 }
f4681817
MD
774objd_error:
775 return ret;
776}
777
e6c12e3d
MD
778static
779int lttng_abi_create_wildcard(int channel_objd,
780 struct lttng_ust_event *event_param)
781{
782 struct ltt_channel *channel = objd_private(channel_objd);
783 struct session_wildcard *wildcard;
784 int wildcard_objd, ret;
785
786 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
787 wildcard_objd = objd_alloc(NULL, &lttng_wildcard_ops);
788 if (wildcard_objd < 0) {
789 ret = wildcard_objd;
790 goto objd_error;
791 }
792 /*
793 * We tolerate no failure path after wildcard creation. It will
794 * stay invariant for the rest of the session.
795 */
796 ret = ltt_wildcard_create(channel, event_param, &wildcard);
797 if (ret < 0) {
798 goto wildcard_error;
799 }
800 objd_set_private(wildcard_objd, wildcard);
801 /* The wildcard holds a reference on the channel */
802 objd_ref(channel_objd);
803 return wildcard_objd;
804
805wildcard_error:
806 {
807 int err;
808
809 err = lttng_ust_objd_unref(wildcard_objd);
810 assert(!err);
811 }
812objd_error:
813 return ret;
814}
815
f4681817
MD
816/**
817 * lttng_channel_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 object descriptor or failure.
827 * (typically, one event stream records events from one CPU)
828 * LTTNG_UST_EVENT
829 * Returns an event object descriptor or failure.
830 * LTTNG_UST_CONTEXT
831 * Prepend a context field to each event in the channel
832 * LTTNG_UST_ENABLE
833 * Enable recording for events in this channel (weak enable)
834 * LTTNG_UST_DISABLE
835 * Disable recording for events in this channel (strong disable)
836 *
837 * Channel and event file descriptors also hold a reference on the session.
838 */
839static
ef9ff354
MD
840long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
841 union ust_args *uargs)
f4681817
MD
842{
843 struct ltt_channel *channel = objd_private(objd);
844
845 switch (cmd) {
846 case LTTNG_UST_STREAM:
381c0f1e
MD
847 {
848 struct lttng_ust_stream *stream;
849
850 stream = (struct lttng_ust_stream *) arg;
851 /* stream used as output */
ef9ff354 852 return lttng_abi_open_stream(objd, stream, uargs);
381c0f1e 853 }
f4681817 854 case LTTNG_UST_EVENT:
1f18504e
MD
855 {
856 struct lttng_ust_event *event_param =
857 (struct lttng_ust_event *) arg;
6b0e60f1
MD
858 if (event_param->name[strlen(event_param->name) - 1] == '*') {
859 /* If ends with wildcard, create wildcard. */
860 return lttng_abi_create_wildcard(objd, event_param);
1f18504e 861 } else {
6b0e60f1 862 return lttng_abi_create_event(objd, event_param);
1f18504e
MD
863 }
864 }
f4681817
MD
865 case LTTNG_UST_CONTEXT:
866 return lttng_abi_add_context(objd,
867 (struct lttng_ust_context *) arg,
868 &channel->ctx, channel->session);
869 case LTTNG_UST_ENABLE:
870 return ltt_channel_enable(channel);
871 case LTTNG_UST_DISABLE:
872 return ltt_channel_disable(channel);
43d330a4
MD
873 case LTTNG_UST_FLUSH_BUFFER:
874 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
875 default:
876 return -EINVAL;
877 }
878}
879
880/**
881 * lttng_metadata_cmd - lttng control through object descriptors
882 *
883 * @objd: the object descriptor
884 * @cmd: the command
885 * @arg: command arg
ef9ff354 886 * @uargs: UST arguments (internal)
f4681817
MD
887 *
888 * This object descriptor implements lttng commands:
889 * LTTNG_UST_STREAM
890 * Returns an event stream file descriptor or failure.
891 *
892 * Channel and event file descriptors also hold a reference on the session.
893 */
894static
ef9ff354
MD
895long lttng_metadata_cmd(int objd, unsigned int cmd, unsigned long arg,
896 union ust_args *uargs)
f4681817 897{
43861eab
MD
898 struct ltt_channel *channel = objd_private(objd);
899
f4681817
MD
900 switch (cmd) {
901 case LTTNG_UST_STREAM:
381c0f1e
MD
902 {
903 struct lttng_ust_stream *stream;
904
905 stream = (struct lttng_ust_stream *) arg;
906 /* stream used as output */
ef9ff354 907 return lttng_abi_open_stream(objd, stream, uargs);
381c0f1e 908 }
43d330a4
MD
909 case LTTNG_UST_FLUSH_BUFFER:
910 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
911 default:
912 return -EINVAL;
913 }
914}
915
916#if 0
917/**
918 * lttng_channel_poll - lttng stream addition/removal monitoring
919 *
920 * @file: the file
921 * @wait: poll table
922 */
923unsigned int lttng_channel_poll(struct file *file, poll_table *wait)
924{
925 struct ltt_channel *channel = file->private_data;
926 unsigned int mask = 0;
927
928 if (file->f_mode & FMODE_READ) {
929 poll_wait_set_exclusive(wait);
930 poll_wait(file, channel->ops->get_hp_wait_queue(channel->chan),
931 wait);
932
933 if (channel->ops->is_disabled(channel->chan))
934 return POLLERR;
935 if (channel->ops->is_finalized(channel->chan))
936 return POLLHUP;
937 if (channel->ops->buffer_has_read_closed_stream(channel->chan))
938 return POLLIN | POLLRDNORM;
939 return 0;
940 }
941 return mask;
942
943}
944#endif //0
945
946static
947int lttng_channel_release(int objd)
948{
949 struct ltt_channel *channel = objd_private(objd);
950
951 if (channel)
d4419b81 952 return lttng_ust_objd_unref(channel->session->objd);
f4681817
MD
953 return 0;
954}
955
b61ce3b2 956static const struct lttng_ust_objd_ops lttng_channel_ops = {
f4681817
MD
957 .release = lttng_channel_release,
958 //.poll = lttng_channel_poll,
959 .cmd = lttng_channel_cmd,
960};
961
b61ce3b2 962static const struct lttng_ust_objd_ops lttng_metadata_ops = {
f4681817
MD
963 .release = lttng_channel_release,
964 .cmd = lttng_metadata_cmd,
965};
966
381c0f1e
MD
967/**
968 * lttng_rb_cmd - lttng ring buffer control through object descriptors
969 *
970 * @objd: the object descriptor
971 * @cmd: the command
972 * @arg: command arg
ef9ff354 973 * @uargs: UST arguments (internal)
381c0f1e
MD
974 *
975 * This object descriptor implements lttng commands:
976 * (None for now. Access is done directly though shm.)
381c0f1e
MD
977 */
978static
ef9ff354
MD
979long lttng_rb_cmd(int objd, unsigned int cmd, unsigned long arg,
980 union ust_args *uargs)
381c0f1e 981{
381c0f1e
MD
982 switch (cmd) {
983 default:
984 return -EINVAL;
985 }
986}
987
988static
989int lttng_rb_release(int objd)
990{
991 struct stream_priv_data *priv = objd_private(objd);
4cfec15c 992 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e
MD
993 struct ltt_channel *channel;
994
995 if (priv) {
996 buf = priv->buf;
997 channel = priv->ltt_chan;
998 free(priv);
45e9e699
MD
999 /*
1000 * If we are at ABI exit, we don't want to close the
1001 * buffer opened for read: it is being shared between
1002 * the parent and child (right after fork), and we don't
1003 * want the child to close it for the parent. For a real
1004 * exit, we don't care about marking it as closed, as
1005 * the consumer daemon (if there is one) will do fine
1006 * even if we don't mark it as "closed" for reading on
1007 * our side.
1008 * We only mark it as closed if it is being explicitely
1009 * released by the session daemon with an explicit
1010 * release command.
1011 */
1012 if (!lttng_ust_abi_close_in_progress)
1013 channel->ops->buffer_read_close(buf, channel->handle);
381c0f1e 1014
d4419b81 1015 return lttng_ust_objd_unref(channel->objd);
381c0f1e
MD
1016 }
1017 return 0;
1018}
1019
b61ce3b2 1020static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops = {
381c0f1e
MD
1021 .release = lttng_rb_release,
1022 .cmd = lttng_rb_cmd,
1023};
1024
f4681817
MD
1025/**
1026 * lttng_event_cmd - lttng control through object descriptors
1027 *
1028 * @objd: the object descriptor
1029 * @cmd: the command
1030 * @arg: command arg
ef9ff354 1031 * @uargs: UST arguments (internal)
f4681817
MD
1032 *
1033 * This object descriptor implements lttng commands:
1034 * LTTNG_UST_CONTEXT
1035 * Prepend a context field to each record of this event
1036 * LTTNG_UST_ENABLE
1037 * Enable recording for this event (weak enable)
1038 * LTTNG_UST_DISABLE
1039 * Disable recording for this event (strong disable)
2d78951a
MD
1040 * LTTNG_UST_FILTER
1041 * Attach a filter to an event.
f4681817
MD
1042 */
1043static
ef9ff354
MD
1044long lttng_event_cmd(int objd, unsigned int cmd, unsigned long arg,
1045 union ust_args *uargs)
f4681817
MD
1046{
1047 struct ltt_event *event = objd_private(objd);
1048
1049 switch (cmd) {
1050 case LTTNG_UST_CONTEXT:
1051 return lttng_abi_add_context(objd,
1052 (struct lttng_ust_context *) arg,
1053 &event->ctx, event->chan->session);
1054 case LTTNG_UST_ENABLE:
1055 return ltt_event_enable(event);
1056 case LTTNG_UST_DISABLE:
1057 return ltt_event_disable(event);
2d78951a
MD
1058 case LTTNG_UST_FILTER:
1059 {
1060 int ret;
1061 ret = lttng_filter_event_attach_bytecode(event,
1062 (struct lttng_ust_filter_bytecode *) arg);
1063 if (ret)
1064 return ret;
1065 lttng_filter_event_link_bytecode(event,
1066 event->filter_bytecode);
1067 return 0;
1068 }
f4681817
MD
1069 default:
1070 return -EINVAL;
1071 }
1072}
1073
1074static
1075int lttng_event_release(int objd)
1076{
1077 struct ltt_event *event = objd_private(objd);
1078
1079 if (event)
d4419b81 1080 return lttng_ust_objd_unref(event->chan->objd);
f4681817
MD
1081 return 0;
1082}
1083
1084/* TODO: filter control ioctl */
b61ce3b2 1085static const struct lttng_ust_objd_ops lttng_event_ops = {
f4681817
MD
1086 .release = lttng_event_release,
1087 .cmd = lttng_event_cmd,
1088};
1089
e6c12e3d
MD
1090/**
1091 * lttng_wildcard_cmd - lttng control through object descriptors
1092 *
1093 * @objd: the object descriptor
1094 * @cmd: the command
1095 * @arg: command arg
ef9ff354 1096 * @uargs: UST arguments (internal)
e6c12e3d
MD
1097 *
1098 * This object descriptor implements lttng commands:
1099 * LTTNG_UST_CONTEXT
1100 * Prepend a context field to each record of events of this
1101 * wildcard.
1102 * LTTNG_UST_ENABLE
1103 * Enable recording for these wildcard events (weak enable)
1104 * LTTNG_UST_DISABLE
1105 * Disable recording for these wildcard events (strong disable)
2d78951a
MD
1106 * LTTNG_UST_FILTER
1107 * Attach a filter to a wildcard.
e6c12e3d
MD
1108 */
1109static
ef9ff354
MD
1110long lttng_wildcard_cmd(int objd, unsigned int cmd, unsigned long arg,
1111 union ust_args *uargs)
e6c12e3d
MD
1112{
1113 struct session_wildcard *wildcard = objd_private(objd);
1114
1115 switch (cmd) {
1116 case LTTNG_UST_CONTEXT:
1117 return -ENOSYS; /* not implemented yet */
1118#if 0
1119 return lttng_abi_add_context(objd,
1120 (struct lttng_ust_context *) arg,
1121 &wildcard->ctx, wildcard->chan->session);
1122#endif
1123 case LTTNG_UST_ENABLE:
1124 return ltt_wildcard_enable(wildcard);
1125 case LTTNG_UST_DISABLE:
1126 return ltt_wildcard_disable(wildcard);
2d78951a
MD
1127 case LTTNG_UST_FILTER:
1128 {
1129 int ret;
1130
1131 ret = lttng_filter_wildcard_attach_bytecode(wildcard,
1132 (struct lttng_ust_filter_bytecode *) arg);
1133 if (ret)
1134 return ret;
1135 lttng_filter_wildcard_link_bytecode(wildcard);
1136 return 0;
1137 }
e6c12e3d
MD
1138 default:
1139 return -EINVAL;
1140 }
1141}
1142
1143static
1144int lttng_wildcard_release(int objd)
1145{
1146 struct session_wildcard *wildcard = objd_private(objd);
1147
1148 if (wildcard)
1149 return lttng_ust_objd_unref(wildcard->chan->objd);
1150 return 0;
1151}
1152
1153/* TODO: filter control ioctl */
1154static const struct lttng_ust_objd_ops lttng_wildcard_ops = {
1155 .release = lttng_wildcard_release,
1156 .cmd = lttng_wildcard_cmd,
1157};
1158
b35d179d 1159void lttng_ust_abi_exit(void)
f4681817 1160{
45e9e699 1161 lttng_ust_abi_close_in_progress = 1;
f4681817 1162 objd_table_destroy();
45e9e699 1163 lttng_ust_abi_close_in_progress = 0;
f4681817 1164}
This page took 0.082589 seconds and 4 git commands to generate.