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