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