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