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