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