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