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