0d2058a3f5947b24eb604c88a0f1c5a20e880a1b
[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_event_notifier_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_event_notifier_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_event_notifier_send_fd(void *owner, int event_notifier_notif_fd)
349 {
350 struct lttng_event_notifier_group *event_notifier_group;
351 int event_notifier_group_objd, ret, fd_flag, close_ret;
352
353 event_notifier_group = lttng_event_notifier_group_create();
354 if (!event_notifier_group)
355 return -ENOMEM;
356
357 /*
358 * Set this file descriptor as NON-BLOCKING.
359 */
360 fd_flag = fcntl(event_notifier_notif_fd, F_GETFL);
361
362 fd_flag |= O_NONBLOCK;
363
364 ret = fcntl(event_notifier_notif_fd, F_SETFL, fd_flag);
365 if (ret) {
366 ret = -errno;
367 goto fd_error;
368 }
369
370 event_notifier_group_objd = objd_alloc(event_notifier_group,
371 &lttng_event_notifier_group_ops, owner, "event_notifier_group");
372 if (event_notifier_group_objd < 0) {
373 ret = event_notifier_group_objd;
374 goto objd_error;
375 }
376
377 event_notifier_group->objd = event_notifier_group_objd;
378 event_notifier_group->owner = owner;
379 event_notifier_group->notification_fd = event_notifier_notif_fd;
380
381 return event_notifier_group_objd;
382
383 objd_error:
384 lttng_event_notifier_group_destroy(event_notifier_group);
385 fd_error:
386 close_ret = close(event_notifier_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_EVENT_NOTIFIER_GROUP_CREATE:
444 return lttng_abi_event_notifier_send_fd(owner,
445 uargs->event_notifier_handle.event_notifier_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_event_notifier_enabler_create(int event_notifier_group_obj,
678 void *owner, struct lttng_ust_event_notifier *event_notifier_param,
679 enum lttng_enabler_format_type type)
680 {
681 struct lttng_event_notifier_group *event_notifier_group =
682 objd_private(event_notifier_group_obj);
683 struct lttng_event_notifier_enabler *event_notifier_enabler;
684 int event_notifier_objd, ret;
685
686 event_notifier_param->event.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
687 event_notifier_objd = objd_alloc(NULL, &lttng_event_notifier_enabler_ops, owner,
688 "event_notifier enabler");
689 if (event_notifier_objd < 0) {
690 ret = event_notifier_objd;
691 goto objd_error;
692 }
693
694 event_notifier_enabler = lttng_event_notifier_enabler_create(
695 event_notifier_group, type, event_notifier_param);
696 if (!event_notifier_enabler) {
697 ret = -ENOMEM;
698 goto event_notifier_error;
699 }
700
701 objd_set_private(event_notifier_objd, event_notifier_enabler);
702 /* The event_notifier holds a reference on the event_notifier group. */
703 objd_ref(event_notifier_enabler->group->objd);
704
705 return event_notifier_objd;
706
707 event_notifier_error:
708 {
709 int err;
710
711 err = lttng_ust_objd_unref(event_notifier_objd, 1);
712 assert(!err);
713 }
714 objd_error:
715 return ret;
716 }
717
718 static
719 long lttng_event_notifier_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
720 union ust_args *uargs, void *owner)
721 {
722 struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
723 switch (cmd) {
724 case LTTNG_UST_FILTER:
725 return lttng_event_notifier_enabler_attach_filter_bytecode(
726 event_notifier_enabler,
727 (struct lttng_ust_bytecode_node *) arg);
728 case LTTNG_UST_EXCLUSION:
729 return lttng_event_notifier_enabler_attach_exclusion(event_notifier_enabler,
730 (struct lttng_ust_excluder_node *) arg);
731 case LTTNG_UST_CAPTURE:
732 return lttng_event_notifier_enabler_attach_capture_bytecode(
733 event_notifier_enabler,
734 (struct lttng_ust_bytecode_node *) arg);
735 case LTTNG_UST_ENABLE:
736 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
737 case LTTNG_UST_DISABLE:
738 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
739 default:
740 return -EINVAL;
741 }
742 }
743
744 /**
745 * lttng_event_notifier_group_error_counter_cmd - lttng event_notifier 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_event_notifier_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_event_notifier_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->event_notifier_group->objd, 0);
786 } else {
787 return -EINVAL;
788 }
789 }
790
791 static const struct lttng_ust_objd_ops lttng_event_notifier_group_error_counter_ops = {
792 .release = lttng_release_event_notifier_group_error_counter,
793 .cmd = lttng_event_notifier_group_error_counter_cmd,
794 };
795
796 static
797 int lttng_ust_event_notifier_group_create_error_counter(int event_notifier_group_objd, void *owner,
798 struct lttng_ust_counter_conf *error_counter_conf)
799 {
800 const char *counter_transport_name;
801 struct lttng_event_notifier_group *event_notifier_group =
802 objd_private(event_notifier_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 (event_notifier_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_event_notifier_group_error_counter_ops, owner,
829 "event_notifier 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 event_notifier_group->error_counter = counter;
849 event_notifier_group->error_counter_len = counter_len;
850
851 counter->objd = counter_objd;
852 counter->event_notifier_group = event_notifier_group; /* owner */
853
854 objd_set_private(counter_objd, counter);
855 /* The error counter holds a reference on the event_notifier group. */
856 objd_ref(event_notifier_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_event_notifier_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_EVENT_NOTIFIER_CREATE:
877 {
878 struct lttng_ust_event_notifier *event_notifier_param =
879 (struct lttng_ust_event_notifier *) arg;
880 if (strutils_is_star_glob_pattern(event_notifier_param->event.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_event_notifier_enabler_create(objd,
886 owner, event_notifier_param,
887 LTTNG_ENABLER_FORMAT_STAR_GLOB);
888 } else {
889 return lttng_ust_event_notifier_enabler_create(objd,
890 owner, event_notifier_param,
891 LTTNG_ENABLER_FORMAT_EVENT);
892 }
893 }
894 case LTTNG_UST_COUNTER:
895 {
896 struct lttng_ust_counter_conf *counter_conf =
897 (struct lttng_ust_counter_conf *) uargs->counter.counter_data;
898 return lttng_ust_event_notifier_group_create_error_counter(
899 objd, owner, counter_conf);
900 }
901 default:
902 return -EINVAL;
903 }
904 }
905
906 static
907 int lttng_event_notifier_enabler_release(int objd)
908 {
909 struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
910
911 if (event_notifier_enabler)
912 return lttng_ust_objd_unref(event_notifier_enabler->group->objd, 0);
913 return 0;
914 }
915
916 static const struct lttng_ust_objd_ops lttng_event_notifier_enabler_ops = {
917 .release = lttng_event_notifier_enabler_release,
918 .cmd = lttng_event_notifier_enabler_cmd,
919 };
920
921 static
922 int lttng_release_event_notifier_group(int objd)
923 {
924 struct lttng_event_notifier_group *event_notifier_group = objd_private(objd);
925
926 if (event_notifier_group) {
927 lttng_event_notifier_group_destroy(event_notifier_group);
928 return 0;
929 } else {
930 return -EINVAL;
931 }
932 }
933
934 static const struct lttng_ust_objd_ops lttng_event_notifier_group_ops = {
935 .release = lttng_release_event_notifier_group,
936 .cmd = lttng_event_notifier_group_cmd,
937 };
938
939 static
940 long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
941 union ust_args *uargs, void *owner)
942 {
943 struct lttng_ust_tracepoint_list *list = objd_private(objd);
944 struct lttng_ust_tracepoint_iter *tp =
945 (struct lttng_ust_tracepoint_iter *) arg;
946 struct lttng_ust_tracepoint_iter *iter;
947
948 switch (cmd) {
949 case LTTNG_UST_TRACEPOINT_LIST_GET:
950 {
951 iter = lttng_ust_tracepoint_list_get_iter_next(list);
952 if (!iter)
953 return -LTTNG_UST_ERR_NOENT;
954 memcpy(tp, iter, sizeof(*tp));
955 return 0;
956 }
957 default:
958 return -EINVAL;
959 }
960 }
961
962 static
963 int lttng_abi_tracepoint_list(void *owner)
964 {
965 int list_objd, ret;
966 struct lttng_ust_tracepoint_list *list;
967
968 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
969 if (list_objd < 0) {
970 ret = list_objd;
971 goto objd_error;
972 }
973 list = zmalloc(sizeof(*list));
974 if (!list) {
975 ret = -ENOMEM;
976 goto alloc_error;
977 }
978 objd_set_private(list_objd, list);
979
980 /* populate list by walking on all registered probes. */
981 ret = lttng_probes_get_event_list(list);
982 if (ret) {
983 goto list_error;
984 }
985 return list_objd;
986
987 list_error:
988 free(list);
989 alloc_error:
990 {
991 int err;
992
993 err = lttng_ust_objd_unref(list_objd, 1);
994 assert(!err);
995 }
996 objd_error:
997 return ret;
998 }
999
1000 static
1001 int lttng_release_tracepoint_list(int objd)
1002 {
1003 struct lttng_ust_tracepoint_list *list = objd_private(objd);
1004
1005 if (list) {
1006 lttng_probes_prune_event_list(list);
1007 free(list);
1008 return 0;
1009 } else {
1010 return -EINVAL;
1011 }
1012 }
1013
1014 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
1015 .release = lttng_release_tracepoint_list,
1016 .cmd = lttng_tracepoint_list_cmd,
1017 };
1018
1019 static
1020 long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
1021 unsigned long arg, union ust_args *uargs, void *owner)
1022 {
1023 struct lttng_ust_field_list *list = objd_private(objd);
1024 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
1025 struct lttng_ust_field_iter *iter;
1026
1027 switch (cmd) {
1028 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
1029 {
1030 iter = lttng_ust_field_list_get_iter_next(list);
1031 if (!iter)
1032 return -LTTNG_UST_ERR_NOENT;
1033 memcpy(tp, iter, sizeof(*tp));
1034 return 0;
1035 }
1036 default:
1037 return -EINVAL;
1038 }
1039 }
1040
1041 static
1042 int lttng_abi_tracepoint_field_list(void *owner)
1043 {
1044 int list_objd, ret;
1045 struct lttng_ust_field_list *list;
1046
1047 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
1048 "tp_field_list");
1049 if (list_objd < 0) {
1050 ret = list_objd;
1051 goto objd_error;
1052 }
1053 list = zmalloc(sizeof(*list));
1054 if (!list) {
1055 ret = -ENOMEM;
1056 goto alloc_error;
1057 }
1058 objd_set_private(list_objd, list);
1059
1060 /* populate list by walking on all registered probes. */
1061 ret = lttng_probes_get_field_list(list);
1062 if (ret) {
1063 goto list_error;
1064 }
1065 return list_objd;
1066
1067 list_error:
1068 free(list);
1069 alloc_error:
1070 {
1071 int err;
1072
1073 err = lttng_ust_objd_unref(list_objd, 1);
1074 assert(!err);
1075 }
1076 objd_error:
1077 return ret;
1078 }
1079
1080 static
1081 int lttng_release_tracepoint_field_list(int objd)
1082 {
1083 struct lttng_ust_field_list *list = objd_private(objd);
1084
1085 if (list) {
1086 lttng_probes_prune_field_list(list);
1087 free(list);
1088 return 0;
1089 } else {
1090 return -EINVAL;
1091 }
1092 }
1093
1094 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
1095 .release = lttng_release_tracepoint_field_list,
1096 .cmd = lttng_tracepoint_field_list_cmd,
1097 };
1098
1099 static
1100 int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
1101 union ust_args *uargs, void *owner)
1102 {
1103 struct lttng_channel *channel = objd_private(channel_objd);
1104 int ret;
1105
1106 ret = channel_handle_add_stream(channel->handle,
1107 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
1108 info->stream_nr, info->len);
1109 if (ret)
1110 goto error_add_stream;
1111
1112 return 0;
1113
1114 error_add_stream:
1115 return ret;
1116 }
1117
1118 static
1119 int lttng_abi_create_event_enabler(int channel_objd,
1120 struct lttng_ust_event *event_param,
1121 void *owner,
1122 enum lttng_enabler_format_type format_type)
1123 {
1124 struct lttng_channel *channel = objd_private(channel_objd);
1125 struct lttng_event_enabler *enabler;
1126 int event_objd, ret;
1127
1128 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1129 event_objd = objd_alloc(NULL, &lttng_event_enabler_ops, owner,
1130 "event enabler");
1131 if (event_objd < 0) {
1132 ret = event_objd;
1133 goto objd_error;
1134 }
1135 /*
1136 * We tolerate no failure path after event creation. It will stay
1137 * invariant for the rest of the session.
1138 */
1139 enabler = lttng_event_enabler_create(format_type, event_param, channel);
1140 if (!enabler) {
1141 ret = -ENOMEM;
1142 goto event_error;
1143 }
1144 objd_set_private(event_objd, enabler);
1145 /* The event holds a reference on the channel */
1146 objd_ref(channel_objd);
1147 return event_objd;
1148
1149 event_error:
1150 {
1151 int err;
1152
1153 err = lttng_ust_objd_unref(event_objd, 1);
1154 assert(!err);
1155 }
1156 objd_error:
1157 return ret;
1158 }
1159
1160 /**
1161 * lttng_channel_cmd - lttng control through object descriptors
1162 *
1163 * @objd: the object descriptor
1164 * @cmd: the command
1165 * @arg: command arg
1166 * @uargs: UST arguments (internal)
1167 * @owner: objd owner
1168 *
1169 * This object descriptor implements lttng commands:
1170 * LTTNG_UST_STREAM
1171 * Returns an event stream object descriptor or failure.
1172 * (typically, one event stream records events from one CPU)
1173 * LTTNG_UST_EVENT
1174 * Returns an event object descriptor or failure.
1175 * LTTNG_UST_CONTEXT
1176 * Prepend a context field to each event in the channel
1177 * LTTNG_UST_ENABLE
1178 * Enable recording for events in this channel (weak enable)
1179 * LTTNG_UST_DISABLE
1180 * Disable recording for events in this channel (strong disable)
1181 *
1182 * Channel and event file descriptors also hold a reference on the session.
1183 */
1184 static
1185 long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
1186 union ust_args *uargs, void *owner)
1187 {
1188 struct lttng_channel *channel = objd_private(objd);
1189
1190 if (cmd != LTTNG_UST_STREAM) {
1191 /*
1192 * Check if channel received all streams.
1193 */
1194 if (!lttng_is_channel_ready(channel))
1195 return -EPERM;
1196 }
1197
1198 switch (cmd) {
1199 case LTTNG_UST_STREAM:
1200 {
1201 struct lttng_ust_stream *stream;
1202
1203 stream = (struct lttng_ust_stream *) arg;
1204 /* stream used as output */
1205 return lttng_abi_map_stream(objd, stream, uargs, owner);
1206 }
1207 case LTTNG_UST_EVENT:
1208 {
1209 struct lttng_ust_event *event_param =
1210 (struct lttng_ust_event *) arg;
1211
1212 if (strutils_is_star_glob_pattern(event_param->name)) {
1213 /*
1214 * If the event name is a star globbing pattern,
1215 * we create the special star globbing enabler.
1216 */
1217 return lttng_abi_create_event_enabler(objd, event_param,
1218 owner, LTTNG_ENABLER_FORMAT_STAR_GLOB);
1219 } else {
1220 return lttng_abi_create_event_enabler(objd, event_param,
1221 owner, LTTNG_ENABLER_FORMAT_EVENT);
1222 }
1223 }
1224 case LTTNG_UST_CONTEXT:
1225 return lttng_abi_add_context(objd,
1226 (struct lttng_ust_context *) arg, uargs,
1227 &channel->ctx, channel->session);
1228 case LTTNG_UST_ENABLE:
1229 return lttng_channel_enable(channel);
1230 case LTTNG_UST_DISABLE:
1231 return lttng_channel_disable(channel);
1232 case LTTNG_UST_FLUSH_BUFFER:
1233 return channel->ops->flush_buffer(channel->chan, channel->handle);
1234 default:
1235 return -EINVAL;
1236 }
1237 }
1238
1239 static
1240 int lttng_channel_release(int objd)
1241 {
1242 struct lttng_channel *channel = objd_private(objd);
1243
1244 if (channel)
1245 return lttng_ust_objd_unref(channel->session->objd, 0);
1246 return 0;
1247 }
1248
1249 static const struct lttng_ust_objd_ops lttng_channel_ops = {
1250 .release = lttng_channel_release,
1251 .cmd = lttng_channel_cmd,
1252 };
1253
1254 /**
1255 * lttng_enabler_cmd - lttng control through object descriptors
1256 *
1257 * @objd: the object descriptor
1258 * @cmd: the command
1259 * @arg: command arg
1260 * @uargs: UST arguments (internal)
1261 * @owner: objd owner
1262 *
1263 * This object descriptor implements lttng commands:
1264 * LTTNG_UST_CONTEXT
1265 * Prepend a context field to each record of events of this
1266 * enabler.
1267 * LTTNG_UST_ENABLE
1268 * Enable recording for this enabler
1269 * LTTNG_UST_DISABLE
1270 * Disable recording for this enabler
1271 * LTTNG_UST_FILTER
1272 * Attach a filter to an enabler.
1273 * LTTNG_UST_EXCLUSION
1274 * Attach exclusions to an enabler.
1275 */
1276 static
1277 long lttng_event_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
1278 union ust_args *uargs, void *owner)
1279 {
1280 struct lttng_event_enabler *enabler = objd_private(objd);
1281
1282 switch (cmd) {
1283 case LTTNG_UST_CONTEXT:
1284 return lttng_event_enabler_attach_context(enabler,
1285 (struct lttng_ust_context *) arg);
1286 case LTTNG_UST_ENABLE:
1287 return lttng_event_enabler_enable(enabler);
1288 case LTTNG_UST_DISABLE:
1289 return lttng_event_enabler_disable(enabler);
1290 case LTTNG_UST_FILTER:
1291 {
1292 int ret;
1293
1294 ret = lttng_event_enabler_attach_filter_bytecode(enabler,
1295 (struct lttng_ust_bytecode_node *) arg);
1296 if (ret)
1297 return ret;
1298 return 0;
1299 }
1300 case LTTNG_UST_EXCLUSION:
1301 {
1302 return lttng_event_enabler_attach_exclusion(enabler,
1303 (struct lttng_ust_excluder_node *) arg);
1304 }
1305 default:
1306 return -EINVAL;
1307 }
1308 }
1309
1310 static
1311 int lttng_event_enabler_release(int objd)
1312 {
1313 struct lttng_event_enabler *event_enabler = objd_private(objd);
1314
1315 if (event_enabler)
1316 return lttng_ust_objd_unref(event_enabler->chan->objd, 0);
1317
1318 return 0;
1319 }
1320
1321 static const struct lttng_ust_objd_ops lttng_event_enabler_ops = {
1322 .release = lttng_event_enabler_release,
1323 .cmd = lttng_event_enabler_cmd,
1324 };
1325
1326 void lttng_ust_abi_exit(void)
1327 {
1328 lttng_ust_abi_close_in_progress = 1;
1329 ust_lock_nocheck();
1330 objd_table_destroy();
1331 ust_unlock();
1332 lttng_ust_abi_close_in_progress = 0;
1333 }
This page took 0.065491 seconds and 3 git commands to generate.