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