Fix: stream fd leaks on error
[lttng-ust.git] / liblttng-ust / lttng-ust-abi.c
... / ...
CommitLineData
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
67static int lttng_ust_abi_close_in_progress;
68
69static
70int lttng_abi_tracepoint_list(void *owner);
71static
72int 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
79struct 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
93struct 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
99static struct lttng_ust_objd_table objd_table = {
100 .freelist_head = -1,
101};
102
103static
104int 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++;
136end:
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
148static
149struct 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
158static
159void *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
166static
167void 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
174const 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
183static
184void 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
195static
196void 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
203int 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
230static
231void 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
252const 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
261void 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
285static const struct lttng_ust_objd_ops lttng_ops;
286static const struct lttng_ust_objd_ops lttng_event_notifier_group_ops;
287static const struct lttng_ust_objd_ops lttng_session_ops;
288static const struct lttng_ust_objd_ops lttng_channel_ops;
289static const struct lttng_ust_objd_ops lttng_event_enabler_ops;
290static const struct lttng_ust_objd_ops lttng_event_notifier_enabler_ops;
291static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
292static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
293
294int 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
303static
304int 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
315static
316int 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
333objd_error:
334 lttng_session_destroy(session);
335 return ret;
336}
337
338static
339long 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
348static
349int 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
384objd_error:
385 lttng_event_notifier_group_destroy(event_notifier_group);
386fd_error:
387 close_ret = close(event_notifier_notif_fd);
388 if (close_ret) {
389 PERROR("close");
390 }
391
392 return ret;
393}
394
395static
396long 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 */
427static
428long 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
452static const struct lttng_ust_objd_ops lttng_ops = {
453 .cmd = lttng_cmd,
454};
455
456static
457int 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 /* Ownership of chan_data and wakeup_fd taken by channel handle. */
502 uargs->channel.chan_data = NULL;
503 uargs->channel.wakeup_fd = -1;
504
505 chan = shmp(channel_handle, channel_handle->chan);
506 assert(chan);
507 chan->handle = channel_handle;
508 config = &chan->backend.config;
509 lttng_chan = channel_get_private(chan);
510 if (!lttng_chan) {
511 ret = -EINVAL;
512 goto alloc_error;
513 }
514
515 /* Lookup transport name */
516 switch (type) {
517 case LTTNG_UST_CHAN_PER_CPU:
518 if (config->output == RING_BUFFER_MMAP) {
519 if (config->mode == RING_BUFFER_OVERWRITE) {
520 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER) {
521 transport_name = "relay-overwrite-mmap";
522 } else {
523 transport_name = "relay-overwrite-rt-mmap";
524 }
525 } else {
526 if (config->wakeup == RING_BUFFER_WAKEUP_BY_WRITER) {
527 transport_name = "relay-discard-mmap";
528 } else {
529 transport_name = "relay-discard-rt-mmap";
530 }
531 }
532 } else {
533 ret = -EINVAL;
534 goto notransport;
535 }
536 chan_name = "channel";
537 break;
538 default:
539 ret = -EINVAL;
540 goto notransport;
541 }
542 transport = lttng_transport_find(transport_name);
543 if (!transport) {
544 DBG("LTTng transport %s not found\n",
545 transport_name);
546 ret = -EINVAL;
547 goto notransport;
548 }
549
550 chan_objd = objd_alloc(NULL, &lttng_channel_ops, owner, chan_name);
551 if (chan_objd < 0) {
552 ret = chan_objd;
553 goto objd_error;
554 }
555
556 /* Initialize our lttng chan */
557 lttng_chan->chan = chan;
558 lttng_chan->tstate = 1;
559 lttng_chan->enabled = 1;
560 lttng_chan->ctx = NULL;
561 lttng_chan->session = session;
562 lttng_chan->ops = &transport->ops;
563 memcpy(&lttng_chan->chan->backend.config,
564 transport->client_config,
565 sizeof(lttng_chan->chan->backend.config));
566 cds_list_add(&lttng_chan->node, &session->chan_head);
567 lttng_chan->header_type = 0;
568 lttng_chan->handle = channel_handle;
569 lttng_chan->type = type;
570
571 /*
572 * We tolerate no failure path after channel creation. It will stay
573 * invariant for the rest of the session.
574 */
575 objd_set_private(chan_objd, lttng_chan);
576 lttng_chan->objd = chan_objd;
577 /* The channel created holds a reference on the session */
578 objd_ref(session_objd);
579 return chan_objd;
580
581 /* error path after channel was created */
582objd_error:
583notransport:
584alloc_error:
585 channel_destroy(chan, channel_handle, 0);
586 return ret;
587
588handle_error:
589active:
590invalid:
591 return ret;
592}
593
594/**
595 * lttng_session_cmd - lttng session object command
596 *
597 * @obj: the object
598 * @cmd: the command
599 * @arg: command arg
600 * @uargs: UST arguments (internal)
601 * @owner: objd owner
602 *
603 * This descriptor implements lttng commands:
604 * LTTNG_UST_CHANNEL
605 * Returns a LTTng channel object descriptor
606 * LTTNG_UST_ENABLE
607 * Enables tracing for a session (weak enable)
608 * LTTNG_UST_DISABLE
609 * Disables tracing for a session (strong disable)
610 *
611 * The returned channel will be deleted when its file descriptor is closed.
612 */
613static
614long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
615 union ust_args *uargs, void *owner)
616{
617 struct lttng_session *session = objd_private(objd);
618
619 switch (cmd) {
620 case LTTNG_UST_CHANNEL:
621 return lttng_abi_map_channel(objd,
622 (struct lttng_ust_channel *) arg,
623 uargs, owner);
624 case LTTNG_UST_SESSION_START:
625 case LTTNG_UST_ENABLE:
626 return lttng_session_enable(session);
627 case LTTNG_UST_SESSION_STOP:
628 case LTTNG_UST_DISABLE:
629 return lttng_session_disable(session);
630 case LTTNG_UST_SESSION_STATEDUMP:
631 return lttng_session_statedump(session);
632 case LTTNG_UST_COUNTER:
633 case LTTNG_UST_COUNTER_GLOBAL:
634 case LTTNG_UST_COUNTER_CPU:
635 /* Not implemented yet. */
636 return -EINVAL;
637 default:
638 return -EINVAL;
639 }
640}
641
642/*
643 * Called when the last file reference is dropped.
644 *
645 * Big fat note: channels and events are invariant for the whole session after
646 * their creation. So this session destruction also destroys all channel and
647 * event structures specific to this session (they are not destroyed when their
648 * individual file is released).
649 */
650static
651int lttng_release_session(int objd)
652{
653 struct lttng_session *session = objd_private(objd);
654
655 if (session) {
656 lttng_session_destroy(session);
657 return 0;
658 } else {
659 return -EINVAL;
660 }
661}
662
663static const struct lttng_ust_objd_ops lttng_session_ops = {
664 .release = lttng_release_session,
665 .cmd = lttng_session_cmd,
666};
667
668static int lttng_ust_event_notifier_enabler_create(int event_notifier_group_obj,
669 void *owner, struct lttng_ust_event_notifier *event_notifier_param,
670 enum lttng_enabler_format_type type)
671{
672 struct lttng_event_notifier_group *event_notifier_group =
673 objd_private(event_notifier_group_obj);
674 struct lttng_event_notifier_enabler *event_notifier_enabler;
675 int event_notifier_objd, ret;
676
677 event_notifier_param->event.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
678 event_notifier_objd = objd_alloc(NULL, &lttng_event_notifier_enabler_ops, owner,
679 "event_notifier enabler");
680 if (event_notifier_objd < 0) {
681 ret = event_notifier_objd;
682 goto objd_error;
683 }
684
685 event_notifier_enabler = lttng_event_notifier_enabler_create(
686 event_notifier_group, type, event_notifier_param);
687 if (!event_notifier_enabler) {
688 ret = -ENOMEM;
689 goto event_notifier_error;
690 }
691
692 objd_set_private(event_notifier_objd, event_notifier_enabler);
693 /* The event_notifier holds a reference on the event_notifier group. */
694 objd_ref(event_notifier_enabler->group->objd);
695
696 return event_notifier_objd;
697
698event_notifier_error:
699 {
700 int err;
701
702 err = lttng_ust_objd_unref(event_notifier_objd, 1);
703 assert(!err);
704 }
705objd_error:
706 return ret;
707}
708
709static
710long lttng_event_notifier_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
711 union ust_args *uargs, void *owner)
712{
713 struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
714 switch (cmd) {
715 case LTTNG_UST_FILTER:
716 return lttng_event_notifier_enabler_attach_filter_bytecode(
717 event_notifier_enabler,
718 (struct lttng_ust_bytecode_node *) arg);
719 case LTTNG_UST_EXCLUSION:
720 return lttng_event_notifier_enabler_attach_exclusion(event_notifier_enabler,
721 (struct lttng_ust_excluder_node *) arg);
722 case LTTNG_UST_CAPTURE:
723 return lttng_event_notifier_enabler_attach_capture_bytecode(
724 event_notifier_enabler,
725 (struct lttng_ust_bytecode_node *) arg);
726 case LTTNG_UST_ENABLE:
727 return lttng_event_notifier_enabler_enable(event_notifier_enabler);
728 case LTTNG_UST_DISABLE:
729 return lttng_event_notifier_enabler_disable(event_notifier_enabler);
730 default:
731 return -EINVAL;
732 }
733}
734
735/**
736 * lttng_event_notifier_group_error_counter_cmd - lttng event_notifier group error counter object command
737 *
738 * @obj: the object
739 * @cmd: the command
740 * @arg: command arg
741 * @uargs: UST arguments (internal)
742 * @owner: objd owner
743 *
744 * This descriptor implements lttng commands:
745 * LTTNG_UST_COUNTER_GLOBAL
746 * Return negative error code on error, 0 on success.
747 * LTTNG_UST_COUNTER_CPU
748 * Return negative error code on error, 0 on success.
749 */
750static
751long lttng_event_notifier_group_error_counter_cmd(int objd, unsigned int cmd, unsigned long arg,
752 union ust_args *uargs, void *owner)
753{
754 struct lttng_counter *counter = objd_private(objd);
755
756 switch (cmd) {
757 case LTTNG_UST_COUNTER_GLOBAL:
758 return -EINVAL; /* Unimplemented. */
759 case LTTNG_UST_COUNTER_CPU:
760 {
761 struct lttng_ust_counter_cpu *counter_cpu =
762 (struct lttng_ust_counter_cpu *)arg;
763 return lttng_counter_set_cpu_shm(counter->counter,
764 counter_cpu->cpu_nr, uargs->counter_shm.shm_fd);
765 }
766 default:
767 return -EINVAL;
768 }
769}
770
771int lttng_release_event_notifier_group_error_counter(int objd)
772{
773 struct lttng_counter *counter = objd_private(objd);
774
775 if (counter) {
776 return lttng_ust_objd_unref(counter->event_notifier_group->objd, 0);
777 } else {
778 return -EINVAL;
779 }
780}
781
782static const struct lttng_ust_objd_ops lttng_event_notifier_group_error_counter_ops = {
783 .release = lttng_release_event_notifier_group_error_counter,
784 .cmd = lttng_event_notifier_group_error_counter_cmd,
785};
786
787static
788int lttng_ust_event_notifier_group_create_error_counter(int event_notifier_group_objd, void *owner,
789 struct lttng_ust_counter_conf *error_counter_conf)
790{
791 const char *counter_transport_name;
792 struct lttng_event_notifier_group *event_notifier_group =
793 objd_private(event_notifier_group_objd);
794 struct lttng_counter *counter;
795 int counter_objd, ret;
796 struct lttng_counter_dimension dimensions[1];
797 size_t counter_len;
798
799 if (event_notifier_group->error_counter)
800 return -EBUSY;
801
802 if (error_counter_conf->arithmetic != LTTNG_UST_COUNTER_ARITHMETIC_MODULAR)
803 return -EINVAL;
804
805 if (error_counter_conf->number_dimensions != 1)
806 return -EINVAL;
807
808 switch (error_counter_conf->bitness) {
809 case LTTNG_UST_COUNTER_BITNESS_64:
810 counter_transport_name = "counter-per-cpu-64-modular";
811 break;
812 case LTTNG_UST_COUNTER_BITNESS_32:
813 counter_transport_name = "counter-per-cpu-32-modular";
814 break;
815 default:
816 return -EINVAL;
817 }
818
819 counter_objd = objd_alloc(NULL, &lttng_event_notifier_group_error_counter_ops, owner,
820 "event_notifier group error counter");
821 if (counter_objd < 0) {
822 ret = counter_objd;
823 goto objd_error;
824 }
825
826 counter_len = error_counter_conf->dimensions[0].size;
827 dimensions[0].size = counter_len;
828 dimensions[0].underflow_index = 0;
829 dimensions[0].overflow_index = 0;
830 dimensions[0].has_underflow = 0;
831 dimensions[0].has_overflow = 0;
832
833 counter = lttng_ust_counter_create(counter_transport_name, 1, dimensions);
834 if (!counter) {
835 ret = -EINVAL;
836 goto create_error;
837 }
838
839 event_notifier_group->error_counter_len = counter_len;
840 /*
841 * store-release to publish error counter matches load-acquire
842 * in record_error. Ensures the counter is created and the
843 * error_counter_len is set before they are used.
844 * Currently a full memory barrier is used, which could be
845 * turned into acquire-release barriers.
846 */
847 cmm_smp_mb();
848 CMM_STORE_SHARED(event_notifier_group->error_counter, counter);
849
850 counter->objd = counter_objd;
851 counter->event_notifier_group = event_notifier_group; /* owner */
852
853 objd_set_private(counter_objd, counter);
854 /* The error counter holds a reference on the event_notifier group. */
855 objd_ref(event_notifier_group->objd);
856
857 return counter_objd;
858
859create_error:
860 {
861 int err;
862
863 err = lttng_ust_objd_unref(counter_objd, 1);
864 assert(!err);
865 }
866objd_error:
867 return ret;
868}
869
870static
871long lttng_event_notifier_group_cmd(int objd, unsigned int cmd, unsigned long arg,
872 union ust_args *uargs, void *owner)
873{
874 switch (cmd) {
875 case LTTNG_UST_EVENT_NOTIFIER_CREATE:
876 {
877 struct lttng_ust_event_notifier *event_notifier_param =
878 (struct lttng_ust_event_notifier *) arg;
879 if (strutils_is_star_glob_pattern(event_notifier_param->event.name)) {
880 /*
881 * If the event name is a star globbing pattern,
882 * we create the special star globbing enabler.
883 */
884 return lttng_ust_event_notifier_enabler_create(objd,
885 owner, event_notifier_param,
886 LTTNG_ENABLER_FORMAT_STAR_GLOB);
887 } else {
888 return lttng_ust_event_notifier_enabler_create(objd,
889 owner, event_notifier_param,
890 LTTNG_ENABLER_FORMAT_EVENT);
891 }
892 }
893 case LTTNG_UST_COUNTER:
894 {
895 struct lttng_ust_counter_conf *counter_conf =
896 (struct lttng_ust_counter_conf *) uargs->counter.counter_data;
897 return lttng_ust_event_notifier_group_create_error_counter(
898 objd, owner, counter_conf);
899 }
900 default:
901 return -EINVAL;
902 }
903}
904
905static
906int lttng_event_notifier_enabler_release(int objd)
907{
908 struct lttng_event_notifier_enabler *event_notifier_enabler = objd_private(objd);
909
910 if (event_notifier_enabler)
911 return lttng_ust_objd_unref(event_notifier_enabler->group->objd, 0);
912 return 0;
913}
914
915static const struct lttng_ust_objd_ops lttng_event_notifier_enabler_ops = {
916 .release = lttng_event_notifier_enabler_release,
917 .cmd = lttng_event_notifier_enabler_cmd,
918};
919
920static
921int lttng_release_event_notifier_group(int objd)
922{
923 struct lttng_event_notifier_group *event_notifier_group = objd_private(objd);
924
925 if (event_notifier_group) {
926 lttng_event_notifier_group_destroy(event_notifier_group);
927 return 0;
928 } else {
929 return -EINVAL;
930 }
931}
932
933static const struct lttng_ust_objd_ops lttng_event_notifier_group_ops = {
934 .release = lttng_release_event_notifier_group,
935 .cmd = lttng_event_notifier_group_cmd,
936};
937
938static
939long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
940 union ust_args *uargs, void *owner)
941{
942 struct lttng_ust_tracepoint_list *list = objd_private(objd);
943 struct lttng_ust_tracepoint_iter *tp =
944 (struct lttng_ust_tracepoint_iter *) arg;
945 struct lttng_ust_tracepoint_iter *iter;
946
947 switch (cmd) {
948 case LTTNG_UST_TRACEPOINT_LIST_GET:
949 {
950 iter = lttng_ust_tracepoint_list_get_iter_next(list);
951 if (!iter)
952 return -LTTNG_UST_ERR_NOENT;
953 memcpy(tp, iter, sizeof(*tp));
954 return 0;
955 }
956 default:
957 return -EINVAL;
958 }
959}
960
961static
962int lttng_abi_tracepoint_list(void *owner)
963{
964 int list_objd, ret;
965 struct lttng_ust_tracepoint_list *list;
966
967 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
968 if (list_objd < 0) {
969 ret = list_objd;
970 goto objd_error;
971 }
972 list = zmalloc(sizeof(*list));
973 if (!list) {
974 ret = -ENOMEM;
975 goto alloc_error;
976 }
977 objd_set_private(list_objd, list);
978
979 /* populate list by walking on all registered probes. */
980 ret = lttng_probes_get_event_list(list);
981 if (ret) {
982 goto list_error;
983 }
984 return list_objd;
985
986list_error:
987 free(list);
988alloc_error:
989 {
990 int err;
991
992 err = lttng_ust_objd_unref(list_objd, 1);
993 assert(!err);
994 }
995objd_error:
996 return ret;
997}
998
999static
1000int lttng_release_tracepoint_list(int objd)
1001{
1002 struct lttng_ust_tracepoint_list *list = objd_private(objd);
1003
1004 if (list) {
1005 lttng_probes_prune_event_list(list);
1006 free(list);
1007 return 0;
1008 } else {
1009 return -EINVAL;
1010 }
1011}
1012
1013static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
1014 .release = lttng_release_tracepoint_list,
1015 .cmd = lttng_tracepoint_list_cmd,
1016};
1017
1018static
1019long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
1020 unsigned long arg, union ust_args *uargs, void *owner)
1021{
1022 struct lttng_ust_field_list *list = objd_private(objd);
1023 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
1024 struct lttng_ust_field_iter *iter;
1025
1026 switch (cmd) {
1027 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
1028 {
1029 iter = lttng_ust_field_list_get_iter_next(list);
1030 if (!iter)
1031 return -LTTNG_UST_ERR_NOENT;
1032 memcpy(tp, iter, sizeof(*tp));
1033 return 0;
1034 }
1035 default:
1036 return -EINVAL;
1037 }
1038}
1039
1040static
1041int lttng_abi_tracepoint_field_list(void *owner)
1042{
1043 int list_objd, ret;
1044 struct lttng_ust_field_list *list;
1045
1046 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
1047 "tp_field_list");
1048 if (list_objd < 0) {
1049 ret = list_objd;
1050 goto objd_error;
1051 }
1052 list = zmalloc(sizeof(*list));
1053 if (!list) {
1054 ret = -ENOMEM;
1055 goto alloc_error;
1056 }
1057 objd_set_private(list_objd, list);
1058
1059 /* populate list by walking on all registered probes. */
1060 ret = lttng_probes_get_field_list(list);
1061 if (ret) {
1062 goto list_error;
1063 }
1064 return list_objd;
1065
1066list_error:
1067 free(list);
1068alloc_error:
1069 {
1070 int err;
1071
1072 err = lttng_ust_objd_unref(list_objd, 1);
1073 assert(!err);
1074 }
1075objd_error:
1076 return ret;
1077}
1078
1079static
1080int lttng_release_tracepoint_field_list(int objd)
1081{
1082 struct lttng_ust_field_list *list = objd_private(objd);
1083
1084 if (list) {
1085 lttng_probes_prune_field_list(list);
1086 free(list);
1087 return 0;
1088 } else {
1089 return -EINVAL;
1090 }
1091}
1092
1093static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
1094 .release = lttng_release_tracepoint_field_list,
1095 .cmd = lttng_tracepoint_field_list_cmd,
1096};
1097
1098static
1099int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
1100 union ust_args *uargs, void *owner)
1101{
1102 struct lttng_channel *channel = objd_private(channel_objd);
1103 int ret;
1104
1105 ret = channel_handle_add_stream(channel->handle,
1106 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
1107 info->stream_nr, info->len);
1108 if (ret)
1109 goto error_add_stream;
1110 /* Take ownership of shm_fd and wakeup_fd. */
1111 uargs->stream.shm_fd = -1;
1112 uargs->stream.wakeup_fd = -1;
1113
1114 return 0;
1115
1116error_add_stream:
1117 return ret;
1118}
1119
1120static
1121int lttng_abi_create_event_enabler(int channel_objd,
1122 struct lttng_ust_event *event_param,
1123 void *owner,
1124 enum lttng_enabler_format_type format_type)
1125{
1126 struct lttng_channel *channel = objd_private(channel_objd);
1127 struct lttng_event_enabler *enabler;
1128 int event_objd, ret;
1129
1130 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
1131 event_objd = objd_alloc(NULL, &lttng_event_enabler_ops, owner,
1132 "event enabler");
1133 if (event_objd < 0) {
1134 ret = event_objd;
1135 goto objd_error;
1136 }
1137 /*
1138 * We tolerate no failure path after event creation. It will stay
1139 * invariant for the rest of the session.
1140 */
1141 enabler = lttng_event_enabler_create(format_type, event_param, channel);
1142 if (!enabler) {
1143 ret = -ENOMEM;
1144 goto event_error;
1145 }
1146 objd_set_private(event_objd, enabler);
1147 /* The event holds a reference on the channel */
1148 objd_ref(channel_objd);
1149 return event_objd;
1150
1151event_error:
1152 {
1153 int err;
1154
1155 err = lttng_ust_objd_unref(event_objd, 1);
1156 assert(!err);
1157 }
1158objd_error:
1159 return ret;
1160}
1161
1162/**
1163 * lttng_channel_cmd - lttng control through object descriptors
1164 *
1165 * @objd: the object descriptor
1166 * @cmd: the command
1167 * @arg: command arg
1168 * @uargs: UST arguments (internal)
1169 * @owner: objd owner
1170 *
1171 * This object descriptor implements lttng commands:
1172 * LTTNG_UST_STREAM
1173 * Returns an event stream object descriptor or failure.
1174 * (typically, one event stream records events from one CPU)
1175 * LTTNG_UST_EVENT
1176 * Returns an event object descriptor or failure.
1177 * LTTNG_UST_CONTEXT
1178 * Prepend a context field to each event in the channel
1179 * LTTNG_UST_ENABLE
1180 * Enable recording for events in this channel (weak enable)
1181 * LTTNG_UST_DISABLE
1182 * Disable recording for events in this channel (strong disable)
1183 *
1184 * Channel and event file descriptors also hold a reference on the session.
1185 */
1186static
1187long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
1188 union ust_args *uargs, void *owner)
1189{
1190 struct lttng_channel *channel = objd_private(objd);
1191
1192 if (cmd != LTTNG_UST_STREAM) {
1193 /*
1194 * Check if channel received all streams.
1195 */
1196 if (!lttng_is_channel_ready(channel))
1197 return -EPERM;
1198 }
1199
1200 switch (cmd) {
1201 case LTTNG_UST_STREAM:
1202 {
1203 struct lttng_ust_stream *stream;
1204
1205 stream = (struct lttng_ust_stream *) arg;
1206 /* stream used as output */
1207 return lttng_abi_map_stream(objd, stream, uargs, owner);
1208 }
1209 case LTTNG_UST_EVENT:
1210 {
1211 struct lttng_ust_event *event_param =
1212 (struct lttng_ust_event *) arg;
1213
1214 if (strutils_is_star_glob_pattern(event_param->name)) {
1215 /*
1216 * If the event name is a star globbing pattern,
1217 * we create the special star globbing enabler.
1218 */
1219 return lttng_abi_create_event_enabler(objd, event_param,
1220 owner, LTTNG_ENABLER_FORMAT_STAR_GLOB);
1221 } else {
1222 return lttng_abi_create_event_enabler(objd, event_param,
1223 owner, LTTNG_ENABLER_FORMAT_EVENT);
1224 }
1225 }
1226 case LTTNG_UST_CONTEXT:
1227 return lttng_abi_add_context(objd,
1228 (struct lttng_ust_context *) arg, uargs,
1229 &channel->ctx, channel->session);
1230 case LTTNG_UST_ENABLE:
1231 return lttng_channel_enable(channel);
1232 case LTTNG_UST_DISABLE:
1233 return lttng_channel_disable(channel);
1234 case LTTNG_UST_FLUSH_BUFFER:
1235 return channel->ops->flush_buffer(channel->chan, channel->handle);
1236 default:
1237 return -EINVAL;
1238 }
1239}
1240
1241static
1242int lttng_channel_release(int objd)
1243{
1244 struct lttng_channel *channel = objd_private(objd);
1245
1246 if (channel)
1247 return lttng_ust_objd_unref(channel->session->objd, 0);
1248 return 0;
1249}
1250
1251static const struct lttng_ust_objd_ops lttng_channel_ops = {
1252 .release = lttng_channel_release,
1253 .cmd = lttng_channel_cmd,
1254};
1255
1256/**
1257 * lttng_enabler_cmd - lttng control through object descriptors
1258 *
1259 * @objd: the object descriptor
1260 * @cmd: the command
1261 * @arg: command arg
1262 * @uargs: UST arguments (internal)
1263 * @owner: objd owner
1264 *
1265 * This object descriptor implements lttng commands:
1266 * LTTNG_UST_CONTEXT
1267 * Prepend a context field to each record of events of this
1268 * enabler.
1269 * LTTNG_UST_ENABLE
1270 * Enable recording for this enabler
1271 * LTTNG_UST_DISABLE
1272 * Disable recording for this enabler
1273 * LTTNG_UST_FILTER
1274 * Attach a filter to an enabler.
1275 * LTTNG_UST_EXCLUSION
1276 * Attach exclusions to an enabler.
1277 */
1278static
1279long lttng_event_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
1280 union ust_args *uargs, void *owner)
1281{
1282 struct lttng_event_enabler *enabler = objd_private(objd);
1283
1284 switch (cmd) {
1285 case LTTNG_UST_CONTEXT:
1286 return lttng_event_enabler_attach_context(enabler,
1287 (struct lttng_ust_context *) arg);
1288 case LTTNG_UST_ENABLE:
1289 return lttng_event_enabler_enable(enabler);
1290 case LTTNG_UST_DISABLE:
1291 return lttng_event_enabler_disable(enabler);
1292 case LTTNG_UST_FILTER:
1293 {
1294 int ret;
1295
1296 ret = lttng_event_enabler_attach_filter_bytecode(enabler,
1297 (struct lttng_ust_bytecode_node *) arg);
1298 if (ret)
1299 return ret;
1300 return 0;
1301 }
1302 case LTTNG_UST_EXCLUSION:
1303 {
1304 return lttng_event_enabler_attach_exclusion(enabler,
1305 (struct lttng_ust_excluder_node *) arg);
1306 }
1307 default:
1308 return -EINVAL;
1309 }
1310}
1311
1312static
1313int lttng_event_enabler_release(int objd)
1314{
1315 struct lttng_event_enabler *event_enabler = objd_private(objd);
1316
1317 if (event_enabler)
1318 return lttng_ust_objd_unref(event_enabler->chan->objd, 0);
1319
1320 return 0;
1321}
1322
1323static const struct lttng_ust_objd_ops lttng_event_enabler_ops = {
1324 .release = lttng_event_enabler_release,
1325 .cmd = lttng_event_enabler_cmd,
1326};
1327
1328void lttng_ust_abi_exit(void)
1329{
1330 lttng_ust_abi_close_in_progress = 1;
1331 ust_lock_nocheck();
1332 objd_table_destroy();
1333 ust_unlock();
1334 lttng_ust_abi_close_in_progress = 0;
1335}
This page took 0.032148 seconds and 4 git commands to generate.