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