Rename "tsc" to "timestamp"
[lttng-ust.git] / liblttng-ust / lttng-ust-abi.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2010-2012 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
25 #define _LGPL_SOURCE
26 #include <fcntl.h>
27 #include <stdint.h>
28 #include <unistd.h>
29
30 #include <urcu/compiler.h>
31 #include <urcu/list.h>
32
33 #include <ust-helper.h>
34 #include <lttng/tracepoint.h>
35 #include <lttng/ust-abi.h>
36 #include <lttng/ust-error.h>
37 #include <lttng/ust-events.h>
38 #include <lttng/ust-version.h>
39 #include <ust-fd.h>
40 #include <usterr-signal-safe.h>
41
42 #include "../libringbuffer/frontend_types.h"
43 #include "../libringbuffer/shm.h"
44 #include "../libcounter/counter.h"
45 #include "tracepoint-internal.h"
46 #include "lttng-tracer.h"
47 #include "string-utils.h"
48 #include "ust-events-internal.h"
49
50 #define OBJ_NAME_LEN 16
51
52 static int lttng_ust_abi_close_in_progress;
53
54 static
55 int lttng_abi_tracepoint_list(void *owner);
56 static
57 int lttng_abi_tracepoint_field_list(void *owner);
58
59 /*
60 * Object descriptor table. Should be protected from concurrent access
61 * by the caller.
62 */
63
64 struct lttng_ust_obj {
65 union {
66 struct {
67 void *private_data;
68 const struct lttng_ust_objd_ops *ops;
69 int f_count;
70 int owner_ref; /* has ref from owner */
71 void *owner;
72 char name[OBJ_NAME_LEN];
73 } s;
74 int freelist_next; /* offset freelist. end is -1. */
75 } u;
76 };
77
78 struct lttng_ust_objd_table {
79 struct lttng_ust_obj *array;
80 unsigned int len, allocated_len;
81 int freelist_head; /* offset freelist head. end is -1 */
82 };
83
84 static struct lttng_ust_objd_table objd_table = {
85 .freelist_head = -1,
86 };
87
88 static
89 int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops,
90 void *owner, const char *name)
91 {
92 struct lttng_ust_obj *obj;
93
94 if (objd_table.freelist_head != -1) {
95 obj = &objd_table.array[objd_table.freelist_head];
96 objd_table.freelist_head = obj->u.freelist_next;
97 goto end;
98 }
99
100 if (objd_table.len >= objd_table.allocated_len) {
101 unsigned int new_allocated_len, old_allocated_len;
102 struct lttng_ust_obj *new_table, *old_table;
103
104 old_allocated_len = objd_table.allocated_len;
105 old_table = objd_table.array;
106 if (!old_allocated_len)
107 new_allocated_len = 1;
108 else
109 new_allocated_len = old_allocated_len << 1;
110 new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len);
111 if (!new_table)
112 return -ENOMEM;
113 memcpy(new_table, old_table,
114 sizeof(struct lttng_ust_obj) * old_allocated_len);
115 free(old_table);
116 objd_table.array = new_table;
117 objd_table.allocated_len = new_allocated_len;
118 }
119 obj = &objd_table.array[objd_table.len];
120 objd_table.len++;
121 end:
122 obj->u.s.private_data = private_data;
123 obj->u.s.ops = ops;
124 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
125 /* count == 2 : allocated + hold ref */
126 obj->u.s.owner_ref = 1; /* One owner reference */
127 obj->u.s.owner = owner;
128 strncpy(obj->u.s.name, name, OBJ_NAME_LEN);
129 obj->u.s.name[OBJ_NAME_LEN - 1] = '\0';
130 return obj - objd_table.array;
131 }
132
133 static
134 struct lttng_ust_obj *_objd_get(int id)
135 {
136 if (id >= objd_table.len)
137 return NULL;
138 if (!objd_table.array[id].u.s.f_count)
139 return NULL;
140 return &objd_table.array[id];
141 }
142
143 static
144 void *objd_private(int id)
145 {
146 struct lttng_ust_obj *obj = _objd_get(id);
147 assert(obj);
148 return obj->u.s.private_data;
149 }
150
151 static
152 void objd_set_private(int id, void *private_data)
153 {
154 struct lttng_ust_obj *obj = _objd_get(id);
155 assert(obj);
156 obj->u.s.private_data = private_data;
157 }
158
159 const struct lttng_ust_objd_ops *objd_ops(int id)
160 {
161 struct lttng_ust_obj *obj = _objd_get(id);
162
163 if (!obj)
164 return NULL;
165 return obj->u.s.ops;
166 }
167
168 static
169 void objd_free(int id)
170 {
171 struct lttng_ust_obj *obj = _objd_get(id);
172
173 assert(obj);
174 obj->u.freelist_next = objd_table.freelist_head;
175 objd_table.freelist_head = obj - objd_table.array;
176 assert(obj->u.s.f_count == 1);
177 obj->u.s.f_count = 0; /* deallocated */
178 }
179
180 static
181 void objd_ref(int id)
182 {
183 struct lttng_ust_obj *obj = _objd_get(id);
184 assert(obj != NULL);
185 obj->u.s.f_count++;
186 }
187
188 int lttng_ust_objd_unref(int id, int is_owner)
189 {
190 struct lttng_ust_obj *obj = _objd_get(id);
191
192 if (!obj)
193 return -EINVAL;
194 if (obj->u.s.f_count == 1) {
195 ERR("Reference counting error\n");
196 return -EINVAL;
197 }
198 if (is_owner) {
199 if (!obj->u.s.owner_ref) {
200 ERR("Error decrementing owner reference");
201 return -EINVAL;
202 }
203 obj->u.s.owner_ref--;
204 }
205 if ((--obj->u.s.f_count) == 1) {
206 const struct lttng_ust_objd_ops *ops = objd_ops(id);
207
208 if (ops->release)
209 ops->release(id);
210 objd_free(id);
211 }
212 return 0;
213 }
214
215 static
216 void objd_table_destroy(void)
217 {
218 int i;
219
220 for (i = 0; i < objd_table.allocated_len; i++) {
221 struct lttng_ust_obj *obj;
222
223 obj = _objd_get(i);
224 if (!obj)
225 continue;
226 if (!obj->u.s.owner_ref)
227 continue; /* only unref owner ref. */
228 (void) lttng_ust_objd_unref(i, 1);
229 }
230 free(objd_table.array);
231 objd_table.array = NULL;
232 objd_table.len = 0;
233 objd_table.allocated_len = 0;
234 objd_table.freelist_head = -1;
235 }
236
237 const char *lttng_ust_obj_get_name(int id)
238 {
239 struct lttng_ust_obj *obj = _objd_get(id);
240
241 if (!obj)
242 return NULL;
243 return obj->u.s.name;
244 }
245
246 void lttng_ust_objd_table_owner_cleanup(void *owner)
247 {
248 int i;
249
250 for (i = 0; i < objd_table.allocated_len; i++) {
251 struct lttng_ust_obj *obj;
252
253 obj = _objd_get(i);
254 if (!obj)
255 continue;
256 if (!obj->u.s.owner)
257 continue; /* skip root handles */
258 if (!obj->u.s.owner_ref)
259 continue; /* only unref owner ref. */
260 if (obj->u.s.owner == owner)
261 (void) lttng_ust_objd_unref(i, 1);
262 }
263 }
264
265 /*
266 * This is LTTng's own personal way to create an ABI for sessiond.
267 * We send commands over a socket.
268 */
269
270 static const struct lttng_ust_objd_ops lttng_ops;
271 static const struct lttng_ust_objd_ops lttng_event_notifier_group_ops;
272 static const struct lttng_ust_objd_ops lttng_session_ops;
273 static const struct lttng_ust_objd_ops lttng_channel_ops;
274 static const struct lttng_ust_objd_ops lttng_event_enabler_ops;
275 static const struct lttng_ust_objd_ops lttng_event_notifier_enabler_ops;
276 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
277 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
278
279 int lttng_abi_create_root_handle(void)
280 {
281 int root_handle;
282
283 /* root handles have NULL owners */
284 root_handle = objd_alloc(NULL, &lttng_ops, NULL, "root");
285 return root_handle;
286 }
287
288 static
289 int lttng_is_channel_ready(struct lttng_channel *lttng_chan)
290 {
291 struct channel *chan;
292 unsigned int nr_streams, exp_streams;
293
294 chan = lttng_chan->chan;
295 nr_streams = channel_handle_get_nr_streams(lttng_chan->handle);
296 exp_streams = chan->nr_streams;
297 return nr_streams == exp_streams;
298 }
299
300 static
301 int lttng_abi_create_session(void *owner)
302 {
303 struct lttng_session *session;
304 int session_objd, ret;
305
306 session = lttng_session_create();
307 if (!session)
308 return -ENOMEM;
309 session_objd = objd_alloc(session, &lttng_session_ops, owner, "session");
310 if (session_objd < 0) {
311 ret = session_objd;
312 goto objd_error;
313 }
314 session->objd = session_objd;
315 session->owner = owner;
316 return session_objd;
317
318 objd_error:
319 lttng_session_destroy(session);
320 return ret;
321 }
322
323 static
324 long lttng_abi_tracer_version(int objd,
325 struct lttng_ust_tracer_version *v)
326 {
327 v->major = LTTNG_UST_MAJOR_VERSION;
328 v->minor = LTTNG_UST_MINOR_VERSION;
329 v->patchlevel = LTTNG_UST_PATCHLEVEL_VERSION;
330 return 0;
331 }
332
333 static
334 int lttng_abi_event_notifier_send_fd(void *owner, int *event_notifier_notif_fd)
335 {
336 struct lttng_event_notifier_group *event_notifier_group;
337 int event_notifier_group_objd, ret, fd_flag;
338
339 event_notifier_group = lttng_event_notifier_group_create();
340 if (!event_notifier_group)
341 return -ENOMEM;
342
343 /*
344 * Set this file descriptor as NON-BLOCKING.
345 */
346 fd_flag = fcntl(*event_notifier_notif_fd, F_GETFL);
347
348 fd_flag |= O_NONBLOCK;
349
350 ret = fcntl(*event_notifier_notif_fd, F_SETFL, fd_flag);
351 if (ret) {
352 ret = -errno;
353 goto fd_error;
354 }
355
356 event_notifier_group_objd = objd_alloc(event_notifier_group,
357 &lttng_event_notifier_group_ops, owner, "event_notifier_group");
358 if (event_notifier_group_objd < 0) {
359 ret = event_notifier_group_objd;
360 goto objd_error;
361 }
362
363 event_notifier_group->objd = event_notifier_group_objd;
364 event_notifier_group->owner = owner;
365 event_notifier_group->notification_fd = *event_notifier_notif_fd;
366 /* Object descriptor takes ownership of notification fd. */
367 *event_notifier_notif_fd = -1;
368
369 return event_notifier_group_objd;
370
371 objd_error:
372 lttng_event_notifier_group_destroy(event_notifier_group);
373 fd_error:
374 return ret;
375 }
376
377 static
378 long lttng_abi_add_context(int objd,
379 struct lttng_ust_context *context_param,
380 union ust_args *uargs,
381 struct lttng_ctx **ctx, struct lttng_session *session)
382 {
383 return lttng_attach_context(context_param, uargs, ctx, session);
384 }
385
386 /**
387 * lttng_cmd - lttng control through socket commands
388 *
389 * @objd: the object descriptor
390 * @cmd: the command
391 * @arg: command arg
392 * @uargs: UST arguments (internal)
393 * @owner: objd owner
394 *
395 * This descriptor implements lttng commands:
396 * LTTNG_UST_SESSION
397 * Returns a LTTng trace session object descriptor
398 * LTTNG_UST_TRACER_VERSION
399 * Returns the LTTng kernel tracer version
400 * LTTNG_UST_TRACEPOINT_LIST
401 * Returns a file descriptor listing available tracepoints
402 * LTTNG_UST_TRACEPOINT_FIELD_LIST
403 * Returns a file descriptor listing available tracepoint fields
404 * LTTNG_UST_WAIT_QUIESCENT
405 * Returns after all previously running probes have completed
406 *
407 * The returned session will be deleted when its file descriptor is closed.
408 */
409 static
410 long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
411 union ust_args *uargs, void *owner)
412 {
413 switch (cmd) {
414 case LTTNG_UST_SESSION:
415 return lttng_abi_create_session(owner);
416 case LTTNG_UST_TRACER_VERSION:
417 return lttng_abi_tracer_version(objd,
418 (struct lttng_ust_tracer_version *) arg);
419 case LTTNG_UST_TRACEPOINT_LIST:
420 return lttng_abi_tracepoint_list(owner);
421 case LTTNG_UST_TRACEPOINT_FIELD_LIST:
422 return lttng_abi_tracepoint_field_list(owner);
423 case LTTNG_UST_WAIT_QUIESCENT:
424 lttng_ust_synchronize_trace();
425 return 0;
426 case LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE:
427 return lttng_abi_event_notifier_send_fd(owner,
428 &uargs->event_notifier_handle.event_notifier_notif_fd);
429 default:
430 return -EINVAL;
431 }
432 }
433
434 static const struct lttng_ust_objd_ops lttng_ops = {
435 .cmd = lttng_cmd,
436 };
437
438 static
439 int lttng_abi_map_channel(int session_objd,
440 struct lttng_ust_channel *ust_chan,
441 union ust_args *uargs,
442 void *owner)
443 {
444 struct lttng_session *session = objd_private(session_objd);
445 const char *transport_name;
446 const struct lttng_transport *transport;
447 const char *chan_name;
448 int chan_objd;
449 struct lttng_ust_shm_handle *channel_handle;
450 struct lttng_channel *lttng_chan;
451 struct channel *chan;
452 struct lttng_ust_lib_ring_buffer_config *config;
453 void *chan_data;
454 int wakeup_fd;
455 uint64_t len;
456 int ret;
457 enum lttng_ust_chan_type type;
458
459 chan_data = uargs->channel.chan_data;
460 wakeup_fd = uargs->channel.wakeup_fd;
461 len = ust_chan->len;
462 type = ust_chan->type;
463
464 switch (type) {
465 case LTTNG_UST_CHAN_PER_CPU:
466 break;
467 default:
468 ret = -EINVAL;
469 goto invalid;
470 }
471
472 if (session->been_active) {
473 ret = -EBUSY;
474 goto active; /* Refuse to add channel to active session */
475 }
476
477 channel_handle = channel_handle_create(chan_data, len, wakeup_fd);
478 if (!channel_handle) {
479 ret = -EINVAL;
480 goto handle_error;
481 }
482
483 /* Ownership of chan_data and wakeup_fd taken by channel handle. */
484 uargs->channel.chan_data = NULL;
485 uargs->channel.wakeup_fd = -1;
486
487 chan = shmp(channel_handle, channel_handle->chan);
488 assert(chan);
489 chan->handle = channel_handle;
490 config = &chan->backend.config;
491 lttng_chan = channel_get_private(chan);
492 if (!lttng_chan) {
493 ret = -EINVAL;
494 goto alloc_error;
495 }
496
497 /* Lookup transport name */
498 switch (type) {
499 case LTTNG_UST_CHAN_PER_CPU:
500 if (config->output == RING_BUFFER_MMAP) {
501 if (config->mode == RING_BUFFER_OVERWRITE) {
502 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER) {
503 transport_name = "relay-overwrite-mmap";
504 } else {
505 transport_name = "relay-overwrite-rt-mmap";
506 }
507 } else {
508 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER) {
509 transport_name = "relay-discard-mmap";
510 } else {
511 transport_name = "relay-discard-rt-mmap";
512 }
513 }
514 } else {
515 ret = -EINVAL;
516 goto notransport;
517 }
518 chan_name = "channel";
519 break;
520 default:
521 ret = -EINVAL;
522 goto notransport;
523 }
524 transport = lttng_transport_find(transport_name);
525 if (!transport) {
526 DBG("LTTng transport %s not found\n",
527 transport_name);
528 ret = -EINVAL;
529 goto notransport;
530 }
531
532 chan_objd = objd_alloc(NULL, &lttng_channel_ops, owner, chan_name);
533 if (chan_objd < 0) {
534 ret = chan_objd;
535 goto objd_error;
536 }
537
538 /* Initialize our lttng chan */
539 lttng_chan->chan = chan;
540 lttng_chan->tstate = 1;
541 lttng_chan->enabled = 1;
542 lttng_chan->ctx = NULL;
543 lttng_chan->session = session;
544 lttng_chan->ops = &transport->ops;
545 memcpy(&lttng_chan->chan->backend.config,
546 transport->client_config,
547 sizeof(lttng_chan->chan->backend.config));
548 cds_list_add(&lttng_chan->node, &session->chan_head);
549 lttng_chan->header_type = 0;
550 lttng_chan->handle = channel_handle;
551 lttng_chan->type = type;
552
553 /*
554 * We tolerate no failure path after channel creation. It will stay
555 * invariant for the rest of the session.
556 */
557 objd_set_private(chan_objd, lttng_chan);
558 lttng_chan->objd = chan_objd;
559 /* The channel created holds a reference on the session */
560 objd_ref(session_objd);
561 return chan_objd;
562
563 /* error path after channel was created */
564 objd_error:
565 notransport:
566 alloc_error:
567 channel_destroy(chan, channel_handle, 0);
568 return ret;
569
570 handle_error:
571 active:
572 invalid:
573 return ret;
574 }
575
576 /**
577 * lttng_session_cmd - lttng session object command
578 *
579 * @obj: the object
580 * @cmd: the command
581 * @arg: command arg
582 * @uargs: UST arguments (internal)
583 * @owner: objd owner
584 *
585 * This descriptor implements lttng commands:
586 * LTTNG_UST_CHANNEL
587 * Returns a LTTng channel object descriptor
588 * LTTNG_UST_ENABLE
589 * Enables tracing for a session (weak enable)
590 * LTTNG_UST_DISABLE
591 * Disables tracing for a session (strong disable)
592 *
593 * The returned channel will be deleted when its file descriptor is closed.
594 */
595 static
596 long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
597 union ust_args *uargs, void *owner)
598 {
599 struct lttng_session *session = objd_private(objd);
600
601 switch (cmd) {
602 case LTTNG_UST_CHANNEL:
603 return lttng_abi_map_channel(objd,
604 (struct lttng_ust_channel *) arg,
605 uargs, owner);
606 case LTTNG_UST_SESSION_START:
607 case LTTNG_UST_ENABLE:
608 return lttng_session_enable(session);
609 case LTTNG_UST_SESSION_STOP:
610 case LTTNG_UST_DISABLE:
611 return lttng_session_disable(session);
612 case LTTNG_UST_SESSION_STATEDUMP:
613 return lttng_session_statedump(session);
614 case LTTNG_UST_COUNTER:
615 case LTTNG_UST_COUNTER_GLOBAL:
616 case LTTNG_UST_COUNTER_CPU:
617 /* Not implemented yet. */
618 return -EINVAL;
619 default:
620 return -EINVAL;
621 }
622 }
623
624 /*
625 * Called when the last file reference is dropped.
626 *
627 * Big fat note: channels and events are invariant for the whole session after
628 * their creation. So this session destruction also destroys all channel and
629 * event structures specific to this session (they are not destroyed when their
630 * individual file is released).
631 */
632 static
633 int lttng_release_session(int objd)
634 {
635 struct lttng_session *session = objd_private(objd);
636
637 if (session) {
638 lttng_session_destroy(session);
639 return 0;
640 } else {
641 return -EINVAL;
642 }
643 }
644
645 static const struct lttng_ust_objd_ops lttng_session_ops = {
646 .release = lttng_release_session,
647 .cmd = lttng_session_cmd,
648 };
649
650 static int lttng_ust_event_notifier_enabler_create(int event_notifier_group_obj,
651 void *owner, struct lttng_ust_event_notifier *event_notifier_param,
652 enum lttng_enabler_format_type type)
653 {
654 struct lttng_event_notifier_group *event_notifier_group =
655 objd_private(event_notifier_group_obj);
656 struct lttng_event_notifier_enabler *event_notifier_enabler;
657 int event_notifier_objd, ret;
658
659 event_notifier_param->event.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
660 event_notifier_objd = objd_alloc(NULL, &lttng_event_notifier_enabler_ops, owner,
661 "event_notifier enabler");
662 if (event_notifier_objd < 0) {
663 ret = event_notifier_objd;
664 goto objd_error;
665 }
666
667 event_notifier_enabler = lttng_event_notifier_enabler_create(
668 event_notifier_group, type, event_notifier_param);
669 if (!event_notifier_enabler) {
670 ret = -ENOMEM;
671 goto event_notifier_error;
672 }
673
674 objd_set_private(event_notifier_objd, event_notifier_enabler);
675 /* The event_notifier holds a reference on the event_notifier group. */
676 objd_ref(event_notifier_enabler->group->objd);
677
678 return event_notifier_objd;
679
680 event_notifier_error:
681 {
682 int err;
683
684 err = lttng_ust_objd_unref(event_notifier_objd, 1);
685 assert(!err);
686 }
687 objd_error:
688 return ret;
689 }
690
691 static
692 long lttng_event_notifier_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
693 union ust_args *uargs, void *owner)
694 {
695 struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
696 switch (cmd) {
697 case LTTNG_UST_FILTER:
698 return lttng_event_notifier_enabler_attach_filter_bytecode(
699 event_notifier_enabler,
700 (struct lttng_ust_bytecode_node **) arg);
701 case LTTNG_UST_EXCLUSION:
702 return lttng_event_notifier_enabler_attach_exclusion(event_notifier_enabler,
703 (struct lttng_ust_excluder_node **) arg);
704 case LTTNG_UST_CAPTURE:
705 return lttng_event_notifier_enabler_attach_capture_bytecode(
706 event_notifier_enabler,
707 (struct lttng_ust_bytecode_node **) arg);
708 case LTTNG_UST_ENABLE:
709 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
710 case LTTNG_UST_DISABLE:
711 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
712 default:
713 return -EINVAL;
714 }
715 }
716
717 /**
718 * lttng_event_notifier_group_error_counter_cmd - lttng event_notifier group error counter object command
719 *
720 * @obj: the object
721 * @cmd: the command
722 * @arg: command arg
723 * @uargs: UST arguments (internal)
724 * @owner: objd owner
725 *
726 * This descriptor implements lttng commands:
727 * LTTNG_UST_COUNTER_GLOBAL
728 * Return negative error code on error, 0 on success.
729 * LTTNG_UST_COUNTER_CPU
730 * Return negative error code on error, 0 on success.
731 */
732 static
733 long lttng_event_notifier_group_error_counter_cmd(int objd, unsigned int cmd, unsigned long arg,
734 union ust_args *uargs, void *owner)
735 {
736 int ret;
737 struct lttng_counter *counter = objd_private(objd);
738
739 switch (cmd) {
740 case LTTNG_UST_COUNTER_GLOBAL:
741 ret = -EINVAL; /* Unimplemented. */
742 break;
743 case LTTNG_UST_COUNTER_CPU:
744 {
745 struct lttng_ust_counter_cpu *counter_cpu =
746 (struct lttng_ust_counter_cpu *)arg;
747
748 ret = lttng_counter_set_cpu_shm(counter->counter,
749 counter_cpu->cpu_nr, uargs->counter_shm.shm_fd);
750 if (!ret) {
751 /* Take ownership of the shm_fd. */
752 uargs->counter_shm.shm_fd = -1;
753 }
754 break;
755 }
756 default:
757 ret = -EINVAL;
758 break;
759 }
760
761 return ret;
762 }
763
764 LTTNG_HIDDEN
765 int lttng_release_event_notifier_group_error_counter(int objd)
766 {
767 struct lttng_counter *counter = objd_private(objd);
768
769 if (counter) {
770 return lttng_ust_objd_unref(counter->event_notifier_group->objd, 0);
771 } else {
772 return -EINVAL;
773 }
774 }
775
776 static const struct lttng_ust_objd_ops lttng_event_notifier_group_error_counter_ops = {
777 .release = lttng_release_event_notifier_group_error_counter,
778 .cmd = lttng_event_notifier_group_error_counter_cmd,
779 };
780
781 static
782 int lttng_ust_event_notifier_group_create_error_counter(int event_notifier_group_objd, void *owner,
783 struct lttng_ust_counter_conf *error_counter_conf)
784 {
785 const char *counter_transport_name;
786 struct lttng_event_notifier_group *event_notifier_group =
787 objd_private(event_notifier_group_objd);
788 struct lttng_counter *counter;
789 int counter_objd, ret;
790 struct lttng_counter_dimension dimensions[1];
791 size_t counter_len;
792
793 if (event_notifier_group->error_counter)
794 return -EBUSY;
795
796 if (error_counter_conf->arithmetic != LTTNG_UST_COUNTER_ARITHMETIC_MODULAR)
797 return -EINVAL;
798
799 if (error_counter_conf->number_dimensions != 1)
800 return -EINVAL;
801
802 switch (error_counter_conf->bitness) {
803 case LTTNG_UST_COUNTER_BITNESS_64:
804 counter_transport_name = "counter-per-cpu-64-modular";
805 break;
806 case LTTNG_UST_COUNTER_BITNESS_32:
807 counter_transport_name = "counter-per-cpu-32-modular";
808 break;
809 default:
810 return -EINVAL;
811 }
812
813 counter_objd = objd_alloc(NULL, &lttng_event_notifier_group_error_counter_ops, owner,
814 "event_notifier group error counter");
815 if (counter_objd < 0) {
816 ret = counter_objd;
817 goto objd_error;
818 }
819
820 counter_len = error_counter_conf->dimensions[0].size;
821 dimensions[0].size = counter_len;
822 dimensions[0].underflow_index = 0;
823 dimensions[0].overflow_index = 0;
824 dimensions[0].has_underflow = 0;
825 dimensions[0].has_overflow = 0;
826
827 counter = lttng_ust_counter_create(counter_transport_name, 1, dimensions);
828 if (!counter) {
829 ret = -EINVAL;
830 goto create_error;
831 }
832
833 event_notifier_group->error_counter_len = counter_len;
834 /*
835 * store-release to publish error counter matches load-acquire
836 * in record_error. Ensures the counter is created and the
837 * error_counter_len is set before they are used.
838 * Currently a full memory barrier is used, which could be
839 * turned into acquire-release barriers.
840 */
841 cmm_smp_mb();
842 CMM_STORE_SHARED(event_notifier_group->error_counter, counter);
843
844 counter->objd = counter_objd;
845 counter->event_notifier_group = event_notifier_group; /* owner */
846
847 objd_set_private(counter_objd, counter);
848 /* The error counter holds a reference on the event_notifier group. */
849 objd_ref(event_notifier_group->objd);
850
851 return counter_objd;
852
853 create_error:
854 {
855 int err;
856
857 err = lttng_ust_objd_unref(counter_objd, 1);
858 assert(!err);
859 }
860 objd_error:
861 return ret;
862 }
863
864 static
865 long lttng_event_notifier_group_cmd(int objd, unsigned int cmd, unsigned long arg,
866 union ust_args *uargs, void *owner)
867 {
868 switch (cmd) {
869 case LTTNG_UST_EVENT_NOTIFIER_CREATE:
870 {
871 struct lttng_ust_event_notifier *event_notifier_param =
872 (struct lttng_ust_event_notifier *) arg;
873 if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
874 /*
875 * If the event name is a star globbing pattern,
876 * we create the special star globbing enabler.
877 */
878 return lttng_ust_event_notifier_enabler_create(objd,
879 owner, event_notifier_param,
880 LTTNG_ENABLER_FORMAT_STAR_GLOB);
881 } else {
882 return lttng_ust_event_notifier_enabler_create(objd,
883 owner, event_notifier_param,
884 LTTNG_ENABLER_FORMAT_EVENT);
885 }
886 }
887 case LTTNG_UST_COUNTER:
888 {
889 struct lttng_ust_counter_conf *counter_conf =
890 (struct lttng_ust_counter_conf *) uargs->counter.counter_data;
891 return lttng_ust_event_notifier_group_create_error_counter(
892 objd, owner, counter_conf);
893 }
894 default:
895 return -EINVAL;
896 }
897 }
898
899 static
900 int lttng_event_notifier_enabler_release(int objd)
901 {
902 struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
903
904 if (event_notifier_enabler)
905 return lttng_ust_objd_unref(event_notifier_enabler->group->objd, 0);
906 return 0;
907 }
908
909 static const struct lttng_ust_objd_ops lttng_event_notifier_enabler_ops = {
910 .release = lttng_event_notifier_enabler_release,
911 .cmd = lttng_event_notifier_enabler_cmd,
912 };
913
914 static
915 int lttng_release_event_notifier_group(int objd)
916 {
917 struct lttng_event_notifier_group *event_notifier_group = objd_private(objd);
918
919 if (event_notifier_group) {
920 lttng_event_notifier_group_destroy(event_notifier_group);
921 return 0;
922 } else {
923 return -EINVAL;
924 }
925 }
926
927 static const struct lttng_ust_objd_ops lttng_event_notifier_group_ops = {
928 .release = lttng_release_event_notifier_group,
929 .cmd = lttng_event_notifier_group_cmd,
930 };
931
932 static
933 long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
934 union ust_args *uargs, void *owner)
935 {
936 struct lttng_ust_tracepoint_list *list = objd_private(objd);
937 struct lttng_ust_tracepoint_iter *tp =
938 (struct lttng_ust_tracepoint_iter *) arg;
939 struct lttng_ust_tracepoint_iter *iter;
940
941 switch (cmd) {
942 case LTTNG_UST_TRACEPOINT_LIST_GET:
943 {
944 iter = lttng_ust_tracepoint_list_get_iter_next(list);
945 if (!iter)
946 return -LTTNG_UST_ERR_NOENT;
947 memcpy(tp, iter, sizeof(*tp));
948 return 0;
949 }
950 default:
951 return -EINVAL;
952 }
953 }
954
955 static
956 int lttng_abi_tracepoint_list(void *owner)
957 {
958 int list_objd, ret;
959 struct lttng_ust_tracepoint_list *list;
960
961 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
962 if (list_objd < 0) {
963 ret = list_objd;
964 goto objd_error;
965 }
966 list = zmalloc(sizeof(*list));
967 if (!list) {
968 ret = -ENOMEM;
969 goto alloc_error;
970 }
971 objd_set_private(list_objd, list);
972
973 /* populate list by walking on all registered probes. */
974 ret = lttng_probes_get_event_list(list);
975 if (ret) {
976 goto list_error;
977 }
978 return list_objd;
979
980 list_error:
981 free(list);
982 alloc_error:
983 {
984 int err;
985
986 err = lttng_ust_objd_unref(list_objd, 1);
987 assert(!err);
988 }
989 objd_error:
990 return ret;
991 }
992
993 static
994 int lttng_release_tracepoint_list(int objd)
995 {
996 struct lttng_ust_tracepoint_list *list = objd_private(objd);
997
998 if (list) {
999 lttng_probes_prune_event_list(list);
1000 free(list);
1001 return 0;
1002 } else {
1003 return -EINVAL;
1004 }
1005 }
1006
1007 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
1008 .release = lttng_release_tracepoint_list,
1009 .cmd = lttng_tracepoint_list_cmd,
1010 };
1011
1012 static
1013 long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
1014 unsigned long arg, union ust_args *uargs, void *owner)
1015 {
1016 struct lttng_ust_field_list *list = objd_private(objd);
1017 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
1018 struct lttng_ust_field_iter *iter;
1019
1020 switch (cmd) {
1021 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
1022 {
1023 iter = lttng_ust_field_list_get_iter_next(list);
1024 if (!iter)
1025 return -LTTNG_UST_ERR_NOENT;
1026 memcpy(tp, iter, sizeof(*tp));
1027 return 0;
1028 }
1029 default:
1030 return -EINVAL;
1031 }
1032 }
1033
1034 static
1035 int lttng_abi_tracepoint_field_list(void *owner)
1036 {
1037 int list_objd, ret;
1038 struct lttng_ust_field_list *list;
1039
1040 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
1041 "tp_field_list");
1042 if (list_objd < 0) {
1043 ret = list_objd;
1044 goto objd_error;
1045 }
1046 list = zmalloc(sizeof(*list));
1047 if (!list) {
1048 ret = -ENOMEM;
1049 goto alloc_error;
1050 }
1051 objd_set_private(list_objd, list);
1052
1053 /* populate list by walking on all registered probes. */
1054 ret = lttng_probes_get_field_list(list);
1055 if (ret) {
1056 goto list_error;
1057 }
1058 return list_objd;
1059
1060 list_error:
1061 free(list);
1062 alloc_error:
1063 {
1064 int err;
1065
1066 err = lttng_ust_objd_unref(list_objd, 1);
1067 assert(!err);
1068 }
1069 objd_error:
1070 return ret;
1071 }
1072
1073 static
1074 int lttng_release_tracepoint_field_list(int objd)
1075 {
1076 struct lttng_ust_field_list *list = objd_private(objd);
1077
1078 if (list) {
1079 lttng_probes_prune_field_list(list);
1080 free(list);
1081 return 0;
1082 } else {
1083 return -EINVAL;
1084 }
1085 }
1086
1087 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
1088 .release = lttng_release_tracepoint_field_list,
1089 .cmd = lttng_tracepoint_field_list_cmd,
1090 };
1091
1092 static
1093 int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
1094 union ust_args *uargs, void *owner)
1095 {
1096 struct lttng_channel *channel = objd_private(channel_objd);
1097 int ret;
1098
1099 ret = channel_handle_add_stream(channel->handle,
1100 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
1101 info->stream_nr, info->len);
1102 if (ret)
1103 goto error_add_stream;
1104 /* Take ownership of shm_fd and wakeup_fd. */
1105 uargs->stream.shm_fd = -1;
1106 uargs->stream.wakeup_fd = -1;
1107
1108 return 0;
1109
1110 error_add_stream:
1111 return ret;
1112 }
1113
1114 static
1115 int lttng_abi_create_event_enabler(int channel_objd,
1116 struct lttng_ust_event *event_param,
1117 void *owner,
1118 enum lttng_enabler_format_type format_type)
1119 {
1120 struct lttng_channel *channel = objd_private(channel_objd);
1121 struct lttng_event_enabler *enabler;
1122 int event_objd, ret;
1123
1124 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1125 event_objd = objd_alloc(NULL, &lttng_event_enabler_ops, owner,
1126 "event enabler");
1127 if (event_objd < 0) {
1128 ret = event_objd;
1129 goto objd_error;
1130 }
1131 /*
1132 * We tolerate no failure path after event creation. It will stay
1133 * invariant for the rest of the session.
1134 */
1135 enabler = lttng_event_enabler_create(format_type, event_param, channel);
1136 if (!enabler) {
1137 ret = -ENOMEM;
1138 goto event_error;
1139 }
1140 objd_set_private(event_objd, enabler);
1141 /* The event holds a reference on the channel */
1142 objd_ref(channel_objd);
1143 return event_objd;
1144
1145 event_error:
1146 {
1147 int err;
1148
1149 err = lttng_ust_objd_unref(event_objd, 1);
1150 assert(!err);
1151 }
1152 objd_error:
1153 return ret;
1154 }
1155
1156 /**
1157 * lttng_channel_cmd - lttng control through object descriptors
1158 *
1159 * @objd: the object descriptor
1160 * @cmd: the command
1161 * @arg: command arg
1162 * @uargs: UST arguments (internal)
1163 * @owner: objd owner
1164 *
1165 * This object descriptor implements lttng commands:
1166 * LTTNG_UST_STREAM
1167 * Returns an event stream object descriptor or failure.
1168 * (typically, one event stream records events from one CPU)
1169 * LTTNG_UST_EVENT
1170 * Returns an event object descriptor or failure.
1171 * LTTNG_UST_CONTEXT
1172 * Prepend a context field to each event in the channel
1173 * LTTNG_UST_ENABLE
1174 * Enable recording for events in this channel (weak enable)
1175 * LTTNG_UST_DISABLE
1176 * Disable recording for events in this channel (strong disable)
1177 *
1178 * Channel and event file descriptors also hold a reference on the session.
1179 */
1180 static
1181 long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
1182 union ust_args *uargs, void *owner)
1183 {
1184 struct lttng_channel *channel = objd_private(objd);
1185
1186 if (cmd != LTTNG_UST_STREAM) {
1187 /*
1188 * Check if channel received all streams.
1189 */
1190 if (!lttng_is_channel_ready(channel))
1191 return -EPERM;
1192 }
1193
1194 switch (cmd) {
1195 case LTTNG_UST_STREAM:
1196 {
1197 struct lttng_ust_stream *stream;
1198
1199 stream = (struct lttng_ust_stream *) arg;
1200 /* stream used as output */
1201 return lttng_abi_map_stream(objd, stream, uargs, owner);
1202 }
1203 case LTTNG_UST_EVENT:
1204 {
1205 struct lttng_ust_event *event_param =
1206 (struct lttng_ust_event *) arg;
1207
1208 if (strutils_is_star_glob_pattern(event_param->name)) {
1209 /*
1210 * If the event name is a star globbing pattern,
1211 * we create the special star globbing enabler.
1212 */
1213 return lttng_abi_create_event_enabler(objd, event_param,
1214 owner, LTTNG_ENABLER_FORMAT_STAR_GLOB);
1215 } else {
1216 return lttng_abi_create_event_enabler(objd, event_param,
1217 owner, LTTNG_ENABLER_FORMAT_EVENT);
1218 }
1219 }
1220 case LTTNG_UST_CONTEXT:
1221 return lttng_abi_add_context(objd,
1222 (struct lttng_ust_context *) arg, uargs,
1223 &channel->ctx, channel->session);
1224 case LTTNG_UST_ENABLE:
1225 return lttng_channel_enable(channel);
1226 case LTTNG_UST_DISABLE:
1227 return lttng_channel_disable(channel);
1228 case LTTNG_UST_FLUSH_BUFFER:
1229 return channel->ops->flush_buffer(channel->chan, channel->handle);
1230 default:
1231 return -EINVAL;
1232 }
1233 }
1234
1235 static
1236 int lttng_channel_release(int objd)
1237 {
1238 struct lttng_channel *channel = objd_private(objd);
1239
1240 if (channel)
1241 return lttng_ust_objd_unref(channel->session->objd, 0);
1242 return 0;
1243 }
1244
1245 static const struct lttng_ust_objd_ops lttng_channel_ops = {
1246 .release = lttng_channel_release,
1247 .cmd = lttng_channel_cmd,
1248 };
1249
1250 /**
1251 * lttng_enabler_cmd - lttng control through object descriptors
1252 *
1253 * @objd: the object descriptor
1254 * @cmd: the command
1255 * @arg: command arg
1256 * @uargs: UST arguments (internal)
1257 * @owner: objd owner
1258 *
1259 * This object descriptor implements lttng commands:
1260 * LTTNG_UST_CONTEXT
1261 * Prepend a context field to each record of events of this
1262 * enabler.
1263 * LTTNG_UST_ENABLE
1264 * Enable recording for this enabler
1265 * LTTNG_UST_DISABLE
1266 * Disable recording for this enabler
1267 * LTTNG_UST_FILTER
1268 * Attach a filter to an enabler.
1269 * LTTNG_UST_EXCLUSION
1270 * Attach exclusions to an enabler.
1271 */
1272 static
1273 long lttng_event_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
1274 union ust_args *uargs, void *owner)
1275 {
1276 struct lttng_event_enabler *enabler = objd_private(objd);
1277
1278 switch (cmd) {
1279 case LTTNG_UST_CONTEXT:
1280 return lttng_event_enabler_attach_context(enabler,
1281 (struct lttng_ust_context *) arg);
1282 case LTTNG_UST_ENABLE:
1283 return lttng_event_enabler_enable(enabler);
1284 case LTTNG_UST_DISABLE:
1285 return lttng_event_enabler_disable(enabler);
1286 case LTTNG_UST_FILTER:
1287 {
1288 int ret;
1289
1290 ret = lttng_event_enabler_attach_filter_bytecode(enabler,
1291 (struct lttng_ust_bytecode_node **) arg);
1292 if (ret)
1293 return ret;
1294 return 0;
1295 }
1296 case LTTNG_UST_EXCLUSION:
1297 {
1298 return lttng_event_enabler_attach_exclusion(enabler,
1299 (struct lttng_ust_excluder_node **) arg);
1300 }
1301 default:
1302 return -EINVAL;
1303 }
1304 }
1305
1306 static
1307 int lttng_event_enabler_release(int objd)
1308 {
1309 struct lttng_event_enabler *event_enabler = objd_private(objd);
1310
1311 if (event_enabler)
1312 return lttng_ust_objd_unref(event_enabler->chan->objd, 0);
1313
1314 return 0;
1315 }
1316
1317 static const struct lttng_ust_objd_ops lttng_event_enabler_ops = {
1318 .release = lttng_event_enabler_release,
1319 .cmd = lttng_event_enabler_cmd,
1320 };
1321
1322 void lttng_ust_abi_exit(void)
1323 {
1324 lttng_ust_abi_close_in_progress = 1;
1325 ust_lock_nocheck();
1326 objd_table_destroy();
1327 ust_unlock();
1328 lttng_ust_abi_close_in_progress = 0;
1329 }
This page took 0.080606 seconds and 4 git commands to generate.