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