Commit | Line | Data |
---|---|---|
57773204 MD |
1 | /* |
2 | * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
e92f3e28 MD |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; version 2 of the License only. | |
57773204 MD |
8 | * |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
e92f3e28 MD |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
57773204 MD |
17 | */ |
18 | ||
9d335227 | 19 | #define _GNU_SOURCE |
57773204 | 20 | #include <string.h> |
4318ae1b MD |
21 | #include <lttng/ust-ctl.h> |
22 | #include <lttng/ust-abi.h> | |
c1fca457 | 23 | #include <lttng/ust-events.h> |
7a784989 | 24 | #include <sys/mman.h> |
44c72f10 MD |
25 | |
26 | #include <usterr-signal-safe.h> | |
b728d87e | 27 | #include <ust-comm.h> |
57773204 MD |
28 | |
29 | #include "../libringbuffer/backend.h" | |
30 | #include "../libringbuffer/frontend.h" | |
31 | ||
6b120308 MD |
32 | volatile enum ust_loglevel ust_loglevel; |
33 | ||
42100b84 CB |
34 | static int dummy_pipe[2]; |
35 | ||
36 | static | |
37 | void __attribute__((constructor)) setup_dummy_pipe(void) | |
38 | { | |
39 | pipe(dummy_pipe); | |
40 | /* Only dummy_pipe[0] is used */ | |
41 | close(dummy_pipe[1]); | |
42 | /* FIXME: Handle pipe() failure */ | |
43 | } | |
44 | ||
45 | static | |
46 | inline int get_dummy_fd(void) | |
47 | { | |
48 | return dup(dummy_pipe[0]); | |
49 | } | |
50 | ||
57773204 | 51 | static |
61f02aea | 52 | void init_object(struct lttng_ust_object_data *data) |
57773204 MD |
53 | { |
54 | data->handle = -1; | |
55 | data->shm_fd = -1; | |
bf5ff35e | 56 | data->shm_path = NULL; |
57773204 | 57 | data->wait_fd = -1; |
bf5ff35e | 58 | data->wait_pipe_path = NULL; |
57773204 MD |
59 | data->memory_map_size = 0; |
60 | } | |
61 | ||
2be0e72c MD |
62 | int ustctl_release_handle(int sock, int handle) |
63 | { | |
64 | struct ustcomm_ust_msg lum; | |
65 | struct ustcomm_ust_reply lur; | |
66 | int ret; | |
67 | ||
68 | if (sock >= 0) { | |
69 | memset(&lum, 0, sizeof(lum)); | |
70 | lum.handle = handle; | |
71 | lum.cmd = LTTNG_UST_RELEASE; | |
72 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
73 | if (ret < 0) { | |
74 | return ret; | |
75 | } | |
76 | } | |
77 | return 0; | |
78 | } | |
12388166 MD |
79 | /* |
80 | * If sock is negative, it means we don't have to notify the other side | |
81 | * (e.g. application has already vanished). | |
82 | */ | |
d26228ae | 83 | int ustctl_release_object(int sock, struct lttng_ust_object_data *data) |
57773204 | 84 | { |
57773204 MD |
85 | int ret; |
86 | ||
9bfc503d MD |
87 | if (!data) |
88 | return -EINVAL; | |
89 | ||
d26228ae MD |
90 | if (data->shm_fd >= 0) { |
91 | ret = close(data->shm_fd); | |
92 | if (ret < 0) { | |
93 | return ret; | |
94 | } | |
95 | } | |
bf5ff35e CB |
96 | |
97 | if (data->shm_path) { | |
98 | free(data->shm_path); | |
99 | } | |
100 | ||
d26228ae MD |
101 | if (data->wait_fd >= 0) { |
102 | ret = close(data->wait_fd); | |
103 | if (ret < 0) { | |
104 | return ret; | |
105 | } | |
106 | } | |
bf5ff35e CB |
107 | |
108 | if (data->wait_pipe_path) { | |
109 | free(data->wait_pipe_path); | |
110 | } | |
111 | ||
2be0e72c | 112 | return ustctl_release_handle(sock, data->handle); |
57773204 MD |
113 | } |
114 | ||
1c5e467e MD |
115 | /* |
116 | * Send registration done packet to the application. | |
117 | */ | |
118 | int ustctl_register_done(int sock) | |
119 | { | |
120 | struct ustcomm_ust_msg lum; | |
121 | struct ustcomm_ust_reply lur; | |
122 | int ret; | |
123 | ||
124 | DBG("Sending register done command to %d", sock); | |
125 | memset(&lum, 0, sizeof(lum)); | |
126 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
127 | lum.cmd = LTTNG_UST_REGISTER_DONE; | |
128 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
129 | if (ret) | |
130 | return ret; | |
131 | if (lur.ret_code != USTCOMM_OK) { | |
132 | DBG("Return code: %s", ustcomm_get_readable_code(lur.ret_code)); | |
133 | goto error; | |
134 | } | |
135 | return 0; | |
136 | ||
137 | error: | |
138 | return -1; | |
139 | } | |
140 | ||
57773204 MD |
141 | /* |
142 | * returns session handle. | |
143 | */ | |
144 | int ustctl_create_session(int sock) | |
145 | { | |
146 | struct ustcomm_ust_msg lum; | |
147 | struct ustcomm_ust_reply lur; | |
148 | int ret, session_handle; | |
149 | ||
150 | /* Create session */ | |
151 | memset(&lum, 0, sizeof(lum)); | |
152 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
153 | lum.cmd = LTTNG_UST_SESSION; | |
154 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
155 | if (ret) | |
156 | return ret; | |
157 | session_handle = lur.ret_val; | |
158 | DBG("received session handle %u", session_handle); | |
159 | return session_handle; | |
160 | } | |
161 | ||
162 | /* open the metadata global channel */ | |
163 | int ustctl_open_metadata(int sock, int session_handle, | |
164 | struct lttng_ust_channel_attr *chops, | |
61f02aea | 165 | struct lttng_ust_object_data **_metadata_data) |
57773204 MD |
166 | { |
167 | struct ustcomm_ust_msg lum; | |
168 | struct ustcomm_ust_reply lur; | |
61f02aea | 169 | struct lttng_ust_object_data *metadata_data; |
eeee05f3 | 170 | int ret, err = 0; |
bf5ff35e | 171 | char *shm_path, *wait_pipe_path; |
57773204 | 172 | |
9bfc503d MD |
173 | if (!chops || !_metadata_data) |
174 | return -EINVAL; | |
175 | ||
57773204 MD |
176 | metadata_data = malloc(sizeof(*metadata_data)); |
177 | if (!metadata_data) | |
178 | return -ENOMEM; | |
179 | init_object(metadata_data); | |
180 | /* Create metadata channel */ | |
181 | memset(&lum, 0, sizeof(lum)); | |
182 | lum.handle = session_handle; | |
183 | lum.cmd = LTTNG_UST_METADATA; | |
184 | lum.u.channel.overwrite = chops->overwrite; | |
185 | lum.u.channel.subbuf_size = chops->subbuf_size; | |
186 | lum.u.channel.num_subbuf = chops->num_subbuf; | |
187 | lum.u.channel.switch_timer_interval = chops->switch_timer_interval; | |
188 | lum.u.channel.read_timer_interval = chops->read_timer_interval; | |
189 | lum.u.channel.output = chops->output; | |
190 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
191 | if (ret) { | |
192 | free(metadata_data); | |
193 | return ret; | |
194 | } | |
195 | if (lur.ret_code != USTCOMM_OK) { | |
196 | free(metadata_data); | |
197 | return lur.ret_code; | |
198 | } | |
199 | metadata_data->handle = lur.ret_val; | |
200 | DBG("received metadata handle %u", metadata_data->handle); | |
201 | metadata_data->memory_map_size = lur.u.channel.memory_map_size; | |
bf5ff35e CB |
202 | /* get shm path */ |
203 | shm_path = ustcomm_recv_string(sock); | |
204 | if (!shm_path) { | |
eeee05f3 | 205 | err = 1; |
bf5ff35e CB |
206 | } else { |
207 | DBG("Received shm path: %s\n", shm_path); | |
42100b84 | 208 | metadata_data->shm_fd = get_dummy_fd(); |
bf5ff35e CB |
209 | metadata_data->shm_path = shm_path; |
210 | } | |
211 | ||
eeee05f3 MD |
212 | /* |
213 | * We need to get the second FD even if the first fails, because | |
214 | * libust expects us to read the two FDs. | |
215 | */ | |
bf5ff35e CB |
216 | |
217 | /* get wait pipe path */ | |
218 | wait_pipe_path = ustcomm_recv_string(sock); | |
219 | if (!wait_pipe_path) { | |
220 | free(shm_path); | |
eeee05f3 | 221 | err = 1; |
bf5ff35e CB |
222 | } else { |
223 | DBG("Received wait pipe path: %s\n", wait_pipe_path); | |
42100b84 | 224 | metadata_data->wait_fd = get_dummy_fd(); |
bf5ff35e CB |
225 | metadata_data->wait_pipe_path = wait_pipe_path; |
226 | } | |
227 | ||
228 | ||
eeee05f3 | 229 | if (err) |
57773204 | 230 | goto error; |
57773204 MD |
231 | *_metadata_data = metadata_data; |
232 | return 0; | |
233 | ||
234 | error: | |
d26228ae | 235 | (void) ustctl_release_object(sock, metadata_data); |
38970582 | 236 | free(metadata_data); |
57773204 MD |
237 | return -EINVAL; |
238 | } | |
239 | ||
616d3093 CB |
240 | int ustctl_open_wait_pipe(int sock, |
241 | struct lttng_ust_object_data *channel_data) | |
242 | { | |
243 | struct ustcomm_ust_msg lum; | |
244 | struct ustcomm_ust_reply lur; | |
245 | int ret; | |
246 | ||
247 | if (!channel_data) | |
248 | return -EINVAL; | |
249 | ||
250 | /* Create metadata channel */ | |
251 | memset(&lum, 0, sizeof(lum)); | |
252 | lum.handle = channel_data->handle; | |
253 | lum.cmd = LTTNG_UST_STREAM_PIPE; | |
254 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
255 | ||
256 | if (ret) { | |
257 | goto error; | |
258 | } | |
259 | if (lur.ret_code != USTCOMM_OK) { | |
260 | ret = lur.ret_code; | |
261 | goto error; | |
262 | } | |
263 | ||
264 | return 0; | |
265 | ||
266 | error: | |
267 | return ret; | |
268 | } | |
269 | ||
57773204 MD |
270 | int ustctl_create_channel(int sock, int session_handle, |
271 | struct lttng_ust_channel_attr *chops, | |
61f02aea | 272 | struct lttng_ust_object_data **_channel_data) |
57773204 MD |
273 | { |
274 | struct ustcomm_ust_msg lum; | |
275 | struct ustcomm_ust_reply lur; | |
61f02aea | 276 | struct lttng_ust_object_data *channel_data; |
eeee05f3 | 277 | int ret, err = 0; |
bf5ff35e | 278 | char *shm_path, *wait_pipe_path; |
57773204 | 279 | |
9bfc503d MD |
280 | if (!chops || !_channel_data) |
281 | return -EINVAL; | |
282 | ||
57773204 MD |
283 | channel_data = malloc(sizeof(*channel_data)); |
284 | if (!channel_data) | |
285 | return -ENOMEM; | |
286 | init_object(channel_data); | |
287 | /* Create metadata channel */ | |
288 | memset(&lum, 0, sizeof(lum)); | |
289 | lum.handle = session_handle; | |
290 | lum.cmd = LTTNG_UST_CHANNEL; | |
291 | lum.u.channel.overwrite = chops->overwrite; | |
292 | lum.u.channel.subbuf_size = chops->subbuf_size; | |
293 | lum.u.channel.num_subbuf = chops->num_subbuf; | |
294 | lum.u.channel.switch_timer_interval = chops->switch_timer_interval; | |
295 | lum.u.channel.read_timer_interval = chops->read_timer_interval; | |
296 | lum.u.channel.output = chops->output; | |
297 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
298 | if (ret) { | |
299 | free(channel_data); | |
300 | return ret; | |
301 | } | |
302 | if (lur.ret_code != USTCOMM_OK) { | |
303 | free(channel_data); | |
304 | return lur.ret_code; | |
305 | } | |
306 | channel_data->handle = lur.ret_val; | |
307 | DBG("received channel handle %u", channel_data->handle); | |
308 | channel_data->memory_map_size = lur.u.channel.memory_map_size; | |
bf5ff35e CB |
309 | /* get shm path */ |
310 | shm_path = ustcomm_recv_string(sock); | |
311 | if (!shm_path) { | |
eeee05f3 | 312 | err = 1; |
bf5ff35e CB |
313 | } else { |
314 | DBG("Received shm path: %s\n", shm_path); | |
42100b84 | 315 | channel_data->shm_fd = get_dummy_fd(); |
bf5ff35e CB |
316 | channel_data->shm_path = shm_path; |
317 | } | |
318 | ||
eeee05f3 MD |
319 | /* |
320 | * We need to get the second FD even if the first fails, because | |
321 | * libust expects us to read the two FDs. | |
322 | */ | |
bf5ff35e CB |
323 | /* get wait pipe path */ |
324 | wait_pipe_path = ustcomm_recv_string(sock); | |
325 | if (!wait_pipe_path) { | |
326 | free(shm_path); | |
eeee05f3 | 327 | err = 1; |
bf5ff35e CB |
328 | } else { |
329 | DBG("Received wait pipe path: %s\n", wait_pipe_path); | |
42100b84 | 330 | channel_data->wait_fd = get_dummy_fd(); |
bf5ff35e CB |
331 | channel_data->wait_pipe_path = wait_pipe_path; |
332 | } | |
333 | ||
eeee05f3 | 334 | if (err) |
57773204 | 335 | goto error; |
57773204 MD |
336 | *_channel_data = channel_data; |
337 | return 0; | |
338 | ||
339 | error: | |
d26228ae | 340 | (void) ustctl_release_object(sock, channel_data); |
38970582 | 341 | free(channel_data); |
57773204 MD |
342 | return -EINVAL; |
343 | } | |
344 | ||
345 | /* | |
346 | * Return -ENOENT if no more stream is available for creation. | |
347 | * Return 0 on success. | |
348 | * Return negative error value on error. | |
349 | */ | |
61f02aea MD |
350 | int ustctl_create_stream(int sock, struct lttng_ust_object_data *channel_data, |
351 | struct lttng_ust_object_data **_stream_data) | |
57773204 MD |
352 | { |
353 | struct ustcomm_ust_msg lum; | |
354 | struct ustcomm_ust_reply lur; | |
61f02aea | 355 | struct lttng_ust_object_data *stream_data; |
bf5ff35e CB |
356 | int ret, err = 0; |
357 | char *shm_path, *wait_pipe_path; | |
57773204 | 358 | |
9bfc503d MD |
359 | if (!channel_data || !_stream_data) |
360 | return -EINVAL; | |
361 | ||
57773204 MD |
362 | stream_data = malloc(sizeof(*stream_data)); |
363 | if (!stream_data) | |
364 | return -ENOMEM; | |
365 | init_object(stream_data); | |
366 | memset(&lum, 0, sizeof(lum)); | |
367 | lum.handle = channel_data->handle; | |
368 | lum.cmd = LTTNG_UST_STREAM; | |
369 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
370 | if (ret) { | |
371 | free(stream_data); | |
372 | return ret; | |
373 | } | |
374 | if (lur.ret_code != USTCOMM_OK) { | |
375 | free(stream_data); | |
376 | return lur.ret_code; | |
377 | } | |
378 | ||
379 | stream_data->handle = lur.ret_val; | |
380 | DBG("received stream handle %u", stream_data->handle); | |
381 | stream_data->memory_map_size = lur.u.stream.memory_map_size; | |
bf5ff35e CB |
382 | /* get shm path */ |
383 | shm_path = ustcomm_recv_string(sock); | |
384 | if (!shm_path) { | |
eeee05f3 | 385 | err = 1; |
bf5ff35e CB |
386 | } else { |
387 | DBG("Received shm path: %s\n", shm_path); | |
42100b84 | 388 | stream_data->shm_fd = get_dummy_fd(); |
bf5ff35e CB |
389 | stream_data->shm_path = shm_path; |
390 | } | |
391 | ||
eeee05f3 MD |
392 | /* |
393 | * We need to get the second FD even if the first fails, because | |
394 | * libust expects us to read the two FDs. | |
395 | */ | |
bf5ff35e CB |
396 | /* get wait pipe path */ |
397 | wait_pipe_path = ustcomm_recv_string(sock); | |
398 | if (!wait_pipe_path) { | |
399 | free(shm_path); | |
eeee05f3 | 400 | err = 1; |
bf5ff35e CB |
401 | } else { |
402 | DBG("Received wait pipe path: %s\n", wait_pipe_path); | |
42100b84 | 403 | stream_data->wait_fd = get_dummy_fd(); |
bf5ff35e CB |
404 | stream_data->wait_pipe_path = wait_pipe_path; |
405 | } | |
406 | ||
eeee05f3 | 407 | if (err) |
57773204 | 408 | goto error; |
57773204 MD |
409 | *_stream_data = stream_data; |
410 | return ret; | |
411 | ||
412 | error: | |
d26228ae | 413 | (void) ustctl_release_object(sock, stream_data); |
38970582 | 414 | free(stream_data); |
57773204 MD |
415 | return -EINVAL; |
416 | } | |
417 | ||
418 | int ustctl_create_event(int sock, struct lttng_ust_event *ev, | |
61f02aea MD |
419 | struct lttng_ust_object_data *channel_data, |
420 | struct lttng_ust_object_data **_event_data) | |
57773204 MD |
421 | { |
422 | struct ustcomm_ust_msg lum; | |
423 | struct ustcomm_ust_reply lur; | |
61f02aea | 424 | struct lttng_ust_object_data *event_data; |
57773204 MD |
425 | int ret; |
426 | ||
9bfc503d MD |
427 | if (!channel_data || !_event_data) |
428 | return -EINVAL; | |
429 | ||
57773204 MD |
430 | event_data = malloc(sizeof(*event_data)); |
431 | if (!event_data) | |
432 | return -ENOMEM; | |
433 | init_object(event_data); | |
434 | memset(&lum, 0, sizeof(lum)); | |
435 | lum.handle = channel_data->handle; | |
436 | lum.cmd = LTTNG_UST_EVENT; | |
437 | strncpy(lum.u.event.name, ev->name, | |
438 | LTTNG_UST_SYM_NAME_LEN); | |
439 | lum.u.event.instrumentation = ev->instrumentation; | |
457a6b58 MD |
440 | lum.u.event.loglevel_type = ev->loglevel_type; |
441 | lum.u.event.loglevel = ev->loglevel; | |
57773204 MD |
442 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); |
443 | if (ret) { | |
444 | free(event_data); | |
445 | return ret; | |
446 | } | |
447 | event_data->handle = lur.ret_val; | |
448 | DBG("received event handle %u", event_data->handle); | |
449 | *_event_data = event_data; | |
450 | return 0; | |
451 | } | |
452 | ||
453 | int ustctl_add_context(int sock, struct lttng_ust_context *ctx, | |
61f02aea MD |
454 | struct lttng_ust_object_data *obj_data, |
455 | struct lttng_ust_object_data **_context_data) | |
57773204 MD |
456 | { |
457 | struct ustcomm_ust_msg lum; | |
458 | struct ustcomm_ust_reply lur; | |
61f02aea | 459 | struct lttng_ust_object_data *context_data; |
57773204 MD |
460 | int ret; |
461 | ||
9bfc503d MD |
462 | if (!obj_data || !_context_data) |
463 | return -EINVAL; | |
464 | ||
57773204 MD |
465 | context_data = malloc(sizeof(*context_data)); |
466 | if (!context_data) | |
467 | return -ENOMEM; | |
468 | init_object(context_data); | |
469 | memset(&lum, 0, sizeof(lum)); | |
3039d8ed | 470 | lum.handle = obj_data->handle; |
57773204 MD |
471 | lum.cmd = LTTNG_UST_CONTEXT; |
472 | lum.u.context.ctx = ctx->ctx; | |
473 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
474 | if (ret) { | |
475 | free(context_data); | |
476 | return ret; | |
477 | } | |
478 | context_data->handle = lur.ret_val; | |
479 | DBG("received context handle %u", context_data->handle); | |
480 | *_context_data = context_data; | |
481 | return ret; | |
482 | } | |
483 | ||
484 | /* Enable event, channel and session ioctl */ | |
61f02aea | 485 | int ustctl_enable(int sock, struct lttng_ust_object_data *object) |
57773204 MD |
486 | { |
487 | struct ustcomm_ust_msg lum; | |
488 | struct ustcomm_ust_reply lur; | |
489 | int ret; | |
490 | ||
9bfc503d MD |
491 | if (!object) |
492 | return -EINVAL; | |
493 | ||
57773204 MD |
494 | memset(&lum, 0, sizeof(lum)); |
495 | lum.handle = object->handle; | |
496 | lum.cmd = LTTNG_UST_ENABLE; | |
497 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
498 | if (ret) | |
499 | return ret; | |
500 | DBG("enabled handle %u", object->handle); | |
501 | return 0; | |
502 | } | |
503 | ||
504 | /* Disable event, channel and session ioctl */ | |
61f02aea | 505 | int ustctl_disable(int sock, struct lttng_ust_object_data *object) |
57773204 MD |
506 | { |
507 | struct ustcomm_ust_msg lum; | |
508 | struct ustcomm_ust_reply lur; | |
509 | int ret; | |
510 | ||
9bfc503d MD |
511 | if (!object) |
512 | return -EINVAL; | |
513 | ||
57773204 MD |
514 | memset(&lum, 0, sizeof(lum)); |
515 | lum.handle = object->handle; | |
516 | lum.cmd = LTTNG_UST_DISABLE; | |
517 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
518 | if (ret) | |
519 | return ret; | |
520 | DBG("disable handle %u", object->handle); | |
521 | return 0; | |
522 | } | |
523 | ||
4a6ca058 | 524 | int ustctl_start_session(int sock, int handle) |
57773204 | 525 | { |
61f02aea | 526 | struct lttng_ust_object_data obj; |
4a6ca058 MD |
527 | |
528 | obj.handle = handle; | |
529 | return ustctl_enable(sock, &obj); | |
57773204 MD |
530 | } |
531 | ||
4a6ca058 | 532 | int ustctl_stop_session(int sock, int handle) |
57773204 | 533 | { |
61f02aea | 534 | struct lttng_ust_object_data obj; |
4a6ca058 MD |
535 | |
536 | obj.handle = handle; | |
537 | return ustctl_disable(sock, &obj); | |
57773204 MD |
538 | } |
539 | ||
57773204 MD |
540 | int ustctl_tracepoint_list(int sock) |
541 | { | |
b115631f MD |
542 | struct ustcomm_ust_msg lum; |
543 | struct ustcomm_ust_reply lur; | |
544 | int ret, tp_list_handle; | |
545 | ||
546 | memset(&lum, 0, sizeof(lum)); | |
547 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
548 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST; | |
549 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
550 | if (ret) | |
551 | return ret; | |
552 | tp_list_handle = lur.ret_val; | |
553 | DBG("received tracepoint list handle %u", tp_list_handle); | |
554 | return tp_list_handle; | |
555 | } | |
556 | ||
557 | int ustctl_tracepoint_list_get(int sock, int tp_list_handle, | |
cbef6901 | 558 | struct lttng_ust_tracepoint_iter *iter) |
b115631f MD |
559 | { |
560 | struct ustcomm_ust_msg lum; | |
561 | struct ustcomm_ust_reply lur; | |
562 | int ret; | |
563 | ||
9bfc503d MD |
564 | if (!iter) |
565 | return -EINVAL; | |
566 | ||
b115631f MD |
567 | memset(&lum, 0, sizeof(lum)); |
568 | lum.handle = tp_list_handle; | |
569 | lum.cmd = LTTNG_UST_TRACEPOINT_LIST_GET; | |
570 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
571 | if (ret) | |
572 | return ret; | |
882a56d7 | 573 | DBG("received tracepoint list entry name %s loglevel %d", |
cbef6901 | 574 | lur.u.tracepoint.name, |
882a56d7 | 575 | lur.u.tracepoint.loglevel); |
cbef6901 | 576 | memcpy(iter, &lur.u.tracepoint, sizeof(*iter)); |
b115631f | 577 | return 0; |
57773204 MD |
578 | } |
579 | ||
580 | int ustctl_tracer_version(int sock, struct lttng_ust_tracer_version *v) | |
581 | { | |
582 | struct ustcomm_ust_msg lum; | |
583 | struct ustcomm_ust_reply lur; | |
584 | int ret; | |
585 | ||
9bfc503d MD |
586 | if (!v) |
587 | return -EINVAL; | |
588 | ||
57773204 MD |
589 | memset(&lum, 0, sizeof(lum)); |
590 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
591 | lum.cmd = LTTNG_UST_TRACER_VERSION; | |
592 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
593 | if (ret) | |
594 | return ret; | |
595 | memcpy(v, &lur.u.version, sizeof(*v)); | |
596 | DBG("received tracer version"); | |
597 | return 0; | |
598 | } | |
599 | ||
600 | int ustctl_wait_quiescent(int sock) | |
601 | { | |
602 | struct ustcomm_ust_msg lum; | |
603 | struct ustcomm_ust_reply lur; | |
604 | int ret; | |
605 | ||
606 | memset(&lum, 0, sizeof(lum)); | |
607 | lum.handle = LTTNG_UST_ROOT_HANDLE; | |
608 | lum.cmd = LTTNG_UST_WAIT_QUIESCENT; | |
609 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
610 | if (ret) | |
611 | return ret; | |
612 | DBG("waited for quiescent state"); | |
613 | return 0; | |
614 | } | |
615 | ||
616 | int ustctl_calibrate(int sock, struct lttng_ust_calibrate *calibrate) | |
617 | { | |
9bfc503d MD |
618 | if (!calibrate) |
619 | return -EINVAL; | |
620 | ||
57773204 MD |
621 | return -ENOSYS; |
622 | } | |
623 | ||
f1fffc57 MD |
624 | int ustctl_sock_flush_buffer(int sock, struct lttng_ust_object_data *object) |
625 | { | |
626 | struct ustcomm_ust_msg lum; | |
627 | struct ustcomm_ust_reply lur; | |
628 | int ret; | |
629 | ||
9bfc503d MD |
630 | if (!object) |
631 | return -EINVAL; | |
632 | ||
f1fffc57 MD |
633 | memset(&lum, 0, sizeof(lum)); |
634 | lum.handle = object->handle; | |
635 | lum.cmd = LTTNG_UST_FLUSH_BUFFER; | |
636 | ret = ustcomm_send_app_cmd(sock, &lum, &lur); | |
637 | if (ret) | |
638 | return ret; | |
639 | DBG("flushed buffer handle %u", object->handle); | |
640 | return 0; | |
641 | } | |
642 | ||
57773204 MD |
643 | /* Buffer operations */ |
644 | ||
645 | /* Map channel shm into process memory */ | |
38fae1d3 | 646 | struct lttng_ust_shm_handle *ustctl_map_channel(struct lttng_ust_object_data *chan_data) |
57773204 | 647 | { |
38fae1d3 | 648 | struct lttng_ust_shm_handle *handle; |
57773204 MD |
649 | struct channel *chan; |
650 | size_t chan_size; | |
c1fca457 | 651 | struct lttng_ust_lib_ring_buffer_config *config; |
7a784989 | 652 | int ret; |
57773204 | 653 | |
9bfc503d MD |
654 | if (!chan_data) |
655 | return NULL; | |
656 | ||
57773204 MD |
657 | handle = channel_handle_create(chan_data->shm_fd, |
658 | chan_data->wait_fd, | |
659 | chan_data->memory_map_size); | |
660 | if (!handle) { | |
661 | ERR("create handle error"); | |
662 | return NULL; | |
663 | } | |
664 | /* | |
0bfe09ec MD |
665 | * Set to -1, and then close the shm fd, and set the handle shm |
666 | * fd to -1 too. We don't need the shm fds after they have been | |
667 | * mapped. | |
668 | * The wait_fd is set to -1 in chan_data because it is now owned | |
669 | * by the handle. | |
57773204 MD |
670 | */ |
671 | chan_data->shm_fd = -1; | |
672 | chan_data->wait_fd = -1; | |
673 | ||
0bfe09ec MD |
674 | /* chan is object 0. This is hardcoded. */ |
675 | if (handle->table->objects[0].shm_fd >= 0) { | |
676 | ret = close(handle->table->objects[0].shm_fd); | |
677 | if (ret) { | |
678 | perror("Error closing shm_fd"); | |
679 | } | |
680 | handle->table->objects[0].shm_fd = -1; | |
681 | } | |
682 | ||
57773204 MD |
683 | /* |
684 | * TODO: add consistency checks to be resilient if the | |
685 | * application try to feed us with incoherent channel structure | |
686 | * values. | |
687 | */ | |
688 | chan = shmp(handle, handle->chan); | |
689 | /* chan is object 0. This is hardcoded. */ | |
690 | chan_size = handle->table->objects[0].allocated_len; | |
691 | handle->shadow_chan = malloc(chan_size); | |
692 | if (!handle->shadow_chan) { | |
693 | channel_destroy(chan, handle, 1); | |
694 | return NULL; | |
695 | } | |
696 | memcpy(handle->shadow_chan, chan, chan_size); | |
bbc70d1b MD |
697 | /* |
698 | * The callback pointers in the producer are invalid in the | |
c1fca457 | 699 | * consumer. We need to look them up here. |
bbc70d1b | 700 | */ |
c1fca457 MD |
701 | config = &handle->shadow_chan->backend.config; |
702 | switch (config->client_type) { | |
703 | case LTTNG_CLIENT_METADATA: | |
704 | memcpy(&config->cb, lttng_client_callbacks_metadata, | |
705 | sizeof(config->cb)); | |
706 | break; | |
707 | case LTTNG_CLIENT_DISCARD: | |
708 | memcpy(&config->cb, lttng_client_callbacks_discard, | |
709 | sizeof(config->cb)); | |
710 | break; | |
711 | case LTTNG_CLIENT_OVERWRITE: | |
712 | memcpy(&config->cb, lttng_client_callbacks_overwrite, | |
713 | sizeof(config->cb)); | |
714 | break; | |
715 | default: | |
716 | ERR("Unknown client type %d", config->client_type); | |
717 | channel_destroy(chan, handle, 1); | |
718 | return NULL; | |
719 | } | |
7a784989 MD |
720 | /* Replace the object table pointer. */ |
721 | ret = munmap(handle->table->objects[0].memory_map, | |
722 | handle->table->objects[0].memory_map_size); | |
723 | if (ret) { | |
724 | perror("munmap"); | |
725 | assert(0); | |
726 | } | |
727 | handle->table->objects[0].memory_map = (char *) handle->shadow_chan; | |
728 | handle->table->objects[0].is_shadow = 1; | |
57773204 MD |
729 | return handle; |
730 | } | |
731 | ||
732 | /* Add stream to channel shm and map its shm into process memory */ | |
38fae1d3 | 733 | int ustctl_add_stream(struct lttng_ust_shm_handle *handle, |
61f02aea | 734 | struct lttng_ust_object_data *stream_data) |
57773204 MD |
735 | { |
736 | int ret; | |
737 | ||
9bfc503d MD |
738 | if (!handle || !stream_data) |
739 | return -EINVAL; | |
740 | ||
57773204 MD |
741 | if (!stream_data->handle) |
742 | return -ENOENT; | |
743 | /* map stream */ | |
744 | ret = channel_handle_add_stream(handle, | |
745 | stream_data->shm_fd, | |
746 | stream_data->wait_fd, | |
747 | stream_data->memory_map_size); | |
748 | if (ret) { | |
749 | ERR("add stream error\n"); | |
750 | return ret; | |
751 | } | |
752 | /* | |
38fae1d3 | 753 | * Set to -1 because the lttng_ust_shm_handle destruction will take care |
57773204 MD |
754 | * of closing shm_fd and wait_fd. |
755 | */ | |
756 | stream_data->shm_fd = -1; | |
757 | stream_data->wait_fd = -1; | |
758 | return 0; | |
759 | } | |
760 | ||
38fae1d3 | 761 | void ustctl_unmap_channel(struct lttng_ust_shm_handle *handle) |
5224b5c8 MD |
762 | { |
763 | struct channel *chan; | |
764 | ||
9bfc503d | 765 | assert(handle); |
5224b5c8 MD |
766 | chan = shmp(handle, handle->chan); |
767 | channel_destroy(chan, handle, 1); | |
768 | } | |
769 | ||
0bfe09ec MD |
770 | /* |
771 | * ustctl closes the shm_fd fds after mapping it. | |
772 | */ | |
4cfec15c | 773 | struct lttng_ust_lib_ring_buffer *ustctl_open_stream_read(struct lttng_ust_shm_handle *handle, |
6e922b24 MD |
774 | int cpu) |
775 | { | |
36860b65 | 776 | struct channel *chan; |
ef9ff354 | 777 | int *shm_fd, *wait_fd; |
bf5ff35e | 778 | char *shm_path, *wait_pipe_path; |
ef9ff354 | 779 | uint64_t *memory_map_size; |
4cfec15c | 780 | struct lttng_ust_lib_ring_buffer *buf; |
6e922b24 MD |
781 | int ret; |
782 | ||
9bfc503d MD |
783 | if (!handle) |
784 | return NULL; | |
785 | ||
36860b65 | 786 | chan = handle->shadow_chan; |
6e922b24 | 787 | buf = channel_get_ring_buffer(&chan->backend.config, |
bf5ff35e CB |
788 | chan, cpu, handle, |
789 | &shm_fd, &shm_path, | |
790 | &wait_fd, &wait_pipe_path, | |
791 | &memory_map_size); | |
6e922b24 MD |
792 | if (!buf) |
793 | return NULL; | |
794 | ret = lib_ring_buffer_open_read(buf, handle, 1); | |
795 | if (ret) | |
796 | return NULL; | |
0bfe09ec MD |
797 | /* |
798 | * We can close shm_fd early, right after is has been mapped. | |
799 | */ | |
800 | if (*shm_fd >= 0) { | |
801 | ret = close(*shm_fd); | |
802 | if (ret) { | |
803 | perror("Error closing shm_fd"); | |
804 | } | |
805 | *shm_fd = -1; | |
806 | } | |
6e922b24 MD |
807 | return buf; |
808 | } | |
809 | ||
38fae1d3 | 810 | void ustctl_close_stream_read(struct lttng_ust_shm_handle *handle, |
4cfec15c | 811 | struct lttng_ust_lib_ring_buffer *buf) |
6e922b24 | 812 | { |
9bfc503d | 813 | assert(handle && buf); |
6e922b24 MD |
814 | lib_ring_buffer_release_read(buf, handle, 1); |
815 | } | |
816 | ||
57773204 MD |
817 | /* For mmap mode, readable without "get" operation */ |
818 | ||
38fae1d3 | 819 | void *ustctl_get_mmap_base(struct lttng_ust_shm_handle *handle, |
4cfec15c | 820 | struct lttng_ust_lib_ring_buffer *buf) |
9095efe9 | 821 | { |
9bfc503d MD |
822 | if (!handle || !buf) |
823 | return NULL; | |
9095efe9 MD |
824 | return shmp(handle, buf->backend.memory_map); |
825 | } | |
826 | ||
57773204 | 827 | /* returns the length to mmap. */ |
38fae1d3 | 828 | int ustctl_get_mmap_len(struct lttng_ust_shm_handle *handle, |
4cfec15c | 829 | struct lttng_ust_lib_ring_buffer *buf, |
57773204 MD |
830 | unsigned long *len) |
831 | { | |
832 | unsigned long mmap_buf_len; | |
36860b65 | 833 | struct channel *chan; |
57773204 | 834 | |
9bfc503d MD |
835 | if (!handle || !buf || !len) |
836 | return -EINVAL; | |
837 | ||
36860b65 | 838 | chan = handle->shadow_chan; |
57773204 MD |
839 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
840 | return -EINVAL; | |
841 | mmap_buf_len = chan->backend.buf_size; | |
842 | if (chan->backend.extra_reader_sb) | |
843 | mmap_buf_len += chan->backend.subbuf_size; | |
844 | if (mmap_buf_len > INT_MAX) | |
845 | return -EFBIG; | |
846 | *len = mmap_buf_len; | |
847 | return 0; | |
848 | } | |
849 | ||
850 | /* returns the maximum size for sub-buffers. */ | |
38fae1d3 | 851 | int ustctl_get_max_subbuf_size(struct lttng_ust_shm_handle *handle, |
4cfec15c | 852 | struct lttng_ust_lib_ring_buffer *buf, |
57773204 MD |
853 | unsigned long *len) |
854 | { | |
36860b65 | 855 | struct channel *chan; |
57773204 | 856 | |
9bfc503d MD |
857 | if (!handle || !buf || !len) |
858 | return -EINVAL; | |
859 | ||
36860b65 | 860 | chan = handle->shadow_chan; |
57773204 MD |
861 | *len = chan->backend.subbuf_size; |
862 | return 0; | |
863 | } | |
864 | ||
865 | /* | |
866 | * For mmap mode, operate on the current packet (between get/put or | |
867 | * get_next/put_next). | |
868 | */ | |
869 | ||
870 | /* returns the offset of the subbuffer belonging to the mmap reader. */ | |
38fae1d3 | 871 | int ustctl_get_mmap_read_offset(struct lttng_ust_shm_handle *handle, |
4cfec15c | 872 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *off) |
57773204 | 873 | { |
36860b65 | 874 | struct channel *chan; |
57773204 MD |
875 | unsigned long sb_bindex; |
876 | ||
9bfc503d MD |
877 | if (!handle || !buf || !off) |
878 | return -EINVAL; | |
879 | ||
36860b65 | 880 | chan = handle->shadow_chan; |
57773204 MD |
881 | if (chan->backend.config.output != RING_BUFFER_MMAP) |
882 | return -EINVAL; | |
883 | sb_bindex = subbuffer_id_get_index(&chan->backend.config, | |
884 | buf->backend.buf_rsb.id); | |
885 | *off = shmp(handle, shmp_index(handle, buf->backend.array, sb_bindex)->shmp)->mmap_offset; | |
886 | return 0; | |
887 | } | |
888 | ||
889 | /* returns the size of the current sub-buffer, without padding (for mmap). */ | |
38fae1d3 | 890 | int ustctl_get_subbuf_size(struct lttng_ust_shm_handle *handle, |
4cfec15c | 891 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *len) |
57773204 | 892 | { |
36860b65 | 893 | struct channel *chan; |
57773204 | 894 | |
9bfc503d MD |
895 | if (!handle || !buf || !len) |
896 | return -EINVAL; | |
897 | ||
36860b65 | 898 | chan = handle->shadow_chan; |
57773204 MD |
899 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
900 | handle); | |
901 | return 0; | |
902 | } | |
903 | ||
904 | /* returns the size of the current sub-buffer, without padding (for mmap). */ | |
38fae1d3 | 905 | int ustctl_get_padded_subbuf_size(struct lttng_ust_shm_handle *handle, |
4cfec15c | 906 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *len) |
57773204 | 907 | { |
36860b65 | 908 | struct channel *chan; |
57773204 | 909 | |
9bfc503d MD |
910 | if (!handle || !buf || !len) |
911 | return -EINVAL; | |
912 | ||
36860b65 | 913 | chan = handle->shadow_chan; |
57773204 MD |
914 | *len = lib_ring_buffer_get_read_data_size(&chan->backend.config, buf, |
915 | handle); | |
916 | *len = PAGE_ALIGN(*len); | |
917 | return 0; | |
918 | } | |
919 | ||
920 | /* Get exclusive read access to the next sub-buffer that can be read. */ | |
38fae1d3 | 921 | int ustctl_get_next_subbuf(struct lttng_ust_shm_handle *handle, |
4cfec15c | 922 | struct lttng_ust_lib_ring_buffer *buf) |
57773204 | 923 | { |
9bfc503d MD |
924 | if (!handle || !buf) |
925 | return -EINVAL; | |
926 | ||
57773204 MD |
927 | return lib_ring_buffer_get_next_subbuf(buf, handle); |
928 | } | |
929 | ||
930 | ||
931 | /* Release exclusive sub-buffer access, move consumer forward. */ | |
38fae1d3 | 932 | int ustctl_put_next_subbuf(struct lttng_ust_shm_handle *handle, |
4cfec15c | 933 | struct lttng_ust_lib_ring_buffer *buf) |
57773204 | 934 | { |
9bfc503d MD |
935 | if (!handle || !buf) |
936 | return -EINVAL; | |
937 | ||
57773204 MD |
938 | lib_ring_buffer_put_next_subbuf(buf, handle); |
939 | return 0; | |
940 | } | |
941 | ||
942 | /* snapshot */ | |
943 | ||
944 | /* Get a snapshot of the current ring buffer producer and consumer positions */ | |
38fae1d3 | 945 | int ustctl_snapshot(struct lttng_ust_shm_handle *handle, |
4cfec15c | 946 | struct lttng_ust_lib_ring_buffer *buf) |
57773204 | 947 | { |
9bfc503d MD |
948 | if (!handle || !buf) |
949 | return -EINVAL; | |
950 | ||
57773204 MD |
951 | return lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, |
952 | &buf->prod_snapshot, handle); | |
953 | } | |
954 | ||
955 | /* Get the consumer position (iteration start) */ | |
38fae1d3 | 956 | int ustctl_snapshot_get_consumed(struct lttng_ust_shm_handle *handle, |
4cfec15c | 957 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos) |
57773204 | 958 | { |
9bfc503d MD |
959 | if (!handle || !buf || !pos) |
960 | return -EINVAL; | |
961 | ||
57773204 MD |
962 | *pos = buf->cons_snapshot; |
963 | return 0; | |
964 | } | |
965 | ||
966 | /* Get the producer position (iteration end) */ | |
38fae1d3 | 967 | int ustctl_snapshot_get_produced(struct lttng_ust_shm_handle *handle, |
4cfec15c | 968 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos) |
57773204 | 969 | { |
9bfc503d MD |
970 | if (!handle || !buf || !pos) |
971 | return -EINVAL; | |
972 | ||
57773204 MD |
973 | *pos = buf->prod_snapshot; |
974 | return 0; | |
975 | } | |
976 | ||
977 | /* Get exclusive read access to the specified sub-buffer position */ | |
38fae1d3 | 978 | int ustctl_get_subbuf(struct lttng_ust_shm_handle *handle, |
4cfec15c | 979 | struct lttng_ust_lib_ring_buffer *buf, unsigned long *pos) |
57773204 | 980 | { |
9bfc503d MD |
981 | if (!handle || !buf || !pos) |
982 | return -EINVAL; | |
983 | ||
57773204 MD |
984 | return lib_ring_buffer_get_subbuf(buf, *pos, handle); |
985 | } | |
986 | ||
987 | /* Release exclusive sub-buffer access */ | |
38fae1d3 | 988 | int ustctl_put_subbuf(struct lttng_ust_shm_handle *handle, |
4cfec15c | 989 | struct lttng_ust_lib_ring_buffer *buf) |
57773204 | 990 | { |
9bfc503d MD |
991 | if (!handle || !buf) |
992 | return -EINVAL; | |
993 | ||
57773204 MD |
994 | lib_ring_buffer_put_subbuf(buf, handle); |
995 | return 0; | |
996 | } | |
997 | ||
02a15cfb | 998 | void ustctl_flush_buffer(struct lttng_ust_shm_handle *handle, |
b52190f2 MD |
999 | struct lttng_ust_lib_ring_buffer *buf, |
1000 | int producer_active) | |
57773204 | 1001 | { |
9bfc503d | 1002 | assert(handle && buf); |
b52190f2 MD |
1003 | lib_ring_buffer_switch_slow(buf, |
1004 | producer_active ? SWITCH_ACTIVE : SWITCH_FLUSH, | |
1005 | handle); | |
57773204 | 1006 | } |