bytecode: generalize `struct lttng_ust_filter_bytecode_node`
[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_filter_bytecode(
720 event_notifier_enabler,
721 (struct lttng_ust_bytecode_node *) arg);
722 case LTTNG_UST_EXCLUSION:
723 return lttng_event_notifier_enabler_attach_exclusion(event_notifier_enabler,
724 (struct lttng_ust_excluder_node *) arg);
725 case LTTNG_UST_ENABLE:
726 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
727 case LTTNG_UST_DISABLE:
728 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
729 default:
730 return -EINVAL;
731 }
732 }
733
734 static
735 long lttng_event_notifier_group_cmd(int objd, unsigned int cmd, unsigned long arg,
736 union ust_args *uargs, void *owner)
737 {
738 switch (cmd) {
739 case LTTNG_UST_EVENT_NOTIFIER_CREATE:
740 {
741 struct lttng_ust_event_notifier *event_notifier_param =
742 (struct lttng_ust_event_notifier *) arg;
743 if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
744 /*
745 * If the event name is a star globbing pattern,
746 * we create the special star globbing enabler.
747 */
748 return lttng_ust_event_notifier_enabler_create(objd,
749 owner, event_notifier_param,
750 LTTNG_ENABLER_FORMAT_STAR_GLOB);
751 } else {
752 return lttng_ust_event_notifier_enabler_create(objd,
753 owner, event_notifier_param,
754 LTTNG_ENABLER_FORMAT_EVENT);
755 }
756 }
757 default:
758 return -EINVAL;
759 }
760 }
761
762 static
763 int lttng_event_notifier_enabler_release(int objd)
764 {
765 struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
766
767 if (event_notifier_enabler)
768 return lttng_ust_objd_unref(event_notifier_enabler->group->objd, 0);
769 return 0;
770 }
771
772 static const struct lttng_ust_objd_ops lttng_event_notifier_enabler_ops = {
773 .release = lttng_event_notifier_enabler_release,
774 .cmd = lttng_event_notifier_enabler_cmd,
775 };
776
777 static
778 int lttng_release_event_notifier_group(int objd)
779 {
780 struct lttng_event_notifier_group *event_notifier_group = objd_private(objd);
781
782 if (event_notifier_group) {
783 lttng_event_notifier_group_destroy(event_notifier_group);
784 return 0;
785 } else {
786 return -EINVAL;
787 }
788 }
789
790 static const struct lttng_ust_objd_ops lttng_event_notifier_group_ops = {
791 .release = lttng_release_event_notifier_group,
792 .cmd = lttng_event_notifier_group_cmd,
793 };
794
795 static
796 long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
797 union ust_args *uargs, void *owner)
798 {
799 struct lttng_ust_tracepoint_list *list = objd_private(objd);
800 struct lttng_ust_tracepoint_iter *tp =
801 (struct lttng_ust_tracepoint_iter *) arg;
802 struct lttng_ust_tracepoint_iter *iter;
803
804 switch (cmd) {
805 case LTTNG_UST_TRACEPOINT_LIST_GET:
806 {
807 iter = lttng_ust_tracepoint_list_get_iter_next(list);
808 if (!iter)
809 return -LTTNG_UST_ERR_NOENT;
810 memcpy(tp, iter, sizeof(*tp));
811 return 0;
812 }
813 default:
814 return -EINVAL;
815 }
816 }
817
818 static
819 int lttng_abi_tracepoint_list(void *owner)
820 {
821 int list_objd, ret;
822 struct lttng_ust_tracepoint_list *list;
823
824 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
825 if (list_objd < 0) {
826 ret = list_objd;
827 goto objd_error;
828 }
829 list = zmalloc(sizeof(*list));
830 if (!list) {
831 ret = -ENOMEM;
832 goto alloc_error;
833 }
834 objd_set_private(list_objd, list);
835
836 /* populate list by walking on all registered probes. */
837 ret = lttng_probes_get_event_list(list);
838 if (ret) {
839 goto list_error;
840 }
841 return list_objd;
842
843 list_error:
844 free(list);
845 alloc_error:
846 {
847 int err;
848
849 err = lttng_ust_objd_unref(list_objd, 1);
850 assert(!err);
851 }
852 objd_error:
853 return ret;
854 }
855
856 static
857 int lttng_release_tracepoint_list(int objd)
858 {
859 struct lttng_ust_tracepoint_list *list = objd_private(objd);
860
861 if (list) {
862 lttng_probes_prune_event_list(list);
863 free(list);
864 return 0;
865 } else {
866 return -EINVAL;
867 }
868 }
869
870 static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
871 .release = lttng_release_tracepoint_list,
872 .cmd = lttng_tracepoint_list_cmd,
873 };
874
875 static
876 long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
877 unsigned long arg, union ust_args *uargs, void *owner)
878 {
879 struct lttng_ust_field_list *list = objd_private(objd);
880 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
881 struct lttng_ust_field_iter *iter;
882
883 switch (cmd) {
884 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
885 {
886 iter = lttng_ust_field_list_get_iter_next(list);
887 if (!iter)
888 return -LTTNG_UST_ERR_NOENT;
889 memcpy(tp, iter, sizeof(*tp));
890 return 0;
891 }
892 default:
893 return -EINVAL;
894 }
895 }
896
897 static
898 int lttng_abi_tracepoint_field_list(void *owner)
899 {
900 int list_objd, ret;
901 struct lttng_ust_field_list *list;
902
903 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
904 "tp_field_list");
905 if (list_objd < 0) {
906 ret = list_objd;
907 goto objd_error;
908 }
909 list = zmalloc(sizeof(*list));
910 if (!list) {
911 ret = -ENOMEM;
912 goto alloc_error;
913 }
914 objd_set_private(list_objd, list);
915
916 /* populate list by walking on all registered probes. */
917 ret = lttng_probes_get_field_list(list);
918 if (ret) {
919 goto list_error;
920 }
921 return list_objd;
922
923 list_error:
924 free(list);
925 alloc_error:
926 {
927 int err;
928
929 err = lttng_ust_objd_unref(list_objd, 1);
930 assert(!err);
931 }
932 objd_error:
933 return ret;
934 }
935
936 static
937 int lttng_release_tracepoint_field_list(int objd)
938 {
939 struct lttng_ust_field_list *list = objd_private(objd);
940
941 if (list) {
942 lttng_probes_prune_field_list(list);
943 free(list);
944 return 0;
945 } else {
946 return -EINVAL;
947 }
948 }
949
950 static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
951 .release = lttng_release_tracepoint_field_list,
952 .cmd = lttng_tracepoint_field_list_cmd,
953 };
954
955 static
956 int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
957 union ust_args *uargs, void *owner)
958 {
959 struct lttng_channel *channel = objd_private(channel_objd);
960 int ret;
961
962 ret = channel_handle_add_stream(channel->handle,
963 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
964 info->stream_nr, info->len);
965 if (ret)
966 goto error_add_stream;
967
968 return 0;
969
970 error_add_stream:
971 return ret;
972 }
973
974 static
975 int lttng_abi_create_event_enabler(int channel_objd,
976 struct lttng_ust_event *event_param,
977 void *owner,
978 enum lttng_enabler_format_type format_type)
979 {
980 struct lttng_channel *channel = objd_private(channel_objd);
981 struct lttng_event_enabler *enabler;
982 int event_objd, ret;
983
984 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
985 event_objd = objd_alloc(NULL, &lttng_event_enabler_ops, owner,
986 "event enabler");
987 if (event_objd < 0) {
988 ret = event_objd;
989 goto objd_error;
990 }
991 /*
992 * We tolerate no failure path after event creation. It will stay
993 * invariant for the rest of the session.
994 */
995 enabler = lttng_event_enabler_create(format_type, event_param, channel);
996 if (!enabler) {
997 ret = -ENOMEM;
998 goto event_error;
999 }
1000 objd_set_private(event_objd, enabler);
1001 /* The event holds a reference on the channel */
1002 objd_ref(channel_objd);
1003 return event_objd;
1004
1005 event_error:
1006 {
1007 int err;
1008
1009 err = lttng_ust_objd_unref(event_objd, 1);
1010 assert(!err);
1011 }
1012 objd_error:
1013 return ret;
1014 }
1015
1016 /**
1017 * lttng_channel_cmd - lttng control through object descriptors
1018 *
1019 * @objd: the object descriptor
1020 * @cmd: the command
1021 * @arg: command arg
1022 * @uargs: UST arguments (internal)
1023 * @owner: objd owner
1024 *
1025 * This object descriptor implements lttng commands:
1026 * LTTNG_UST_STREAM
1027 * Returns an event stream object descriptor or failure.
1028 * (typically, one event stream records events from one CPU)
1029 * LTTNG_UST_EVENT
1030 * Returns an event object descriptor or failure.
1031 * LTTNG_UST_CONTEXT
1032 * Prepend a context field to each event in the channel
1033 * LTTNG_UST_ENABLE
1034 * Enable recording for events in this channel (weak enable)
1035 * LTTNG_UST_DISABLE
1036 * Disable recording for events in this channel (strong disable)
1037 *
1038 * Channel and event file descriptors also hold a reference on the session.
1039 */
1040 static
1041 long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
1042 union ust_args *uargs, void *owner)
1043 {
1044 struct lttng_channel *channel = objd_private(objd);
1045
1046 if (cmd != LTTNG_UST_STREAM) {
1047 /*
1048 * Check if channel received all streams.
1049 */
1050 if (!lttng_is_channel_ready(channel))
1051 return -EPERM;
1052 }
1053
1054 switch (cmd) {
1055 case LTTNG_UST_STREAM:
1056 {
1057 struct lttng_ust_stream *stream;
1058
1059 stream = (struct lttng_ust_stream *) arg;
1060 /* stream used as output */
1061 return lttng_abi_map_stream(objd, stream, uargs, owner);
1062 }
1063 case LTTNG_UST_EVENT:
1064 {
1065 struct lttng_ust_event *event_param =
1066 (struct lttng_ust_event *) arg;
1067
1068 if (strutils_is_star_glob_pattern(event_param->name)) {
1069 /*
1070 * If the event name is a star globbing pattern,
1071 * we create the special star globbing enabler.
1072 */
1073 return lttng_abi_create_event_enabler(objd, event_param,
1074 owner, LTTNG_ENABLER_FORMAT_STAR_GLOB);
1075 } else {
1076 return lttng_abi_create_event_enabler(objd, event_param,
1077 owner, LTTNG_ENABLER_FORMAT_EVENT);
1078 }
1079 }
1080 case LTTNG_UST_CONTEXT:
1081 return lttng_abi_add_context(objd,
1082 (struct lttng_ust_context *) arg, uargs,
1083 &channel->ctx, channel->session);
1084 case LTTNG_UST_ENABLE:
1085 return lttng_channel_enable(channel);
1086 case LTTNG_UST_DISABLE:
1087 return lttng_channel_disable(channel);
1088 case LTTNG_UST_FLUSH_BUFFER:
1089 return channel->ops->flush_buffer(channel->chan, channel->handle);
1090 default:
1091 return -EINVAL;
1092 }
1093 }
1094
1095 static
1096 int lttng_channel_release(int objd)
1097 {
1098 struct lttng_channel *channel = objd_private(objd);
1099
1100 if (channel)
1101 return lttng_ust_objd_unref(channel->session->objd, 0);
1102 return 0;
1103 }
1104
1105 static const struct lttng_ust_objd_ops lttng_channel_ops = {
1106 .release = lttng_channel_release,
1107 .cmd = lttng_channel_cmd,
1108 };
1109
1110 /**
1111 * lttng_enabler_cmd - lttng control through object descriptors
1112 *
1113 * @objd: the object descriptor
1114 * @cmd: the command
1115 * @arg: command arg
1116 * @uargs: UST arguments (internal)
1117 * @owner: objd owner
1118 *
1119 * This object descriptor implements lttng commands:
1120 * LTTNG_UST_CONTEXT
1121 * Prepend a context field to each record of events of this
1122 * enabler.
1123 * LTTNG_UST_ENABLE
1124 * Enable recording for this enabler
1125 * LTTNG_UST_DISABLE
1126 * Disable recording for this enabler
1127 * LTTNG_UST_FILTER
1128 * Attach a filter to an enabler.
1129 * LTTNG_UST_EXCLUSION
1130 * Attach exclusions to an enabler.
1131 */
1132 static
1133 long lttng_event_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
1134 union ust_args *uargs, void *owner)
1135 {
1136 struct lttng_event_enabler *enabler = objd_private(objd);
1137
1138 switch (cmd) {
1139 case LTTNG_UST_CONTEXT:
1140 return lttng_event_enabler_attach_context(enabler,
1141 (struct lttng_ust_context *) arg);
1142 case LTTNG_UST_ENABLE:
1143 return lttng_event_enabler_enable(enabler);
1144 case LTTNG_UST_DISABLE:
1145 return lttng_event_enabler_disable(enabler);
1146 case LTTNG_UST_FILTER:
1147 {
1148 int ret;
1149
1150 ret = lttng_event_enabler_attach_filter_bytecode(enabler,
1151 (struct lttng_ust_bytecode_node *) arg);
1152 if (ret)
1153 return ret;
1154 return 0;
1155 }
1156 case LTTNG_UST_EXCLUSION:
1157 {
1158 return lttng_event_enabler_attach_exclusion(enabler,
1159 (struct lttng_ust_excluder_node *) arg);
1160 }
1161 default:
1162 return -EINVAL;
1163 }
1164 }
1165
1166 static
1167 int lttng_event_enabler_release(int objd)
1168 {
1169 struct lttng_event_enabler *event_enabler = objd_private(objd);
1170
1171 if (event_enabler)
1172 return lttng_ust_objd_unref(event_enabler->chan->objd, 0);
1173
1174 return 0;
1175 }
1176
1177 static const struct lttng_ust_objd_ops lttng_event_enabler_ops = {
1178 .release = lttng_event_enabler_release,
1179 .cmd = lttng_event_enabler_cmd,
1180 };
1181
1182 void lttng_ust_abi_exit(void)
1183 {
1184 lttng_ust_abi_close_in_progress = 1;
1185 ust_lock_nocheck();
1186 objd_table_destroy();
1187 ust_unlock();
1188 lttng_ust_abi_close_in_progress = 0;
1189 }
This page took 0.085992 seconds and 4 git commands to generate.