Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
eb5c4f4e | 2 | * lttng-ctl.c |
82a3637f DG |
3 | * |
4 | * Linux Trace Toolkit Control Library | |
5 | * | |
826d496d | 6 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
ab5be9fa | 7 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
fac6795d | 8 | * |
ab5be9fa | 9 | * SPDX-License-Identifier: LGPL-2.1-only |
82a3637f | 10 | * |
fac6795d DG |
11 | */ |
12 | ||
6c1c0768 | 13 | #define _LGPL_SOURCE |
44a5e5eb | 14 | #include <assert.h> |
fac6795d DG |
15 | #include <grp.h> |
16 | #include <stdio.h> | |
17 | #include <stdlib.h> | |
18 | #include <string.h> | |
19 | #include <unistd.h> | |
20 | ||
990570ed | 21 | #include <common/common.h> |
edf4b93e | 22 | #include <common/compat/errno.h> |
15e37663 | 23 | #include <common/compat/string.h> |
990570ed | 24 | #include <common/defaults.h> |
159b042f | 25 | #include <common/dynamic-buffer.h> |
fe489250 | 26 | #include <common/dynamic-array.h> |
9e620ea7 JG |
27 | #include <common/payload.h> |
28 | #include <common/payload-view.h> | |
db758600 | 29 | #include <common/sessiond-comm/sessiond-comm.h> |
159b042f | 30 | #include <common/tracker.h> |
fe489250 | 31 | #include <common/unix.h> |
a4b92340 | 32 | #include <common/uri.h> |
feb0f3e5 | 33 | #include <common/utils.h> |
a58c490f | 34 | #include <lttng/channel-internal.h> |
159b042f JG |
35 | #include <lttng/destruction-handle.h> |
36 | #include <lttng/endpoint.h> | |
de453daa | 37 | #include <lttng/event-internal.h> |
159b042f JG |
38 | #include <lttng/health-internal.h> |
39 | #include <lttng/lttng.h> | |
b178f53e | 40 | #include <lttng/session-descriptor-internal.h> |
159b042f JG |
41 | #include <lttng/session-internal.h> |
42 | #include <lttng/trigger/trigger-internal.h> | |
43 | #include <lttng/userspace-probe-internal.h> | |
fe489250 | 44 | #include <lttng/lttng-error.h> |
fac6795d | 45 | |
e4d2f27a JR |
46 | #include <common/filter/filter-ast.h> |
47 | #include <common/filter/filter-parser.h> | |
48 | #include <common/filter/filter-bytecode.h> | |
49 | #include <common/filter/memstream.h> | |
cac3069d | 50 | #include "lttng-ctl-helper.h" |
53a80697 | 51 | |
60160d2a JG |
52 | #define COPY_DOMAIN_PACKED(dst, src) \ |
53 | do { \ | |
54 | struct lttng_domain _tmp_domain; \ | |
55 | \ | |
56 | lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \ | |
57 | dst = _tmp_domain; \ | |
58 | } while (0) | |
53a80697 | 59 | |
fac6795d | 60 | /* Socket to session daemon for communication */ |
3e3665b8 | 61 | static int sessiond_socket = -1; |
fac6795d DG |
62 | static char sessiond_sock_path[PATH_MAX]; |
63 | ||
fac6795d DG |
64 | /* Variables */ |
65 | static char *tracing_group; | |
66 | static int connected; | |
67 | ||
97e19046 DG |
68 | /* Global */ |
69 | ||
70 | /* | |
71 | * Those two variables are used by error.h to silent or control the verbosity of | |
72 | * error message. They are global to the library so application linking with it | |
73 | * are able to compile correctly and also control verbosity of the library. | |
97e19046 DG |
74 | */ |
75 | int lttng_opt_quiet; | |
76 | int lttng_opt_verbose; | |
c7e35b03 | 77 | int lttng_opt_mi; |
97e19046 | 78 | |
99497cd0 MD |
79 | /* |
80 | * Copy string from src to dst and enforce null terminated byte. | |
81 | */ | |
cac3069d DG |
82 | LTTNG_HIDDEN |
83 | void lttng_ctl_copy_string(char *dst, const char *src, size_t len) | |
99497cd0 | 84 | { |
e7d6716d | 85 | if (src && dst) { |
99497cd0 MD |
86 | strncpy(dst, src, len); |
87 | /* Enforce the NULL terminated byte */ | |
88 | dst[len - 1] = '\0'; | |
cd80958d DG |
89 | } else if (dst) { |
90 | dst[0] = '\0'; | |
99497cd0 MD |
91 | } |
92 | } | |
93 | ||
fac6795d | 94 | /* |
cd80958d | 95 | * Copy domain to lttcomm_session_msg domain. |
fac6795d | 96 | * |
cd80958d DG |
97 | * If domain is unknown, default domain will be the kernel. |
98 | */ | |
cac3069d DG |
99 | LTTNG_HIDDEN |
100 | void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst, | |
101 | struct lttng_domain *src) | |
cd80958d DG |
102 | { |
103 | if (src && dst) { | |
104 | switch (src->type) { | |
00e2e675 DG |
105 | case LTTNG_DOMAIN_KERNEL: |
106 | case LTTNG_DOMAIN_UST: | |
b9dfb167 | 107 | case LTTNG_DOMAIN_JUL: |
5cdb6027 | 108 | case LTTNG_DOMAIN_LOG4J: |
0e115563 | 109 | case LTTNG_DOMAIN_PYTHON: |
00e2e675 DG |
110 | memcpy(dst, src, sizeof(struct lttng_domain)); |
111 | break; | |
112 | default: | |
113 | memset(dst, 0, sizeof(struct lttng_domain)); | |
00e2e675 | 114 | break; |
cd80958d DG |
115 | } |
116 | } | |
117 | } | |
118 | ||
119 | /* | |
120 | * Send lttcomm_session_msg to the session daemon. | |
fac6795d | 121 | * |
1c8d13c8 TD |
122 | * On success, returns the number of bytes sent (>=0) |
123 | * On error, returns -1 | |
fac6795d | 124 | */ |
cd80958d | 125 | static int send_session_msg(struct lttcomm_session_msg *lsm) |
fac6795d DG |
126 | { |
127 | int ret; | |
128 | ||
129 | if (!connected) { | |
2f70b271 | 130 | ret = -LTTNG_ERR_NO_SESSIOND; |
e065084a | 131 | goto end; |
fac6795d DG |
132 | } |
133 | ||
a4b92340 DG |
134 | DBG("LSM cmd type : %d", lsm->cmd_type); |
135 | ||
be040666 | 136 | ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm, |
cd80958d | 137 | sizeof(struct lttcomm_session_msg)); |
2f70b271 DG |
138 | if (ret < 0) { |
139 | ret = -LTTNG_ERR_FATAL; | |
140 | } | |
e065084a DG |
141 | |
142 | end: | |
143 | return ret; | |
144 | } | |
145 | ||
53a80697 MD |
146 | /* |
147 | * Send var len data to the session daemon. | |
148 | * | |
149 | * On success, returns the number of bytes sent (>=0) | |
150 | * On error, returns -1 | |
151 | */ | |
c2d69327 | 152 | static int send_session_varlen(const void *data, size_t len) |
53a80697 MD |
153 | { |
154 | int ret; | |
155 | ||
156 | if (!connected) { | |
2f70b271 | 157 | ret = -LTTNG_ERR_NO_SESSIOND; |
53a80697 MD |
158 | goto end; |
159 | } | |
a4b92340 | 160 | |
53a80697 MD |
161 | if (!data || !len) { |
162 | ret = 0; | |
163 | goto end; | |
164 | } | |
165 | ||
166 | ret = lttcomm_send_unix_sock(sessiond_socket, data, len); | |
2f70b271 DG |
167 | if (ret < 0) { |
168 | ret = -LTTNG_ERR_FATAL; | |
169 | } | |
53a80697 MD |
170 | |
171 | end: | |
172 | return ret; | |
173 | } | |
174 | ||
a04d53fc FD |
175 | /* |
176 | * Send file descriptors to the session daemon. | |
177 | * | |
178 | * On success, returns the number of bytes sent (>=0) | |
179 | * On error, returns -1 | |
180 | */ | |
181 | static int send_session_fds(const int *fds, size_t nb_fd) | |
182 | { | |
183 | int ret; | |
184 | ||
185 | if (!connected) { | |
186 | ret = -LTTNG_ERR_NO_SESSIOND; | |
187 | goto end; | |
188 | } | |
189 | ||
190 | if (!fds || !nb_fd) { | |
191 | ret = 0; | |
192 | goto end; | |
193 | } | |
194 | ||
195 | ret = lttcomm_send_fds_unix_sock(sessiond_socket, fds, nb_fd); | |
196 | if (ret < 0) { | |
197 | ret = -LTTNG_ERR_FATAL; | |
198 | } | |
199 | ||
200 | end: | |
201 | return ret; | |
202 | } | |
203 | ||
e065084a | 204 | /* |
cd80958d | 205 | * Receive data from the sessiond socket. |
e065084a | 206 | * |
1c8d13c8 | 207 | * On success, returns the number of bytes received (>=0) |
e368fb43 | 208 | * On error, returns a negative lttng_error_code. |
e065084a | 209 | */ |
ca95a216 | 210 | static int recv_data_sessiond(void *buf, size_t len) |
e065084a DG |
211 | { |
212 | int ret; | |
213 | ||
214 | if (!connected) { | |
2f70b271 | 215 | ret = -LTTNG_ERR_NO_SESSIOND; |
e065084a | 216 | goto end; |
fac6795d DG |
217 | } |
218 | ||
ca95a216 | 219 | ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); |
2f70b271 DG |
220 | if (ret < 0) { |
221 | ret = -LTTNG_ERR_FATAL; | |
222 | } | |
fac6795d | 223 | |
e065084a | 224 | end: |
fac6795d DG |
225 | return ret; |
226 | } | |
227 | ||
e368fb43 JG |
228 | /* |
229 | * Receive a payload from the session daemon by appending to an existing | |
230 | * payload. | |
231 | * On success, returns the number of bytes received (>=0) | |
232 | * On error, returns a negative lttng_error_code. | |
233 | */ | |
234 | static int recv_payload_sessiond(struct lttng_payload *payload, size_t len) | |
235 | { | |
236 | int ret; | |
237 | const size_t original_payload_size = payload->buffer.size; | |
238 | ||
239 | ret = lttng_dynamic_buffer_set_size( | |
240 | &payload->buffer, payload->buffer.size + len); | |
241 | if (ret) { | |
242 | ret = -LTTNG_ERR_NOMEM; | |
243 | goto end; | |
244 | } | |
245 | ||
246 | ret = recv_data_sessiond( | |
247 | payload->buffer.data + original_payload_size, len); | |
248 | end: | |
249 | return ret; | |
250 | } | |
251 | ||
fac6795d | 252 | /* |
9ae110e2 | 253 | * Check if we are in the specified group. |
65beb5ff | 254 | * |
9ae110e2 | 255 | * If yes return 1, else return -1. |
947308c4 | 256 | */ |
6c71277b MD |
257 | LTTNG_HIDDEN |
258 | int lttng_check_tracing_group(void) | |
947308c4 | 259 | { |
28ab59d0 | 260 | gid_t *grp_list, tracing_gid; |
947308c4 DG |
261 | int grp_list_size, grp_id, i; |
262 | int ret = -1; | |
6c71277b | 263 | const char *grp_name = tracing_group; |
947308c4 DG |
264 | |
265 | /* Get GID of group 'tracing' */ | |
28ab59d0 | 266 | if (utils_get_group_id(grp_name, false, &tracing_gid)) { |
b4d8603b | 267 | /* If grp_tracing is NULL, the group does not exist. */ |
947308c4 DG |
268 | goto end; |
269 | } | |
270 | ||
271 | /* Get number of supplementary group IDs */ | |
272 | grp_list_size = getgroups(0, NULL); | |
273 | if (grp_list_size < 0) { | |
6f04ed72 | 274 | PERROR("getgroups"); |
947308c4 DG |
275 | goto end; |
276 | } | |
277 | ||
278 | /* Alloc group list of the right size */ | |
3f451dc0 | 279 | grp_list = zmalloc(grp_list_size * sizeof(gid_t)); |
00795392 | 280 | if (!grp_list) { |
6f04ed72 | 281 | PERROR("malloc"); |
00795392 MD |
282 | goto end; |
283 | } | |
947308c4 | 284 | grp_id = getgroups(grp_list_size, grp_list); |
1c8d13c8 | 285 | if (grp_id < 0) { |
6f04ed72 | 286 | PERROR("getgroups"); |
947308c4 DG |
287 | goto free_list; |
288 | } | |
289 | ||
290 | for (i = 0; i < grp_list_size; i++) { | |
28ab59d0 | 291 | if (grp_list[i] == tracing_gid) { |
2269e89e | 292 | ret = 1; |
947308c4 DG |
293 | break; |
294 | } | |
295 | } | |
296 | ||
297 | free_list: | |
298 | free(grp_list); | |
299 | ||
300 | end: | |
301 | return ret; | |
302 | } | |
303 | ||
09b72f7a FD |
304 | static int check_enough_available_memory(size_t num_bytes_requested_per_cpu) |
305 | { | |
306 | int ret; | |
307 | long num_cpu; | |
308 | size_t best_mem_info; | |
309 | size_t num_bytes_requested_total; | |
310 | ||
311 | /* | |
312 | * Get the number of CPU currently online to compute the amount of | |
313 | * memory needed to create a buffer for every CPU. | |
314 | */ | |
315 | num_cpu = sysconf(_SC_NPROCESSORS_ONLN); | |
316 | if (num_cpu == -1) { | |
317 | goto error; | |
318 | } | |
319 | ||
320 | num_bytes_requested_total = num_bytes_requested_per_cpu * num_cpu; | |
321 | ||
322 | /* | |
323 | * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most | |
324 | * reliable estimate we can get but it is only exposed by the kernel | |
325 | * since 3.14. (See Linux kernel commit: | |
326 | * 34e431b0ae398fc54ea69ff85ec700722c9da773) | |
327 | */ | |
328 | ret = utils_get_memory_available(&best_mem_info); | |
329 | if (ret >= 0) { | |
330 | goto success; | |
331 | } | |
332 | ||
333 | /* | |
334 | * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This | |
335 | * is a sanity check for obvious user error. | |
336 | */ | |
337 | ret = utils_get_memory_total(&best_mem_info); | |
338 | if (ret >= 0) { | |
339 | goto success; | |
340 | } | |
341 | ||
342 | error: | |
343 | return -1; | |
344 | success: | |
345 | return best_mem_info >= num_bytes_requested_total; | |
346 | } | |
347 | ||
947308c4 | 348 | /* |
2269e89e DG |
349 | * Try connect to session daemon with sock_path. |
350 | * | |
351 | * Return 0 on success, else -1 | |
352 | */ | |
353 | static int try_connect_sessiond(const char *sock_path) | |
354 | { | |
355 | int ret; | |
356 | ||
357 | /* If socket exist, we check if the daemon listens for connect. */ | |
358 | ret = access(sock_path, F_OK); | |
359 | if (ret < 0) { | |
360 | /* Not alive */ | |
2f70b271 | 361 | goto error; |
2269e89e DG |
362 | } |
363 | ||
364 | ret = lttcomm_connect_unix_sock(sock_path); | |
365 | if (ret < 0) { | |
9ae110e2 | 366 | /* Not alive. */ |
2f70b271 | 367 | goto error; |
2269e89e DG |
368 | } |
369 | ||
370 | ret = lttcomm_close_unix_sock(ret); | |
371 | if (ret < 0) { | |
6f04ed72 | 372 | PERROR("lttcomm_close_unix_sock"); |
2269e89e DG |
373 | } |
374 | ||
375 | return 0; | |
2f70b271 DG |
376 | |
377 | error: | |
378 | return -1; | |
2269e89e DG |
379 | } |
380 | ||
381 | /* | |
2f70b271 DG |
382 | * Set sessiond socket path by putting it in the global sessiond_sock_path |
383 | * variable. | |
384 | * | |
385 | * Returns 0 on success, negative value on failure (the sessiond socket path | |
386 | * is somehow too long or ENOMEM). | |
947308c4 DG |
387 | */ |
388 | static int set_session_daemon_path(void) | |
389 | { | |
9ae110e2 | 390 | int in_tgroup = 0; /* In tracing group. */ |
2269e89e DG |
391 | uid_t uid; |
392 | ||
393 | uid = getuid(); | |
947308c4 | 394 | |
2269e89e DG |
395 | if (uid != 0) { |
396 | /* Are we in the tracing group ? */ | |
6c71277b | 397 | in_tgroup = lttng_check_tracing_group(); |
2269e89e DG |
398 | } |
399 | ||
08a9c49f | 400 | if ((uid == 0) || in_tgroup) { |
cac3069d DG |
401 | lttng_ctl_copy_string(sessiond_sock_path, |
402 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path)); | |
08a9c49f | 403 | } |
2269e89e | 404 | |
08a9c49f | 405 | if (uid != 0) { |
c617c0c6 MD |
406 | int ret; |
407 | ||
08a9c49f | 408 | if (in_tgroup) { |
9ae110e2 | 409 | /* Tracing group. */ |
08a9c49f TD |
410 | ret = try_connect_sessiond(sessiond_sock_path); |
411 | if (ret >= 0) { | |
412 | goto end; | |
2269e89e | 413 | } |
08a9c49f | 414 | /* Global session daemon not available... */ |
2269e89e | 415 | } |
08a9c49f TD |
416 | /* ...or not in tracing group (and not root), default */ |
417 | ||
418 | /* | |
9ae110e2 JG |
419 | * With GNU C < 2.1, snprintf returns -1 if the target buffer |
420 | * is too small; | |
421 | * With GNU C >= 2.1, snprintf returns the required size | |
422 | * (excluding closing null) | |
08a9c49f TD |
423 | */ |
424 | ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), | |
feb0f3e5 | 425 | DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir()); |
08a9c49f | 426 | if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) { |
2f70b271 | 427 | goto error; |
947308c4 | 428 | } |
947308c4 | 429 | } |
08a9c49f | 430 | end: |
947308c4 | 431 | return 0; |
2f70b271 DG |
432 | |
433 | error: | |
434 | return -1; | |
947308c4 DG |
435 | } |
436 | ||
65beb5ff | 437 | /* |
9ae110e2 | 438 | * Connect to the LTTng session daemon. |
65beb5ff | 439 | * |
3e3665b8 | 440 | * On success, return the socket's file descriptor. On error, return -1. |
65beb5ff | 441 | */ |
3e3665b8 | 442 | LTTNG_HIDDEN int connect_sessiond(void) |
65beb5ff DG |
443 | { |
444 | int ret; | |
445 | ||
446 | ret = set_session_daemon_path(); | |
447 | if (ret < 0) { | |
2f70b271 | 448 | goto error; |
65beb5ff DG |
449 | } |
450 | ||
9ae110e2 | 451 | /* Connect to the sesssion daemon. */ |
65beb5ff DG |
452 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); |
453 | if (ret < 0) { | |
2f70b271 | 454 | goto error; |
65beb5ff DG |
455 | } |
456 | ||
3e3665b8 | 457 | return ret; |
2f70b271 DG |
458 | |
459 | error: | |
460 | return -1; | |
65beb5ff DG |
461 | } |
462 | ||
3e3665b8 JG |
463 | static void reset_global_sessiond_connection_state(void) |
464 | { | |
465 | sessiond_socket = -1; | |
466 | connected = 0; | |
467 | } | |
468 | ||
65beb5ff | 469 | /* |
1c8d13c8 | 470 | * Clean disconnect from the session daemon. |
9ae110e2 | 471 | * |
1c8d13c8 | 472 | * On success, return 0. On error, return -1. |
65beb5ff DG |
473 | */ |
474 | static int disconnect_sessiond(void) | |
475 | { | |
476 | int ret = 0; | |
477 | ||
478 | if (connected) { | |
479 | ret = lttcomm_close_unix_sock(sessiond_socket); | |
3e3665b8 | 480 | reset_global_sessiond_connection_state(); |
65beb5ff DG |
481 | } |
482 | ||
483 | return ret; | |
484 | } | |
485 | ||
795a978d PP |
486 | static int recv_sessiond_optional_data(size_t len, void **user_buf, |
487 | size_t *user_len) | |
488 | { | |
489 | int ret = 0; | |
490 | void *buf = NULL; | |
491 | ||
492 | if (len) { | |
493 | if (!user_len) { | |
494 | ret = -LTTNG_ERR_INVALID; | |
495 | goto end; | |
496 | } | |
497 | ||
498 | buf = zmalloc(len); | |
499 | if (!buf) { | |
500 | ret = -ENOMEM; | |
501 | goto end; | |
502 | } | |
503 | ||
504 | ret = recv_data_sessiond(buf, len); | |
505 | if (ret < 0) { | |
506 | goto end; | |
507 | } | |
508 | ||
509 | if (!user_buf) { | |
510 | ret = -LTTNG_ERR_INVALID; | |
511 | goto end; | |
512 | } | |
513 | ||
514 | /* Move ownership of command header buffer to user. */ | |
515 | *user_buf = buf; | |
516 | buf = NULL; | |
517 | *user_len = len; | |
518 | } else { | |
519 | /* No command header. */ | |
520 | if (user_len) { | |
521 | *user_len = 0; | |
522 | } | |
523 | ||
524 | if (user_buf) { | |
525 | *user_buf = NULL; | |
526 | } | |
527 | } | |
528 | ||
529 | end: | |
530 | free(buf); | |
531 | return ret; | |
532 | } | |
533 | ||
35a6fdb7 | 534 | /* |
cd80958d | 535 | * Ask the session daemon a specific command and put the data into buf. |
a04d53fc FD |
536 | * Takes extra var. len. data and file descriptors as input to send to the |
537 | * session daemon. | |
65beb5ff | 538 | * |
af87c45a | 539 | * Return size of data (only payload, not header) or a negative error code. |
65beb5ff | 540 | */ |
cac3069d | 541 | LTTNG_HIDDEN |
a04d53fc FD |
542 | int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm, |
543 | const int *fds, size_t nb_fd, const void *vardata, | |
544 | size_t vardata_len, void **user_payload_buf, | |
545 | void **user_cmd_header_buf, size_t *user_cmd_header_len) | |
65beb5ff DG |
546 | { |
547 | int ret; | |
795a978d | 548 | size_t payload_len; |
cd80958d | 549 | struct lttcomm_lttng_msg llm; |
65beb5ff DG |
550 | |
551 | ret = connect_sessiond(); | |
552 | if (ret < 0) { | |
2f70b271 | 553 | ret = -LTTNG_ERR_NO_SESSIOND; |
65beb5ff | 554 | goto end; |
3e3665b8 JG |
555 | } else { |
556 | sessiond_socket = ret; | |
557 | connected = 1; | |
65beb5ff DG |
558 | } |
559 | ||
cd80958d | 560 | ret = send_session_msg(lsm); |
65beb5ff | 561 | if (ret < 0) { |
2f70b271 | 562 | /* Ret value is a valid lttng error code. */ |
65beb5ff DG |
563 | goto end; |
564 | } | |
53a80697 | 565 | /* Send var len data */ |
795a978d | 566 | ret = send_session_varlen(vardata, vardata_len); |
53a80697 | 567 | if (ret < 0) { |
2f70b271 | 568 | /* Ret value is a valid lttng error code. */ |
53a80697 MD |
569 | goto end; |
570 | } | |
65beb5ff | 571 | |
a04d53fc FD |
572 | /* Send fds */ |
573 | ret = send_session_fds(fds, nb_fd); | |
574 | if (ret < 0) { | |
575 | /* Ret value is a valid lttng error code. */ | |
576 | goto end; | |
577 | } | |
578 | ||
65beb5ff DG |
579 | /* Get header from data transmission */ |
580 | ret = recv_data_sessiond(&llm, sizeof(llm)); | |
581 | if (ret < 0) { | |
2f70b271 | 582 | /* Ret value is a valid lttng error code. */ |
65beb5ff DG |
583 | goto end; |
584 | } | |
585 | ||
586 | /* Check error code if OK */ | |
f73fabfd | 587 | if (llm.ret_code != LTTNG_OK) { |
65beb5ff DG |
588 | ret = -llm.ret_code; |
589 | goto end; | |
590 | } | |
591 | ||
795a978d PP |
592 | /* Get command header from data transmission */ |
593 | ret = recv_sessiond_optional_data(llm.cmd_header_size, | |
594 | user_cmd_header_buf, user_cmd_header_len); | |
65beb5ff | 595 | if (ret < 0) { |
65beb5ff DG |
596 | goto end; |
597 | } | |
598 | ||
795a978d PP |
599 | /* Get payload from data transmission */ |
600 | ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf, | |
601 | &payload_len); | |
602 | if (ret < 0) { | |
83009e5e DG |
603 | goto end; |
604 | } | |
605 | ||
795a978d | 606 | ret = llm.data_size; |
65beb5ff DG |
607 | |
608 | end: | |
609 | disconnect_sessiond(); | |
610 | return ret; | |
611 | } | |
612 | ||
e368fb43 JG |
613 | LTTNG_HIDDEN |
614 | int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message, | |
615 | struct lttng_payload *reply) | |
616 | { | |
617 | int ret; | |
618 | struct lttcomm_lttng_msg llm; | |
fe489250 | 619 | const int fd_count = lttng_payload_view_get_fd_handle_count(message); |
e368fb43 JG |
620 | |
621 | assert(reply->buffer.size == 0); | |
fe489250 | 622 | assert(lttng_dynamic_pointer_array_get_count(&reply->_fd_handles) == 0); |
e368fb43 JG |
623 | |
624 | ret = connect_sessiond(); | |
625 | if (ret < 0) { | |
626 | ret = -LTTNG_ERR_NO_SESSIOND; | |
627 | goto end; | |
628 | } else { | |
629 | sessiond_socket = ret; | |
630 | connected = 1; | |
631 | } | |
632 | ||
633 | /* Send command to session daemon */ | |
634 | ret = lttcomm_send_creds_unix_sock(sessiond_socket, message->buffer.data, | |
635 | message->buffer.size); | |
636 | if (ret < 0) { | |
637 | ret = -LTTNG_ERR_FATAL; | |
638 | goto end; | |
639 | } | |
640 | ||
fe489250 JG |
641 | if (fd_count > 0) { |
642 | ret = lttcomm_send_payload_view_fds_unix_sock(sessiond_socket, | |
643 | message); | |
644 | if (ret < 0) { | |
645 | ret = -LTTNG_ERR_FATAL; | |
646 | goto end; | |
647 | } | |
e368fb43 JG |
648 | } |
649 | ||
650 | /* Get header from data transmission */ | |
651 | ret = recv_payload_sessiond(reply, sizeof(llm)); | |
652 | if (ret < 0) { | |
653 | /* Ret value is a valid lttng error code. */ | |
654 | goto end; | |
655 | } | |
656 | ||
657 | llm = *((typeof(llm) *) reply->buffer.data); | |
658 | ||
659 | /* Check error code if OK */ | |
660 | if (llm.ret_code != LTTNG_OK) { | |
661 | ret = -llm.ret_code; | |
662 | goto end; | |
663 | } | |
664 | ||
665 | if (llm.cmd_header_size > 0) { | |
666 | ret = recv_payload_sessiond(reply, llm.cmd_header_size); | |
667 | if (ret < 0) { | |
668 | goto end; | |
669 | } | |
670 | } | |
671 | ||
672 | /* Get command header from data transmission */ | |
673 | if (llm.data_size > 0) { | |
674 | ret = recv_payload_sessiond(reply, llm.data_size); | |
675 | if (ret < 0) { | |
676 | goto end; | |
677 | } | |
678 | } | |
679 | ||
680 | if (llm.fd_count > 0) { | |
fe489250 JG |
681 | ret = lttcomm_recv_payload_fds_unix_sock( |
682 | sessiond_socket, llm.fd_count, reply); | |
683 | if (ret < 0) { | |
e368fb43 JG |
684 | goto end; |
685 | } | |
686 | } | |
687 | ||
688 | /* Don't return the llm header to the caller. */ | |
689 | memmove(reply->buffer.data, reply->buffer.data + sizeof(llm), | |
690 | reply->buffer.size - sizeof(llm)); | |
691 | ret = lttng_dynamic_buffer_set_size( | |
692 | &reply->buffer, reply->buffer.size - sizeof(llm)); | |
693 | if (ret) { | |
694 | /* Can't happen as size is reduced. */ | |
695 | abort(); | |
696 | } | |
697 | ||
698 | ret = reply->buffer.size; | |
699 | ||
700 | end: | |
701 | disconnect_sessiond(); | |
702 | return ret; | |
703 | } | |
704 | ||
9f19cc17 | 705 | /* |
cd80958d | 706 | * Create lttng handle and return pointer. |
9ae110e2 | 707 | * |
1c8d13c8 | 708 | * The returned pointer will be NULL in case of malloc() error. |
9f19cc17 | 709 | */ |
cd80958d DG |
710 | struct lttng_handle *lttng_create_handle(const char *session_name, |
711 | struct lttng_domain *domain) | |
9f19cc17 | 712 | { |
2f70b271 DG |
713 | struct lttng_handle *handle = NULL; |
714 | ||
3f451dc0 | 715 | handle = zmalloc(sizeof(struct lttng_handle)); |
cd80958d | 716 | if (handle == NULL) { |
2f70b271 | 717 | PERROR("malloc handle"); |
cd80958d DG |
718 | goto end; |
719 | } | |
720 | ||
721 | /* Copy session name */ | |
cac3069d | 722 | lttng_ctl_copy_string(handle->session_name, session_name, |
cd80958d DG |
723 | sizeof(handle->session_name)); |
724 | ||
95681498 JG |
725 | /* Copy lttng domain or leave initialized to 0. */ |
726 | if (domain) { | |
727 | lttng_ctl_copy_lttng_domain(&handle->domain, domain); | |
728 | } | |
cd80958d DG |
729 | |
730 | end: | |
731 | return handle; | |
732 | } | |
733 | ||
734 | /* | |
735 | * Destroy handle by free(3) the pointer. | |
736 | */ | |
737 | void lttng_destroy_handle(struct lttng_handle *handle) | |
738 | { | |
0e428499 | 739 | free(handle); |
eb354453 DG |
740 | } |
741 | ||
d9800920 DG |
742 | /* |
743 | * Register an outside consumer. | |
9ae110e2 | 744 | * |
1c8d13c8 | 745 | * Returns size of returned session payload data or a negative error code. |
d9800920 DG |
746 | */ |
747 | int lttng_register_consumer(struct lttng_handle *handle, | |
748 | const char *socket_path) | |
749 | { | |
750 | struct lttcomm_session_msg lsm; | |
751 | ||
2f70b271 DG |
752 | if (handle == NULL || socket_path == NULL) { |
753 | return -LTTNG_ERR_INVALID; | |
754 | } | |
755 | ||
53efb85a | 756 | memset(&lsm, 0, sizeof(lsm)); |
d9800920 | 757 | lsm.cmd_type = LTTNG_REGISTER_CONSUMER; |
cac3069d | 758 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
d9800920 | 759 | sizeof(lsm.session.name)); |
60160d2a | 760 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
d9800920 | 761 | |
9ae110e2 JG |
762 | lttng_ctl_copy_string(lsm.u.reg.path, socket_path, |
763 | sizeof(lsm.u.reg.path)); | |
d9800920 | 764 | |
cac3069d | 765 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
d9800920 DG |
766 | } |
767 | ||
1df4dedd | 768 | /* |
9ae110e2 JG |
769 | * Start tracing for all traces of the session. |
770 | * | |
771 | * Returns size of returned session payload data or a negative error code. | |
1df4dedd | 772 | */ |
6a4f824d | 773 | int lttng_start_tracing(const char *session_name) |
f3ed775e | 774 | { |
cd80958d DG |
775 | struct lttcomm_session_msg lsm; |
776 | ||
6a4f824d | 777 | if (session_name == NULL) { |
2f70b271 | 778 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
779 | } |
780 | ||
53efb85a | 781 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 782 | lsm.cmd_type = LTTNG_START_TRACE; |
6a4f824d | 783 | |
cac3069d DG |
784 | lttng_ctl_copy_string(lsm.session.name, session_name, |
785 | sizeof(lsm.session.name)); | |
cd80958d | 786 | |
cac3069d | 787 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
f3ed775e | 788 | } |
1df4dedd DG |
789 | |
790 | /* | |
38ee087f | 791 | * Stop tracing for all traces of the session. |
f3ed775e | 792 | */ |
38ee087f | 793 | static int _lttng_stop_tracing(const char *session_name, int wait) |
f3ed775e | 794 | { |
38ee087f | 795 | int ret, data_ret; |
cd80958d DG |
796 | struct lttcomm_session_msg lsm; |
797 | ||
6a4f824d | 798 | if (session_name == NULL) { |
2f70b271 | 799 | return -LTTNG_ERR_INVALID; |
6a4f824d DG |
800 | } |
801 | ||
53efb85a | 802 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 803 | lsm.cmd_type = LTTNG_STOP_TRACE; |
6a4f824d | 804 | |
cac3069d DG |
805 | lttng_ctl_copy_string(lsm.session.name, session_name, |
806 | sizeof(lsm.session.name)); | |
cd80958d | 807 | |
cac3069d | 808 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
38ee087f DG |
809 | if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { |
810 | goto error; | |
811 | } | |
812 | ||
813 | if (!wait) { | |
814 | goto end; | |
815 | } | |
816 | ||
38ee087f DG |
817 | /* Check for data availability */ |
818 | do { | |
6d805429 | 819 | data_ret = lttng_data_pending(session_name); |
38ee087f DG |
820 | if (data_ret < 0) { |
821 | /* Return the data available call error. */ | |
822 | ret = data_ret; | |
823 | goto error; | |
824 | } | |
825 | ||
826 | /* | |
9ae110e2 JG |
827 | * Data sleep time before retrying (in usec). Don't sleep if the |
828 | * call returned value indicates availability. | |
38ee087f | 829 | */ |
6d805429 | 830 | if (data_ret) { |
c8f61fd4 | 831 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US); |
38ee087f | 832 | } |
6d805429 | 833 | } while (data_ret != 0); |
38ee087f | 834 | |
38ee087f DG |
835 | end: |
836 | error: | |
837 | return ret; | |
838 | } | |
839 | ||
840 | /* | |
841 | * Stop tracing and wait for data availability. | |
842 | */ | |
843 | int lttng_stop_tracing(const char *session_name) | |
844 | { | |
845 | return _lttng_stop_tracing(session_name, 1); | |
846 | } | |
847 | ||
848 | /* | |
849 | * Stop tracing but _don't_ wait for data availability. | |
850 | */ | |
851 | int lttng_stop_tracing_no_wait(const char *session_name) | |
852 | { | |
853 | return _lttng_stop_tracing(session_name, 0); | |
f3ed775e DG |
854 | } |
855 | ||
856 | /* | |
601d5acf DG |
857 | * Add context to a channel. |
858 | * | |
859 | * If the given channel is NULL, add the contexts to all channels. | |
860 | * The event_name param is ignored. | |
af87c45a DG |
861 | * |
862 | * Returns the size of the returned payload data or a negative error code. | |
1df4dedd | 863 | */ |
cd80958d | 864 | int lttng_add_context(struct lttng_handle *handle, |
38057ed1 DG |
865 | struct lttng_event_context *ctx, const char *event_name, |
866 | const char *channel_name) | |
d65106b1 | 867 | { |
2001793c JG |
868 | int ret; |
869 | size_t len = 0; | |
870 | char *buf = NULL; | |
cd80958d DG |
871 | struct lttcomm_session_msg lsm; |
872 | ||
9ae110e2 | 873 | /* Safety check. Both are mandatory. */ |
9d697d3d | 874 | if (handle == NULL || ctx == NULL) { |
2001793c JG |
875 | ret = -LTTNG_ERR_INVALID; |
876 | goto end; | |
cd80958d DG |
877 | } |
878 | ||
441c16a7 | 879 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d DG |
880 | lsm.cmd_type = LTTNG_ADD_CONTEXT; |
881 | ||
85076754 MD |
882 | /* If no channel name, send empty string. */ |
883 | if (channel_name == NULL) { | |
884 | lttng_ctl_copy_string(lsm.u.context.channel_name, "", | |
885 | sizeof(lsm.u.context.channel_name)); | |
886 | } else { | |
887 | lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name, | |
888 | sizeof(lsm.u.context.channel_name)); | |
889 | } | |
cd80958d | 890 | |
60160d2a | 891 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
cac3069d | 892 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d DG |
893 | sizeof(lsm.session.name)); |
894 | ||
2001793c JG |
895 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
896 | size_t provider_len, ctx_len; | |
897 | const char *provider_name = ctx->u.app_ctx.provider_name; | |
898 | const char *ctx_name = ctx->u.app_ctx.ctx_name; | |
899 | ||
900 | if (!provider_name || !ctx_name) { | |
901 | ret = -LTTNG_ERR_INVALID; | |
902 | goto end; | |
903 | } | |
904 | ||
905 | provider_len = strlen(provider_name); | |
906 | if (provider_len == 0) { | |
907 | ret = -LTTNG_ERR_INVALID; | |
908 | goto end; | |
909 | } | |
910 | lsm.u.context.provider_name_len = provider_len; | |
911 | ||
912 | ctx_len = strlen(ctx_name); | |
913 | if (ctx_len == 0) { | |
914 | ret = -LTTNG_ERR_INVALID; | |
915 | goto end; | |
916 | } | |
917 | lsm.u.context.context_name_len = ctx_len; | |
918 | ||
919 | len = provider_len + ctx_len; | |
920 | buf = zmalloc(len); | |
921 | if (!buf) { | |
922 | ret = -LTTNG_ERR_NOMEM; | |
923 | goto end; | |
924 | } | |
925 | ||
926 | memcpy(buf, provider_name, provider_len); | |
927 | memcpy(buf + provider_len, ctx_name, ctx_len); | |
928 | } | |
929 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); | |
46f44e2a JR |
930 | |
931 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { | |
932 | /* | |
933 | * Don't leak application addresses to the sessiond. | |
934 | * This is only necessary when ctx is for an app ctx otherwise | |
935 | * the values inside the union (type & config) are overwritten. | |
936 | */ | |
937 | lsm.u.context.ctx.u.app_ctx.provider_name = NULL; | |
938 | lsm.u.context.ctx.u.app_ctx.ctx_name = NULL; | |
939 | } | |
2001793c | 940 | |
795a978d | 941 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL); |
2001793c JG |
942 | end: |
943 | free(buf); | |
944 | return ret; | |
d65106b1 DG |
945 | } |
946 | ||
f3ed775e | 947 | /* |
9ae110e2 JG |
948 | * Enable event(s) for a channel. |
949 | * | |
950 | * If no event name is specified, all events are enabled. | |
951 | * If no channel name is specified, the default 'channel0' is used. | |
952 | * | |
953 | * Returns size of returned session payload data or a negative error code. | |
f3ed775e | 954 | */ |
cd80958d | 955 | int lttng_enable_event(struct lttng_handle *handle, |
38057ed1 | 956 | struct lttng_event *ev, const char *channel_name) |
1df4dedd | 957 | { |
f8a96544 JI |
958 | return lttng_enable_event_with_exclusions(handle, ev, channel_name, |
959 | NULL, 0, NULL); | |
1df4dedd DG |
960 | } |
961 | ||
53a80697 | 962 | /* |
025faf73 | 963 | * Create or enable an event with a filter expression. |
178191b3 | 964 | * |
53a80697 MD |
965 | * Return negative error value on error. |
966 | * Return size of returned session payload data if OK. | |
967 | */ | |
025faf73 | 968 | int lttng_enable_event_with_filter(struct lttng_handle *handle, |
178191b3 | 969 | struct lttng_event *event, const char *channel_name, |
53a80697 MD |
970 | const char *filter_expression) |
971 | { | |
f8a96544 JI |
972 | return lttng_enable_event_with_exclusions(handle, event, channel_name, |
973 | filter_expression, 0, NULL); | |
53a80697 MD |
974 | } |
975 | ||
347c5ab5 | 976 | /* |
0e115563 | 977 | * Depending on the event, return a newly allocated agent filter expression or |
347c5ab5 DG |
978 | * NULL if not applicable. |
979 | * | |
980 | * An event with NO loglevel and the name is * will return NULL. | |
981 | */ | |
0e115563 | 982 | static char *set_agent_filter(const char *filter, struct lttng_event *ev) |
347c5ab5 DG |
983 | { |
984 | int err; | |
0e115563 | 985 | char *agent_filter = NULL; |
347c5ab5 DG |
986 | |
987 | assert(ev); | |
988 | ||
989 | /* Don't add filter for the '*' event. */ | |
9f449915 | 990 | if (strcmp(ev->name, "*") != 0) { |
347c5ab5 | 991 | if (filter) { |
0e115563 | 992 | err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter, |
347c5ab5 DG |
993 | ev->name); |
994 | } else { | |
0e115563 | 995 | err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name); |
347c5ab5 DG |
996 | } |
997 | if (err < 0) { | |
998 | PERROR("asprintf"); | |
6a556f7b | 999 | goto error; |
347c5ab5 DG |
1000 | } |
1001 | } | |
1002 | ||
1003 | /* Add loglevel filtering if any for the JUL domain. */ | |
1004 | if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) { | |
b53d4e59 | 1005 | const char *op; |
347c5ab5 DG |
1006 | |
1007 | if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) { | |
1008 | op = ">="; | |
1009 | } else { | |
1010 | op = "=="; | |
1011 | } | |
1012 | ||
0e115563 | 1013 | if (filter || agent_filter) { |
6a556f7b JG |
1014 | char *new_filter; |
1015 | ||
fb0edb23 | 1016 | err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)", |
0e115563 | 1017 | agent_filter ? agent_filter : filter, op, |
347c5ab5 | 1018 | ev->loglevel); |
0e115563 DG |
1019 | if (agent_filter) { |
1020 | free(agent_filter); | |
6a556f7b | 1021 | } |
0e115563 | 1022 | agent_filter = new_filter; |
347c5ab5 | 1023 | } else { |
0e115563 | 1024 | err = asprintf(&agent_filter, "int_loglevel %s %d", op, |
347c5ab5 DG |
1025 | ev->loglevel); |
1026 | } | |
1027 | if (err < 0) { | |
1028 | PERROR("asprintf"); | |
6a556f7b | 1029 | goto error; |
347c5ab5 DG |
1030 | } |
1031 | } | |
1032 | ||
0e115563 | 1033 | return agent_filter; |
6a556f7b | 1034 | error: |
0e115563 | 1035 | free(agent_filter); |
6a556f7b | 1036 | return NULL; |
347c5ab5 DG |
1037 | } |
1038 | ||
93deb080 JI |
1039 | /* |
1040 | * Enable event(s) for a channel, possibly with exclusions and a filter. | |
1041 | * If no event name is specified, all events are enabled. | |
1042 | * If no channel name is specified, the default name is used. | |
1043 | * If filter expression is not NULL, the filter is set for the event. | |
1044 | * If exclusion count is not zero, the exclusions are set for the event. | |
1045 | * Returns size of returned session payload data or a negative error code. | |
1046 | */ | |
1047 | int lttng_enable_event_with_exclusions(struct lttng_handle *handle, | |
1048 | struct lttng_event *ev, const char *channel_name, | |
e9efbcd3 | 1049 | const char *original_filter_expression, |
93deb080 JI |
1050 | int exclusion_count, char **exclusion_list) |
1051 | { | |
1052 | struct lttcomm_session_msg lsm; | |
e368fb43 JG |
1053 | struct lttng_payload payload; |
1054 | int ret = 0, i; | |
137b9942 | 1055 | unsigned int free_filter_expression = 0; |
93deb080 | 1056 | struct filter_parser_ctx *ctx = NULL; |
ec005ec6 | 1057 | |
6a0838b7 YL |
1058 | /* |
1059 | * We have either a filter or some exclusions, so we need to set up | |
e368fb43 | 1060 | * a variable-length payload from where to send the data. |
6a0838b7 | 1061 | */ |
e368fb43 | 1062 | lttng_payload_init(&payload); |
ec005ec6 | 1063 | |
e9efbcd3 JG |
1064 | /* |
1065 | * Cast as non-const since we may replace the filter expression | |
1066 | * by a dynamically allocated string. Otherwise, the original | |
1067 | * string is not modified. | |
1068 | */ | |
1069 | char *filter_expression = (char *) original_filter_expression; | |
93deb080 JI |
1070 | |
1071 | if (handle == NULL || ev == NULL) { | |
137b9942 DG |
1072 | ret = -LTTNG_ERR_INVALID; |
1073 | goto error; | |
93deb080 JI |
1074 | } |
1075 | ||
9ae110e2 JG |
1076 | /* |
1077 | * Empty filter string will always be rejected by the parser | |
93deb080 JI |
1078 | * anyway, so treat this corner-case early to eliminate |
1079 | * lttng_fmemopen error for 0-byte allocation. | |
1080 | */ | |
1081 | if (filter_expression && filter_expression[0] == '\0') { | |
137b9942 DG |
1082 | ret = -LTTNG_ERR_INVALID; |
1083 | goto error; | |
93deb080 JI |
1084 | } |
1085 | ||
1086 | memset(&lsm, 0, sizeof(lsm)); | |
1087 | ||
1088 | /* If no channel name, send empty string. */ | |
1089 | if (channel_name == NULL) { | |
1090 | lttng_ctl_copy_string(lsm.u.enable.channel_name, "", | |
1091 | sizeof(lsm.u.enable.channel_name)); | |
1092 | } else { | |
1093 | lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name, | |
1094 | sizeof(lsm.u.enable.channel_name)); | |
1095 | } | |
1096 | ||
18a720cd MD |
1097 | lsm.cmd_type = LTTNG_ENABLE_EVENT; |
1098 | if (ev->name[0] == '\0') { | |
1099 | /* Enable all events */ | |
1100 | lttng_ctl_copy_string(ev->name, "*", sizeof(ev->name)); | |
6565421f | 1101 | } |
93deb080 | 1102 | |
60160d2a | 1103 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
93deb080 JI |
1104 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); |
1105 | ||
1106 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, | |
1107 | sizeof(lsm.session.name)); | |
1108 | lsm.u.enable.exclusion_count = exclusion_count; | |
1109 | lsm.u.enable.bytecode_len = 0; | |
1110 | ||
9ae110e2 | 1111 | /* Parse filter expression. */ |
5cdb6027 | 1112 | if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL |
0e115563 DG |
1113 | || handle->domain.type == LTTNG_DOMAIN_LOG4J |
1114 | || handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
5cdb6027 | 1115 | if (handle->domain.type == LTTNG_DOMAIN_JUL || |
0e115563 DG |
1116 | handle->domain.type == LTTNG_DOMAIN_LOG4J || |
1117 | handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
1118 | char *agent_filter; | |
64226865 | 1119 | |
347c5ab5 | 1120 | /* Setup JUL filter if needed. */ |
0e115563 DG |
1121 | agent_filter = set_agent_filter(filter_expression, ev); |
1122 | if (!agent_filter) { | |
137b9942 | 1123 | if (!filter_expression) { |
9ae110e2 JG |
1124 | /* |
1125 | * No JUL and no filter, just skip | |
1126 | * everything below. | |
1127 | */ | |
137b9942 DG |
1128 | goto ask_sessiond; |
1129 | } | |
64226865 DG |
1130 | } else { |
1131 | /* | |
9ae110e2 JG |
1132 | * With an agent filter, the original filter has |
1133 | * been added to it thus replace the filter | |
1134 | * expression. | |
64226865 | 1135 | */ |
0e115563 | 1136 | filter_expression = agent_filter; |
e9efbcd3 | 1137 | free_filter_expression = 1; |
9b21e6d5 | 1138 | } |
9b21e6d5 | 1139 | } |
93deb080 | 1140 | |
e4d2f27a | 1141 | ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx); |
93deb080 | 1142 | if (ret) { |
137b9942 | 1143 | goto filter_error; |
93deb080 | 1144 | } |
e4d2f27a JR |
1145 | |
1146 | lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b) | |
1147 | + bytecode_get_len(&ctx->bytecode->b); | |
1148 | lsm.u.enable.expression_len = strlen(filter_expression) + 1; | |
6b453b5e JG |
1149 | } |
1150 | ||
e368fb43 JG |
1151 | ret = lttng_dynamic_buffer_set_capacity(&payload.buffer, |
1152 | lsm.u.enable.bytecode_len + | |
1153 | lsm.u.enable.expression_len + | |
1154 | LTTNG_SYMBOL_NAME_LEN * | |
1155 | exclusion_count); | |
15e37663 | 1156 | if (ret) { |
6b453b5e | 1157 | ret = -LTTNG_ERR_EXCLUSION_NOMEM; |
7ca1dc6f | 1158 | goto mem_error; |
6b453b5e | 1159 | } |
137b9942 | 1160 | |
9ae110e2 | 1161 | /* Put exclusion names first in the data. */ |
15e37663 JG |
1162 | for (i = 0; i < exclusion_count; i++) { |
1163 | size_t exclusion_len; | |
1164 | ||
1165 | exclusion_len = lttng_strnlen(*(exclusion_list + i), | |
1166 | LTTNG_SYMBOL_NAME_LEN); | |
1167 | if (exclusion_len == LTTNG_SYMBOL_NAME_LEN) { | |
1168 | /* Exclusion is not NULL-terminated. */ | |
1169 | ret = -LTTNG_ERR_INVALID; | |
1170 | goto mem_error; | |
1171 | } | |
1172 | ||
e368fb43 JG |
1173 | ret = lttng_dynamic_buffer_append(&payload.buffer, |
1174 | *(exclusion_list + i), LTTNG_SYMBOL_NAME_LEN); | |
15e37663 JG |
1175 | if (ret) { |
1176 | goto mem_error; | |
1177 | } | |
6b453b5e | 1178 | } |
15e37663 | 1179 | |
9ae110e2 | 1180 | /* Add filter expression next. */ |
15e37663 | 1181 | if (filter_expression) { |
e368fb43 | 1182 | ret = lttng_dynamic_buffer_append(&payload.buffer, |
15e37663 JG |
1183 | filter_expression, lsm.u.enable.expression_len); |
1184 | if (ret) { | |
1185 | goto mem_error; | |
1186 | } | |
6b453b5e | 1187 | } |
9ae110e2 | 1188 | /* Add filter bytecode next. */ |
137b9942 | 1189 | if (ctx && lsm.u.enable.bytecode_len != 0) { |
e368fb43 | 1190 | ret = lttng_dynamic_buffer_append(&payload.buffer, |
15e37663 JG |
1191 | &ctx->bytecode->b, lsm.u.enable.bytecode_len); |
1192 | if (ret) { | |
1193 | goto mem_error; | |
1194 | } | |
1195 | } | |
1196 | if (ev->extended.ptr) { | |
1197 | struct lttng_event_extended *ev_ext = | |
1198 | (struct lttng_event_extended *) ev->extended.ptr; | |
1199 | ||
1200 | if (ev_ext->probe_location) { | |
1201 | /* | |
1202 | * lttng_userspace_probe_location_serialize returns the | |
1203 | * number of bytes that was appended to the buffer. | |
1204 | */ | |
1205 | ret = lttng_userspace_probe_location_serialize( | |
e368fb43 | 1206 | ev_ext->probe_location, &payload); |
56f0bc67 | 1207 | if (ret < 0) { |
15e37663 JG |
1208 | goto mem_error; |
1209 | } | |
1210 | ||
15e37663 JG |
1211 | /* |
1212 | * Set the size of the userspace probe location element | |
1213 | * of the buffer so that the receiving side knows where | |
1214 | * to split it. | |
1215 | */ | |
1216 | lsm.u.enable.userspace_probe_location_len = ret; | |
1217 | } | |
93deb080 JI |
1218 | } |
1219 | ||
e368fb43 JG |
1220 | { |
1221 | struct lttng_payload_view view = lttng_payload_view_from_payload( | |
1222 | &payload, 0, -1); | |
fe489250 | 1223 | int fd_count = lttng_payload_view_get_fd_handle_count(&view); |
e368fb43 JG |
1224 | int fd_to_send; |
1225 | ||
1226 | if (fd_count < 0) { | |
1227 | goto mem_error; | |
1228 | } | |
1229 | ||
1230 | assert(fd_count == 0 || fd_count == 1); | |
1231 | if (fd_count == 1) { | |
fe489250 JG |
1232 | struct fd_handle *handle = |
1233 | lttng_payload_view_pop_fd_handle(&view); | |
1234 | ||
1235 | if (!handle) { | |
e368fb43 JG |
1236 | goto mem_error; |
1237 | } | |
1238 | ||
fe489250 JG |
1239 | fd_to_send = fd_handle_get_fd(handle); |
1240 | fd_handle_put(handle); | |
e368fb43 JG |
1241 | } |
1242 | ||
1243 | ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, | |
1244 | fd_count ? &fd_to_send : NULL, fd_count, | |
1245 | view.buffer.size ? view.buffer.data : NULL, | |
1246 | view.buffer.size, NULL, NULL, 0); | |
1247 | } | |
93deb080 | 1248 | |
7ca1dc6f DG |
1249 | mem_error: |
1250 | if (filter_expression && ctx) { | |
93deb080 JI |
1251 | filter_bytecode_free(ctx); |
1252 | filter_ir_free(ctx); | |
1253 | filter_parser_ctx_free(ctx); | |
7ca1dc6f DG |
1254 | } |
1255 | filter_error: | |
1256 | if (free_filter_expression) { | |
1257 | /* | |
9ae110e2 JG |
1258 | * The filter expression has been replaced and must be freed as |
1259 | * it is not the original filter expression received as a | |
1260 | * parameter. | |
7ca1dc6f DG |
1261 | */ |
1262 | free(filter_expression); | |
93deb080 | 1263 | } |
137b9942 DG |
1264 | error: |
1265 | /* | |
9ae110e2 JG |
1266 | * Return directly to the caller and don't ask the sessiond since |
1267 | * something went wrong in the parsing of data above. | |
137b9942 | 1268 | */ |
e368fb43 | 1269 | lttng_payload_reset(&payload); |
93deb080 | 1270 | return ret; |
3e1c9ff7 DG |
1271 | |
1272 | ask_sessiond: | |
1273 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
1274 | return ret; | |
93deb080 JI |
1275 | } |
1276 | ||
6e911cad MD |
1277 | int lttng_disable_event_ext(struct lttng_handle *handle, |
1278 | struct lttng_event *ev, const char *channel_name, | |
1279 | const char *original_filter_expression) | |
1df4dedd | 1280 | { |
cd80958d | 1281 | struct lttcomm_session_msg lsm; |
6e911cad MD |
1282 | char *varlen_data; |
1283 | int ret = 0; | |
1284 | unsigned int free_filter_expression = 0; | |
1285 | struct filter_parser_ctx *ctx = NULL; | |
1286 | /* | |
1287 | * Cast as non-const since we may replace the filter expression | |
1288 | * by a dynamically allocated string. Otherwise, the original | |
1289 | * string is not modified. | |
1290 | */ | |
1291 | char *filter_expression = (char *) original_filter_expression; | |
1df4dedd | 1292 | |
6e911cad MD |
1293 | if (handle == NULL || ev == NULL) { |
1294 | ret = -LTTNG_ERR_INVALID; | |
1295 | goto error; | |
1296 | } | |
1297 | ||
9ae110e2 JG |
1298 | /* |
1299 | * Empty filter string will always be rejected by the parser | |
6e911cad MD |
1300 | * anyway, so treat this corner-case early to eliminate |
1301 | * lttng_fmemopen error for 0-byte allocation. | |
1302 | */ | |
1303 | if (filter_expression && filter_expression[0] == '\0') { | |
1304 | ret = -LTTNG_ERR_INVALID; | |
1305 | goto error; | |
cd80958d DG |
1306 | } |
1307 | ||
441c16a7 MD |
1308 | memset(&lsm, 0, sizeof(lsm)); |
1309 | ||
85076754 MD |
1310 | /* If no channel name, send empty string. */ |
1311 | if (channel_name == NULL) { | |
1312 | lttng_ctl_copy_string(lsm.u.disable.channel_name, "", | |
cd80958d | 1313 | sizeof(lsm.u.disable.channel_name)); |
f3ed775e | 1314 | } else { |
85076754 | 1315 | lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name, |
cd80958d | 1316 | sizeof(lsm.u.disable.channel_name)); |
eb354453 DG |
1317 | } |
1318 | ||
18a720cd | 1319 | lsm.cmd_type = LTTNG_DISABLE_EVENT; |
f3ed775e | 1320 | |
60160d2a | 1321 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
6e911cad MD |
1322 | memcpy(&lsm.u.disable.event, ev, sizeof(lsm.u.disable.event)); |
1323 | ||
cac3069d | 1324 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d | 1325 | sizeof(lsm.session.name)); |
6e911cad | 1326 | lsm.u.disable.bytecode_len = 0; |
cd80958d | 1327 | |
6e911cad MD |
1328 | /* |
1329 | * For the JUL domain, a filter is enforced except for the | |
1330 | * disable all event. This is done to avoid having the event in | |
1331 | * all sessions thus filtering by logger name. | |
1332 | */ | |
1333 | if (filter_expression == NULL && | |
1334 | (handle->domain.type != LTTNG_DOMAIN_JUL && | |
0e115563 DG |
1335 | handle->domain.type != LTTNG_DOMAIN_LOG4J && |
1336 | handle->domain.type != LTTNG_DOMAIN_PYTHON)) { | |
6e911cad MD |
1337 | goto ask_sessiond; |
1338 | } | |
1339 | ||
1340 | /* | |
1341 | * We have a filter, so we need to set up a variable-length | |
1342 | * memory block from where to send the data. | |
1343 | */ | |
1344 | ||
1345 | /* Parse filter expression */ | |
1346 | if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL | |
0e115563 DG |
1347 | || handle->domain.type == LTTNG_DOMAIN_LOG4J |
1348 | || handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
6e911cad | 1349 | if (handle->domain.type == LTTNG_DOMAIN_JUL || |
0e115563 DG |
1350 | handle->domain.type == LTTNG_DOMAIN_LOG4J || |
1351 | handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
1352 | char *agent_filter; | |
6e911cad MD |
1353 | |
1354 | /* Setup JUL filter if needed. */ | |
0e115563 DG |
1355 | agent_filter = set_agent_filter(filter_expression, ev); |
1356 | if (!agent_filter) { | |
6e911cad | 1357 | if (!filter_expression) { |
9ae110e2 JG |
1358 | /* |
1359 | * No JUL and no filter, just skip | |
1360 | * everything below. | |
1361 | */ | |
6e911cad MD |
1362 | goto ask_sessiond; |
1363 | } | |
1364 | } else { | |
1365 | /* | |
9ae110e2 JG |
1366 | * With a JUL filter, the original filter has |
1367 | * been added to it thus replace the filter | |
1368 | * expression. | |
6e911cad | 1369 | */ |
0e115563 | 1370 | filter_expression = agent_filter; |
6e911cad MD |
1371 | free_filter_expression = 1; |
1372 | } | |
1373 | } | |
1374 | ||
e4d2f27a | 1375 | ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx); |
6e911cad MD |
1376 | if (ret) { |
1377 | goto filter_error; | |
1378 | } | |
e4d2f27a JR |
1379 | |
1380 | lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b) | |
1381 | + bytecode_get_len(&ctx->bytecode->b); | |
1382 | lsm.u.enable.expression_len = strlen(filter_expression) + 1; | |
6e911cad MD |
1383 | } |
1384 | ||
1385 | varlen_data = zmalloc(lsm.u.disable.bytecode_len | |
1386 | + lsm.u.disable.expression_len); | |
1387 | if (!varlen_data) { | |
1388 | ret = -LTTNG_ERR_EXCLUSION_NOMEM; | |
1389 | goto mem_error; | |
1390 | } | |
1391 | ||
9ae110e2 | 1392 | /* Add filter expression. */ |
6e911cad MD |
1393 | if (lsm.u.disable.expression_len != 0) { |
1394 | memcpy(varlen_data, | |
1395 | filter_expression, | |
1396 | lsm.u.disable.expression_len); | |
1397 | } | |
9ae110e2 | 1398 | /* Add filter bytecode next. */ |
6e911cad MD |
1399 | if (ctx && lsm.u.disable.bytecode_len != 0) { |
1400 | memcpy(varlen_data | |
1401 | + lsm.u.disable.expression_len, | |
1402 | &ctx->bytecode->b, | |
1403 | lsm.u.disable.bytecode_len); | |
1404 | } | |
1405 | ||
795a978d | 1406 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, varlen_data, |
6e911cad MD |
1407 | lsm.u.disable.bytecode_len + lsm.u.disable.expression_len, NULL); |
1408 | free(varlen_data); | |
1409 | ||
1410 | mem_error: | |
1411 | if (filter_expression && ctx) { | |
1412 | filter_bytecode_free(ctx); | |
1413 | filter_ir_free(ctx); | |
1414 | filter_parser_ctx_free(ctx); | |
1415 | } | |
1416 | filter_error: | |
1417 | if (free_filter_expression) { | |
1418 | /* | |
9ae110e2 JG |
1419 | * The filter expression has been replaced and must be freed as |
1420 | * it is not the original filter expression received as a | |
1421 | * parameter. | |
6e911cad MD |
1422 | */ |
1423 | free(filter_expression); | |
1424 | } | |
1425 | error: | |
1426 | /* | |
9ae110e2 JG |
1427 | * Return directly to the caller and don't ask the sessiond since |
1428 | * something went wrong in the parsing of data above. | |
6e911cad MD |
1429 | */ |
1430 | return ret; | |
1431 | ||
1432 | ask_sessiond: | |
1433 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
1434 | return ret; | |
1435 | } | |
1436 | ||
1437 | /* | |
9ae110e2 JG |
1438 | * Disable event(s) of a channel and domain. |
1439 | * If no event name is specified, all events are disabled. | |
1440 | * If no channel name is specified, the default 'channel0' is used. | |
1441 | * Returns size of returned session payload data or a negative error code. | |
6e911cad MD |
1442 | */ |
1443 | int lttng_disable_event(struct lttng_handle *handle, const char *name, | |
1444 | const char *channel_name) | |
1445 | { | |
1446 | struct lttng_event ev; | |
1447 | ||
1448 | memset(&ev, 0, sizeof(ev)); | |
9b7431cf | 1449 | ev.loglevel = -1; |
6e911cad MD |
1450 | ev.type = LTTNG_EVENT_ALL; |
1451 | lttng_ctl_copy_string(ev.name, name, sizeof(ev.name)); | |
1452 | return lttng_disable_event_ext(handle, &ev, channel_name, NULL); | |
1df4dedd DG |
1453 | } |
1454 | ||
cf0bcb51 JG |
1455 | struct lttng_channel *lttng_channel_create(struct lttng_domain *domain) |
1456 | { | |
1457 | struct lttng_channel *channel = NULL; | |
1458 | struct lttng_channel_extended *extended = NULL; | |
1459 | ||
1460 | if (!domain) { | |
1461 | goto error; | |
1462 | } | |
1463 | ||
1464 | /* Validate domain. */ | |
1465 | switch (domain->type) { | |
1466 | case LTTNG_DOMAIN_UST: | |
1467 | switch (domain->buf_type) { | |
1468 | case LTTNG_BUFFER_PER_UID: | |
1469 | case LTTNG_BUFFER_PER_PID: | |
1470 | break; | |
1471 | default: | |
1472 | goto error; | |
1473 | } | |
1474 | break; | |
1475 | case LTTNG_DOMAIN_KERNEL: | |
1476 | if (domain->buf_type != LTTNG_BUFFER_GLOBAL) { | |
1477 | goto error; | |
1478 | } | |
1479 | break; | |
1480 | default: | |
1481 | goto error; | |
1482 | } | |
1483 | ||
1484 | channel = zmalloc(sizeof(*channel)); | |
1485 | if (!channel) { | |
1486 | goto error; | |
1487 | } | |
1488 | ||
1489 | extended = zmalloc(sizeof(*extended)); | |
1490 | if (!extended) { | |
1491 | goto error; | |
1492 | } | |
1493 | ||
1494 | channel->attr.extended.ptr = extended; | |
1495 | ||
1496 | lttng_channel_set_default_attr(domain, &channel->attr); | |
1497 | return channel; | |
1498 | error: | |
1499 | free(channel); | |
1500 | free(extended); | |
1501 | return NULL; | |
1502 | } | |
1503 | ||
1504 | void lttng_channel_destroy(struct lttng_channel *channel) | |
1505 | { | |
1506 | if (!channel) { | |
1507 | return; | |
1508 | } | |
1509 | ||
1510 | if (channel->attr.extended.ptr) { | |
1511 | free(channel->attr.extended.ptr); | |
1512 | } | |
1513 | free(channel); | |
1514 | } | |
1515 | ||
1df4dedd | 1516 | /* |
9ae110e2 JG |
1517 | * Enable channel per domain |
1518 | * Returns size of returned session payload data or a negative error code. | |
a5c5a2bd | 1519 | */ |
cd80958d | 1520 | int lttng_enable_channel(struct lttng_handle *handle, |
cf0bcb51 | 1521 | struct lttng_channel *in_chan) |
a5c5a2bd | 1522 | { |
cd80958d | 1523 | struct lttcomm_session_msg lsm; |
09b72f7a | 1524 | size_t total_buffer_size_needed_per_cpu = 0; |
cd80958d | 1525 | |
9ae110e2 | 1526 | /* NULL arguments are forbidden. No default values. */ |
cf0bcb51 | 1527 | if (handle == NULL || in_chan == NULL) { |
2f70b271 | 1528 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1529 | } |
1530 | ||
441c16a7 | 1531 | memset(&lsm, 0, sizeof(lsm)); |
cf0bcb51 JG |
1532 | memcpy(&lsm.u.channel.chan, in_chan, sizeof(lsm.u.channel.chan)); |
1533 | lsm.u.channel.chan.attr.extended.ptr = NULL; | |
441c16a7 | 1534 | |
cf0bcb51 JG |
1535 | if (!in_chan->attr.extended.ptr) { |
1536 | struct lttng_channel *channel; | |
1537 | struct lttng_channel_extended *extended; | |
7d29a247 | 1538 | |
cf0bcb51 JG |
1539 | channel = lttng_channel_create(&handle->domain); |
1540 | if (!channel) { | |
1541 | return -LTTNG_ERR_NOMEM; | |
1542 | } | |
1543 | ||
1544 | /* | |
1545 | * Create a new channel in order to use default extended | |
1546 | * attribute values. | |
1547 | */ | |
1548 | extended = (struct lttng_channel_extended *) | |
1549 | channel->attr.extended.ptr; | |
1550 | memcpy(&lsm.u.channel.extended, extended, sizeof(*extended)); | |
1551 | lttng_channel_destroy(channel); | |
1552 | } else { | |
1553 | struct lttng_channel_extended *extended; | |
1554 | ||
1555 | extended = (struct lttng_channel_extended *) | |
1556 | in_chan->attr.extended.ptr; | |
1557 | memcpy(&lsm.u.channel.extended, extended, sizeof(*extended)); | |
1558 | } | |
cd80958d | 1559 | |
09b72f7a FD |
1560 | /* |
1561 | * Verify that the amount of memory required to create the requested | |
1562 | * buffer is available on the system at the moment. | |
1563 | */ | |
1564 | total_buffer_size_needed_per_cpu = lsm.u.channel.chan.attr.num_subbuf * | |
1565 | lsm.u.channel.chan.attr.subbuf_size; | |
1566 | if (!check_enough_available_memory(total_buffer_size_needed_per_cpu)) { | |
1567 | return -LTTNG_ERR_NOMEM; | |
1568 | } | |
1569 | ||
cf0bcb51 | 1570 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
60160d2a | 1571 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
7d29a247 | 1572 | |
cac3069d | 1573 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d DG |
1574 | sizeof(lsm.session.name)); |
1575 | ||
cac3069d | 1576 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
8c0faa1d | 1577 | } |
1df4dedd | 1578 | |
2ef84c95 | 1579 | /* |
9ae110e2 JG |
1580 | * All tracing will be stopped for registered events of the channel. |
1581 | * Returns size of returned session payload data or a negative error code. | |
2ef84c95 | 1582 | */ |
cd80958d | 1583 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
2ef84c95 | 1584 | { |
cd80958d DG |
1585 | struct lttcomm_session_msg lsm; |
1586 | ||
9ae110e2 | 1587 | /* Safety check. Both are mandatory. */ |
9d697d3d | 1588 | if (handle == NULL || name == NULL) { |
2f70b271 | 1589 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1590 | } |
1591 | ||
441c16a7 MD |
1592 | memset(&lsm, 0, sizeof(lsm)); |
1593 | ||
cd80958d | 1594 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
1df4dedd | 1595 | |
cac3069d | 1596 | lttng_ctl_copy_string(lsm.u.disable.channel_name, name, |
9d697d3d DG |
1597 | sizeof(lsm.u.disable.channel_name)); |
1598 | ||
60160d2a | 1599 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
cd80958d | 1600 | |
cac3069d | 1601 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d DG |
1602 | sizeof(lsm.session.name)); |
1603 | ||
cac3069d | 1604 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
ca95a216 DG |
1605 | } |
1606 | ||
fac6795d | 1607 | /* |
9ae110e2 JG |
1608 | * Lists all available tracepoints of domain. |
1609 | * Sets the contents of the events array. | |
1610 | * Returns the number of lttng_event entries in events; | |
1611 | * on error, returns a negative value. | |
fac6795d | 1612 | */ |
cd80958d | 1613 | int lttng_list_tracepoints(struct lttng_handle *handle, |
2a71efd5 | 1614 | struct lttng_event **events) |
fac6795d | 1615 | { |
052da939 | 1616 | int ret; |
cd80958d DG |
1617 | struct lttcomm_session_msg lsm; |
1618 | ||
9d697d3d | 1619 | if (handle == NULL) { |
2f70b271 | 1620 | return -LTTNG_ERR_INVALID; |
cd80958d | 1621 | } |
fac6795d | 1622 | |
53efb85a | 1623 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 1624 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
60160d2a | 1625 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
2a71efd5 | 1626 | |
cac3069d | 1627 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); |
052da939 DG |
1628 | if (ret < 0) { |
1629 | return ret; | |
eb354453 | 1630 | } |
fac6795d | 1631 | |
9f19cc17 | 1632 | return ret / sizeof(struct lttng_event); |
fac6795d DG |
1633 | } |
1634 | ||
f37d259d | 1635 | /* |
9ae110e2 JG |
1636 | * Lists all available tracepoint fields of domain. |
1637 | * Sets the contents of the event field array. | |
1638 | * Returns the number of lttng_event_field entries in events; | |
1639 | * on error, returns a negative value. | |
f37d259d MD |
1640 | */ |
1641 | int lttng_list_tracepoint_fields(struct lttng_handle *handle, | |
1642 | struct lttng_event_field **fields) | |
1643 | { | |
1644 | int ret; | |
1645 | struct lttcomm_session_msg lsm; | |
1646 | ||
1647 | if (handle == NULL) { | |
2f70b271 | 1648 | return -LTTNG_ERR_INVALID; |
f37d259d MD |
1649 | } |
1650 | ||
53efb85a | 1651 | memset(&lsm, 0, sizeof(lsm)); |
f37d259d | 1652 | lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS; |
60160d2a | 1653 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
f37d259d | 1654 | |
cac3069d | 1655 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields); |
f37d259d MD |
1656 | if (ret < 0) { |
1657 | return ret; | |
1658 | } | |
1659 | ||
1660 | return ret / sizeof(struct lttng_event_field); | |
1661 | } | |
1662 | ||
834978fd | 1663 | /* |
9ae110e2 JG |
1664 | * Lists all available kernel system calls. Allocates and sets the contents of |
1665 | * the events array. | |
834978fd | 1666 | * |
9ae110e2 JG |
1667 | * Returns the number of lttng_event entries in events; on error, returns a |
1668 | * negative value. | |
834978fd DG |
1669 | */ |
1670 | int lttng_list_syscalls(struct lttng_event **events) | |
1671 | { | |
1672 | int ret; | |
1673 | struct lttcomm_session_msg lsm; | |
1674 | ||
1675 | if (!events) { | |
1676 | return -LTTNG_ERR_INVALID; | |
1677 | } | |
1678 | ||
1679 | memset(&lsm, 0, sizeof(lsm)); | |
1680 | lsm.cmd_type = LTTNG_LIST_SYSCALLS; | |
1681 | /* Force kernel domain for system calls. */ | |
1682 | lsm.domain.type = LTTNG_DOMAIN_KERNEL; | |
1683 | ||
1684 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); | |
1685 | if (ret < 0) { | |
1686 | return ret; | |
1687 | } | |
1688 | ||
1689 | return ret / sizeof(struct lttng_event); | |
1690 | } | |
1691 | ||
1657e9bb | 1692 | /* |
9ae110e2 JG |
1693 | * Returns a human readable string describing |
1694 | * the error code (a negative value). | |
1657e9bb | 1695 | */ |
9a745bc7 | 1696 | const char *lttng_strerror(int code) |
1657e9bb | 1697 | { |
f73fabfd | 1698 | return error_get_str(code); |
1657e9bb DG |
1699 | } |
1700 | ||
b178f53e JG |
1701 | enum lttng_error_code lttng_create_session_ext( |
1702 | struct lttng_session_descriptor *session_descriptor) | |
1703 | { | |
1704 | enum lttng_error_code ret_code; | |
1705 | struct lttcomm_session_msg lsm = { | |
1706 | .cmd_type = LTTNG_CREATE_SESSION_EXT, | |
1707 | }; | |
1708 | void *reply = NULL; | |
1709 | struct lttng_buffer_view reply_view; | |
1710 | int reply_ret; | |
1711 | bool sessiond_must_generate_ouput; | |
1712 | struct lttng_dynamic_buffer payload; | |
1713 | int ret; | |
1714 | size_t descriptor_size; | |
1715 | struct lttng_session_descriptor *descriptor_reply = NULL; | |
1716 | ||
1717 | lttng_dynamic_buffer_init(&payload); | |
1718 | if (!session_descriptor) { | |
1719 | ret_code = LTTNG_ERR_INVALID; | |
1720 | goto end; | |
1721 | } | |
1722 | ||
1723 | sessiond_must_generate_ouput = | |
1724 | !lttng_session_descriptor_is_output_destination_initialized( | |
1725 | session_descriptor); | |
1726 | if (sessiond_must_generate_ouput) { | |
1727 | const char *home_dir = utils_get_home_dir(); | |
1728 | size_t home_dir_len = home_dir ? strlen(home_dir) + 1 : 0; | |
1729 | ||
1730 | if (!home_dir || home_dir_len > LTTNG_PATH_MAX) { | |
1731 | ret_code = LTTNG_ERR_FATAL; | |
1732 | goto end; | |
1733 | } | |
1734 | ||
1735 | lsm.u.create_session.home_dir_size = (uint16_t) home_dir_len; | |
1736 | ret = lttng_dynamic_buffer_append(&payload, home_dir, | |
1737 | home_dir_len); | |
1738 | if (ret) { | |
1739 | ret_code = LTTNG_ERR_NOMEM; | |
1740 | goto end; | |
1741 | } | |
1742 | } | |
1743 | ||
1744 | descriptor_size = payload.size; | |
1745 | ret = lttng_session_descriptor_serialize(session_descriptor, | |
1746 | &payload); | |
1747 | if (ret) { | |
1748 | ret_code = LTTNG_ERR_INVALID; | |
1749 | goto end; | |
1750 | } | |
1751 | descriptor_size = payload.size - descriptor_size; | |
1752 | lsm.u.create_session.session_descriptor_size = descriptor_size; | |
1753 | ||
1754 | /* Command returns a session descriptor on success. */ | |
1755 | reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, payload.data, | |
1756 | payload.size, &reply); | |
1757 | if (reply_ret < 0) { | |
1758 | ret_code = -reply_ret; | |
1759 | goto end; | |
1760 | } else if (reply_ret == 0) { | |
1761 | /* Socket unexpectedly closed by the session daemon. */ | |
1762 | ret_code = LTTNG_ERR_FATAL; | |
1763 | goto end; | |
1764 | } | |
1765 | ||
1766 | reply_view = lttng_buffer_view_init(reply, 0, reply_ret); | |
1767 | ret = lttng_session_descriptor_create_from_buffer(&reply_view, | |
1768 | &descriptor_reply); | |
1769 | if (ret < 0) { | |
1770 | ret_code = LTTNG_ERR_FATAL; | |
1771 | goto end; | |
1772 | } | |
1773 | ret_code = LTTNG_OK; | |
1774 | lttng_session_descriptor_assign(session_descriptor, descriptor_reply); | |
1775 | end: | |
1776 | free(reply); | |
1777 | lttng_dynamic_buffer_reset(&payload); | |
1778 | lttng_session_descriptor_destroy(descriptor_reply); | |
1779 | return ret_code; | |
1780 | } | |
1781 | ||
aaf97519 | 1782 | /* |
b178f53e | 1783 | * Create a new session using name and url for destination. |
a4b92340 | 1784 | * |
780d4bb8 | 1785 | * Return 0 on success else a negative LTTng error code. |
00e2e675 | 1786 | */ |
a4b92340 | 1787 | int lttng_create_session(const char *name, const char *url) |
00e2e675 | 1788 | { |
3dd05a85 | 1789 | int ret; |
a4b92340 | 1790 | ssize_t size; |
a4b92340 | 1791 | struct lttng_uri *uris = NULL; |
b178f53e JG |
1792 | struct lttng_session_descriptor *descriptor = NULL; |
1793 | enum lttng_error_code ret_code; | |
00e2e675 | 1794 | |
b178f53e JG |
1795 | if (!name) { |
1796 | ret = -LTTNG_ERR_INVALID; | |
1797 | goto end; | |
00e2e675 DG |
1798 | } |
1799 | ||
bc894455 | 1800 | size = uri_parse_str_urls(url, NULL, &uris); |
a4b92340 | 1801 | if (size < 0) { |
b178f53e JG |
1802 | ret = -LTTNG_ERR_INVALID; |
1803 | goto end; | |
1804 | } | |
1805 | switch (size) { | |
1806 | case 0: | |
1807 | descriptor = lttng_session_descriptor_create(name); | |
1808 | break; | |
1809 | case 1: | |
1810 | if (uris[0].dtype != LTTNG_DST_PATH) { | |
1811 | ret = -LTTNG_ERR_INVALID; | |
1812 | goto end; | |
1813 | } | |
1814 | descriptor = lttng_session_descriptor_local_create(name, | |
1815 | uris[0].dst.path); | |
1816 | break; | |
1817 | case 2: | |
1818 | descriptor = lttng_session_descriptor_network_create(name, url, | |
1819 | NULL); | |
1820 | break; | |
1821 | default: | |
1822 | ret = -LTTNG_ERR_INVALID; | |
1823 | goto end; | |
1824 | } | |
1825 | if (!descriptor) { | |
1826 | ret = -LTTNG_ERR_INVALID; | |
1827 | goto end; | |
00e2e675 | 1828 | } |
b178f53e JG |
1829 | ret_code = lttng_create_session_ext(descriptor); |
1830 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; | |
1831 | end: | |
1832 | lttng_session_descriptor_destroy(descriptor); | |
1833 | free(uris); | |
1834 | return ret; | |
1835 | } | |
00e2e675 | 1836 | |
b178f53e JG |
1837 | /* |
1838 | * Create a session exclusively used for snapshot. | |
1839 | * | |
780d4bb8 | 1840 | * Return 0 on success else a negative LTTng error code. |
b178f53e JG |
1841 | */ |
1842 | int lttng_create_session_snapshot(const char *name, const char *snapshot_url) | |
1843 | { | |
1844 | int ret; | |
1845 | enum lttng_error_code ret_code; | |
1846 | ssize_t size; | |
1847 | struct lttng_uri *uris = NULL; | |
1848 | struct lttng_session_descriptor *descriptor = NULL; | |
a4b92340 | 1849 | |
b178f53e JG |
1850 | if (!name) { |
1851 | ret = -LTTNG_ERR_INVALID; | |
1852 | goto end; | |
1853 | } | |
1854 | ||
1855 | size = uri_parse_str_urls(snapshot_url, NULL, &uris); | |
1856 | if (size < 0) { | |
1857 | ret = -LTTNG_ERR_INVALID; | |
1858 | goto end; | |
1859 | } | |
1860 | /* | |
1861 | * If the user does not specify a custom subdir, use the session name. | |
1862 | */ | |
1863 | if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && | |
1864 | strlen(uris[0].subdir) == 0) { | |
1865 | ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", | |
1866 | name); | |
1867 | if (ret < 0) { | |
1868 | PERROR("Failed to set session name as network destination sub-directory"); | |
1869 | ret = -LTTNG_ERR_FATAL; | |
1870 | goto end; | |
1871 | } else if (ret >= sizeof(uris[0].subdir)) { | |
1872 | /* Truncated output. */ | |
1873 | ret = -LTTNG_ERR_INVALID; | |
1874 | goto end; | |
1875 | } | |
1876 | } | |
3dd05a85 | 1877 | |
b178f53e JG |
1878 | switch (size) { |
1879 | case 0: | |
1880 | descriptor = lttng_session_descriptor_snapshot_create(name); | |
1881 | break; | |
1882 | case 1: | |
1883 | if (uris[0].dtype != LTTNG_DST_PATH) { | |
1884 | ret = -LTTNG_ERR_INVALID; | |
1885 | goto end; | |
1886 | } | |
1887 | descriptor = lttng_session_descriptor_snapshot_local_create( | |
1888 | name, | |
1889 | uris[0].dst.path); | |
1890 | break; | |
1891 | case 2: | |
1892 | descriptor = lttng_session_descriptor_snapshot_network_create( | |
1893 | name, | |
1894 | snapshot_url, | |
1895 | NULL); | |
1896 | break; | |
1897 | default: | |
1898 | ret = -LTTNG_ERR_INVALID; | |
1899 | goto end; | |
1900 | } | |
1901 | if (!descriptor) { | |
1902 | ret = -LTTNG_ERR_INVALID; | |
1903 | goto end; | |
1904 | } | |
1905 | ret_code = lttng_create_session_ext(descriptor); | |
1906 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; | |
1907 | end: | |
1908 | lttng_session_descriptor_destroy(descriptor); | |
3dd05a85 DG |
1909 | free(uris); |
1910 | return ret; | |
00e2e675 DG |
1911 | } |
1912 | ||
b178f53e JG |
1913 | /* |
1914 | * Create a session exclusively used for live. | |
1915 | * | |
780d4bb8 | 1916 | * Return 0 on success else a negative LTTng error code. |
b178f53e JG |
1917 | */ |
1918 | int lttng_create_session_live(const char *name, const char *url, | |
1919 | unsigned int timer_interval) | |
1920 | { | |
1921 | int ret; | |
1922 | enum lttng_error_code ret_code; | |
1923 | struct lttng_session_descriptor *descriptor = NULL; | |
1924 | ||
1925 | if (!name) { | |
1926 | ret = -LTTNG_ERR_INVALID; | |
1927 | goto end; | |
1928 | } | |
1929 | ||
1930 | if (url) { | |
1931 | descriptor = lttng_session_descriptor_live_network_create( | |
1932 | name, url, NULL, timer_interval); | |
1933 | } else { | |
1934 | descriptor = lttng_session_descriptor_live_create( | |
1935 | name, timer_interval); | |
1936 | } | |
1937 | if (!descriptor) { | |
1938 | ret = -LTTNG_ERR_INVALID; | |
1939 | goto end; | |
1940 | } | |
1941 | ret_code = lttng_create_session_ext(descriptor); | |
1942 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; | |
1943 | end: | |
1944 | lttng_session_descriptor_destroy(descriptor); | |
1945 | return ret; | |
1946 | } | |
1947 | ||
e20ee7c2 JD |
1948 | /* |
1949 | * Stop the session and wait for the data before destroying it | |
780d4bb8 MD |
1950 | * |
1951 | * Return 0 on success else a negative LTTng error code. | |
e20ee7c2 JD |
1952 | */ |
1953 | int lttng_destroy_session(const char *session_name) | |
1954 | { | |
1955 | int ret; | |
3e3665b8 JG |
1956 | enum lttng_error_code ret_code; |
1957 | enum lttng_destruction_handle_status status; | |
1958 | struct lttng_destruction_handle *handle = NULL; | |
e20ee7c2 JD |
1959 | |
1960 | /* | |
3e3665b8 JG |
1961 | * Stop the tracing and wait for the data to be |
1962 | * consumed. | |
e20ee7c2 JD |
1963 | */ |
1964 | ret = _lttng_stop_tracing(session_name, 1); | |
1965 | if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { | |
1966 | goto end; | |
1967 | } | |
1968 | ||
3e3665b8 JG |
1969 | ret_code = lttng_destroy_session_ext(session_name, &handle); |
1970 | if (ret_code != LTTNG_OK) { | |
1971 | ret = (int) -ret_code; | |
1972 | goto end; | |
1973 | } | |
1974 | assert(handle); | |
1975 | ||
1976 | /* Block until the completion of the destruction of the session. */ | |
1977 | status = lttng_destruction_handle_wait_for_completion(handle, -1); | |
1978 | if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED) { | |
1979 | ret = -LTTNG_ERR_UNK; | |
1980 | goto end; | |
1981 | } | |
1982 | ||
1983 | status = lttng_destruction_handle_get_result(handle, &ret_code); | |
1984 | if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) { | |
1985 | ret = -LTTNG_ERR_UNK; | |
1986 | goto end; | |
1987 | } | |
780d4bb8 | 1988 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; |
e20ee7c2 | 1989 | end: |
3e3665b8 | 1990 | lttng_destruction_handle_destroy(handle); |
e20ee7c2 JD |
1991 | return ret; |
1992 | } | |
1993 | ||
1994 | /* | |
1995 | * Destroy the session without waiting for the data. | |
1996 | */ | |
1997 | int lttng_destroy_session_no_wait(const char *session_name) | |
1998 | { | |
3e3665b8 | 1999 | enum lttng_error_code ret_code; |
e20ee7c2 | 2000 | |
3e3665b8 JG |
2001 | ret_code = lttng_destroy_session_ext(session_name, NULL); |
2002 | return ret_code == LTTNG_OK ? ret_code : -ret_code; | |
e20ee7c2 JD |
2003 | } |
2004 | ||
57167058 | 2005 | /* |
9ae110e2 JG |
2006 | * Ask the session daemon for all available sessions. |
2007 | * Sets the contents of the sessions array. | |
2008 | * Returns the number of lttng_session entries in sessions; | |
2009 | * on error, returns a negative value. | |
57167058 | 2010 | */ |
b178f53e | 2011 | int lttng_list_sessions(struct lttng_session **out_sessions) |
57167058 | 2012 | { |
ca95a216 | 2013 | int ret; |
cd80958d | 2014 | struct lttcomm_session_msg lsm; |
b178f53e JG |
2015 | const size_t session_size = sizeof(struct lttng_session) + |
2016 | sizeof(struct lttng_session_extended); | |
2017 | size_t session_count, i; | |
2018 | struct lttng_session_extended *sessions_extended_begin; | |
2019 | struct lttng_session *sessions = NULL; | |
57167058 | 2020 | |
53efb85a | 2021 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 2022 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
b178f53e JG |
2023 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions); |
2024 | if (ret <= 0) { | |
b178f53e JG |
2025 | goto end; |
2026 | } | |
2027 | if (!sessions) { | |
2028 | ret = -LTTNG_ERR_FATAL; | |
2029 | goto end; | |
2030 | } | |
2031 | ||
2032 | if (ret % session_size) { | |
2033 | ret = -LTTNG_ERR_UNK; | |
2034 | free(sessions); | |
2035 | *out_sessions = NULL; | |
2036 | goto end; | |
57167058 | 2037 | } |
b178f53e JG |
2038 | session_count = (size_t) ret / session_size; |
2039 | sessions_extended_begin = (struct lttng_session_extended *) | |
2040 | (&sessions[session_count]); | |
57167058 | 2041 | |
b178f53e JG |
2042 | /* Set extended session info pointers. */ |
2043 | for (i = 0; i < session_count; i++) { | |
2044 | struct lttng_session *session = &sessions[i]; | |
2045 | struct lttng_session_extended *extended = | |
2046 | &(sessions_extended_begin[i]); | |
2047 | ||
2048 | session->extended.ptr = extended; | |
2049 | } | |
2050 | ||
2051 | ret = (int) session_count; | |
2052 | *out_sessions = sessions; | |
2053 | end: | |
2054 | return ret; | |
2055 | } | |
2056 | ||
2057 | enum lttng_error_code lttng_session_get_creation_time( | |
2058 | const struct lttng_session *session, uint64_t *creation_time) | |
2059 | { | |
2060 | enum lttng_error_code ret = LTTNG_OK; | |
2061 | struct lttng_session_extended *extended; | |
2062 | ||
2063 | if (!session || !creation_time || !session->extended.ptr) { | |
2064 | ret = LTTNG_ERR_INVALID; | |
2065 | goto end; | |
2066 | } | |
2067 | ||
2068 | extended = session->extended.ptr; | |
2069 | if (!extended->creation_time.is_set) { | |
2070 | /* Not created on the session daemon yet. */ | |
2071 | ret = LTTNG_ERR_SESSION_NOT_EXIST; | |
2072 | goto end; | |
2073 | } | |
2074 | *creation_time = extended->creation_time.value; | |
2075 | end: | |
2076 | return ret; | |
57167058 DG |
2077 | } |
2078 | ||
d7ba1388 MD |
2079 | int lttng_set_session_shm_path(const char *session_name, |
2080 | const char *shm_path) | |
2081 | { | |
2082 | struct lttcomm_session_msg lsm; | |
2083 | ||
2084 | if (session_name == NULL) { | |
2085 | return -LTTNG_ERR_INVALID; | |
2086 | } | |
2087 | ||
2088 | memset(&lsm, 0, sizeof(lsm)); | |
2089 | lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH; | |
2090 | ||
2091 | lttng_ctl_copy_string(lsm.session.name, session_name, | |
2092 | sizeof(lsm.session.name)); | |
2093 | lttng_ctl_copy_string(lsm.u.set_shm_path.shm_path, shm_path, | |
2094 | sizeof(lsm.u.set_shm_path.shm_path)); | |
2095 | ||
2096 | return lttng_ctl_ask_sessiond(&lsm, NULL); | |
2097 | } | |
2098 | ||
9f19cc17 | 2099 | /* |
9ae110e2 JG |
2100 | * Ask the session daemon for all available domains of a session. |
2101 | * Sets the contents of the domains array. | |
2102 | * Returns the number of lttng_domain entries in domains; | |
2103 | * on error, returns a negative value. | |
9f19cc17 | 2104 | */ |
330be774 | 2105 | int lttng_list_domains(const char *session_name, |
cd80958d | 2106 | struct lttng_domain **domains) |
9f19cc17 DG |
2107 | { |
2108 | int ret; | |
cd80958d DG |
2109 | struct lttcomm_session_msg lsm; |
2110 | ||
330be774 | 2111 | if (session_name == NULL) { |
2f70b271 | 2112 | return -LTTNG_ERR_INVALID; |
cd80958d | 2113 | } |
9f19cc17 | 2114 | |
53efb85a | 2115 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d DG |
2116 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
2117 | ||
cac3069d DG |
2118 | lttng_ctl_copy_string(lsm.session.name, session_name, |
2119 | sizeof(lsm.session.name)); | |
cd80958d | 2120 | |
cac3069d | 2121 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains); |
9f19cc17 DG |
2122 | if (ret < 0) { |
2123 | return ret; | |
2124 | } | |
2125 | ||
2126 | return ret / sizeof(struct lttng_domain); | |
2127 | } | |
2128 | ||
2129 | /* | |
9ae110e2 JG |
2130 | * Ask the session daemon for all available channels of a session. |
2131 | * Sets the contents of the channels array. | |
2132 | * Returns the number of lttng_channel entries in channels; | |
2133 | * on error, returns a negative value. | |
9f19cc17 | 2134 | */ |
cd80958d DG |
2135 | int lttng_list_channels(struct lttng_handle *handle, |
2136 | struct lttng_channel **channels) | |
9f19cc17 DG |
2137 | { |
2138 | int ret; | |
53e367f9 JG |
2139 | size_t channel_count, i; |
2140 | const size_t channel_size = sizeof(struct lttng_channel) + | |
cf0bcb51 | 2141 | sizeof(struct lttng_channel_extended); |
cd80958d | 2142 | struct lttcomm_session_msg lsm; |
53e367f9 | 2143 | void *extended_at; |
cd80958d | 2144 | |
9d697d3d | 2145 | if (handle == NULL) { |
53e367f9 JG |
2146 | ret = -LTTNG_ERR_INVALID; |
2147 | goto end; | |
cd80958d DG |
2148 | } |
2149 | ||
53efb85a | 2150 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 2151 | lsm.cmd_type = LTTNG_LIST_CHANNELS; |
cac3069d | 2152 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d | 2153 | sizeof(lsm.session.name)); |
9f19cc17 | 2154 | |
60160d2a | 2155 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
9f19cc17 | 2156 | |
cac3069d | 2157 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels); |
9f19cc17 | 2158 | if (ret < 0) { |
53e367f9 JG |
2159 | goto end; |
2160 | } | |
2161 | ||
2162 | if (ret % channel_size) { | |
2163 | ret = -LTTNG_ERR_UNK; | |
2164 | free(*channels); | |
2165 | *channels = NULL; | |
2166 | goto end; | |
9f19cc17 | 2167 | } |
53e367f9 | 2168 | channel_count = (size_t) ret / channel_size; |
9f19cc17 | 2169 | |
53e367f9 JG |
2170 | /* Set extended info pointers */ |
2171 | extended_at = ((void *) *channels) + | |
2172 | channel_count * sizeof(struct lttng_channel); | |
2173 | for (i = 0; i < channel_count; i++) { | |
2174 | struct lttng_channel *chan = &(*channels)[i]; | |
2175 | ||
2176 | chan->attr.extended.ptr = extended_at; | |
cf0bcb51 | 2177 | extended_at += sizeof(struct lttng_channel_extended); |
53e367f9 JG |
2178 | } |
2179 | ||
2180 | ret = (int) channel_count; | |
2181 | end: | |
2182 | return ret; | |
9f19cc17 DG |
2183 | } |
2184 | ||
2185 | /* | |
9ae110e2 JG |
2186 | * Ask the session daemon for all available events of a session channel. |
2187 | * Sets the contents of the events array. | |
2188 | * Returns the number of lttng_event entries in events; | |
2189 | * on error, returns a negative value. | |
9f19cc17 | 2190 | */ |
cd80958d DG |
2191 | int lttng_list_events(struct lttng_handle *handle, |
2192 | const char *channel_name, struct lttng_event **events) | |
9f19cc17 DG |
2193 | { |
2194 | int ret; | |
e368fb43 JG |
2195 | struct lttcomm_session_msg lsm = {}; |
2196 | const struct lttcomm_event_command_header *cmd_header = NULL; | |
b4e3ceb9 | 2197 | uint32_t nb_events, i; |
e368fb43 | 2198 | const void *comm_ext_at; |
56f0bc67 | 2199 | struct lttng_dynamic_buffer listing; |
de453daa | 2200 | size_t storage_req; |
e368fb43 JG |
2201 | struct lttng_payload payload; |
2202 | struct lttng_payload payload_copy; | |
2203 | struct lttng_payload_view lsm_view = | |
2204 | lttng_payload_view_init_from_buffer( | |
2205 | (const char *) &lsm, 0, sizeof(lsm)); | |
2206 | struct lttng_buffer_view cmd_header_view; | |
2207 | struct lttng_buffer_view cmd_payload_view; | |
2208 | struct lttng_buffer_view flat_events_view; | |
2209 | struct lttng_buffer_view ext_view; | |
9f19cc17 | 2210 | |
9d697d3d DG |
2211 | /* Safety check. An handle and channel name are mandatory */ |
2212 | if (handle == NULL || channel_name == NULL) { | |
e368fb43 JG |
2213 | ret = -LTTNG_ERR_INVALID; |
2214 | goto end; | |
cd80958d DG |
2215 | } |
2216 | ||
e368fb43 JG |
2217 | lttng_payload_init(&payload); |
2218 | lttng_payload_init(&payload_copy); | |
2219 | ||
cd80958d | 2220 | lsm.cmd_type = LTTNG_LIST_EVENTS; |
cac3069d | 2221 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d | 2222 | sizeof(lsm.session.name)); |
cac3069d | 2223 | lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name, |
cd80958d | 2224 | sizeof(lsm.u.list.channel_name)); |
60160d2a | 2225 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
9f19cc17 | 2226 | |
e368fb43 | 2227 | ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &payload); |
9f19cc17 | 2228 | if (ret < 0) { |
de453daa | 2229 | goto end; |
9f19cc17 DG |
2230 | } |
2231 | ||
e368fb43 JG |
2232 | /* |
2233 | * A copy of the payload is performed since it will be | |
2234 | * consumed twice. Consuming the same payload twice is invalid since | |
2235 | * it will cause any received file descriptor to become "shared" | |
2236 | * between different instances of the resulting objects. | |
2237 | */ | |
2238 | ret = lttng_payload_copy(&payload, &payload_copy); | |
2239 | if (ret) { | |
2240 | ret = -LTTNG_ERR_NOMEM; | |
2241 | goto end; | |
2242 | } | |
2243 | ||
2244 | cmd_header_view = lttng_buffer_view_from_dynamic_buffer( | |
2245 | &payload.buffer, 0, sizeof(*cmd_header)); | |
3e6e0df2 | 2246 | if (!lttng_buffer_view_is_valid(&cmd_header_view)) { |
e368fb43 | 2247 | ret = -LTTNG_ERR_INVALID_PROTOCOL; |
d9f484bc JG |
2248 | goto end; |
2249 | } | |
2250 | ||
e368fb43 JG |
2251 | cmd_header = (typeof(cmd_header)) cmd_header_view.data; |
2252 | ||
b4e3ceb9 PP |
2253 | /* Set number of events and free command header */ |
2254 | nb_events = cmd_header->nb_events; | |
2255 | if (nb_events > INT_MAX) { | |
55c9e7ca | 2256 | ret = -LTTNG_ERR_OVERFLOW; |
de453daa | 2257 | goto end; |
b4e3ceb9 | 2258 | } |
e368fb43 JG |
2259 | |
2260 | cmd_payload_view = lttng_buffer_view_from_dynamic_buffer( | |
2261 | &payload.buffer, sizeof(*cmd_header), -1); | |
b4e3ceb9 | 2262 | |
de453daa JG |
2263 | /* |
2264 | * The buffer that is returned must contain a "flat" version of | |
2265 | * the events that are returned. In other words, all pointers | |
2266 | * within an lttng_event must point to a location within the returned | |
56f0bc67 JG |
2267 | * buffer so that the user may free everything by simply calling free() |
2268 | * on the returned buffer. This is needed in order to maintain API | |
de453daa JG |
2269 | * compatibility. |
2270 | * | |
56f0bc67 JG |
2271 | * A first pass is performed to compute the size of the buffer that |
2272 | * must be allocated. A second pass is then performed to setup | |
de453daa JG |
2273 | * the returned events so that their members always point within the |
2274 | * buffer. | |
2275 | * | |
2276 | * The layout of the returned buffer is as follows: | |
2277 | * - struct lttng_event[nb_events], | |
2278 | * - nb_events times the following: | |
2279 | * - struct lttng_event_extended, | |
2280 | * - flattened version of userspace_probe_location | |
2281 | * - filter_expression | |
2282 | * - exclusions | |
2283 | * - padding to align to 64-bits | |
2284 | */ | |
e368fb43 JG |
2285 | ext_view = lttng_buffer_view_from_view(&cmd_payload_view, |
2286 | nb_events * sizeof(struct lttng_event), -1); | |
2287 | comm_ext_at = ext_view.data; | |
de453daa | 2288 | storage_req = nb_events * sizeof(struct lttng_event); |
e368fb43 JG |
2289 | { |
2290 | struct lttng_payload_view payload_view = | |
2291 | lttng_payload_view_from_payload(&payload, 0, -1); | |
b4e3ceb9 | 2292 | |
e368fb43 JG |
2293 | for (i = 0; i < nb_events; i++) { |
2294 | const struct lttcomm_event_extended_header *ext_comm = | |
2295 | (struct lttcomm_event_extended_header *) | |
2296 | comm_ext_at; | |
2297 | int probe_storage_req = 0; | |
b4e3ceb9 | 2298 | |
e368fb43 JG |
2299 | comm_ext_at += sizeof(*ext_comm); |
2300 | comm_ext_at += ext_comm->filter_len; | |
2301 | comm_ext_at += ext_comm->nb_exclusions * | |
2302 | LTTNG_SYMBOL_NAME_LEN; | |
2303 | ||
2304 | if (ext_comm->userspace_probe_location_len) { | |
2305 | struct lttng_userspace_probe_location | |
2306 | *probe_location = NULL; | |
2307 | struct lttng_payload_view probe_location_view = lttng_payload_view_from_view( | |
2308 | &payload_view, | |
2309 | (const char *) comm_ext_at - | |
2310 | payload_view.buffer.data, | |
2311 | ext_comm->userspace_probe_location_len); | |
56f0bc67 | 2312 | |
3e6e0df2 JG |
2313 | if (!lttng_payload_view_is_valid(&probe_location_view)) { |
2314 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2315 | goto end; | |
2316 | } | |
2317 | ||
e368fb43 JG |
2318 | /* |
2319 | * Create a temporary userspace probe location | |
2320 | * to determine the size needed by a "flattened" | |
2321 | * version of that same probe location. | |
2322 | */ | |
2323 | ret = lttng_userspace_probe_location_create_from_payload( | |
2324 | &probe_location_view, | |
2325 | &probe_location); | |
2326 | if (ret < 0) { | |
2327 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2328 | goto end; | |
2329 | } | |
56f0bc67 | 2330 | |
e368fb43 JG |
2331 | ret = lttng_userspace_probe_location_flatten( |
2332 | probe_location, NULL); | |
2333 | lttng_userspace_probe_location_destroy( | |
2334 | probe_location); | |
2335 | if (ret < 0) { | |
2336 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2337 | goto end; | |
2338 | } | |
56f0bc67 | 2339 | |
e368fb43 JG |
2340 | probe_storage_req = ret; |
2341 | comm_ext_at += ext_comm->userspace_probe_location_len; | |
56f0bc67 JG |
2342 | } |
2343 | ||
e368fb43 JG |
2344 | storage_req += sizeof(struct lttng_event_extended); |
2345 | storage_req += ext_comm->filter_len; | |
2346 | storage_req += ext_comm->nb_exclusions * | |
2347 | LTTNG_SYMBOL_NAME_LEN; | |
2348 | /* Padding to ensure the flat probe is aligned. */ | |
2349 | storage_req = ALIGN_TO(storage_req, sizeof(uint64_t)); | |
2350 | storage_req += probe_storage_req; | |
56f0bc67 | 2351 | } |
b4e3ceb9 PP |
2352 | } |
2353 | ||
56f0bc67 JG |
2354 | lttng_dynamic_buffer_init(&listing); |
2355 | /* | |
2356 | * We must ensure that "listing" is never resized so as to preserve | |
2357 | * the validity of the flattened objects. | |
2358 | */ | |
2359 | ret = lttng_dynamic_buffer_set_capacity(&listing, storage_req); | |
2360 | if (ret) { | |
2361 | ret = -LTTNG_ERR_NOMEM; | |
de453daa JG |
2362 | goto end; |
2363 | } | |
56f0bc67 | 2364 | |
e368fb43 JG |
2365 | cmd_payload_view = lttng_buffer_view_from_dynamic_buffer( |
2366 | &payload_copy.buffer, sizeof(*cmd_header), -1); | |
2367 | flat_events_view = lttng_buffer_view_from_view(&cmd_payload_view, 0, | |
56f0bc67 | 2368 | nb_events * sizeof(struct lttng_event)); |
e368fb43 | 2369 | ret = lttng_dynamic_buffer_append_view(&listing, &flat_events_view); |
56f0bc67 JG |
2370 | if (ret) { |
2371 | ret = -LTTNG_ERR_NOMEM; | |
2372 | goto free_dynamic_buffer; | |
2373 | } | |
de453daa | 2374 | |
e368fb43 JG |
2375 | ext_view = lttng_buffer_view_from_view(&cmd_payload_view, |
2376 | nb_events * sizeof(struct lttng_event), -1); | |
2377 | comm_ext_at = ext_view.data; | |
2378 | ||
2379 | { | |
2380 | struct lttng_payload_view payload_copy_view = | |
2381 | lttng_payload_view_from_payload( | |
2382 | &payload_copy, 0, -1); | |
2383 | ||
2384 | for (i = 0; i < nb_events; i++) { | |
2385 | struct lttng_event *event = (typeof(event))( | |
2386 | listing.data + | |
2387 | (sizeof(struct lttng_event) * i)); | |
2388 | const struct lttcomm_event_extended_header *ext_comm = | |
2389 | (typeof(ext_comm)) comm_ext_at; | |
2390 | struct lttng_event_extended *event_extended = | |
2391 | (typeof(event_extended))(listing.data + | |
2392 | listing.size); | |
2393 | ||
2394 | /* Insert struct lttng_event_extended. */ | |
2395 | ret = lttng_dynamic_buffer_set_size(&listing, | |
2396 | listing.size + sizeof(*event_extended)); | |
56f0bc67 JG |
2397 | if (ret) { |
2398 | ret = -LTTNG_ERR_NOMEM; | |
2399 | goto free_dynamic_buffer; | |
2400 | } | |
e368fb43 JG |
2401 | event->extended.ptr = event_extended; |
2402 | ||
2403 | comm_ext_at += sizeof(*ext_comm); | |
2404 | ||
2405 | /* Insert filter expression. */ | |
2406 | if (ext_comm->filter_len) { | |
2407 | event_extended->filter_expression = | |
2408 | listing.data + listing.size; | |
2409 | ret = lttng_dynamic_buffer_append(&listing, | |
2410 | comm_ext_at, | |
2411 | ext_comm->filter_len); | |
2412 | if (ret) { | |
2413 | ret = -LTTNG_ERR_NOMEM; | |
2414 | goto free_dynamic_buffer; | |
2415 | } | |
2416 | comm_ext_at += ext_comm->filter_len; | |
2417 | } | |
56f0bc67 | 2418 | |
e368fb43 JG |
2419 | /* Insert exclusions. */ |
2420 | if (ext_comm->nb_exclusions) { | |
2421 | event_extended->exclusions.count = | |
2422 | ext_comm->nb_exclusions; | |
2423 | event_extended->exclusions.strings = | |
2424 | listing.data + listing.size; | |
2425 | ||
2426 | ret = lttng_dynamic_buffer_append(&listing, | |
2427 | comm_ext_at, | |
2428 | ext_comm->nb_exclusions * | |
2429 | LTTNG_SYMBOL_NAME_LEN); | |
2430 | if (ret) { | |
2431 | ret = -LTTNG_ERR_NOMEM; | |
2432 | goto free_dynamic_buffer; | |
2433 | } | |
2434 | comm_ext_at += ext_comm->nb_exclusions * | |
2435 | LTTNG_SYMBOL_NAME_LEN; | |
2436 | } | |
56f0bc67 | 2437 | |
e368fb43 JG |
2438 | /* Insert padding to align to 64-bits. */ |
2439 | ret = lttng_dynamic_buffer_set_size(&listing, | |
2440 | ALIGN_TO(listing.size, | |
2441 | sizeof(uint64_t))); | |
56f0bc67 JG |
2442 | if (ret) { |
2443 | ret = -LTTNG_ERR_NOMEM; | |
5e4f4898 | 2444 | goto free_dynamic_buffer; |
56f0bc67 | 2445 | } |
56f0bc67 | 2446 | |
e368fb43 JG |
2447 | /* Insert flattened userspace probe location. */ |
2448 | if (ext_comm->userspace_probe_location_len) { | |
2449 | struct lttng_userspace_probe_location | |
2450 | *probe_location = NULL; | |
2451 | struct lttng_payload_view probe_location_view = lttng_payload_view_from_view( | |
2452 | &payload_copy_view, | |
2453 | (const char *) comm_ext_at - | |
2454 | payload_copy_view.buffer.data, | |
2455 | ext_comm->userspace_probe_location_len); | |
2456 | ||
3e6e0df2 JG |
2457 | if (!lttng_payload_view_is_valid(&probe_location_view)) { |
2458 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2459 | goto free_dynamic_buffer; | |
2460 | } | |
2461 | ||
e368fb43 JG |
2462 | ret = lttng_userspace_probe_location_create_from_payload( |
2463 | &probe_location_view, | |
2464 | &probe_location); | |
2465 | if (ret < 0) { | |
2466 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2467 | goto free_dynamic_buffer; | |
2468 | } | |
de453daa | 2469 | |
e368fb43 JG |
2470 | event_extended->probe_location = (struct lttng_userspace_probe_location |
2471 | *) (listing.data + | |
2472 | listing.size); | |
2473 | ret = lttng_userspace_probe_location_flatten( | |
2474 | probe_location, &listing); | |
2475 | lttng_userspace_probe_location_destroy( | |
2476 | probe_location); | |
2477 | if (ret < 0) { | |
2478 | ret = -LTTNG_ERR_PROBE_LOCATION_INVAL; | |
2479 | goto free_dynamic_buffer; | |
2480 | } | |
56f0bc67 | 2481 | |
e368fb43 | 2482 | comm_ext_at += ext_comm->userspace_probe_location_len; |
56f0bc67 | 2483 | } |
56f0bc67 | 2484 | } |
de453daa JG |
2485 | } |
2486 | ||
56f0bc67 JG |
2487 | /* Don't reset listing buffer as we return its content. */ |
2488 | *events = (struct lttng_event *) listing.data; | |
2489 | lttng_dynamic_buffer_init(&listing); | |
de453daa | 2490 | ret = (int) nb_events; |
56f0bc67 JG |
2491 | free_dynamic_buffer: |
2492 | lttng_dynamic_buffer_reset(&listing); | |
de453daa | 2493 | end: |
e368fb43 JG |
2494 | lttng_payload_reset(&payload); |
2495 | lttng_payload_reset(&payload_copy); | |
b4e3ceb9 | 2496 | return ret; |
9f19cc17 DG |
2497 | } |
2498 | ||
fac6795d | 2499 | /* |
1c8d13c8 TD |
2500 | * Sets the tracing_group variable with name. |
2501 | * This function allocates memory pointed to by tracing_group. | |
2502 | * On success, returns 0, on error, returns -1 (null name) or -ENOMEM. | |
fac6795d DG |
2503 | */ |
2504 | int lttng_set_tracing_group(const char *name) | |
2505 | { | |
9d697d3d | 2506 | if (name == NULL) { |
2f70b271 | 2507 | return -LTTNG_ERR_INVALID; |
9d697d3d DG |
2508 | } |
2509 | ||
fac6795d | 2510 | if (asprintf(&tracing_group, "%s", name) < 0) { |
2f70b271 | 2511 | return -LTTNG_ERR_FATAL; |
fac6795d DG |
2512 | } |
2513 | ||
2514 | return 0; | |
2515 | } | |
2516 | ||
cd80958d | 2517 | int lttng_calibrate(struct lttng_handle *handle, |
d0254c7c MD |
2518 | struct lttng_calibrate *calibrate) |
2519 | { | |
b812e5ca PP |
2520 | /* |
2521 | * This command was removed in LTTng 2.9. | |
2522 | */ | |
2523 | return -LTTNG_ERR_UND; | |
d0254c7c MD |
2524 | } |
2525 | ||
5edd7e09 DG |
2526 | /* |
2527 | * Set default channel attributes. | |
441c16a7 | 2528 | * If either or both of the arguments are null, attr content is zeroe'd. |
5edd7e09 DG |
2529 | */ |
2530 | void lttng_channel_set_default_attr(struct lttng_domain *domain, | |
2531 | struct lttng_channel_attr *attr) | |
2532 | { | |
294851b0 | 2533 | struct lttng_channel_extended *extended; |
cf0bcb51 | 2534 | |
5edd7e09 DG |
2535 | /* Safety check */ |
2536 | if (attr == NULL || domain == NULL) { | |
2537 | return; | |
2538 | } | |
2539 | ||
294851b0 | 2540 | extended = (struct lttng_channel_extended *) attr->extended.ptr; |
eacaa7a6 DS |
2541 | memset(attr, 0, sizeof(struct lttng_channel_attr)); |
2542 | ||
0a9c6494 DG |
2543 | /* Same for all domains. */ |
2544 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
2545 | attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE; | |
2546 | attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT; | |
2547 | ||
5edd7e09 DG |
2548 | switch (domain->type) { |
2549 | case LTTNG_DOMAIN_KERNEL: | |
9ae110e2 JG |
2550 | attr->switch_timer_interval = |
2551 | DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; | |
d92ff3ef | 2552 | attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; |
3e230f92 | 2553 | attr->subbuf_size = default_get_kernel_channel_subbuf_size(); |
5edd7e09 DG |
2554 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
2555 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
cf0bcb51 JG |
2556 | if (extended) { |
2557 | extended->monitor_timer_interval = | |
2558 | DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER; | |
491d1539 MD |
2559 | extended->blocking_timeout = |
2560 | DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT; | |
cf0bcb51 | 2561 | } |
5edd7e09 DG |
2562 | break; |
2563 | case LTTNG_DOMAIN_UST: | |
0a9c6494 DG |
2564 | switch (domain->buf_type) { |
2565 | case LTTNG_BUFFER_PER_UID: | |
2566 | attr->subbuf_size = default_get_ust_uid_channel_subbuf_size(); | |
2567 | attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM; | |
2568 | attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT; | |
9ae110e2 JG |
2569 | attr->switch_timer_interval = |
2570 | DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER; | |
2571 | attr->read_timer_interval = | |
2572 | DEFAULT_UST_UID_CHANNEL_READ_TIMER; | |
cf0bcb51 JG |
2573 | if (extended) { |
2574 | extended->monitor_timer_interval = | |
2575 | DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER; | |
491d1539 MD |
2576 | extended->blocking_timeout = |
2577 | DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT; | |
cf0bcb51 | 2578 | } |
0a9c6494 DG |
2579 | break; |
2580 | case LTTNG_BUFFER_PER_PID: | |
2581 | default: | |
2582 | attr->subbuf_size = default_get_ust_pid_channel_subbuf_size(); | |
2583 | attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM; | |
2584 | attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT; | |
9ae110e2 JG |
2585 | attr->switch_timer_interval = |
2586 | DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER; | |
2587 | attr->read_timer_interval = | |
2588 | DEFAULT_UST_PID_CHANNEL_READ_TIMER; | |
cf0bcb51 JG |
2589 | if (extended) { |
2590 | extended->monitor_timer_interval = | |
2591 | DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER; | |
491d1539 MD |
2592 | extended->blocking_timeout = |
2593 | DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT; | |
cf0bcb51 | 2594 | } |
0a9c6494 DG |
2595 | break; |
2596 | } | |
5edd7e09 | 2597 | default: |
441c16a7 | 2598 | /* Default behavior: leave set to 0. */ |
5edd7e09 DG |
2599 | break; |
2600 | } | |
cf0bcb51 JG |
2601 | |
2602 | attr->extended.ptr = extended; | |
5edd7e09 DG |
2603 | } |
2604 | ||
5ba3702f JG |
2605 | int lttng_channel_get_discarded_event_count(struct lttng_channel *channel, |
2606 | uint64_t *discarded_events) | |
2607 | { | |
2608 | int ret = 0; | |
cf0bcb51 | 2609 | struct lttng_channel_extended *chan_ext; |
5ba3702f JG |
2610 | |
2611 | if (!channel || !discarded_events) { | |
2612 | ret = -LTTNG_ERR_INVALID; | |
2613 | goto end; | |
2614 | } | |
2615 | ||
2616 | chan_ext = channel->attr.extended.ptr; | |
2617 | if (!chan_ext) { | |
2618 | /* | |
2619 | * This can happen since the lttng_channel structure is | |
2620 | * used for other tasks where this pointer is never set. | |
2621 | */ | |
2622 | *discarded_events = 0; | |
2623 | goto end; | |
2624 | } | |
2625 | ||
2626 | *discarded_events = chan_ext->discarded_events; | |
2627 | end: | |
2628 | return ret; | |
2629 | } | |
2630 | ||
2631 | int lttng_channel_get_lost_packet_count(struct lttng_channel *channel, | |
2632 | uint64_t *lost_packets) | |
2633 | { | |
2634 | int ret = 0; | |
cf0bcb51 | 2635 | struct lttng_channel_extended *chan_ext; |
5ba3702f JG |
2636 | |
2637 | if (!channel || !lost_packets) { | |
2638 | ret = -LTTNG_ERR_INVALID; | |
2639 | goto end; | |
2640 | } | |
2641 | ||
2642 | chan_ext = channel->attr.extended.ptr; | |
2643 | if (!chan_ext) { | |
2644 | /* | |
2645 | * This can happen since the lttng_channel structure is | |
2646 | * used for other tasks where this pointer is never set. | |
2647 | */ | |
2648 | *lost_packets = 0; | |
2649 | goto end; | |
2650 | } | |
2651 | ||
2652 | *lost_packets = chan_ext->lost_packets; | |
2653 | end: | |
2654 | return ret; | |
2655 | } | |
2656 | ||
cf0bcb51 JG |
2657 | int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan, |
2658 | uint64_t *monitor_timer_interval) | |
2659 | { | |
2660 | int ret = 0; | |
2661 | ||
2662 | if (!chan || !monitor_timer_interval) { | |
2663 | ret = -LTTNG_ERR_INVALID; | |
2664 | goto end; | |
2665 | } | |
2666 | ||
2667 | if (!chan->attr.extended.ptr) { | |
2668 | ret = -LTTNG_ERR_INVALID; | |
2669 | goto end; | |
2670 | } | |
2671 | ||
2672 | *monitor_timer_interval = ((struct lttng_channel_extended *) | |
2673 | chan->attr.extended.ptr)->monitor_timer_interval; | |
2674 | end: | |
2675 | return ret; | |
2676 | } | |
2677 | ||
2678 | int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan, | |
2679 | uint64_t monitor_timer_interval) | |
2680 | { | |
2681 | int ret = 0; | |
2682 | ||
2683 | if (!chan || !chan->attr.extended.ptr) { | |
2684 | ret = -LTTNG_ERR_INVALID; | |
2685 | goto end; | |
2686 | } | |
2687 | ||
2688 | ((struct lttng_channel_extended *) | |
2689 | chan->attr.extended.ptr)->monitor_timer_interval = | |
2690 | monitor_timer_interval; | |
2691 | end: | |
2692 | return ret; | |
2693 | } | |
2694 | ||
491d1539 MD |
2695 | int lttng_channel_get_blocking_timeout(struct lttng_channel *chan, |
2696 | int64_t *blocking_timeout) | |
2697 | { | |
2698 | int ret = 0; | |
2699 | ||
2700 | if (!chan || !blocking_timeout) { | |
2701 | ret = -LTTNG_ERR_INVALID; | |
2702 | goto end; | |
2703 | } | |
2704 | ||
2705 | if (!chan->attr.extended.ptr) { | |
2706 | ret = -LTTNG_ERR_INVALID; | |
2707 | goto end; | |
2708 | } | |
2709 | ||
2710 | *blocking_timeout = ((struct lttng_channel_extended *) | |
2711 | chan->attr.extended.ptr)->blocking_timeout; | |
2712 | end: | |
2713 | return ret; | |
2714 | } | |
2715 | ||
2716 | int lttng_channel_set_blocking_timeout(struct lttng_channel *chan, | |
2717 | int64_t blocking_timeout) | |
2718 | { | |
2719 | int ret = 0; | |
2720 | int64_t msec_timeout; | |
2721 | ||
2722 | if (!chan || !chan->attr.extended.ptr) { | |
2723 | ret = -LTTNG_ERR_INVALID; | |
2724 | goto end; | |
2725 | } | |
2726 | ||
2727 | if (blocking_timeout < 0 && blocking_timeout != -1) { | |
2728 | ret = -LTTNG_ERR_INVALID; | |
2729 | goto end; | |
2730 | } | |
2731 | ||
2732 | /* | |
2733 | * LTTng-ust's use of poll() to implement this timeout mechanism forces | |
2734 | * us to accept a narrower range of values (msecs expressed as a signed | |
2735 | * 32-bit integer). | |
2736 | */ | |
2737 | msec_timeout = blocking_timeout / 1000; | |
2738 | if (msec_timeout != (int32_t) msec_timeout) { | |
2739 | ret = -LTTNG_ERR_INVALID; | |
2740 | goto end; | |
2741 | } | |
2742 | ||
2743 | ((struct lttng_channel_extended *) | |
2744 | chan->attr.extended.ptr)->blocking_timeout = | |
2745 | blocking_timeout; | |
2746 | end: | |
2747 | return ret; | |
2748 | } | |
2749 | ||
fac6795d | 2750 | /* |
2269e89e | 2751 | * Check if session daemon is alive. |
fac6795d | 2752 | * |
2269e89e | 2753 | * Return 1 if alive or 0 if not. |
1c8d13c8 | 2754 | * On error returns a negative value. |
fac6795d | 2755 | */ |
947308c4 | 2756 | int lttng_session_daemon_alive(void) |
fac6795d DG |
2757 | { |
2758 | int ret; | |
2759 | ||
2760 | ret = set_session_daemon_path(); | |
2761 | if (ret < 0) { | |
9ae110e2 | 2762 | /* Error. */ |
fac6795d DG |
2763 | return ret; |
2764 | } | |
2765 | ||
9d035200 | 2766 | if (*sessiond_sock_path == '\0') { |
2f70b271 | 2767 | /* |
9ae110e2 JG |
2768 | * No socket path set. Weird error which means the constructor |
2769 | * was not called. | |
2f70b271 DG |
2770 | */ |
2771 | assert(0); | |
fac6795d DG |
2772 | } |
2773 | ||
2269e89e | 2774 | ret = try_connect_sessiond(sessiond_sock_path); |
7d8234d9 | 2775 | if (ret < 0) { |
9ae110e2 | 2776 | /* Not alive. */ |
7d8234d9 MD |
2777 | return 0; |
2778 | } | |
7d8234d9 | 2779 | |
9ae110e2 | 2780 | /* Is alive. */ |
947308c4 | 2781 | return 1; |
fac6795d DG |
2782 | } |
2783 | ||
00e2e675 | 2784 | /* |
a4b92340 | 2785 | * Set URL for a consumer for a session and domain. |
00e2e675 DG |
2786 | * |
2787 | * Return 0 on success, else a negative value. | |
2788 | */ | |
a4b92340 DG |
2789 | int lttng_set_consumer_url(struct lttng_handle *handle, |
2790 | const char *control_url, const char *data_url) | |
00e2e675 | 2791 | { |
3dd05a85 | 2792 | int ret; |
a4b92340 | 2793 | ssize_t size; |
00e2e675 | 2794 | struct lttcomm_session_msg lsm; |
a4b92340 | 2795 | struct lttng_uri *uris = NULL; |
00e2e675 | 2796 | |
a4b92340 | 2797 | if (handle == NULL || (control_url == NULL && data_url == NULL)) { |
2f70b271 | 2798 | return -LTTNG_ERR_INVALID; |
00e2e675 DG |
2799 | } |
2800 | ||
a4b92340 DG |
2801 | memset(&lsm, 0, sizeof(lsm)); |
2802 | ||
00e2e675 DG |
2803 | lsm.cmd_type = LTTNG_SET_CONSUMER_URI; |
2804 | ||
cac3069d | 2805 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
00e2e675 | 2806 | sizeof(lsm.session.name)); |
60160d2a | 2807 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
00e2e675 | 2808 | |
bc894455 | 2809 | size = uri_parse_str_urls(control_url, data_url, &uris); |
a4b92340 | 2810 | if (size < 0) { |
2f70b271 | 2811 | return -LTTNG_ERR_INVALID; |
a4b92340 DG |
2812 | } |
2813 | ||
2814 | lsm.u.uri.size = size; | |
00e2e675 | 2815 | |
795a978d | 2816 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris, |
cac3069d | 2817 | sizeof(struct lttng_uri) * size, NULL); |
3dd05a85 DG |
2818 | |
2819 | free(uris); | |
2820 | return ret; | |
00e2e675 DG |
2821 | } |
2822 | ||
2823 | /* | |
9c6bda17 | 2824 | * [OBSOLETE] |
00e2e675 | 2825 | */ |
2ec40111 | 2826 | int lttng_enable_consumer(struct lttng_handle *handle); |
00e2e675 DG |
2827 | int lttng_enable_consumer(struct lttng_handle *handle) |
2828 | { | |
785d2d0d | 2829 | return -ENOSYS; |
00e2e675 DG |
2830 | } |
2831 | ||
2832 | /* | |
9c6bda17 | 2833 | * [OBSOLETE] |
00e2e675 | 2834 | */ |
2ec40111 | 2835 | int lttng_disable_consumer(struct lttng_handle *handle); |
00e2e675 DG |
2836 | int lttng_disable_consumer(struct lttng_handle *handle) |
2837 | { | |
785d2d0d | 2838 | return -ENOSYS; |
00e2e675 DG |
2839 | } |
2840 | ||
07424f16 | 2841 | /* |
b178f53e | 2842 | * [OBSOLETE] |
07424f16 | 2843 | */ |
2ec40111 SM |
2844 | int _lttng_create_session_ext(const char *name, const char *url, |
2845 | const char *datetime); | |
07424f16 DG |
2846 | int _lttng_create_session_ext(const char *name, const char *url, |
2847 | const char *datetime) | |
2848 | { | |
b178f53e | 2849 | return -ENOSYS; |
07424f16 DG |
2850 | } |
2851 | ||
806e2684 DG |
2852 | /* |
2853 | * For a given session name, this call checks if the data is ready to be read | |
2854 | * or is still being extracted by the consumer(s) hence not ready to be used by | |
2855 | * any readers. | |
2856 | */ | |
6d805429 | 2857 | int lttng_data_pending(const char *session_name) |
806e2684 DG |
2858 | { |
2859 | int ret; | |
2860 | struct lttcomm_session_msg lsm; | |
f6151c55 | 2861 | uint8_t *pending = NULL; |
806e2684 DG |
2862 | |
2863 | if (session_name == NULL) { | |
2864 | return -LTTNG_ERR_INVALID; | |
2865 | } | |
2866 | ||
53efb85a | 2867 | memset(&lsm, 0, sizeof(lsm)); |
6d805429 | 2868 | lsm.cmd_type = LTTNG_DATA_PENDING; |
806e2684 | 2869 | |
cac3069d DG |
2870 | lttng_ctl_copy_string(lsm.session.name, session_name, |
2871 | sizeof(lsm.session.name)); | |
806e2684 | 2872 | |
f6151c55 JG |
2873 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending); |
2874 | if (ret < 0) { | |
2875 | goto end; | |
2876 | } else if (ret != 1) { | |
2877 | /* Unexpected payload size */ | |
2878 | ret = -LTTNG_ERR_INVALID; | |
2879 | goto end; | |
e848d36d JG |
2880 | } else if (!pending) { |
2881 | /* Internal error. */ | |
2882 | ret = -LTTNG_ERR_UNK; | |
2883 | goto end; | |
806e2684 DG |
2884 | } |
2885 | ||
f6151c55 JG |
2886 | ret = (int) *pending; |
2887 | end: | |
2888 | free(pending); | |
806e2684 DG |
2889 | return ret; |
2890 | } | |
2891 | ||
93ec662e JD |
2892 | /* |
2893 | * Regenerate the metadata for a session. | |
2894 | * Return 0 on success, a negative error code on error. | |
2895 | */ | |
eded6438 | 2896 | int lttng_regenerate_metadata(const char *session_name) |
93ec662e JD |
2897 | { |
2898 | int ret; | |
2899 | struct lttcomm_session_msg lsm; | |
2900 | ||
2901 | if (!session_name) { | |
2902 | ret = -LTTNG_ERR_INVALID; | |
2903 | goto end; | |
2904 | } | |
2905 | ||
2906 | memset(&lsm, 0, sizeof(lsm)); | |
eded6438 | 2907 | lsm.cmd_type = LTTNG_REGENERATE_METADATA; |
93ec662e JD |
2908 | |
2909 | lttng_ctl_copy_string(lsm.session.name, session_name, | |
2910 | sizeof(lsm.session.name)); | |
2911 | ||
2912 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
2913 | if (ret < 0) { | |
2914 | goto end; | |
2915 | } | |
2916 | ||
2917 | ret = 0; | |
2918 | end: | |
2919 | return ret; | |
2920 | } | |
2921 | ||
eded6438 JD |
2922 | /* |
2923 | * Deprecated, replaced by lttng_regenerate_metadata. | |
2924 | */ | |
2925 | int lttng_metadata_regenerate(const char *session_name) | |
2926 | { | |
2927 | return lttng_regenerate_metadata(session_name); | |
2928 | } | |
2929 | ||
c2561365 JD |
2930 | /* |
2931 | * Regenerate the statedump of a session. | |
2932 | * Return 0 on success, a negative error code on error. | |
2933 | */ | |
2934 | int lttng_regenerate_statedump(const char *session_name) | |
2935 | { | |
2936 | int ret; | |
2937 | struct lttcomm_session_msg lsm; | |
2938 | ||
2939 | if (!session_name) { | |
2940 | ret = -LTTNG_ERR_INVALID; | |
2941 | goto end; | |
2942 | } | |
2943 | ||
2944 | memset(&lsm, 0, sizeof(lsm)); | |
2945 | lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP; | |
2946 | ||
2947 | lttng_ctl_copy_string(lsm.session.name, session_name, | |
2948 | sizeof(lsm.session.name)); | |
2949 | ||
2950 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
2951 | if (ret < 0) { | |
2952 | goto end; | |
2953 | } | |
2954 | ||
2955 | ret = 0; | |
2956 | end: | |
2957 | return ret; | |
2958 | } | |
2959 | ||
a58c490f JG |
2960 | int lttng_register_trigger(struct lttng_trigger *trigger) |
2961 | { | |
2962 | int ret; | |
99608320 JR |
2963 | struct lttcomm_session_msg lsm = { |
2964 | .cmd_type = LTTNG_REGISTER_TRIGGER, | |
2965 | }; | |
2966 | struct lttcomm_session_msg *message_lsm; | |
2967 | struct lttng_payload message; | |
2968 | struct lttng_payload reply; | |
242388e4 | 2969 | struct lttng_trigger *reply_trigger = NULL; |
64eafdf6 JR |
2970 | const struct lttng_credentials user_creds = { |
2971 | .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()), | |
2972 | .gid = LTTNG_OPTIONAL_INIT_UNSET, | |
2973 | }; | |
99608320 | 2974 | |
242388e4 | 2975 | |
99608320 JR |
2976 | lttng_payload_init(&message); |
2977 | lttng_payload_init(&reply); | |
a58c490f JG |
2978 | |
2979 | if (!trigger) { | |
2980 | ret = -LTTNG_ERR_INVALID; | |
2981 | goto end; | |
2982 | } | |
2983 | ||
64eafdf6 JR |
2984 | if (!trigger->creds.uid.is_set) { |
2985 | /* Use the client's credentials as the trigger credentials. */ | |
2986 | lttng_trigger_set_credentials(trigger, &user_creds); | |
2987 | } else { | |
2988 | /* | |
2989 | * Validate that either the current trigger credentials and the | |
2990 | * client credentials are identical or that the current user is | |
2991 | * root. The root user can register, unregister triggers for | |
2992 | * himself and other users. | |
2993 | * | |
2994 | * This check is also present on the sessiond side, using the | |
2995 | * credentials passed on the socket. These check are all | |
2996 | * "safety" checks. | |
2997 | */ | |
2998 | const struct lttng_credentials *trigger_creds = | |
2999 | lttng_trigger_get_credentials(trigger); | |
3000 | ||
3001 | if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) { | |
3002 | if (lttng_credentials_get_uid(&user_creds) != 0) { | |
3003 | ret = -LTTNG_ERR_EPERM; | |
3004 | goto end; | |
3005 | } | |
3006 | } | |
3007 | } | |
3008 | ||
a58c490f | 3009 | if (!lttng_trigger_validate(trigger)) { |
eac4828d | 3010 | ret = -LTTNG_ERR_INVALID_TRIGGER; |
a58c490f JG |
3011 | goto end; |
3012 | } | |
3013 | ||
97285430 JG |
3014 | ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm)); |
3015 | if (ret) { | |
3016 | ret = -LTTNG_ERR_NOMEM; | |
3017 | goto end; | |
3018 | } | |
99608320 JR |
3019 | |
3020 | /* | |
3021 | * This is needed to populate the trigger object size for the command | |
3022 | * header. | |
3023 | */ | |
3024 | message_lsm = (struct lttcomm_session_msg *) message.buffer.data; | |
3025 | ||
3026 | ret = lttng_trigger_serialize(trigger, &message); | |
3647288f | 3027 | if (ret < 0) { |
a58c490f JG |
3028 | ret = -LTTNG_ERR_UNK; |
3029 | goto end; | |
3030 | } | |
3031 | ||
99608320 JR |
3032 | message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm); |
3033 | ||
3034 | { | |
3035 | struct lttng_payload_view message_view = | |
3036 | lttng_payload_view_from_payload( | |
3037 | &message, 0, -1); | |
3038 | ||
3039 | message_lsm->fd_count = lttng_payload_view_get_fd_handle_count( | |
3040 | &message_view); | |
3041 | ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); | |
3042 | if (ret < 0) { | |
3043 | goto end; | |
3044 | } | |
3045 | } | |
3046 | ||
242388e4 JR |
3047 | { |
3048 | struct lttng_payload_view reply_view = | |
3049 | lttng_payload_view_from_payload( | |
3050 | &reply, 0, reply.buffer.size); | |
3051 | ||
3052 | ret = lttng_trigger_create_from_payload( | |
3053 | &reply_view, &reply_trigger); | |
3054 | if (ret < 0) { | |
3055 | ret = -LTTNG_ERR_FATAL; | |
3056 | goto end; | |
3057 | } | |
3058 | } | |
3059 | ||
3060 | ret = lttng_trigger_assign_name(trigger, reply_trigger); | |
3061 | if (ret < 0) { | |
3062 | ret = -LTTNG_ERR_FATAL; | |
3063 | goto end; | |
3064 | } | |
3065 | ||
99608320 | 3066 | ret = 0; |
a58c490f | 3067 | end: |
99608320 JR |
3068 | lttng_payload_reset(&message); |
3069 | lttng_payload_reset(&reply); | |
242388e4 | 3070 | lttng_trigger_destroy(reply_trigger); |
a58c490f JG |
3071 | return ret; |
3072 | } | |
3073 | ||
3074 | int lttng_unregister_trigger(struct lttng_trigger *trigger) | |
3075 | { | |
3076 | int ret; | |
3077 | struct lttcomm_session_msg lsm; | |
99608320 JR |
3078 | struct lttcomm_session_msg *message_lsm; |
3079 | struct lttng_payload message; | |
3080 | struct lttng_payload reply; | |
64eafdf6 JR |
3081 | const struct lttng_credentials user_creds = { |
3082 | .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()), | |
3083 | .gid = LTTNG_OPTIONAL_INIT_UNSET, | |
3084 | }; | |
99608320 JR |
3085 | |
3086 | lttng_payload_init(&message); | |
3087 | lttng_payload_init(&reply); | |
a58c490f JG |
3088 | |
3089 | if (!trigger) { | |
3090 | ret = -LTTNG_ERR_INVALID; | |
3091 | goto end; | |
3092 | } | |
3093 | ||
64eafdf6 JR |
3094 | if (!trigger->creds.uid.is_set) { |
3095 | /* Use the client's credentials as the trigger credentials. */ | |
3096 | lttng_trigger_set_credentials(trigger, &user_creds); | |
3097 | } else { | |
3098 | /* | |
3099 | * Validate that either the current trigger credentials and the | |
3100 | * client credentials are identical or that the current user is | |
3101 | * root. The root user can register, unregister triggers for | |
3102 | * himself and other users. | |
3103 | * | |
3104 | * This check is also present on the sessiond side, using the | |
3105 | * credentials passed on the socket. These check are all | |
3106 | * "safety" checks. | |
3107 | */ | |
3108 | const struct lttng_credentials *trigger_creds = | |
3109 | lttng_trigger_get_credentials(trigger); | |
3110 | ||
3111 | if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) { | |
3112 | if (lttng_credentials_get_uid(&user_creds) != 0) { | |
3113 | ret = -LTTNG_ERR_EPERM; | |
3114 | goto end; | |
3115 | } | |
3116 | } | |
3117 | } | |
3118 | ||
a58c490f | 3119 | if (!lttng_trigger_validate(trigger)) { |
3647288f | 3120 | ret = -LTTNG_ERR_INVALID_TRIGGER; |
a58c490f JG |
3121 | goto end; |
3122 | } | |
3123 | ||
99608320 JR |
3124 | memset(&lsm, 0, sizeof(lsm)); |
3125 | lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER; | |
3126 | ||
97285430 JG |
3127 | ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm)); |
3128 | if (ret) { | |
3129 | ret = -LTTNG_ERR_NOMEM; | |
3130 | goto end; | |
3131 | } | |
99608320 JR |
3132 | |
3133 | /* | |
3134 | * This is needed to populate the trigger object size for the command | |
3135 | * header and number of fds sent. | |
3136 | */ | |
3137 | message_lsm = (struct lttcomm_session_msg *) message.buffer.data; | |
3138 | ||
3139 | ret = lttng_trigger_serialize(trigger, &message); | |
3647288f | 3140 | if (ret < 0) { |
a58c490f JG |
3141 | ret = -LTTNG_ERR_UNK; |
3142 | goto end; | |
3143 | } | |
3144 | ||
99608320 JR |
3145 | message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm); |
3146 | ||
3147 | { | |
3148 | struct lttng_payload_view message_view = | |
3149 | lttng_payload_view_from_payload( | |
3150 | &message, 0, -1); | |
3151 | ||
3152 | /* | |
3153 | * Update the message header with the number of fd that will be | |
3154 | * sent. | |
3155 | */ | |
3156 | message_lsm->fd_count = lttng_payload_view_get_fd_handle_count( | |
3157 | &message_view); | |
3158 | ||
3159 | ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); | |
3160 | if (ret < 0) { | |
3161 | goto end; | |
3162 | } | |
3163 | } | |
3164 | ||
3165 | ret = 0; | |
a58c490f | 3166 | end: |
99608320 JR |
3167 | lttng_payload_reset(&message); |
3168 | lttng_payload_reset(&reply); | |
a58c490f JG |
3169 | return ret; |
3170 | } | |
3171 | ||
fac6795d | 3172 | /* |
9ae110e2 | 3173 | * lib constructor. |
fac6795d | 3174 | */ |
b2b89e8a | 3175 | static void __attribute__((constructor)) init(void) |
fac6795d DG |
3176 | { |
3177 | /* Set default session group */ | |
bbccc3d2 | 3178 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
fac6795d | 3179 | } |
49cca668 DG |
3180 | |
3181 | /* | |
9ae110e2 | 3182 | * lib destructor. |
49cca668 | 3183 | */ |
b2b89e8a | 3184 | static void __attribute__((destructor)) lttng_ctl_exit(void) |
49cca668 DG |
3185 | { |
3186 | free(tracing_group); | |
3187 | } |