Fix: liblttng-ust-fork Makefile flags mismatch
[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
4318ae1b 40#include <lttng/ust-abi.h>
7bc53e94 41#include <lttng/ust-error.h>
f4681817
MD
42#include <urcu/compiler.h>
43#include <urcu/list.h>
4318ae1b 44#include <lttng/ust-events.h>
23c8854a 45#include <lttng/ust-version.h>
902e1379 46#include <lttng/tracepoint.h>
d8de1354 47#include "tracepoint-internal.h"
44c72f10
MD
48#include <usterr-signal-safe.h>
49#include <helper.h>
7dd08bec 50#include "lttng-tracer.h"
4ab44fbe 51
7047f3da
MD
52#define OBJ_NAME_LEN 16
53
45e9e699
MD
54static int lttng_ust_abi_close_in_progress;
55
51489cad 56static
f59ed768 57int lttng_abi_tracepoint_list(void *owner);
06d4f27e 58static
f59ed768 59int lttng_abi_tracepoint_field_list(void *owner);
51489cad 60
f4681817
MD
61/*
62 * Object descriptor table. Should be protected from concurrent access
63 * by the caller.
64 */
65
b61ce3b2 66struct lttng_ust_obj {
f4681817
MD
67 union {
68 struct {
69 void *private_data;
b61ce3b2 70 const struct lttng_ust_objd_ops *ops;
f4681817 71 int f_count;
164931d2 72 int owner_ref; /* has ref from owner */
f59ed768 73 void *owner;
7047f3da 74 char name[OBJ_NAME_LEN];
f4681817
MD
75 } s;
76 int freelist_next; /* offset freelist. end is -1. */
77 } u;
78};
79
b61ce3b2
MD
80struct lttng_ust_objd_table {
81 struct lttng_ust_obj *array;
f4681817
MD
82 unsigned int len, allocated_len;
83 int freelist_head; /* offset freelist head. end is -1 */
84};
85
b61ce3b2 86static struct lttng_ust_objd_table objd_table = {
f4681817
MD
87 .freelist_head = -1,
88};
89
90static
f59ed768 91int objd_alloc(void *private_data, const struct lttng_ust_objd_ops *ops,
7047f3da 92 void *owner, const char *name)
f4681817 93{
b61ce3b2 94 struct lttng_ust_obj *obj;
f4681817
MD
95
96 if (objd_table.freelist_head != -1) {
97 obj = &objd_table.array[objd_table.freelist_head];
98 objd_table.freelist_head = obj->u.freelist_next;
99 goto end;
100 }
101
102 if (objd_table.len >= objd_table.allocated_len) {
103 unsigned int new_allocated_len, old_allocated_len;
b61ce3b2 104 struct lttng_ust_obj *new_table, *old_table;
f4681817
MD
105
106 old_allocated_len = objd_table.allocated_len;
107 old_table = objd_table.array;
108 if (!old_allocated_len)
109 new_allocated_len = 1;
110 else
111 new_allocated_len = old_allocated_len << 1;
b61ce3b2 112 new_table = zmalloc(sizeof(struct lttng_ust_obj) * new_allocated_len);
f4681817
MD
113 if (!new_table)
114 return -ENOMEM;
115 memcpy(new_table, old_table,
b61ce3b2 116 sizeof(struct lttng_ust_obj) * old_allocated_len);
f4681817
MD
117 free(old_table);
118 objd_table.array = new_table;
119 objd_table.allocated_len = new_allocated_len;
120 }
121 obj = &objd_table.array[objd_table.len];
122 objd_table.len++;
123end:
124 obj->u.s.private_data = private_data;
125 obj->u.s.ops = ops;
a4be8962
MD
126 obj->u.s.f_count = 2; /* count == 1 : object is allocated */
127 /* count == 2 : allocated + hold ref */
164931d2 128 obj->u.s.owner_ref = 1; /* One owner reference */
f59ed768 129 obj->u.s.owner = owner;
7047f3da
MD
130 strncpy(obj->u.s.name, name, OBJ_NAME_LEN);
131 obj->u.s.name[OBJ_NAME_LEN - 1] = '\0';
f4681817
MD
132 return obj - objd_table.array;
133}
134
135static
b61ce3b2 136struct lttng_ust_obj *_objd_get(int id)
f4681817
MD
137{
138 if (id >= objd_table.len)
139 return NULL;
a4be8962
MD
140 if (!objd_table.array[id].u.s.f_count)
141 return NULL;
f4681817
MD
142 return &objd_table.array[id];
143}
144
145static
146void *objd_private(int id)
147{
b61ce3b2 148 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
149 assert(obj);
150 return obj->u.s.private_data;
151}
152
153static
154void objd_set_private(int id, void *private_data)
155{
b61ce3b2 156 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
157 assert(obj);
158 obj->u.s.private_data = private_data;
159}
160
b61ce3b2 161const struct lttng_ust_objd_ops *objd_ops(int id)
f4681817 162{
b61ce3b2 163 struct lttng_ust_obj *obj = _objd_get(id);
46050b1a 164
a4be8962
MD
165 if (!obj)
166 return NULL;
f4681817
MD
167 return obj->u.s.ops;
168}
169
170static
171void objd_free(int id)
172{
b61ce3b2 173 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
174
175 assert(obj);
176 obj->u.freelist_next = objd_table.freelist_head;
177 objd_table.freelist_head = obj - objd_table.array;
a4be8962
MD
178 assert(obj->u.s.f_count == 1);
179 obj->u.s.f_count = 0; /* deallocated */
f4681817
MD
180}
181
182static
183void objd_ref(int id)
184{
b61ce3b2 185 struct lttng_ust_obj *obj = _objd_get(id);
f4681817
MD
186 obj->u.s.f_count++;
187}
188
164931d2 189int lttng_ust_objd_unref(int id, int is_owner)
f4681817 190{
b61ce3b2 191 struct lttng_ust_obj *obj = _objd_get(id);
f4681817 192
1ea11eab
MD
193 if (!obj)
194 return -EINVAL;
a4be8962
MD
195 if (obj->u.s.f_count == 1) {
196 ERR("Reference counting error\n");
197 return -EINVAL;
198 }
164931d2
MD
199 if (is_owner) {
200 if (!obj->u.s.owner_ref) {
201 ERR("Error decrementing owner reference");
202 return -EINVAL;
203 }
204 obj->u.s.owner_ref--;
205 }
a4be8962 206 if ((--obj->u.s.f_count) == 1) {
b61ce3b2 207 const struct lttng_ust_objd_ops *ops = objd_ops(id);
a4be8962 208
f4681817
MD
209 if (ops->release)
210 ops->release(id);
211 objd_free(id);
212 }
1ea11eab 213 return 0;
f4681817
MD
214}
215
216static
217void objd_table_destroy(void)
218{
a4be8962
MD
219 int i;
220
164931d2
MD
221 for (i = 0; i < objd_table.allocated_len; i++) {
222 struct lttng_ust_obj *obj;
223
224 obj = _objd_get(i);
225 if (!obj)
226 continue;
227 if (!obj->u.s.owner_ref)
228 continue; /* only unref owner ref. */
229 (void) lttng_ust_objd_unref(i, 1);
230 }
f4681817 231 free(objd_table.array);
02fb3381
MD
232 objd_table.array = NULL;
233 objd_table.len = 0;
234 objd_table.allocated_len = 0;
17dfb34b 235 objd_table.freelist_head = -1;
f4681817
MD
236}
237
f59ed768
MD
238void lttng_ust_objd_table_owner_cleanup(void *owner)
239{
240 int i;
241
242 for (i = 0; i < objd_table.allocated_len; i++) {
243 struct lttng_ust_obj *obj;
244
245 obj = _objd_get(i);
246 if (!obj)
247 continue;
248 if (!obj->u.s.owner)
249 continue; /* skip root handles */
164931d2
MD
250 if (!obj->u.s.owner_ref)
251 continue; /* only unref owner ref. */
f59ed768 252 if (obj->u.s.owner == owner)
164931d2 253 (void) lttng_ust_objd_unref(i, 1);
f59ed768
MD
254 }
255}
256
f4681817
MD
257/*
258 * This is LTTng's own personal way to create an ABI for sessiond.
259 * We send commands over a socket.
260 */
261
b61ce3b2
MD
262static const struct lttng_ust_objd_ops lttng_ops;
263static const struct lttng_ust_objd_ops lttng_session_ops;
264static const struct lttng_ust_objd_ops lttng_channel_ops;
265static const struct lttng_ust_objd_ops lttng_metadata_ops;
e58095ef 266static const struct lttng_ust_objd_ops lttng_enabler_ops;
b61ce3b2 267static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops;
51489cad 268static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops;
06d4f27e 269static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops;
f4681817
MD
270
271enum channel_type {
272 PER_CPU_CHANNEL,
273 METADATA_CHANNEL,
274};
275
46050b1a
MD
276int lttng_abi_create_root_handle(void)
277{
278 int root_handle;
279
f59ed768 280 /* root handles have NULL owners */
7047f3da 281 root_handle = objd_alloc(NULL, &lttng_ops, NULL, "root");
46050b1a
MD
282 return root_handle;
283}
284
818173b9 285static
f59ed768 286int lttng_abi_create_session(void *owner)
f4681817 287{
7dd08bec 288 struct lttng_session *session;
f4681817
MD
289 int session_objd, ret;
290
7dd08bec 291 session = lttng_session_create();
f4681817
MD
292 if (!session)
293 return -ENOMEM;
7047f3da 294 session_objd = objd_alloc(session, &lttng_session_ops, owner, "session");
f4681817
MD
295 if (session_objd < 0) {
296 ret = session_objd;
297 goto objd_error;
298 }
299 session->objd = session_objd;
300 return session_objd;
301
302objd_error:
7dd08bec 303 lttng_session_destroy(session);
f4681817
MD
304 return ret;
305}
306
f4681817
MD
307static
308long lttng_abi_tracer_version(int objd,
309 struct lttng_ust_tracer_version *v)
310{
0f4eaec3
MD
311 v->major = LTTNG_UST_INTERNAL_MAJOR_VERSION;
312 v->minor = LTTNG_UST_INTERNAL_MINOR_VERSION;
313 v->patchlevel = LTTNG_UST_INTERNAL_PATCHLEVEL_VERSION;
f4681817
MD
314 return 0;
315}
316
317static
318long lttng_abi_add_context(int objd,
319 struct lttng_ust_context *context_param,
7dd08bec 320 struct lttng_ctx **ctx, struct lttng_session *session)
f4681817 321{
e58095ef 322 return lttng_attach_context(context_param, ctx, session);
f4681817
MD
323}
324
325/**
326 * lttng_cmd - lttng control through socket commands
327 *
328 * @objd: the object descriptor
329 * @cmd: the command
330 * @arg: command arg
ef9ff354 331 * @uargs: UST arguments (internal)
f59ed768 332 * @owner: objd owner
f4681817
MD
333 *
334 * This descriptor implements lttng commands:
335 * LTTNG_UST_SESSION
336 * Returns a LTTng trace session object descriptor
337 * LTTNG_UST_TRACER_VERSION
338 * Returns the LTTng kernel tracer version
339 * LTTNG_UST_TRACEPOINT_LIST
340 * Returns a file descriptor listing available tracepoints
06d4f27e
MD
341 * LTTNG_UST_TRACEPOINT_FIELD_LIST
342 * Returns a file descriptor listing available tracepoint fields
f4681817
MD
343 * LTTNG_UST_WAIT_QUIESCENT
344 * Returns after all previously running probes have completed
345 *
346 * The returned session will be deleted when its file descriptor is closed.
347 */
348static
ef9ff354 349long lttng_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 350 union ust_args *uargs, void *owner)
f4681817
MD
351{
352 switch (cmd) {
353 case LTTNG_UST_SESSION:
f59ed768 354 return lttng_abi_create_session(owner);
f4681817
MD
355 case LTTNG_UST_TRACER_VERSION:
356 return lttng_abi_tracer_version(objd,
357 (struct lttng_ust_tracer_version *) arg);
358 case LTTNG_UST_TRACEPOINT_LIST:
f59ed768 359 return lttng_abi_tracepoint_list(owner);
06d4f27e 360 case LTTNG_UST_TRACEPOINT_FIELD_LIST:
f59ed768 361 return lttng_abi_tracepoint_field_list(owner);
f4681817
MD
362 case LTTNG_UST_WAIT_QUIESCENT:
363 synchronize_trace();
364 return 0;
365 default:
366 return -EINVAL;
367 }
368}
369
b61ce3b2 370static const struct lttng_ust_objd_ops lttng_ops = {
f4681817
MD
371 .cmd = lttng_cmd,
372};
373
374/*
375 * We tolerate no failure in this function (if one happens, we print a dmesg
376 * error, but cannot return any error, because the channel information is
377 * invariant.
378 */
379static
380void lttng_metadata_create_events(int channel_objd)
381{
e58095ef
MD
382 struct lttng_channel *chan = objd_private(channel_objd);
383 struct lttng_enabler *enabler;
f4681817
MD
384 static struct lttng_ust_event metadata_params = {
385 .instrumentation = LTTNG_UST_TRACEPOINT,
a4ada9b8 386 .name = "lttng_ust:metadata",
457a6b58
MD
387 .loglevel_type = LTTNG_UST_LOGLEVEL_ALL,
388 .loglevel = TRACE_DEFAULT,
f4681817 389 };
f4681817
MD
390
391 /*
392 * We tolerate no failure path after event creation. It will stay
393 * invariant for the rest of the session.
394 */
e58095ef
MD
395 enabler = lttng_enabler_create(LTTNG_ENABLER_EVENT,
396 &metadata_params, chan);
397 if (!enabler) {
f4681817
MD
398 goto create_error;
399 }
400 return;
401
402create_error:
403 WARN_ON(1);
404 return; /* not allowed to return error */
405}
406
f4681817
MD
407int lttng_abi_create_channel(int session_objd,
408 struct lttng_ust_channel *chan_param,
ef9ff354 409 enum channel_type channel_type,
f59ed768
MD
410 union ust_args *uargs,
411 void *owner)
f4681817 412{
7dd08bec 413 struct lttng_session *session = objd_private(session_objd);
b61ce3b2 414 const struct lttng_ust_objd_ops *ops;
f4681817 415 const char *transport_name;
7dd08bec 416 struct lttng_channel *chan;
f4681817
MD
417 int chan_objd;
418 int ret = 0;
7dd08bec 419 struct lttng_channel chan_priv_init;
f4681817 420
f4681817
MD
421 switch (channel_type) {
422 case PER_CPU_CHANNEL:
423 if (chan_param->output == LTTNG_UST_MMAP) {
424 transport_name = chan_param->overwrite ?
425 "relay-overwrite-mmap" : "relay-discard-mmap";
426 } else {
427 return -EINVAL;
428 }
429 ops = &lttng_channel_ops;
430 break;
431 case METADATA_CHANNEL:
432 if (chan_param->output == LTTNG_UST_MMAP)
433 transport_name = "relay-metadata-mmap";
434 else
435 return -EINVAL;
436 ops = &lttng_metadata_ops;
437 break;
438 default:
439 transport_name = "<unknown>";
fe38d4af
MD
440 return -EINVAL;
441 }
7047f3da 442 chan_objd = objd_alloc(NULL, ops, owner, "channel");
fe38d4af
MD
443 if (chan_objd < 0) {
444 ret = chan_objd;
445 goto objd_error;
f4681817 446 }
d028eddb
MD
447 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
448 /* Copy of session UUID for consumer (availability through shm) */
449 memcpy(chan_priv_init.uuid, session->uuid, sizeof(session->uuid));
450
f4681817
MD
451 /*
452 * We tolerate no failure path after channel creation. It will stay
453 * invariant for the rest of the session.
454 */
7dd08bec 455 chan = lttng_channel_create(session, transport_name, NULL,
f4681817
MD
456 chan_param->subbuf_size,
457 chan_param->num_subbuf,
458 chan_param->switch_timer_interval,
193183fb 459 chan_param->read_timer_interval,
ef9ff354
MD
460 &uargs->channel.shm_fd,
461 &uargs->channel.wait_fd,
462 &uargs->channel.memory_map_size,
d028eddb 463 &chan_priv_init);
f4681817
MD
464 if (!chan) {
465 ret = -EINVAL;
466 goto chan_error;
467 }
468 objd_set_private(chan_objd, chan);
469 chan->objd = chan_objd;
470 if (channel_type == METADATA_CHANNEL) {
471 session->metadata = chan;
472 lttng_metadata_create_events(chan_objd);
473 }
f4681817
MD
474 /* The channel created holds a reference on the session */
475 objd_ref(session_objd);
476
477 return chan_objd;
478
479chan_error:
1ea11eab
MD
480 {
481 int err;
482
164931d2 483 err = lttng_ust_objd_unref(chan_objd, 1);
1ea11eab
MD
484 assert(!err);
485 }
f4681817
MD
486objd_error:
487 return ret;
488}
489
490/**
491 * lttng_session_cmd - lttng session object command
492 *
493 * @obj: the object
494 * @cmd: the command
495 * @arg: command arg
ef9ff354 496 * @uargs: UST arguments (internal)
f59ed768 497 * @owner: objd owner
f4681817
MD
498 *
499 * This descriptor implements lttng commands:
500 * LTTNG_UST_CHANNEL
501 * Returns a LTTng channel object descriptor
502 * LTTNG_UST_ENABLE
503 * Enables tracing for a session (weak enable)
504 * LTTNG_UST_DISABLE
505 * Disables tracing for a session (strong disable)
506 * LTTNG_UST_METADATA
507 * Returns a LTTng metadata object descriptor
508 *
509 * The returned channel will be deleted when its file descriptor is closed.
510 */
511static
ef9ff354 512long lttng_session_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 513 union ust_args *uargs, void *owner)
f4681817 514{
7dd08bec 515 struct lttng_session *session = objd_private(objd);
f4681817
MD
516
517 switch (cmd) {
518 case LTTNG_UST_CHANNEL:
519 return lttng_abi_create_channel(objd,
520 (struct lttng_ust_channel *) arg,
f59ed768 521 PER_CPU_CHANNEL, uargs, owner);
f4681817
MD
522 case LTTNG_UST_SESSION_START:
523 case LTTNG_UST_ENABLE:
7dd08bec 524 return lttng_session_enable(session);
f4681817
MD
525 case LTTNG_UST_SESSION_STOP:
526 case LTTNG_UST_DISABLE:
7dd08bec 527 return lttng_session_disable(session);
f4681817
MD
528 case LTTNG_UST_METADATA:
529 return lttng_abi_create_channel(objd,
530 (struct lttng_ust_channel *) arg,
f59ed768 531 METADATA_CHANNEL, uargs, owner);
f4681817
MD
532 default:
533 return -EINVAL;
534 }
535}
536
537/*
538 * Called when the last file reference is dropped.
539 *
540 * Big fat note: channels and events are invariant for the whole session after
541 * their creation. So this session destruction also destroys all channel and
542 * event structures specific to this session (they are not destroyed when their
543 * individual file is released).
544 */
545static
1ea11eab 546int lttng_release_session(int objd)
f4681817 547{
7dd08bec 548 struct lttng_session *session = objd_private(objd);
f4681817 549
1ea11eab 550 if (session) {
7dd08bec 551 lttng_session_destroy(session);
1ea11eab
MD
552 return 0;
553 } else {
554 return -EINVAL;
555 }
f4681817
MD
556}
557
b61ce3b2 558static const struct lttng_ust_objd_ops lttng_session_ops = {
1ea11eab 559 .release = lttng_release_session,
f4681817
MD
560 .cmd = lttng_session_cmd,
561};
562
51489cad 563static
ef9ff354 564long lttng_tracepoint_list_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 565 union ust_args *uargs, void *owner)
51489cad 566{
c8fcf224 567 struct lttng_ust_tracepoint_list *list = objd_private(objd);
cbef6901
MD
568 struct lttng_ust_tracepoint_iter *tp =
569 (struct lttng_ust_tracepoint_iter *) arg;
c8fcf224 570 struct lttng_ust_tracepoint_iter *iter;
51489cad
MD
571
572 switch (cmd) {
573 case LTTNG_UST_TRACEPOINT_LIST_GET:
c8fcf224
MD
574 {
575 retry:
576 iter = lttng_ust_tracepoint_list_get_iter_next(list);
577 if (!iter)
7bc53e94 578 return -LTTNG_UST_ERR_NOENT;
c8fcf224
MD
579 if (!strcmp(iter->name, "lttng_ust:metadata"))
580 goto retry;
581 memcpy(tp, iter, sizeof(*tp));
51489cad 582 return 0;
c8fcf224 583 }
51489cad
MD
584 default:
585 return -EINVAL;
586 }
587}
588
589static
f59ed768 590int lttng_abi_tracepoint_list(void *owner)
51489cad
MD
591{
592 int list_objd, ret;
c8fcf224 593 struct lttng_ust_tracepoint_list *list;
51489cad 594
7047f3da 595 list_objd = objd_alloc(NULL, &lttng_tracepoint_list_ops, owner, "tp_list");
51489cad
MD
596 if (list_objd < 0) {
597 ret = list_objd;
598 goto objd_error;
599 }
600 list = zmalloc(sizeof(*list));
601 if (!list) {
602 ret = -ENOMEM;
603 goto alloc_error;
604 }
605 objd_set_private(list_objd, list);
606
c8fcf224 607 /* populate list by walking on all registered probes. */
7dd08bec 608 ret = lttng_probes_get_event_list(list);
c8fcf224
MD
609 if (ret) {
610 goto list_error;
611 }
51489cad
MD
612 return list_objd;
613
c8fcf224
MD
614list_error:
615 free(list);
51489cad
MD
616alloc_error:
617 {
618 int err;
619
164931d2 620 err = lttng_ust_objd_unref(list_objd, 1);
51489cad
MD
621 assert(!err);
622 }
623objd_error:
624 return ret;
625}
626
627static
628int lttng_release_tracepoint_list(int objd)
629{
c8fcf224 630 struct lttng_ust_tracepoint_list *list = objd_private(objd);
51489cad
MD
631
632 if (list) {
7dd08bec 633 lttng_probes_prune_event_list(list);
51489cad
MD
634 free(list);
635 return 0;
636 } else {
637 return -EINVAL;
638 }
639}
640
641static const struct lttng_ust_objd_ops lttng_tracepoint_list_ops = {
642 .release = lttng_release_tracepoint_list,
643 .cmd = lttng_tracepoint_list_cmd,
644};
645
06d4f27e
MD
646static
647long lttng_tracepoint_field_list_cmd(int objd, unsigned int cmd,
f59ed768 648 unsigned long arg, union ust_args *uargs, void *owner)
06d4f27e
MD
649{
650 struct lttng_ust_field_list *list = objd_private(objd);
40003310 651 struct lttng_ust_field_iter *tp = &uargs->field_list.entry;
06d4f27e
MD
652 struct lttng_ust_field_iter *iter;
653
654 switch (cmd) {
655 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
656 {
657 retry:
658 iter = lttng_ust_field_list_get_iter_next(list);
659 if (!iter)
7bc53e94 660 return -LTTNG_UST_ERR_NOENT;
06d4f27e
MD
661 if (!strcmp(iter->event_name, "lttng_ust:metadata"))
662 goto retry;
663 memcpy(tp, iter, sizeof(*tp));
664 return 0;
665 }
666 default:
667 return -EINVAL;
668 }
669}
670
671static
f59ed768 672int lttng_abi_tracepoint_field_list(void *owner)
06d4f27e
MD
673{
674 int list_objd, ret;
675 struct lttng_ust_field_list *list;
676
7047f3da
MD
677 list_objd = objd_alloc(NULL, &lttng_tracepoint_field_list_ops, owner,
678 "tp_field_list");
06d4f27e
MD
679 if (list_objd < 0) {
680 ret = list_objd;
681 goto objd_error;
682 }
683 list = zmalloc(sizeof(*list));
684 if (!list) {
685 ret = -ENOMEM;
686 goto alloc_error;
687 }
688 objd_set_private(list_objd, list);
689
690 /* populate list by walking on all registered probes. */
7dd08bec 691 ret = lttng_probes_get_field_list(list);
06d4f27e
MD
692 if (ret) {
693 goto list_error;
694 }
695 return list_objd;
696
697list_error:
698 free(list);
699alloc_error:
700 {
701 int err;
702
164931d2 703 err = lttng_ust_objd_unref(list_objd, 1);
06d4f27e
MD
704 assert(!err);
705 }
706objd_error:
707 return ret;
708}
709
710static
711int lttng_release_tracepoint_field_list(int objd)
712{
713 struct lttng_ust_field_list *list = objd_private(objd);
714
715 if (list) {
7dd08bec 716 lttng_probes_prune_field_list(list);
06d4f27e
MD
717 free(list);
718 return 0;
719 } else {
720 return -EINVAL;
721 }
722}
723
724static const struct lttng_ust_objd_ops lttng_tracepoint_field_list_ops = {
725 .release = lttng_release_tracepoint_field_list,
726 .cmd = lttng_tracepoint_field_list_cmd,
727};
728
381c0f1e 729struct stream_priv_data {
4cfec15c 730 struct lttng_ust_lib_ring_buffer *buf;
7dd08bec 731 struct lttng_channel *lttng_chan;
381c0f1e
MD
732};
733
f4681817 734static
ef9ff354 735int lttng_abi_open_stream(int channel_objd, struct lttng_ust_stream *info,
f59ed768 736 union ust_args *uargs, void *owner)
f4681817 737{
7dd08bec 738 struct lttng_channel *channel = objd_private(channel_objd);
4cfec15c 739 struct lttng_ust_lib_ring_buffer *buf;
381c0f1e 740 struct stream_priv_data *priv;
f4681817
MD
741 int stream_objd, ret;
742
381c0f1e 743 buf = channel->ops->buffer_read_open(channel->chan, channel->handle,
ef9ff354
MD
744 &uargs->stream.shm_fd,
745 &uargs->stream.wait_fd,
746 &uargs->stream.memory_map_size);
f4681817
MD
747 if (!buf)
748 return -ENOENT;
749
381c0f1e
MD
750 priv = zmalloc(sizeof(*priv));
751 if (!priv) {
752 ret = -ENOMEM;
753 goto alloc_error;
754 }
755 priv->buf = buf;
7dd08bec 756 priv->lttng_chan = channel;
7047f3da 757 stream_objd = objd_alloc(priv, &lib_ring_buffer_objd_ops, owner, "open_stream");
f4681817
MD
758 if (stream_objd < 0) {
759 ret = stream_objd;
760 goto objd_error;
761 }
381c0f1e
MD
762 /* Hold a reference on the channel object descriptor */
763 objd_ref(channel_objd);
f4681817
MD
764 return stream_objd;
765
766objd_error:
381c0f1e
MD
767 free(priv);
768alloc_error:
769 channel->ops->buffer_read_close(buf, channel->handle);
f4681817
MD
770 return ret;
771}
f4681817
MD
772
773static
e58095ef 774int lttng_abi_create_enabler(int channel_objd,
f59ed768 775 struct lttng_ust_event *event_param,
e58095ef
MD
776 void *owner,
777 enum lttng_enabler_type type)
f4681817 778{
7dd08bec 779 struct lttng_channel *channel = objd_private(channel_objd);
e58095ef 780 struct lttng_enabler *enabler;
f4681817
MD
781 int event_objd, ret;
782
783 event_param->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
7047f3da 784 event_objd = objd_alloc(NULL, &lttng_enabler_ops, owner, "enabler");
f4681817
MD
785 if (event_objd < 0) {
786 ret = event_objd;
787 goto objd_error;
788 }
789 /*
790 * We tolerate no failure path after event creation. It will stay
791 * invariant for the rest of the session.
792 */
e58095ef
MD
793 enabler = lttng_enabler_create(type, event_param, channel);
794 if (!enabler) {
795 ret = -ENOMEM;
f4681817
MD
796 goto event_error;
797 }
e58095ef 798 objd_set_private(event_objd, enabler);
f4681817
MD
799 /* The event holds a reference on the channel */
800 objd_ref(channel_objd);
801 return event_objd;
802
803event_error:
1ea11eab
MD
804 {
805 int err;
806
164931d2 807 err = lttng_ust_objd_unref(event_objd, 1);
1ea11eab
MD
808 assert(!err);
809 }
f4681817
MD
810objd_error:
811 return ret;
812}
813
814/**
815 * lttng_channel_cmd - lttng control through object descriptors
816 *
817 * @objd: the object descriptor
818 * @cmd: the command
819 * @arg: command arg
ef9ff354 820 * @uargs: UST arguments (internal)
f59ed768 821 * @owner: objd owner
f4681817
MD
822 *
823 * This object descriptor implements lttng commands:
824 * LTTNG_UST_STREAM
825 * Returns an event stream object descriptor or failure.
826 * (typically, one event stream records events from one CPU)
827 * LTTNG_UST_EVENT
828 * Returns an event object descriptor or failure.
829 * LTTNG_UST_CONTEXT
830 * Prepend a context field to each event in the channel
831 * LTTNG_UST_ENABLE
832 * Enable recording for events in this channel (weak enable)
833 * LTTNG_UST_DISABLE
834 * Disable recording for events in this channel (strong disable)
835 *
836 * Channel and event file descriptors also hold a reference on the session.
837 */
838static
ef9ff354 839long lttng_channel_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 840 union ust_args *uargs, void *owner)
f4681817 841{
7dd08bec 842 struct lttng_channel *channel = objd_private(objd);
f4681817
MD
843
844 switch (cmd) {
845 case LTTNG_UST_STREAM:
381c0f1e
MD
846 {
847 struct lttng_ust_stream *stream;
848
849 stream = (struct lttng_ust_stream *) arg;
850 /* stream used as output */
f59ed768 851 return lttng_abi_open_stream(objd, stream, uargs, owner);
381c0f1e 852 }
f4681817 853 case LTTNG_UST_EVENT:
1f18504e
MD
854 {
855 struct lttng_ust_event *event_param =
856 (struct lttng_ust_event *) arg;
6b0e60f1
MD
857 if (event_param->name[strlen(event_param->name) - 1] == '*') {
858 /* If ends with wildcard, create wildcard. */
e58095ef
MD
859 return lttng_abi_create_enabler(objd, event_param,
860 owner, LTTNG_ENABLER_WILDCARD);
1f18504e 861 } else {
e58095ef
MD
862 return lttng_abi_create_enabler(objd, event_param,
863 owner, LTTNG_ENABLER_EVENT);
1f18504e
MD
864 }
865 }
f4681817
MD
866 case LTTNG_UST_CONTEXT:
867 return lttng_abi_add_context(objd,
868 (struct lttng_ust_context *) arg,
869 &channel->ctx, channel->session);
870 case LTTNG_UST_ENABLE:
7dd08bec 871 return lttng_channel_enable(channel);
f4681817 872 case LTTNG_UST_DISABLE:
7dd08bec 873 return lttng_channel_disable(channel);
43d330a4
MD
874 case LTTNG_UST_FLUSH_BUFFER:
875 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
876 default:
877 return -EINVAL;
878 }
879}
880
881/**
882 * lttng_metadata_cmd - lttng control through object descriptors
883 *
884 * @objd: the object descriptor
885 * @cmd: the command
886 * @arg: command arg
ef9ff354 887 * @uargs: UST arguments (internal)
f59ed768 888 * @owner: objd owner
f4681817
MD
889 *
890 * This object descriptor implements lttng commands:
891 * LTTNG_UST_STREAM
892 * Returns an event stream file descriptor or failure.
893 *
894 * Channel and event file descriptors also hold a reference on the session.
895 */
896static
ef9ff354 897long lttng_metadata_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 898 union ust_args *uargs, void *owner)
f4681817 899{
7dd08bec 900 struct lttng_channel *channel = objd_private(objd);
43861eab 901
f4681817
MD
902 switch (cmd) {
903 case LTTNG_UST_STREAM:
381c0f1e
MD
904 {
905 struct lttng_ust_stream *stream;
906
907 stream = (struct lttng_ust_stream *) arg;
908 /* stream used as output */
f59ed768 909 return lttng_abi_open_stream(objd, stream, uargs, owner);
381c0f1e 910 }
43d330a4
MD
911 case LTTNG_UST_FLUSH_BUFFER:
912 return channel->ops->flush_buffer(channel->chan, channel->handle);
f4681817
MD
913 default:
914 return -EINVAL;
915 }
916}
917
f4681817
MD
918static
919int lttng_channel_release(int objd)
920{
7dd08bec 921 struct lttng_channel *channel = objd_private(objd);
f4681817
MD
922
923 if (channel)
164931d2 924 return lttng_ust_objd_unref(channel->session->objd, 0);
f4681817
MD
925 return 0;
926}
927
b61ce3b2 928static const struct lttng_ust_objd_ops lttng_channel_ops = {
f4681817 929 .release = lttng_channel_release,
f4681817
MD
930 .cmd = lttng_channel_cmd,
931};
932
b61ce3b2 933static const struct lttng_ust_objd_ops lttng_metadata_ops = {
f4681817
MD
934 .release = lttng_channel_release,
935 .cmd = lttng_metadata_cmd,
936};
937
381c0f1e
MD
938/**
939 * lttng_rb_cmd - lttng ring buffer control through object descriptors
940 *
941 * @objd: the object descriptor
942 * @cmd: the command
943 * @arg: command arg
ef9ff354 944 * @uargs: UST arguments (internal)
f59ed768 945 * @owner: objd owner
381c0f1e
MD
946 *
947 * This object descriptor implements lttng commands:
948 * (None for now. Access is done directly though shm.)
381c0f1e
MD
949 */
950static
ef9ff354 951long lttng_rb_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 952 union ust_args *uargs, void *owner)
381c0f1e 953{
381c0f1e
MD
954 switch (cmd) {
955 default:
956 return -EINVAL;
957 }
958}
959
960static
961int lttng_rb_release(int objd)
962{
963 struct stream_priv_data *priv = objd_private(objd);
4cfec15c 964 struct lttng_ust_lib_ring_buffer *buf;
7dd08bec 965 struct lttng_channel *channel;
381c0f1e
MD
966
967 if (priv) {
968 buf = priv->buf;
7dd08bec 969 channel = priv->lttng_chan;
381c0f1e 970 free(priv);
45e9e699
MD
971 /*
972 * If we are at ABI exit, we don't want to close the
973 * buffer opened for read: it is being shared between
974 * the parent and child (right after fork), and we don't
975 * want the child to close it for the parent. For a real
976 * exit, we don't care about marking it as closed, as
977 * the consumer daemon (if there is one) will do fine
978 * even if we don't mark it as "closed" for reading on
979 * our side.
980 * We only mark it as closed if it is being explicitely
981 * released by the session daemon with an explicit
982 * release command.
983 */
984 if (!lttng_ust_abi_close_in_progress)
985 channel->ops->buffer_read_close(buf, channel->handle);
381c0f1e 986
164931d2 987 return lttng_ust_objd_unref(channel->objd, 0);
381c0f1e
MD
988 }
989 return 0;
990}
991
b61ce3b2 992static const struct lttng_ust_objd_ops lib_ring_buffer_objd_ops = {
381c0f1e
MD
993 .release = lttng_rb_release,
994 .cmd = lttng_rb_cmd,
995};
996
f4681817 997/**
e58095ef 998 * lttng_enabler_cmd - lttng control through object descriptors
e6c12e3d
MD
999 *
1000 * @objd: the object descriptor
1001 * @cmd: the command
1002 * @arg: command arg
ef9ff354 1003 * @uargs: UST arguments (internal)
f59ed768 1004 * @owner: objd owner
e6c12e3d
MD
1005 *
1006 * This object descriptor implements lttng commands:
1007 * LTTNG_UST_CONTEXT
1008 * Prepend a context field to each record of events of this
e58095ef 1009 * enabler.
e6c12e3d 1010 * LTTNG_UST_ENABLE
e58095ef 1011 * Enable recording for this enabler
e6c12e3d 1012 * LTTNG_UST_DISABLE
e58095ef 1013 * Disable recording for this enabler
2d78951a 1014 * LTTNG_UST_FILTER
e58095ef 1015 * Attach a filter to an enabler.
e6c12e3d
MD
1016 */
1017static
e58095ef 1018long lttng_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
f59ed768 1019 union ust_args *uargs, void *owner)
e6c12e3d 1020{
e58095ef 1021 struct lttng_enabler *enabler = objd_private(objd);
e6c12e3d
MD
1022
1023 switch (cmd) {
1024 case LTTNG_UST_CONTEXT:
e58095ef
MD
1025 return lttng_enabler_attach_context(enabler,
1026 (struct lttng_ust_context *) arg);
e6c12e3d 1027 case LTTNG_UST_ENABLE:
e58095ef 1028 return lttng_enabler_enable(enabler);
e6c12e3d 1029 case LTTNG_UST_DISABLE:
e58095ef 1030 return lttng_enabler_disable(enabler);
2d78951a
MD
1031 case LTTNG_UST_FILTER:
1032 {
1033 int ret;
1034
e58095ef 1035 ret = lttng_enabler_attach_bytecode(enabler,
f488575f 1036 (struct lttng_ust_filter_bytecode_node *) arg);
2d78951a
MD
1037 if (ret)
1038 return ret;
2d78951a
MD
1039 return 0;
1040 }
e6c12e3d
MD
1041 default:
1042 return -EINVAL;
1043 }
1044}
1045
1046static
e58095ef 1047int lttng_enabler_release(int objd)
e6c12e3d 1048{
e58095ef 1049 struct lttng_enabler *enabler = objd_private(objd);
e6c12e3d 1050
e58095ef 1051 if (enabler)
164931d2 1052 return lttng_ust_objd_unref(enabler->chan->objd, 0);
e6c12e3d
MD
1053 return 0;
1054}
1055
e58095ef
MD
1056static const struct lttng_ust_objd_ops lttng_enabler_ops = {
1057 .release = lttng_enabler_release,
1058 .cmd = lttng_enabler_cmd,
e6c12e3d
MD
1059};
1060
b35d179d 1061void lttng_ust_abi_exit(void)
f4681817 1062{
45e9e699 1063 lttng_ust_abi_close_in_progress = 1;
f4681817 1064 objd_table_destroy();
45e9e699 1065 lttng_ust_abi_close_in_progress = 0;
f4681817 1066}
This page took 0.079617 seconds and 4 git commands to generate.