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