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