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