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