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