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