SoW-2019-0002: Dynamic Snapshot
[deliverable/lttng-ust.git] / liblttng-ust / lttng-ust-abi.c
CommitLineData
f4681817
MD
1/*
2 * lttng-ust-abi.c
3 *
f4681817
MD
4 * LTTng UST ABI
5 *
e92f3e28
MD
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 *
f4681817
MD
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.
f4681817
MD
38 */
39
3fbec7dc 40#define _LGPL_SOURCE
d5fc3224 41#include <fcntl.h>
fb31eb73 42#include <stdint.h>
4e79769f 43#include <unistd.h>
fb31eb73 44
f4681817
MD
45#include <urcu/compiler.h>
46#include <urcu/list.h>
fb31eb73
FD
47
48#include <helper.h>
49#include <lttng/tracepoint.h>
50#include <lttng/ust-abi.h>
51#include <lttng/ust-error.h>
4318ae1b 52#include <lttng/ust-events.h>
23c8854a 53#include <lttng/ust-version.h>
6548fca4 54#include <ust-fd.h>
44c72f10 55#include <usterr-signal-safe.h>
fb31eb73
FD
56
57#include "../libringbuffer/frontend_types.h"
58#include "../libringbuffer/shm.h"
7dd08bec 59#include "lttng-tracer.h"
196ec2df 60#include "string-utils.h"
d5fc3224 61#include "ust-events-internal.h"
4ab44fbe 62
7047f3da
MD
63#define OBJ_NAME_LEN 16
64
45e9e699
MD
65static int lttng_ust_abi_close_in_progress;
66
51489cad 67static
f59ed768 68int lttng_abi_tracepoint_list(void *owner);
06d4f27e 69static
f59ed768 70int lttng_abi_tracepoint_field_list(void *owner);
51489cad 71
f4681817
MD
72/*
73 * Object descriptor table. Should be protected from concurrent access
74 * by the caller.
75 */
76
b61ce3b2 77struct lttng_ust_obj {
f4681817
MD
78 union {
79 struct {
80 void *private_data;
b61ce3b2 81 const struct lttng_ust_objd_ops *ops;
f4681817 82 int f_count;
1849ef7c 83 int owner_ref; /* has ref from owner */
f59ed768 84 void *owner;
7047f3da 85 char name[OBJ_NAME_LEN];
f4681817
MD
86 } s;
87 int freelist_next; /* offset freelist. end is -1. */
88 } u;
89};
90
b61ce3b2
MD
91struct lttng_ust_objd_table {
92 struct lttng_ust_obj *array;
f4681817
MD
93 unsigned int len, allocated_len;
94 int freelist_head; /* offset freelist head. end is -1 */
95};
96
b61ce3b2 97static struct lttng_ust_objd_table objd_table = {
f4681817
MD
98 .freelist_head = -1,
99};
100
101static
f59ed768 102int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops,
7047f3da 103 void *owner, const char *name)
f4681817 104{
b61ce3b2 105 struct lttng_ust_obj *obj;
f4681817
MD
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;
b61ce3b2 115 struct lttng_ust_obj *new_table, *old_table;
f4681817
MD
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;
b61ce3b2 123 new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len);
f4681817
MD
124 if (!new_table)
125 return -ENOMEM;
126 memcpy(new_table, old_table,
b61ce3b2 127 sizeof(struct lttng_ust_obj) * old_allocated_len);
f4681817
MD
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++;
134end:
135 obj->u.s.private_data = private_data;
136 obj->u.s.ops = ops;
a4be8962
MD
137 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
138 /* count == 2 : allocated + hold ref */
1849ef7c 139 obj->u.s.owner_ref = 1; /* One owner reference */
f59ed768 140 obj->u.s.owner = owner;
7047f3da
MD
141 strncpy(obj->u.s.name, name, OBJ_NAME_LEN);
142 obj->u.s.name[OBJ_NAME_LEN - 1] = '\0';
f4681817
MD
143 return obj - objd_table.array;
144}
145
146static
b61ce3b2 147struct lttng_ust_obj *_objd_get(int id)
f4681817
MD
148{
149 if (id >= objd_table.len)
150 return NULL;
a4be8962
MD
151 if (!objd_table.array[id].u.s.f_count)
152 return NULL;
f4681817
MD
153 return &objd_table.array[id];
154}
155
156static
157void *objd_private(int id)
158{
b61ce3b2 159 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
160 assert(obj);
161 return obj->u.s.private_data;
162}
163
164static
165void objd_set_private(int id, void *private_data)
166{
b61ce3b2 167 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
168 assert(obj);
169 obj->u.s.private_data = private_data;
170}
171
b61ce3b2 172const struct lttng_ust_objd_ops *objd_ops(int id)
f4681817 173{
b61ce3b2 174 struct lttng_ust_obj *obj = _objd_get(id);
46050b1a 175
a4be8962
MD
176 if (!obj)
177 return NULL;
f4681817
MD
178 return obj->u.s.ops;
179}
180
181static
182void objd_free(int id)
183{
b61ce3b2 184 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
185
186 assert(obj);
187 obj->u.freelist_next = objd_table.freelist_head;
188 objd_table.freelist_head = obj - objd_table.array;
a4be8962
MD
189 assert(obj->u.s.f_count == 1);
190 obj->u.s.f_count = 0; /* deallocated */
f4681817
MD
191}
192
193static
194void objd_ref(int id)
195{
b61ce3b2 196 struct lttng_ust_obj *obj = _objd_get(id);
072886c4 197 assert(obj != NULL);
f4681817
MD
198 obj->u.s.f_count++;
199}
200
1849ef7c 201int lttng_ust_objd_unref(int id, int is_owner)
f4681817 202{
b61ce3b2 203 struct lttng_ust_obj *obj = _objd_get(id);
f4681817 204
1ea11eab
MD
205 if (!obj)
206 return -EINVAL;
a4be8962
MD
207 if (obj->u.s.f_count == 1) {
208 ERR("Reference counting error\n");
209 return -EINVAL;
210 }
1849ef7c
MD
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 }
a4be8962 218 if ((--obj->u.s.f_count) == 1) {
b61ce3b2 219 const struct lttng_ust_objd_ops *ops = objd_ops(id);
196ec2df 220
f4681817
MD
221 if (ops->release)
222 ops->release(id);
223 objd_free(id);
224 }
1ea11eab 225 return 0;
f4681817
MD
226}
227
228static
229void objd_table_destroy(void)
230{
a4be8962
MD
231 int i;
232
1849ef7c
MD
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 }
f4681817 243 free(objd_table.array);
02fb3381
MD
244 objd_table.array = NULL;
245 objd_table.len = 0;
246 objd_table.allocated_len = 0;
17dfb34b 247 objd_table.freelist_head = -1;
f4681817
MD
248}
249
74d81a6c
MD
250const 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
f59ed768
MD
259void 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 */
1849ef7c
MD
271 if (!obj->u.s.owner_ref)
272 continue; /* only unref owner ref. */
f59ed768 273 if (obj->u.s.owner == owner)
1849ef7c 274 (void) lttng_ust_objd_unref(i, 1);
f59ed768
MD
275 }
276}
277
f4681817
MD
278/*
279 * This is LTTng's own personal way to create an ABI for sessiond.
280 * We send commands over a socket.
281 */
282
b61ce3b2 283static const struct lttng_ust_objd_ops lttng_ops;
d5fc3224 284static const struct lttng_ust_objd_ops lttng_trigger_group_ops;
b61ce3b2
MD
285static const struct lttng_ust_objd_ops lttng_session_ops;
286static const struct lttng_ust_objd_ops lttng_channel_ops;
d5fc3224
FD
287static const struct lttng_ust_objd_ops lttng_event_enabler_ops;
288static const struct lttng_ust_objd_ops lttng_trigger_enabler_ops;
51489cad 289static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
06d4f27e 290static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
f4681817 291
46050b1a
MD
292int lttng_abi_create_root_handle(void)
293{
294 int root_handle;
295
f59ed768 296 /* root handles have NULL owners */
7047f3da 297 root_handle = objd_alloc(NULL, &lttng_ops, NULL, "root");
46050b1a
MD
298 return root_handle;
299}
300
74d81a6c
MD
301static
302int 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
818173b9 313static
f59ed768 314int lttng_abi_create_session(void *owner)
f4681817 315{
7dd08bec 316 struct lttng_session *session;
f4681817
MD
317 int session_objd, ret;
318
7dd08bec 319 session = lttng_session_create();
f4681817
MD
320 if (!session)
321 return -ENOMEM;
7047f3da 322 session_objd = objd_alloc(session, &lttng_session_ops, owner, "session");
f4681817
MD
323 if (session_objd < 0) {
324 ret = session_objd;
325 goto objd_error;
326 }
327 session->objd = session_objd;
32ce8569 328 session->owner = owner;
f4681817
MD
329 return session_objd;
330
331objd_error:
7dd08bec 332 lttng_session_destroy(session);
f4681817
MD
333 return ret;
334}
335
f4681817
MD
336static
337long lttng_abi_tracer_version(int objd,
338 struct lttng_ust_tracer_version *v)
339{
32ce8569
MD
340 v->major = LTTNG_UST_MAJOR_VERSION;
341 v->minor = LTTNG_UST_MINOR_VERSION;
342 v->patchlevel = LTTNG_UST_PATCHLEVEL_VERSION;
f4681817
MD
343 return 0;
344}
345
d5fc3224
FD
346static
347int lttng_abi_trigger_send_fd(void *owner, int trigger_notif_fd)
348{
349 struct lttng_trigger_group *trigger_group;
350 int trigger_group_objd, ret, fd_flag, close_ret;
351
352 trigger_group = lttng_trigger_group_create();
353 if (!trigger_group)
354 return -ENOMEM;
355
356 /*
357 * Set this file descriptor as NON-BLOCKING.
358 */
359 fd_flag = fcntl(trigger_notif_fd, F_GETFL);
360
361 fd_flag |= O_NONBLOCK;
362
363 ret = fcntl(trigger_notif_fd, F_SETFL, fd_flag);
364 if (ret) {
365 ret = -errno;
366 goto fd_error;
367 }
368
369 trigger_group_objd = objd_alloc(trigger_group,
370 &lttng_trigger_group_ops, owner, "trigger_group");
371 if (trigger_group_objd < 0) {
372 ret = trigger_group_objd;
373 goto objd_error;
374 }
375
376 trigger_group->objd = trigger_group_objd;
377 trigger_group->owner = owner;
378 trigger_group->notification_fd = trigger_notif_fd;
379
380 return trigger_group_objd;
381
382objd_error:
383 lttng_trigger_group_destroy(trigger_group);
384fd_error:
385 close_ret = close(trigger_notif_fd);
386 if (close_ret) {
387 PERROR("close");
388 }
389
390 return ret;
391}
392
f4681817
MD
393static
394long lttng_abi_add_context(int objd,
395 struct lttng_ust_context *context_param,
8e696cfa 396 union ust_args *uargs,
7dd08bec 397 struct lttng_ctx **ctx, struct lttng_session *session)
f4681817 398{
8e696cfa 399 return lttng_attach_context(context_param, uargs, ctx, session);
f4681817
MD
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
ef9ff354 408 * @uargs: UST arguments (internal)
f59ed768 409 * @owner: objd owner
f4681817
MD
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
06d4f27e
MD
418 * LTTNG_UST_TRACEPOINT_FIELD_LIST
419 * Returns a file descriptor listing available tracepoint fields
f4681817
MD
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 */
425static
ef9ff354 426long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 427 union ust_args *uargs, void *owner)
f4681817
MD
428{
429 switch (cmd) {
430 case LTTNG_UST_SESSION:
f59ed768 431 return lttng_abi_create_session(owner);
f4681817
MD
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:
f59ed768 436 return lttng_abi_tracepoint_list(owner);
06d4f27e 437 case LTTNG_UST_TRACEPOINT_FIELD_LIST:
f59ed768 438 return lttng_abi_tracepoint_field_list(owner);
f4681817
MD
439 case LTTNG_UST_WAIT_QUIESCENT:
440 synchronize_trace();
441 return 0;
d5fc3224
FD
442 case LTTNG_UST_TRIGGER_GROUP_CREATE:
443 return lttng_abi_trigger_send_fd(owner,
444 uargs->trigger_handle.trigger_notif_fd);
f4681817
MD
445 default:
446 return -EINVAL;
447 }
448}
449
b61ce3b2 450static const struct lttng_ust_objd_ops lttng_ops = {
f4681817
MD
451 .cmd = lttng_cmd,
452};
453
74d81a6c
MD
454int lttng_abi_map_channel(int session_objd,
455 struct lttng_ust_channel *ust_chan,
456 union ust_args *uargs,
457 void *owner)
f4681817 458{
7dd08bec 459 struct lttng_session *session = objd_private(session_objd);
f4681817 460 const char *transport_name;
74d81a6c
MD
461 const struct lttng_transport *transport;
462 const char *chan_name;
f4681817 463 int chan_objd;
74d81a6c
MD
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;
ff0f5728 469 int wakeup_fd;
74d81a6c
MD
470 uint64_t len;
471 int ret;
472 enum lttng_ust_chan_type type;
473
474 chan_data = uargs->channel.chan_data;
ff0f5728 475 wakeup_fd = uargs->channel.wakeup_fd;
74d81a6c
MD
476 len = ust_chan->len;
477 type = ust_chan->type;
478
479 switch (type) {
480 case LTTNG_UST_CHAN_PER_CPU:
74d81a6c
MD
481 break;
482 default:
ff0f5728
MD
483 ret = -EINVAL;
484 goto invalid;
74d81a6c
MD
485 }
486
487 if (session->been_active) {
488 ret = -EBUSY;
489 goto active; /* Refuse to add channel to active session */
490 }
f4681817 491
ff0f5728 492 channel_handle = channel_handle_create(chan_data, len, wakeup_fd);
74d81a6c
MD
493 if (!channel_handle) {
494 ret = -EINVAL;
495 goto handle_error;
496 }
497
498 chan = shmp(channel_handle, channel_handle->chan);
499 assert(chan);
03d2d293 500 chan->handle = channel_handle;
74d81a6c
MD
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) {
34a91bdb
MD
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 }
f4681817 525 } else {
74d81a6c
MD
526 ret = -EINVAL;
527 goto notransport;
f4681817 528 }
74d81a6c 529 chan_name = "channel";
f4681817 530 break;
f4681817 531 default:
74d81a6c
MD
532 ret = -EINVAL;
533 goto notransport;
fe38d4af 534 }
74d81a6c
MD
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);
fe38d4af
MD
544 if (chan_objd < 0) {
545 ret = chan_objd;
546 goto objd_error;
f4681817 547 }
74d81a6c
MD
548
549 /* Initialize our lttng chan */
550 lttng_chan->chan = chan;
ac6b4ac6 551 lttng_chan->tstate = 1;
74d81a6c
MD
552 lttng_chan->enabled = 1;
553 lttng_chan->ctx = NULL;
554 lttng_chan->session = session;
74d81a6c
MD
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;
74d81a6c
MD
562 lttng_chan->type = type;
563
f4681817
MD
564 /*
565 * We tolerate no failure path after channel creation. It will stay
566 * invariant for the rest of the session.
567 */
74d81a6c
MD
568 objd_set_private(chan_objd, lttng_chan);
569 lttng_chan->objd = chan_objd;
f4681817
MD
570 /* The channel created holds a reference on the session */
571 objd_ref(session_objd);
f4681817
MD
572 return chan_objd;
573
ff0f5728 574 /* error path after channel was created */
f4681817 575objd_error:
74d81a6c 576notransport:
74d81a6c
MD
577alloc_error:
578 channel_destroy(chan, channel_handle, 0);
ff0f5728
MD
579 return ret;
580
581 /*
582 * error path before channel creation (owning chan_data and
583 * wakeup_fd).
584 */
74d81a6c
MD
585handle_error:
586active:
ff0f5728
MD
587invalid:
588 {
589 int close_ret;
590
6548fca4 591 lttng_ust_lock_fd_tracker();
ff0f5728 592 close_ret = close(wakeup_fd);
6548fca4 593 lttng_ust_unlock_fd_tracker();
ff0f5728
MD
594 if (close_ret) {
595 PERROR("close");
596 }
597 }
598 free(chan_data);
f4681817
MD
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
ef9ff354 608 * @uargs: UST arguments (internal)
f59ed768 609 * @owner: objd owner
f4681817
MD
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)
f4681817
MD
618 *
619 * The returned channel will be deleted when its file descriptor is closed.
620 */
621static
ef9ff354 622long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 623 union ust_args *uargs, void *owner)
f4681817 624{
7dd08bec 625 struct lttng_session *session = objd_private(objd);
f4681817
MD
626
627 switch (cmd) {
628 case LTTNG_UST_CHANNEL:
74d81a6c 629 return lttng_abi_map_channel(objd,
f4681817 630 (struct lttng_ust_channel *) arg,
74d81a6c 631 uargs, owner);
f4681817
MD
632 case LTTNG_UST_SESSION_START:
633 case LTTNG_UST_ENABLE:
7dd08bec 634 return lttng_session_enable(session);
f4681817
MD
635 case LTTNG_UST_SESSION_STOP:
636 case LTTNG_UST_DISABLE:
7dd08bec 637 return lttng_session_disable(session);
710b8ee3
MD
638 case LTTNG_UST_SESSION_STATEDUMP:
639 return lttng_session_statedump(session);
f4681817
MD
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 */
653static
1ea11eab 654int lttng_release_session(int objd)
f4681817 655{
7dd08bec 656 struct lttng_session *session = objd_private(objd);
f4681817 657
1ea11eab 658 if (session) {
7dd08bec 659 lttng_session_destroy(session);
1ea11eab
MD
660 return 0;
661 } else {
662 return -EINVAL;
663 }
f4681817
MD
664}
665
b61ce3b2 666static const struct lttng_ust_objd_ops lttng_session_ops = {
1ea11eab 667 .release = lttng_release_session,
f4681817
MD
668 .cmd = lttng_session_cmd,
669};
670
d5fc3224
FD
671static int lttng_ust_trigger_enabler_create(int trigger_group_obj, void *owner,
672 struct lttng_ust_trigger *trigger_param,
673 enum lttng_enabler_format_type type)
674{
675 struct lttng_trigger_group *trigger_group =
676 objd_private(trigger_group_obj);
677 struct lttng_trigger_enabler *trigger_enabler;
678 int trigger_objd, ret;
679
680 trigger_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
681 trigger_objd = objd_alloc(NULL, &lttng_trigger_enabler_ops, owner,
682 "trigger enabler");
683 if (trigger_objd < 0) {
684 ret = trigger_objd;
685 goto objd_error;
686 }
687
688 trigger_enabler = lttng_trigger_enabler_create(trigger_group, type,
689 trigger_param);
690 if (!trigger_enabler) {
691 ret = -ENOMEM;
692 goto trigger_error;
693 }
694
695 objd_set_private(trigger_objd, trigger_enabler);
696 /* The trigger holds a reference on the trigger group. */
697 objd_ref(trigger_enabler->group->objd);
698
699 return trigger_objd;
700
701trigger_error:
702 {
703 int err;
704
705 err = lttng_ust_objd_unref(trigger_objd, 1);
706 assert(!err);
707 }
708objd_error:
709 return ret;
710}
711
712static
713long lttng_trigger_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
714 union ust_args *uargs, void *owner)
715{
716 struct lttng_trigger_enabler *trigger_enabler = objd_private(objd);
717 switch (cmd) {
718 case LTTNG_UST_FILTER:
719 return lttng_trigger_enabler_attach_bytecode(trigger_enabler,
720 (struct lttng_ust_filter_bytecode_node *) arg);
721 case LTTNG_UST_EXCLUSION:
722 return lttng_trigger_enabler_attach_exclusion(trigger_enabler,
723 (struct lttng_ust_excluder_node *) arg);
724 case LTTNG_UST_ENABLE:
725 return lttng_trigger_enabler_enable(trigger_enabler);
726 case LTTNG_UST_DISABLE:
727 return lttng_trigger_enabler_disable(trigger_enabler);
728 default:
729 return -EINVAL;
730 }
731}
732
733static
734long lttng_trigger_group_cmd(int objd, unsigned int cmd, unsigned long arg,
735 union ust_args *uargs, void *owner)
736{
737 switch (cmd) {
738 case LTTNG_UST_TRIGGER_CREATE:
739 {
740 struct lttng_ust_trigger *trigger_param =
741 (struct lttng_ust_trigger *) arg;
742 if (strutils_is_star_glob_pattern(trigger_param->name)) {
743 /*
744 * If the event name is a star globbing pattern,
745 * we create the special star globbing enabler.
746 */
747 return lttng_ust_trigger_enabler_create(objd, owner,
748 trigger_param, LTTNG_ENABLER_FORMAT_STAR_GLOB);
749 } else {
750 return lttng_ust_trigger_enabler_create(objd, owner,
751 trigger_param, LTTNG_ENABLER_FORMAT_EVENT);
752 }
753 }
754 default:
755 return -EINVAL;
756 }
757}
758
759static
760int lttng_trigger_enabler_release(int objd)
761{
762 struct lttng_trigger_enabler *trigger_enabler = objd_private(objd);
763
764 if (trigger_enabler)
765 return lttng_ust_objd_unref(trigger_enabler->group->objd, 0);
766 return 0;
767}
768
769static const struct lttng_ust_objd_ops lttng_trigger_enabler_ops = {
770 .release = lttng_trigger_enabler_release,
771 .cmd = lttng_trigger_enabler_cmd,
772};
773
774static
775int lttng_release_trigger_group(int objd)
776{
777 struct lttng_trigger_group *trigger_group = objd_private(objd);
778
779 if (trigger_group) {
780 lttng_trigger_group_destroy(trigger_group);
781 return 0;
782 } else {
783 return -EINVAL;
784 }
785}
786
787static const struct lttng_ust_objd_ops lttng_trigger_group_ops = {
788 .release = lttng_release_trigger_group,
789 .cmd = lttng_trigger_group_cmd,
790};
791
51489cad 792static
ef9ff354 793long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 794 union ust_args *uargs, void *owner)
51489cad 795{
c8fcf224 796 struct lttng_ust_tracepoint_list *list = objd_private(objd);
cbef6901
MD
797 struct lttng_ust_tracepoint_iter *tp =
798 (struct lttng_ust_tracepoint_iter *) arg;
c8fcf224 799 struct lttng_ust_tracepoint_iter *iter;
51489cad
MD
800
801 switch (cmd) {
802 case LTTNG_UST_TRACEPOINT_LIST_GET:
c8fcf224 803 {
c8fcf224
MD
804 iter = lttng_ust_tracepoint_list_get_iter_next(list);
805 if (!iter)
7bc53e94 806 return -LTTNG_UST_ERR_NOENT;
c8fcf224 807 memcpy(tp, iter, sizeof(*tp));
51489cad 808 return 0;
c8fcf224 809 }
51489cad
MD
810 default:
811 return -EINVAL;
812 }
813}
814
815static
f59ed768 816int lttng_abi_tracepoint_list(void *owner)
51489cad
MD
817{
818 int list_objd, ret;
c8fcf224 819 struct lttng_ust_tracepoint_list *list;
51489cad 820
7047f3da 821 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
51489cad
MD
822 if (list_objd < 0) {
823 ret = list_objd;
824 goto objd_error;
825 }
826 list = zmalloc(sizeof(*list));
827 if (!list) {
828 ret = -ENOMEM;
829 goto alloc_error;
830 }
831 objd_set_private(list_objd, list);
832
c8fcf224 833 /* populate list by walking on all registered probes. */
7dd08bec 834 ret = lttng_probes_get_event_list(list);
c8fcf224
MD
835 if (ret) {
836 goto list_error;
837 }
51489cad
MD
838 return list_objd;
839
c8fcf224
MD
840list_error:
841 free(list);
51489cad
MD
842alloc_error:
843 {
844 int err;
845
1849ef7c 846 err = lttng_ust_objd_unref(list_objd, 1);
51489cad
MD
847 assert(!err);
848 }
849objd_error:
850 return ret;
851}
852
853static
854int lttng_release_tracepoint_list(int objd)
855{
c8fcf224 856 struct lttng_ust_tracepoint_list *list = objd_private(objd);
51489cad
MD
857
858 if (list) {
7dd08bec 859 lttng_probes_prune_event_list(list);
51489cad
MD
860 free(list);
861 return 0;
862 } else {
863 return -EINVAL;
864 }
865}
866
867static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
868 .release = lttng_release_tracepoint_list,
869 .cmd = lttng_tracepoint_list_cmd,
870};
871
06d4f27e
MD
872static
873long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
f59ed768 874 unsigned long arg, union ust_args *uargs, void *owner)
06d4f27e
MD
875{
876 struct lttng_ust_field_list *list = objd_private(objd);
40003310 877 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
06d4f27e
MD
878 struct lttng_ust_field_iter *iter;
879
880 switch (cmd) {
881 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
882 {
06d4f27e
MD
883 iter = lttng_ust_field_list_get_iter_next(list);
884 if (!iter)
7bc53e94 885 return -LTTNG_UST_ERR_NOENT;
06d4f27e
MD
886 memcpy(tp, iter, sizeof(*tp));
887 return 0;
888 }
889 default:
890 return -EINVAL;
891 }
892}
893
894static
f59ed768 895int lttng_abi_tracepoint_field_list(void *owner)
06d4f27e
MD
896{
897 int list_objd, ret;
898 struct lttng_ust_field_list *list;
899
7047f3da
MD
900 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
901 "tp_field_list");
06d4f27e
MD
902 if (list_objd < 0) {
903 ret = list_objd;
904 goto objd_error;
905 }
906 list = zmalloc(sizeof(*list));
907 if (!list) {
908 ret = -ENOMEM;
909 goto alloc_error;
910 }
911 objd_set_private(list_objd, list);
912
913 /* populate list by walking on all registered probes. */
7dd08bec 914 ret = lttng_probes_get_field_list(list);
06d4f27e
MD
915 if (ret) {
916 goto list_error;
917 }
918 return list_objd;
919
920list_error:
921 free(list);
922alloc_error:
923 {
924 int err;
925
1849ef7c 926 err = lttng_ust_objd_unref(list_objd, 1);
06d4f27e
MD
927 assert(!err);
928 }
929objd_error:
930 return ret;
931}
932
933static
934int lttng_release_tracepoint_field_list(int objd)
935{
936 struct lttng_ust_field_list *list = objd_private(objd);
937
938 if (list) {
7dd08bec 939 lttng_probes_prune_field_list(list);
06d4f27e
MD
940 free(list);
941 return 0;
942 } else {
943 return -EINVAL;
944 }
945}
946
947static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
948 .release = lttng_release_tracepoint_field_list,
949 .cmd = lttng_tracepoint_field_list_cmd,
950};
951
f4681817 952static
74d81a6c 953int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
f59ed768 954 union ust_args *uargs, void *owner)
f4681817 955{
7dd08bec 956 struct lttng_channel *channel = objd_private(channel_objd);
74d81a6c 957 int ret;
f4681817 958
74d81a6c
MD
959 ret = channel_handle_add_stream(channel->handle,
960 uargs->stream.shm_fd, uargs->stream.wakeup_fd,
961 info->stream_nr, info->len);
962 if (ret)
963 goto error_add_stream;
964
965 return 0;
966
967error_add_stream:
f4681817
MD
968 return ret;
969}
f4681817
MD
970
971static
d5fc3224 972int lttng_abi_create_event_enabler(int channel_objd,
f59ed768 973 struct lttng_ust_event *event_param,
e58095ef 974 void *owner,
d5fc3224 975 enum lttng_enabler_format_type format_type)
f4681817 976{
7dd08bec 977 struct lttng_channel *channel = objd_private(channel_objd);
d5fc3224 978 struct lttng_event_enabler *enabler;
f4681817
MD
979 int event_objd, ret;
980
981 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
d5fc3224
FD
982 event_objd = objd_alloc(NULL, &lttng_event_enabler_ops, owner,
983 "event enabler");
f4681817
MD
984 if (event_objd < 0) {
985 ret = event_objd;
986 goto objd_error;
987 }
988 /*
989 * We tolerate no failure path after event creation. It will stay
990 * invariant for the rest of the session.
991 */
d5fc3224 992 enabler = lttng_event_enabler_create(format_type, event_param, channel);
e58095ef
MD
993 if (!enabler) {
994 ret = -ENOMEM;
f4681817
MD
995 goto event_error;
996 }
e58095ef 997 objd_set_private(event_objd, enabler);
f4681817
MD
998 /* The event holds a reference on the channel */
999 objd_ref(channel_objd);
1000 return event_objd;
1001
1002event_error:
1ea11eab
MD
1003 {
1004 int err;
1005
1849ef7c 1006 err = lttng_ust_objd_unref(event_objd, 1);
1ea11eab
MD
1007 assert(!err);
1008 }
f4681817
MD
1009objd_error:
1010 return ret;
1011}
1012
1013/**
1014 * lttng_channel_cmd - lttng control through object descriptors
1015 *
1016 * @objd: the object descriptor
1017 * @cmd: the command
1018 * @arg: command arg
ef9ff354 1019 * @uargs: UST arguments (internal)
f59ed768 1020 * @owner: objd owner
f4681817
MD
1021 *
1022 * This object descriptor implements lttng commands:
1023 * LTTNG_UST_STREAM
1024 * Returns an event stream object descriptor or failure.
1025 * (typically, one event stream records events from one CPU)
1026 * LTTNG_UST_EVENT
1027 * Returns an event object descriptor or failure.
1028 * LTTNG_UST_CONTEXT
1029 * Prepend a context field to each event in the channel
1030 * LTTNG_UST_ENABLE
1031 * Enable recording for events in this channel (weak enable)
1032 * LTTNG_UST_DISABLE
1033 * Disable recording for events in this channel (strong disable)
1034 *
1035 * Channel and event file descriptors also hold a reference on the session.
1036 */
1037static
ef9ff354 1038long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 1039 union ust_args *uargs, void *owner)
f4681817 1040{
7dd08bec 1041 struct lttng_channel *channel = objd_private(objd);
f4681817 1042
74d81a6c
MD
1043 if (cmd != LTTNG_UST_STREAM) {
1044 /*
1045 * Check if channel received all streams.
1046 */
1047 if (!lttng_is_channel_ready(channel))
1048 return -EPERM;
1049 }
1050
f4681817
MD
1051 switch (cmd) {
1052 case LTTNG_UST_STREAM:
381c0f1e
MD
1053 {
1054 struct lttng_ust_stream *stream;
1055
1056 stream = (struct lttng_ust_stream *) arg;
1057 /* stream used as output */
74d81a6c 1058 return lttng_abi_map_stream(objd, stream, uargs, owner);
381c0f1e 1059 }
f4681817 1060 case LTTNG_UST_EVENT:
1f18504e
MD
1061 {
1062 struct lttng_ust_event *event_param =
1063 (struct lttng_ust_event *) arg;
196ec2df
PP
1064
1065 if (strutils_is_star_glob_pattern(event_param->name)) {
1066 /*
1067 * If the event name is a star globbing pattern,
1068 * we create the special star globbing enabler.
1069 */
d5fc3224
FD
1070 return lttng_abi_create_event_enabler(objd, event_param,
1071 owner, LTTNG_ENABLER_FORMAT_STAR_GLOB);
1f18504e 1072 } else {
d5fc3224
FD
1073 return lttng_abi_create_event_enabler(objd, event_param,
1074 owner, LTTNG_ENABLER_FORMAT_EVENT);
1f18504e
MD
1075 }
1076 }
f4681817
MD
1077 case LTTNG_UST_CONTEXT:
1078 return lttng_abi_add_context(objd,
8e696cfa 1079 (struct lttng_ust_context *) arg, uargs,
f4681817
MD
1080 &channel->ctx, channel->session);
1081 case LTTNG_UST_ENABLE:
7dd08bec 1082 return lttng_channel_enable(channel);
f4681817 1083 case LTTNG_UST_DISABLE:
7dd08bec 1084 return lttng_channel_disable(channel);
43d330a4
MD
1085 case LTTNG_UST_FLUSH_BUFFER:
1086 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
1087 default:
1088 return -EINVAL;
1089 }
1090}
1091
f4681817
MD
1092static
1093int lttng_channel_release(int objd)
1094{
7dd08bec 1095 struct lttng_channel *channel = objd_private(objd);
f4681817
MD
1096
1097 if (channel)
1849ef7c 1098 return lttng_ust_objd_unref(channel->session->objd, 0);
f4681817
MD
1099 return 0;
1100}
1101
b61ce3b2 1102static const struct lttng_ust_objd_ops lttng_channel_ops = {
f4681817 1103 .release = lttng_channel_release,
f4681817
MD
1104 .cmd = lttng_channel_cmd,
1105};
1106
f4681817 1107/**
e58095ef 1108 * lttng_enabler_cmd - lttng control through object descriptors
e6c12e3d
MD
1109 *
1110 * @objd: the object descriptor
1111 * @cmd: the command
1112 * @arg: command arg
ef9ff354 1113 * @uargs: UST arguments (internal)
f59ed768 1114 * @owner: objd owner
e6c12e3d
MD
1115 *
1116 * This object descriptor implements lttng commands:
1117 * LTTNG_UST_CONTEXT
1118 * Prepend a context field to each record of events of this
e58095ef 1119 * enabler.
e6c12e3d 1120 * LTTNG_UST_ENABLE
e58095ef 1121 * Enable recording for this enabler
e6c12e3d 1122 * LTTNG_UST_DISABLE
e58095ef 1123 * Disable recording for this enabler
2d78951a 1124 * LTTNG_UST_FILTER
e58095ef 1125 * Attach a filter to an enabler.
0bfb5cbd
JI
1126 * LTTNG_UST_EXCLUSION
1127 * Attach exclusions to an enabler.
e6c12e3d
MD
1128 */
1129static
d5fc3224 1130long lttng_event_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 1131 union ust_args *uargs, void *owner)
e6c12e3d 1132{
d5fc3224 1133 struct lttng_event_enabler *enabler = objd_private(objd);
e6c12e3d
MD
1134
1135 switch (cmd) {
1136 case LTTNG_UST_CONTEXT:
d5fc3224 1137 return lttng_event_enabler_attach_context(enabler,
e58095ef 1138 (struct lttng_ust_context *) arg);
e6c12e3d 1139 case LTTNG_UST_ENABLE:
d5fc3224 1140 return lttng_event_enabler_enable(enabler);
e6c12e3d 1141 case LTTNG_UST_DISABLE:
d5fc3224 1142 return lttng_event_enabler_disable(enabler);
2d78951a
MD
1143 case LTTNG_UST_FILTER:
1144 {
1145 int ret;
1146
d5fc3224 1147 ret = lttng_event_enabler_attach_bytecode(enabler,
f488575f 1148 (struct lttng_ust_filter_bytecode_node *) arg);
2d78951a
MD
1149 if (ret)
1150 return ret;
2d78951a
MD
1151 return 0;
1152 }
0bfb5cbd
JI
1153 case LTTNG_UST_EXCLUSION:
1154 {
d5fc3224 1155 return lttng_event_enabler_attach_exclusion(enabler,
0bfb5cbd
JI
1156 (struct lttng_ust_excluder_node *) arg);
1157 }
e6c12e3d
MD
1158 default:
1159 return -EINVAL;
1160 }
1161}
1162
1163static
d5fc3224 1164int lttng_event_enabler_release(int objd)
e6c12e3d 1165{
d5fc3224
FD
1166 struct lttng_event_enabler *event_enabler = objd_private(objd);
1167
1168 if (event_enabler)
1169 return lttng_ust_objd_unref(event_enabler->chan->objd, 0);
e6c12e3d 1170
e6c12e3d
MD
1171 return 0;
1172}
1173
d5fc3224
FD
1174static const struct lttng_ust_objd_ops lttng_event_enabler_ops = {
1175 .release = lttng_event_enabler_release,
1176 .cmd = lttng_event_enabler_cmd,
e6c12e3d
MD
1177};
1178
b35d179d 1179void lttng_ust_abi_exit(void)
f4681817 1180{
45e9e699 1181 lttng_ust_abi_close_in_progress = 1;
701a658f 1182 ust_lock_nocheck();
f4681817 1183 objd_table_destroy();
701a658f 1184 ust_unlock();
45e9e699 1185 lttng_ust_abi_close_in_progress = 0;
f4681817 1186}
This page took 0.098347 seconds and 5 git commands to generate.