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