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