Error out if filter expression is attached to unsupported event types
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
f3ed775e
DG
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
f3ed775e
DG
16 */
17
6c1c0768 18#define _LGPL_SOURCE
b2064f54 19#include <assert.h>
f3ed775e
DG
20#include <popt.h>
21#include <stdio.h>
22#include <stdlib.h>
f3ed775e
DG
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
5a0de755 26#include <inttypes.h>
8f0d098b 27#include <ctype.h>
f3ed775e 28
343af227 29#include <common/sessiond-comm/sessiond-comm.h>
f5436bfc 30#include <common/compat/string.h>
dbc478f3 31#include <common/compat/getenv.h>
9f449915 32#include <common/string-utils/string-utils.h>
dbc478f3 33#include <common/utils.h>
f3ed775e 34
dbc478f3 35#include <lttng/constant.h>
89476427
JRJ
36/* Mi dependancy */
37#include <common/mi-lttng.h>
38
39#include "../command.h"
40
8ab7c0d9
MD
41#if (LTTNG_SYMBOL_NAME_LEN == 256)
42#define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
43#endif
44
f3ed775e
DG
45static char *opt_event_list;
46static int opt_event_type;
0cda4f28
MD
47static const char *opt_loglevel;
48static int opt_loglevel_type;
6181537c 49static int opt_kernel;
5440dc42 50static char *opt_session_name;
f3ed775e 51static int opt_userspace;
b9dfb167 52static int opt_jul;
5cdb6027 53static int opt_log4j;
0e115563 54static int opt_python;
f3ed775e 55static int opt_enable_all;
cf0e5467 56static char *opt_probe;
dcabc190 57static char *opt_userspace_probe;
8f0d098b 58static char *opt_function;
f3ed775e 59static char *opt_channel_name;
53a80697 60static char *opt_filter;
fac3366c 61static char *opt_exclude;
f3ed775e 62
4fc83d94
PP
63#ifdef LTTNG_EMBED_HELP
64static const char help_msg[] =
65#include <lttng-enable-event.1.h>
66;
67#endif
68
f3ed775e
DG
69enum {
70 OPT_HELP = 1,
f3ed775e 71 OPT_TRACEPOINT,
cf0e5467 72 OPT_PROBE,
dcabc190 73 OPT_USERSPACE_PROBE,
f3ed775e 74 OPT_FUNCTION,
a54bd42d 75 OPT_SYSCALL,
eeac7d46 76 OPT_USERSPACE,
0cda4f28
MD
77 OPT_LOGLEVEL,
78 OPT_LOGLEVEL_ONLY,
679b4943 79 OPT_LIST_OPTIONS,
53a80697 80 OPT_FILTER,
fac3366c 81 OPT_EXCLUDE,
f3ed775e
DG
82};
83
cd80958d 84static struct lttng_handle *handle;
89476427 85static struct mi_writer *writer;
cd80958d 86
f3ed775e
DG
87static struct poptOption long_options[] = {
88 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
89 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 90 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e14f64a8 91 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
f3ed775e
DG
92 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
93 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610 94 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
b9dfb167 95 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
5cdb6027 96 {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0},
0e115563 97 {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0},
f3ed775e 98 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
7ebae521 99 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
dcabc190 100 {"userspace-probe",0, POPT_ARG_STRING, &opt_userspace_probe, OPT_USERSPACE_PROBE, 0, 0},
40e9d5d3 101 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
7ebae521 102 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
0cda4f28
MD
103 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
104 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
679b4943 105 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
53a80697 106 {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0},
fac3366c 107 {"exclude", 'x', POPT_ARG_STRING, &opt_exclude, OPT_EXCLUDE, 0, 0},
f3ed775e
DG
108 {0, 0, 0, 0, 0, 0, 0}
109};
110
0d63dd19 111/*
6181537c 112 * Parse probe options.
0d63dd19 113 */
cf0e5467 114static int parse_probe_opts(struct lttng_event *ev, char *opt)
0d63dd19 115{
49d4e302
JRJ
116 int ret = CMD_SUCCESS;
117 int match;
8ff0bbd0 118 char s_hex[19];
8ab7c0d9 119#define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
0d63dd19
DG
120 char name[LTTNG_SYMBOL_NAME_LEN];
121
122 if (opt == NULL) {
49d4e302 123 ret = CMD_ERROR;
8f0d098b 124 goto end;
0d63dd19
DG
125 }
126
127 /* Check for symbol+offset */
49d4e302 128 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
8ab7c0d9 129 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", name, s_hex);
49d4e302 130 if (match == 2) {
7d29a247 131 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 132 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
cf0e5467 133 DBG("probe symbol %s", ev->attr.probe.symbol_name);
9d035200 134 if (*s_hex == '\0') {
8ff0bbd0 135 ERR("Invalid probe offset %s", s_hex);
49d4e302 136 ret = CMD_ERROR;
8f0d098b 137 goto end;
0d63dd19 138 }
8ff0bbd0 139 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
cf0e5467 140 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
3000dc78 141 ev->attr.probe.addr = 0;
8f0d098b
MD
142 goto end;
143 }
144
145 /* Check for symbol */
146 if (isalpha(name[0])) {
49d4e302 147 match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s",
8ab7c0d9 148 name);
49d4e302 149 if (match == 1) {
8f0d098b 150 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 151 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
8f0d098b
MD
152 DBG("probe symbol %s", ev->attr.probe.symbol_name);
153 ev->attr.probe.offset = 0;
154 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
155 ev->attr.probe.addr = 0;
156 goto end;
157 }
0d63dd19
DG
158 }
159
160 /* Check for address */
49d4e302
JRJ
161 match = sscanf(opt, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", s_hex);
162 if (match > 0) {
9d035200 163 if (*s_hex == '\0') {
8ff0bbd0 164 ERR("Invalid probe address %s", s_hex);
49d4e302 165 ret = CMD_ERROR;
8f0d098b 166 goto end;
0d63dd19 167 }
8ff0bbd0 168 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
cf0e5467 169 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
3000dc78
DG
170 ev->attr.probe.offset = 0;
171 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
8f0d098b 172 goto end;
0d63dd19
DG
173 }
174
175 /* No match */
49d4e302 176 ret = CMD_ERROR;
0d63dd19 177
8f0d098b 178end:
0d63dd19
DG
179 return ret;
180}
181
dbc478f3
FD
182/*
183 * Walk the directories in the PATH environment variable to find the target
184 * binary passed as parameter.
185 *
186 * On success, the full path of the binary is copied in binary_full_path out
187 * parameter. This buffer is allocated by the caller and must be at least
188 * LTTNG_PATH_MAX bytes long.
189 * On failure, returns -1;
190 */
191static int walk_command_search_path(const char *binary, char *binary_full_path)
192{
193 char *tentative_binary_path = NULL;
194 char *command_search_path = NULL;
195 char *curr_search_dir_end = NULL;
196 char *curr_search_dir = NULL;
197 struct stat stat_output;
198 int ret = 0;
199
200 command_search_path = lttng_secure_getenv("PATH");
201 if (!command_search_path) {
202 ret = -1;
203 goto end;
204 }
205
206 /*
207 * Duplicate the $PATH string as the char pointer returned by getenv() should
208 * not be modified.
209 */
210 command_search_path = strdup(command_search_path);
211 if (!command_search_path) {
212 ret = -1;
213 goto end;
214 }
215
216 /*
217 * This char array is used to concatenate path to binary to look for
218 * the binary.
219 */
220 tentative_binary_path = zmalloc(LTTNG_PATH_MAX * sizeof(char));
221 if (!tentative_binary_path) {
222 ret = -1;
223 goto alloc_error;
224 }
225
226 curr_search_dir = command_search_path;
227 do {
228 /*
229 * Split on ':'. The return value of this call points to the
230 * matching character.
231 */
232 curr_search_dir_end = strchr(curr_search_dir, ':');
233 if (curr_search_dir_end != NULL) {
234 /*
235 * Add a NULL byte to the end of the first token so it
236 * can be used as a string.
237 */
238 curr_search_dir_end[0] = '\0';
239 }
240
241 /* Empty the tentative path */
242 memset(tentative_binary_path, 0, LTTNG_PATH_MAX * sizeof(char));
243
244 /*
245 * Build the tentative path to the binary using the current
246 * search directory and the name of the binary.
247 */
248 ret = snprintf(tentative_binary_path, LTTNG_PATH_MAX, "%s/%s",
249 curr_search_dir, binary);
250 if (ret < 0) {
251 goto free_binary_path;
252 }
253 if (ret < LTTNG_PATH_MAX) {
254 /*
255 * Use STAT(2) to see if the file exists.
256 */
257 ret = stat(tentative_binary_path, &stat_output);
258 if (ret == 0) {
259 /*
260 * Verify that it is a regular file or a
261 * symlink and not a special file (e.g.
262 * device).
263 */
264 if (S_ISREG(stat_output.st_mode)
265 || S_ISLNK(stat_output.st_mode)) {
266 /*
267 * Found a match, set the out parameter
268 * and return success.
269 */
270 ret = lttng_strncpy(binary_full_path,
271 tentative_binary_path,
272 LTTNG_PATH_MAX);
273 if (ret == -1) {
274 ERR("Source path does not fit "
275 "in destination buffer.");
276 }
277 goto free_binary_path;
278 }
279 }
280 }
281 /* Go to the next entry in the $PATH variable. */
282 curr_search_dir = curr_search_dir_end + 1;
283 } while (curr_search_dir_end != NULL);
284
285free_binary_path:
286 free(tentative_binary_path);
287alloc_error:
288 free(command_search_path);
289end:
290 return ret;
291}
292
dcabc190
FD
293/*
294 * Parse userspace probe options
295 * Set the userspace probe fields in the lttng_event struct and set the
296 * target_path to the path to the binary.
297 */
298static int parse_userspace_probe_opts(struct lttng_event *ev, char *opt)
299{
300 int ret = CMD_SUCCESS;
301 int num_token;
302 char **tokens;
303 char *target_path = NULL;
304 char *unescaped_target_path = NULL;
305 char *real_target_path = NULL;
306 char *symbol_name = NULL, *probe_name = NULL, *provider_name = NULL;
307 struct lttng_userspace_probe_location *probe_location = NULL;
308 struct lttng_userspace_probe_location_lookup_method *lookup_method =
309 NULL;
310
311 if (opt == NULL) {
312 ret = CMD_ERROR;
313 goto end;
314 }
315
316 /*
317 * userspace probe fields are separated by ':'.
318 */
319 tokens = strutils_split(opt, ':', 1);
320 num_token = strutils_array_of_strings_len(tokens);
321
322 /*
323 * Early sanity check that the number of parameter is between 2 and 4
324 * inclusively.
325 * elf:PATH:SYMBOL
326 * std:PATH:PROVIDER_NAME:PROBE_NAME
327 * PATH:SYMBOL (same behavior as ELF)
328 */
329 if (num_token < 2 || num_token > 4) {
330 ret = CMD_ERROR;
331 goto end_string;
332 }
333
334 /*
335 * Looking up the first parameter will tell the technique to use to
336 * interpret the userspace probe/function description.
337 */
338 switch (num_token) {
339 case 2:
340 /* When the probe type is omitted we assume ELF for now. */
341 case 3:
342 if (num_token == 3 && strcmp(tokens[0], "elf") == 0) {
343 target_path = tokens[1];
344 symbol_name = tokens[2];
345 } else if (num_token == 2) {
346 target_path = tokens[0];
347 symbol_name = tokens[1];
348 } else {
349 ret = CMD_ERROR;
350 goto end_string;
351 }
352 lookup_method =
353 lttng_userspace_probe_location_lookup_method_function_elf_create();
354 if (!lookup_method) {
355 WARN("Failed to create ELF lookup method");
356 ret = CMD_ERROR;
357 goto end_string;
358 }
359 break;
360 case 4:
361 if (strcmp(tokens[0], "sdt") == 0) {
362 target_path = tokens[1];
363 provider_name = tokens[2];
364 probe_name = tokens[3];
365 } else {
366 ret = CMD_ERROR;
367 goto end_string;
368 }
369 lookup_method =
370 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
371 if (!lookup_method) {
372 WARN("Failed to create ELF lookup method");
373 ret = CMD_ERROR;
374 goto end_string;
375 }
376 break;
377 default:
378 ret = CMD_ERROR;
379 goto end_string;
380 }
381
382 /* strutils_unescape_string allocates a new char *. */
383 unescaped_target_path = strutils_unescape_string(target_path, 0);
384 if (!unescaped_target_path) {
385 ret = -LTTNG_ERR_INVALID;
386 goto end_string;
387 }
388
389 /*
390 * If there is not forward slash in the path. Walk the $PATH else
391 * expand.
392 */
393 if (strchr(target_path, '/') == NULL) {
394 /* Walk the $PATH variable to find the targeted binary. */
395 real_target_path = zmalloc(LTTNG_PATH_MAX * sizeof(char));
396 if (!real_target_path) {
397 PERROR("Error allocating path buffer");
398 ret = CMD_ERROR;
399 goto end_unescaped_string;
400 }
401 ret = walk_command_search_path(target_path, real_target_path);
402 if (ret) {
403 ERR("Binary not found.");
404 ret = CMD_ERROR;
405 goto end_free_path;
406 }
407 } else {
408 /*
409 * Expand references to `/./` and `/../`. This function does not check
410 * if the file exists.
411 */
412 real_target_path = utils_expand_path_keep_symlink(target_path);
413 if (!real_target_path) {
414 ERR("Error expanding the path to binary.");
415 ret = CMD_ERROR;
416 goto end_free_path;
417 }
418
419 /*
420 * Check if the file exists using access(2). If it does not, walk the
421 * $PATH.
422 */
423 ret = access(real_target_path, F_OK);
424 if (ret) {
425 ret = CMD_ERROR;
426 goto end_free_path;
427 }
428 }
429
430 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup_method)) {
431 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
432 probe_location = lttng_userspace_probe_location_function_create(
433 real_target_path, symbol_name, lookup_method);
434 if (!probe_location) {
435 WARN("Failed to create function probe location");
436 ret = CMD_ERROR;
437 goto end_destroy_lookup_method;
438 }
439
440 /* Ownership transferred to probe_location. */
441 lookup_method = NULL;
442
443 ret = lttng_event_set_userspace_probe_location(ev, probe_location);
444 if (ret) {
445 WARN("Failed to set probe location on event");
446 ret = CMD_ERROR;
447 goto end_destroy_location;
448 }
449 break;
450 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
451 probe_location = lttng_userspace_probe_location_tracepoint_create(
452 real_target_path, provider_name, probe_name, lookup_method);
453 if (!probe_location) {
454 WARN("Failed to create function probe location");
455 ret = CMD_ERROR;
456 goto end_destroy_lookup_method;
457 }
458
459 /* Ownership transferred to probe_location. */
460 lookup_method = NULL;
461
462 ret = lttng_event_set_userspace_probe_location(ev, probe_location);
463 if (ret) {
464 WARN("Failed to set probe location on event");
465 ret = CMD_ERROR;
466 goto end_destroy_location;
467 }
468 break;
469 default:
470 ret = CMD_ERROR;
471 goto end_string;
472 }
473
474 switch (ev->type) {
475 case LTTNG_EVENT_USERSPACE_PROBE:
476 break;
477 default:
478 assert(0);
479 }
480
481 goto end;
482
483end_destroy_location:
484 lttng_userspace_probe_location_destroy(probe_location);
485end_destroy_lookup_method:
486 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
487end_free_path:
488 /*
489 * Free path that was allocated by the call to realpath() or when walking
490 * the PATH.
491 */
492 free(real_target_path);
493end_unescaped_string:
494 free(unescaped_target_path);
495end_string:
496 strutils_free_null_terminated_array_of_strings(tokens);
497end:
498 return ret;
499}
500
5cdb6027
DG
501/*
502 * Maps LOG4j loglevel from string to value
503 */
504static int loglevel_log4j_str_to_value(const char *inputstr)
505{
506 int i = 0;
507 char str[LTTNG_SYMBOL_NAME_LEN];
508
3712b110
JG
509 if (!inputstr || strlen(inputstr) == 0) {
510 return -1;
511 }
512
5cdb6027
DG
513 /*
514 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
515 * added at the end of the loop so a the upper bound we avoid the overflow.
516 */
517 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
518 str[i] = toupper(inputstr[i]);
519 i++;
520 }
521 str[i] = '\0';
522
523 if (!strcmp(str, "LOG4J_OFF") || !strcmp(str, "OFF")) {
524 return LTTNG_LOGLEVEL_LOG4J_OFF;
525 } else if (!strcmp(str, "LOG4J_FATAL") || !strcmp(str, "FATAL")) {
526 return LTTNG_LOGLEVEL_LOG4J_FATAL;
527 } else if (!strcmp(str, "LOG4J_ERROR") || !strcmp(str, "ERROR")) {
528 return LTTNG_LOGLEVEL_LOG4J_ERROR;
529 } else if (!strcmp(str, "LOG4J_WARN") || !strcmp(str, "WARN")) {
530 return LTTNG_LOGLEVEL_LOG4J_WARN;
531 } else if (!strcmp(str, "LOG4J_INFO") || !strcmp(str, "INFO")) {
532 return LTTNG_LOGLEVEL_LOG4J_INFO;
533 } else if (!strcmp(str, "LOG4J_DEBUG") || !strcmp(str, "DEBUG")) {
534 return LTTNG_LOGLEVEL_LOG4J_DEBUG;
535 } else if (!strcmp(str, "LOG4J_TRACE") || !strcmp(str, "TRACE")) {
536 return LTTNG_LOGLEVEL_LOG4J_TRACE;
537 } else if (!strcmp(str, "LOG4J_ALL") || !strcmp(str, "ALL")) {
538 return LTTNG_LOGLEVEL_LOG4J_ALL;
539 } else {
540 return -1;
541 }
542}
543
b2064f54
DG
544/*
545 * Maps JUL loglevel from string to value
546 */
547static int loglevel_jul_str_to_value(const char *inputstr)
548{
549 int i = 0;
550 char str[LTTNG_SYMBOL_NAME_LEN];
551
3712b110
JG
552 if (!inputstr || strlen(inputstr) == 0) {
553 return -1;
554 }
555
b2064f54
DG
556 /*
557 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
558 * added at the end of the loop so a the upper bound we avoid the overflow.
559 */
560 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
561 str[i] = toupper(inputstr[i]);
562 i++;
563 }
564 str[i] = '\0';
565
566 if (!strcmp(str, "JUL_OFF") || !strcmp(str, "OFF")) {
567 return LTTNG_LOGLEVEL_JUL_OFF;
568 } else if (!strcmp(str, "JUL_SEVERE") || !strcmp(str, "SEVERE")) {
569 return LTTNG_LOGLEVEL_JUL_SEVERE;
570 } else if (!strcmp(str, "JUL_WARNING") || !strcmp(str, "WARNING")) {
571 return LTTNG_LOGLEVEL_JUL_WARNING;
572 } else if (!strcmp(str, "JUL_INFO") || !strcmp(str, "INFO")) {
573 return LTTNG_LOGLEVEL_JUL_INFO;
574 } else if (!strcmp(str, "JUL_CONFIG") || !strcmp(str, "CONFIG")) {
575 return LTTNG_LOGLEVEL_JUL_CONFIG;
576 } else if (!strcmp(str, "JUL_FINE") || !strcmp(str, "FINE")) {
577 return LTTNG_LOGLEVEL_JUL_FINE;
578 } else if (!strcmp(str, "JUL_FINER") || !strcmp(str, "FINER")) {
579 return LTTNG_LOGLEVEL_JUL_FINER;
580 } else if (!strcmp(str, "JUL_FINEST") || !strcmp(str, "FINEST")) {
581 return LTTNG_LOGLEVEL_JUL_FINEST;
582 } else if (!strcmp(str, "JUL_ALL") || !strcmp(str, "ALL")) {
583 return LTTNG_LOGLEVEL_JUL_ALL;
584 } else {
585 return -1;
586 }
587}
588
0e115563
DG
589/*
590 * Maps Python loglevel from string to value
591 */
592static int loglevel_python_str_to_value(const char *inputstr)
593{
594 int i = 0;
595 char str[LTTNG_SYMBOL_NAME_LEN];
596
3712b110
JG
597 if (!inputstr || strlen(inputstr) == 0) {
598 return -1;
599 }
600
0e115563
DG
601 /*
602 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
603 * added at the end of the loop so a the upper bound we avoid the overflow.
604 */
605 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
606 str[i] = toupper(inputstr[i]);
607 i++;
608 }
609 str[i] = '\0';
610
611 if (!strcmp(str, "PYTHON_CRITICAL") || !strcmp(str, "CRITICAL")) {
612 return LTTNG_LOGLEVEL_PYTHON_CRITICAL;
613 } else if (!strcmp(str, "PYTHON_ERROR") || !strcmp(str, "ERROR")) {
614 return LTTNG_LOGLEVEL_PYTHON_ERROR;
615 } else if (!strcmp(str, "PYTHON_WARNING") || !strcmp(str, "WARNING")) {
616 return LTTNG_LOGLEVEL_PYTHON_WARNING;
617 } else if (!strcmp(str, "PYTHON_INFO") || !strcmp(str, "INFO")) {
618 return LTTNG_LOGLEVEL_PYTHON_INFO;
619 } else if (!strcmp(str, "PYTNON_DEBUG") || !strcmp(str, "DEBUG")) {
620 return LTTNG_LOGLEVEL_PYTHON_DEBUG;
621 } else if (!strcmp(str, "PYTHON_NOTSET") || !strcmp(str, "NOTSET")) {
622 return LTTNG_LOGLEVEL_PYTHON_NOTSET;
623 } else {
624 return -1;
625 }
626}
627
8005f29a 628/*
0c8477c8 629 * Maps loglevel from string to value
8005f29a
MD
630 */
631static
46839cc2 632int loglevel_str_to_value(const char *inputstr)
8005f29a 633{
46839cc2
MD
634 int i = 0;
635 char str[LTTNG_SYMBOL_NAME_LEN];
636
3712b110
JG
637 if (!inputstr || strlen(inputstr) == 0) {
638 return -1;
639 }
640
e051e07c
DG
641 /*
642 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
643 * added at the end of the loop so a the upper bound we avoid the overflow.
644 */
645 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
46839cc2
MD
646 str[i] = toupper(inputstr[i]);
647 i++;
648 }
649 str[i] = '\0';
650 if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) {
651 return LTTNG_LOGLEVEL_EMERG;
652 } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) {
653 return LTTNG_LOGLEVEL_ALERT;
654 } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) {
655 return LTTNG_LOGLEVEL_CRIT;
656 } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) {
657 return LTTNG_LOGLEVEL_ERR;
658 } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) {
659 return LTTNG_LOGLEVEL_WARNING;
660 } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) {
661 return LTTNG_LOGLEVEL_NOTICE;
662 } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) {
663 return LTTNG_LOGLEVEL_INFO;
664 } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) {
665 return LTTNG_LOGLEVEL_DEBUG_SYSTEM;
666 } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) {
667 return LTTNG_LOGLEVEL_DEBUG_PROGRAM;
668 } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) {
669 return LTTNG_LOGLEVEL_DEBUG_PROCESS;
670 } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) {
671 return LTTNG_LOGLEVEL_DEBUG_MODULE;
672 } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) {
673 return LTTNG_LOGLEVEL_DEBUG_UNIT;
674 } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) {
675 return LTTNG_LOGLEVEL_DEBUG_FUNCTION;
676 } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) {
677 return LTTNG_LOGLEVEL_DEBUG_LINE;
678 } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) {
679 return LTTNG_LOGLEVEL_DEBUG;
8005f29a
MD
680 } else {
681 return -1;
682 }
683}
684
85076754
MD
685static
686const char *print_channel_name(const char *name)
687{
688 return name ? : DEFAULT_CHANNEL_NAME;
689}
690
691static
692const char *print_raw_channel_name(const char *name)
693{
694 return name ? : "<default>";
695}
696
89476427
JRJ
697/*
698 * Mi print exlcusion list
699 */
700static
9f449915 701int mi_print_exclusion(char **names)
89476427
JRJ
702{
703 int i, ret;
9f449915 704 int count = names ? strutils_array_of_strings_len(names) : 0;
89476427
JRJ
705
706 assert(writer);
707
708 if (count == 0) {
709 ret = 0;
710 goto end;
711 }
712 ret = mi_lttng_writer_open_element(writer, config_element_exclusions);
713 if (ret) {
714 goto end;
715 }
716
717 for (i = 0; i < count; i++) {
718 ret = mi_lttng_writer_write_element_string(writer,
719 config_element_exclusion, names[i]);
720 if (ret) {
721 goto end;
722 }
723 }
724
725 /* Close exclusions element */
726 ret = mi_lttng_writer_close_element(writer);
727
728end:
729 return ret;
730}
731
9c48cab3
JI
732/*
733 * Return allocated string for pretty-printing exclusion names.
734 */
735static
9f449915 736char *print_exclusions(char **names)
9c48cab3
JI
737{
738 int length = 0;
739 int i;
740 const char *preamble = " excluding ";
741 char *ret;
9f449915 742 int count = names ? strutils_array_of_strings_len(names) : 0;
9c48cab3
JI
743
744 if (count == 0) {
745 return strdup("");
746 }
747
748 /* calculate total required length */
749 for (i = 0; i < count; i++) {
9f449915 750 length += strlen(names[i]) + 4;
9c48cab3
JI
751 }
752
753 /* add length of preamble + one for NUL - one for last (missing) comma */
754 length += strlen(preamble);
9f449915 755 ret = zmalloc(length + 1);
fd591b18
MD
756 if (!ret) {
757 return NULL;
758 }
9c48cab3
JI
759 strncpy(ret, preamble, length);
760 for (i = 0; i < count; i++) {
9f449915 761 strcat(ret, "\"");
9c48cab3 762 strcat(ret, names[i]);
9f449915 763 strcat(ret, "\"");
9c48cab3 764 if (i != count - 1) {
9f449915 765 strcat(ret, ", ");
9c48cab3
JI
766 }
767 }
89476427 768
9c48cab3
JI
769 return ret;
770}
771
748bde76 772static
9f449915 773int check_exclusion_subsets(const char *event_name, const char *exclusion)
748bde76 774{
9f449915
PP
775 bool warn = false;
776 int ret = 0;
777 const char *e = event_name;
778 const char *x = exclusion;
779
780 /* Scan both the excluder and the event letter by letter */
781 while (true) {
782 if (*e == '\\') {
783 if (*x != *e) {
784 warn = true;
785 goto end;
786 }
748bde76 787
9f449915
PP
788 e++;
789 x++;
790 goto cmp_chars;
791 }
792
793 if (*x == '*') {
794 /* Event is a subset of the excluder */
795 ERR("Event %s: %s excludes all events from %s",
796 event_name, exclusion, event_name);
797 goto error;
798 }
799
800 if (*e == '*') {
801 /*
802 * Reached the end of the event name before the
803 * end of the exclusion: this is valid.
804 */
805 goto end;
806 }
807
808cmp_chars:
809 if (*x != *e) {
810 warn = true;
811 break;
812 }
813
814 x++;
815 e++;
748bde76
JI
816 }
817
9f449915 818 goto end;
748bde76 819
9f449915
PP
820error:
821 ret = -1;
748bde76 822
9f449915
PP
823end:
824 if (warn) {
825 WARN("Event %s: %s does not exclude any events from %s",
826 event_name, exclusion, event_name);
827 }
748bde76 828
9f449915
PP
829 return ret;
830}
831
9f449915
PP
832static
833int create_exclusion_list_and_validate(const char *event_name,
834 const char *exclusions_arg,
835 char ***exclusion_list)
836{
837 int ret = 0;
838 char **exclusions = NULL;
839
840 /* Event name must be a valid globbing pattern to allow exclusions. */
841 if (!strutils_is_star_glob_pattern(event_name)) {
842 ERR("Event %s: Exclusions can only be used with a globbing pattern",
843 event_name);
844 goto error;
845 }
846
847 /* Split exclusions. */
848 exclusions = strutils_split(exclusions_arg, ',', true);
849 if (!exclusions) {
850 goto error;
851 }
852
853 /*
854 * If the event name is a star-at-end only globbing pattern,
855 * then we can validate the individual exclusions. Otherwise
856 * all exclusions are passed to the session daemon.
857 */
858 if (strutils_is_star_at_the_end_only_glob_pattern(event_name)) {
859 char * const *exclusion;
860
861 for (exclusion = exclusions; *exclusion; exclusion++) {
862 if (!strutils_is_star_glob_pattern(*exclusion) ||
863 strutils_is_star_at_the_end_only_glob_pattern(*exclusion)) {
8aa579d4 864 ret = check_exclusion_subsets(event_name, *exclusion);
9f449915 865 if (ret) {
5ef79758
MD
866 goto error;
867 }
748bde76 868 }
748bde76
JI
869 }
870 }
9f449915
PP
871
872 *exclusion_list = exclusions;
873
748bde76 874 goto end;
9f449915 875
748bde76 876error:
9f449915
PP
877 ret = -1;
878 strutils_free_null_terminated_array_of_strings(exclusions);
879
748bde76 880end:
748bde76
JI
881 return ret;
882}
502bbe89 883
9f449915
PP
884static void warn_on_truncated_exclusion_names(char * const *exclusion_list,
885 int *warn)
502bbe89 886{
9f449915 887 char * const *exclusion;
502bbe89 888
9f449915
PP
889 for (exclusion = exclusion_list; *exclusion; exclusion++) {
890 if (strlen(*exclusion) >= LTTNG_SYMBOL_NAME_LEN) {
502bbe89 891 WARN("Event exclusion \"%s\" will be truncated",
9f449915 892 *exclusion);
502bbe89
PP
893 *warn = 1;
894 }
895 }
896}
897
f3ed775e 898/*
6181537c 899 * Enabling event using the lttng API.
89476427 900 * Note: in case of error only the last error code will be return.
f3ed775e 901 */
cd80958d 902static int enable_events(char *session_name)
f3ed775e 903{
89476427
JRJ
904 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
905 int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
b73d0b29 906 char *event_name, *channel_name = NULL;
91744e14 907 struct lttng_event *ev;
7d29a247 908 struct lttng_domain dom;
7ed70bc9 909 char **exclusion_list = NULL;
f3ed775e 910
441c16a7
MD
911 memset(&dom, 0, sizeof(dom));
912
91744e14
FD
913 ev = lttng_event_create();
914 if (!ev) {
915 ret = CMD_ERROR;
916 goto error;
917 }
918
53a80697 919 if (opt_kernel) {
67b58630
JG
920 if (opt_loglevel) {
921 WARN("Kernel loglevels are not supported.");
922 }
53a80697
MD
923 }
924
7d29a247
DG
925 /* Create lttng domain */
926 if (opt_kernel) {
927 dom.type = LTTNG_DOMAIN_KERNEL;
7972aab2 928 dom.buf_type = LTTNG_BUFFER_GLOBAL;
d78d6610 929 } else if (opt_userspace) {
2bdd86d4 930 dom.type = LTTNG_DOMAIN_UST;
7972aab2 931 /* Default. */
8692d4e5 932 dom.buf_type = LTTNG_BUFFER_PER_UID;
b9dfb167
DG
933 } else if (opt_jul) {
934 dom.type = LTTNG_DOMAIN_JUL;
935 /* Default. */
936 dom.buf_type = LTTNG_BUFFER_PER_UID;
5cdb6027
DG
937 } else if (opt_log4j) {
938 dom.type = LTTNG_DOMAIN_LOG4J;
939 /* Default. */
940 dom.buf_type = LTTNG_BUFFER_PER_UID;
0e115563
DG
941 } else if (opt_python) {
942 dom.type = LTTNG_DOMAIN_PYTHON;
943 /* Default. */
944 dom.buf_type = LTTNG_BUFFER_PER_UID;
6181537c 945 } else {
3ecec76a
PP
946 /* Checked by the caller. */
947 assert(0);
2bdd86d4 948 }
7d29a247 949
8b08c525
AB
950 if (opt_exclude) {
951 switch (dom.type) {
952 case LTTNG_DOMAIN_KERNEL:
953 case LTTNG_DOMAIN_JUL:
954 case LTTNG_DOMAIN_LOG4J:
955 case LTTNG_DOMAIN_PYTHON:
956 ERR("Event name exclusions are not yet implemented for %s events",
957 get_domain_str(dom.type));
958 ret = CMD_ERROR;
959 goto error;
960 case LTTNG_DOMAIN_UST:
961 /* Exclusions supported */
962 break;
963 default:
964 assert(0);
965 }
d5dd17fd
JI
966 }
967
4fd2697f
FD
968 /*
969 * Adding a filter to a probe, function or userspace-probe would be
970 * denied by the kernel tracer as it's not supported at the moment. We
971 * do an early check here to warn the user.
972 */
973 if (opt_filter && opt_kernel) {
974 switch (opt_event_type) {
975 case LTTNG_EVENT_ALL:
976 case LTTNG_EVENT_TRACEPOINT:
977 case LTTNG_EVENT_SYSCALL:
978 break;
979 case LTTNG_EVENT_PROBE:
980 case LTTNG_EVENT_USERSPACE_PROBE:
981 case LTTNG_EVENT_FUNCTION:
982 ERR("Filter expressions are not supported for %s events",
983 get_event_type_str(opt_event_type));
984 ret = CMD_ERROR;
985 goto error;
986 default:
987 ret = CMD_UNDEFINED;
988 goto error;
989 }
990 }
991
85076754 992 channel_name = opt_channel_name;
ae856491 993
cd80958d
DG
994 handle = lttng_create_handle(session_name, &dom);
995 if (handle == NULL) {
996 ret = -1;
997 goto error;
998 }
1aef21b6 999
89476427
JRJ
1000 /* Prepare Mi */
1001 if (lttng_opt_mi) {
1002 /* Open a events element */
1003 ret = mi_lttng_writer_open_element(writer, config_element_events);
1004 if (ret) {
1005 ret = CMD_ERROR;
1006 goto error;
1007 }
1008 }
1009
cd80958d 1010 if (opt_enable_all) {
8c9ae521 1011 /* Default setup for enable all */
75e8c5ab 1012 if (opt_kernel) {
91744e14
FD
1013 ev->type = opt_event_type;
1014 strcpy(ev->name, "*");
300b8fd5 1015 /* kernel loglevels not implemented */
91744e14 1016 ev->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
75e8c5ab 1017 } else {
91744e14
FD
1018 ev->type = LTTNG_EVENT_TRACEPOINT;
1019 strcpy(ev->name, "*");
1020 ev->loglevel_type = opt_loglevel_type;
300b8fd5 1021 if (opt_loglevel) {
0e115563 1022 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
b2064f54 1023 if (opt_userspace) {
91744e14 1024 ev->loglevel = loglevel_str_to_value(opt_loglevel);
b2064f54 1025 } else if (opt_jul) {
91744e14 1026 ev->loglevel = loglevel_jul_str_to_value(opt_loglevel);
5cdb6027 1027 } else if (opt_log4j) {
91744e14 1028 ev->loglevel = loglevel_log4j_str_to_value(opt_loglevel);
0e115563 1029 } else if (opt_python) {
91744e14 1030 ev->loglevel = loglevel_python_str_to_value(opt_loglevel);
b2064f54 1031 }
91744e14 1032 if (ev->loglevel == -1) {
300b8fd5 1033 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 1034 ret = -LTTNG_ERR_INVALID;
300b8fd5
MD
1035 goto error;
1036 }
22e25b71 1037 } else {
0e115563 1038 assert(opt_userspace || opt_jul || opt_log4j || opt_python);
b2064f54 1039 if (opt_userspace) {
91744e14 1040 ev->loglevel = -1;
34aa3685 1041 } else if (opt_jul) {
91744e14 1042 ev->loglevel = LTTNG_LOGLEVEL_JUL_ALL;
34aa3685 1043 } else if (opt_log4j) {
91744e14 1044 ev->loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
0e115563 1045 } else if (opt_python) {
91744e14 1046 ev->loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
b2064f54 1047 }
300b8fd5 1048 }
75e8c5ab 1049 }
8c9ae521 1050
7ed70bc9 1051 if (opt_exclude) {
9f449915
PP
1052 ret = create_exclusion_list_and_validate("*",
1053 opt_exclude, &exclusion_list);
1054 if (ret) {
1055 ret = CMD_ERROR;
7ed70bc9
JI
1056 goto error;
1057 }
502bbe89 1058
91744e14 1059 ev->exclusion = 1;
502bbe89 1060 warn_on_truncated_exclusion_names(exclusion_list,
9f449915 1061 &warn);
7ed70bc9 1062 }
025faf73 1063 if (!opt_filter) {
7ed70bc9 1064 ret = lttng_enable_event_with_exclusions(handle,
91744e14 1065 ev, channel_name,
7ed70bc9 1066 NULL,
9f449915
PP
1067 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1068 exclusion_list);
025faf73
DG
1069 if (ret < 0) {
1070 switch (-ret) {
1071 case LTTNG_ERR_KERN_EVENT_EXIST:
1072 WARN("Kernel events already enabled (channel %s, session %s)",
85076754 1073 print_channel_name(channel_name), session_name);
89476427 1074 warn = 1;
025faf73 1075 break;
45d5d421
CB
1076 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1077 {
1078 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1079 ERR("Events: %s (channel %s, session %s)",
1080 msg,
1081 print_channel_name(channel_name),
1082 session_name);
1083 error = 1;
1084 break;
1085 }
025faf73
DG
1086 default:
1087 ERR("Events: %s (channel %s, session %s)",
85076754
MD
1088 lttng_strerror(ret),
1089 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
1090 ? print_raw_channel_name(channel_name)
1091 : print_channel_name(channel_name),
1092 session_name);
89476427 1093 error = 1;
025faf73
DG
1094 break;
1095 }
1096 goto end;
42224349 1097 }
8c9ae521 1098
025faf73
DG
1099 switch (opt_event_type) {
1100 case LTTNG_EVENT_TRACEPOINT:
67b58630 1101 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
9f449915 1102 char *exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1103
1104 if (!exclusion_string) {
1105 PERROR("Cannot allocate exclusion_string");
1106 error = 1;
1107 goto end;
1108 }
9c48cab3 1109 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
b9dfb167 1110 get_domain_str(dom.type),
9c48cab3 1111 exclusion_string,
85076754 1112 print_channel_name(channel_name),
025faf73 1113 opt_loglevel);
9c48cab3 1114 free(exclusion_string);
025faf73 1115 } else {
9f449915 1116 char *exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1117
1118 if (!exclusion_string) {
1119 PERROR("Cannot allocate exclusion_string");
1120 error = 1;
1121 goto end;
1122 }
9c48cab3 1123 MSG("All %s tracepoints%s are enabled in channel %s",
b9dfb167 1124 get_domain_str(dom.type),
9c48cab3 1125 exclusion_string,
85076754 1126 print_channel_name(channel_name));
9c48cab3 1127 free(exclusion_string);
025faf73
DG
1128 }
1129 break;
1130 case LTTNG_EVENT_SYSCALL:
1131 if (opt_kernel) {
6e911cad
MD
1132 MSG("All %s system calls are enabled in channel %s",
1133 get_domain_str(dom.type),
85076754 1134 print_channel_name(channel_name));
025faf73
DG
1135 }
1136 break;
1137 case LTTNG_EVENT_ALL:
67b58630 1138 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
9f449915 1139 char *exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1140
1141 if (!exclusion_string) {
1142 PERROR("Cannot allocate exclusion_string");
1143 error = 1;
1144 goto end;
1145 }
9c48cab3 1146 MSG("All %s events%s are enabled in channel %s for loglevel %s",
b9dfb167 1147 get_domain_str(dom.type),
9c48cab3 1148 exclusion_string,
85076754 1149 print_channel_name(channel_name),
025faf73 1150 opt_loglevel);
9c48cab3 1151 free(exclusion_string);
025faf73 1152 } else {
9f449915 1153 char *exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1154
1155 if (!exclusion_string) {
1156 PERROR("Cannot allocate exclusion_string");
1157 error = 1;
1158 goto end;
1159 }
9c48cab3 1160 MSG("All %s events%s are enabled in channel %s",
b9dfb167 1161 get_domain_str(dom.type),
9c48cab3 1162 exclusion_string,
85076754 1163 print_channel_name(channel_name));
9c48cab3 1164 free(exclusion_string);
025faf73
DG
1165 }
1166 break;
1167 default:
1168 /*
1169 * We should not be here since lttng_enable_event should have
1170 * failed on the event type.
1171 */
1172 goto error;
57064ada 1173 }
f3ed775e 1174 }
89476427 1175
025faf73 1176 if (opt_filter) {
91744e14 1177 command_ret = lttng_enable_event_with_exclusions(handle, ev, channel_name,
9f449915
PP
1178 opt_filter,
1179 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1180 exclusion_list);
89476427
JRJ
1181 if (command_ret < 0) {
1182 switch (-command_ret) {
16363652 1183 case LTTNG_ERR_FILTER_EXIST:
85076754 1184 WARN("Filter on all events is already enabled"
16363652 1185 " (channel %s, session %s)",
85076754 1186 print_channel_name(channel_name), session_name);
89476427 1187 warn = 1;
16363652 1188 break;
45d5d421
CB
1189 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1190 {
1191 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1192 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
1193 msg,
1194 print_channel_name(channel_name),
1195 session_name, opt_filter);
1196 error = 1;
1197 break;
1198 }
16363652 1199 default:
85076754 1200 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
da3d7d0e
JRJ
1201 lttng_strerror(command_ret),
1202 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
1203 ? print_raw_channel_name(channel_name)
1204 : print_channel_name(channel_name),
1205 session_name, opt_filter);
89476427 1206 error = 1;
16363652
DG
1207 break;
1208 }
89476427 1209 error_holder = command_ret;
16363652 1210 } else {
91744e14 1211 ev->filter = 1;
16363652
DG
1212 MSG("Filter '%s' successfully set", opt_filter);
1213 }
1214 }
89476427
JRJ
1215
1216 if (lttng_opt_mi) {
1217 /* The wildcard * is used for kernel and ust domain to
1218 * represent ALL. We copy * in event name to force the wildcard use
1219 * for kernel domain
1220 *
1221 * Note: this is strictly for semantic and printing while in
1222 * machine interface mode.
1223 */
91744e14 1224 strcpy(ev->name, "*");
89476427
JRJ
1225
1226 /* If we reach here the events are enabled */
1227 if (!error && !warn) {
91744e14 1228 ev->enabled = 1;
89476427 1229 } else {
91744e14 1230 ev->enabled = 0;
89476427
JRJ
1231 success = 0;
1232 }
91744e14 1233 ret = mi_lttng_event(writer, ev, 1, handle->domain.type);
89476427
JRJ
1234 if (ret) {
1235 ret = CMD_ERROR;
1236 goto error;
1237 }
1238
1239 /* print exclusion */
9f449915 1240 ret = mi_print_exclusion(exclusion_list);
89476427
JRJ
1241 if (ret) {
1242 ret = CMD_ERROR;
1243 goto error;
1244 }
1245
1246 /* Success ? */
1247 ret = mi_lttng_writer_write_element_bool(writer,
1248 mi_lttng_element_command_success, success);
1249 if (ret) {
1250 ret = CMD_ERROR;
1251 goto error;
1252 }
1253
1254 /* Close event element */
1255 ret = mi_lttng_writer_close_element(writer);
1256 if (ret) {
1257 ret = CMD_ERROR;
1258 goto error;
1259 }
1260 }
1261
8c9ae521 1262 goto end;
f3ed775e
DG
1263 }
1264
1265 /* Strip event list */
1266 event_name = strtok(opt_event_list, ",");
1267 while (event_name != NULL) {
6181537c 1268 /* Copy name and type of the event */
91744e14
FD
1269 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
1270 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1271 ev->type = opt_event_type;
6181537c 1272
f3ed775e
DG
1273 /* Kernel tracer action */
1274 if (opt_kernel) {
1275 DBG("Enabling kernel event %s for channel %s",
85076754
MD
1276 event_name,
1277 print_channel_name(channel_name));
f3ed775e
DG
1278
1279 switch (opt_event_type) {
29c62722
MD
1280 case LTTNG_EVENT_ALL: /* Enable tracepoints and syscalls */
1281 /* If event name differs from *, select tracepoint. */
91744e14
FD
1282 if (strcmp(ev->name, "*")) {
1283 ev->type = LTTNG_EVENT_TRACEPOINT;
29c62722
MD
1284 }
1285 break;
e6ddca71 1286 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 1287 break;
7d29a247 1288 case LTTNG_EVENT_PROBE:
91744e14 1289 ret = parse_probe_opts(ev, opt_probe);
49d4e302 1290 if (ret) {
cf0e5467 1291 ERR("Unable to parse probe options");
91744e14 1292 ret = CMD_ERROR;
0d63dd19
DG
1293 goto error;
1294 }
f3ed775e 1295 break;
dcabc190
FD
1296 case LTTNG_EVENT_USERSPACE_PROBE:
1297 ret = parse_userspace_probe_opts(ev, opt_userspace_probe);
1298 if (ret) {
1299 ERR("Unable to parse userspace probe options");
1300 ret = CMD_ERROR;
1301 goto error;
1302 }
1303 break;
f3ed775e 1304 case LTTNG_EVENT_FUNCTION:
91744e14 1305 ret = parse_probe_opts(ev, opt_function);
49d4e302 1306 if (ret) {
8f0d098b 1307 ERR("Unable to parse function probe options");
91744e14 1308 ret = CMD_ERROR;
8f0d098b
MD
1309 goto error;
1310 }
1311 break;
a54bd42d 1312 case LTTNG_EVENT_SYSCALL:
91744e14 1313 ev->type = LTTNG_EVENT_SYSCALL;
c6aa2d41 1314 break;
f3ed775e 1315 default:
1ab1ea0b 1316 ret = CMD_UNDEFINED;
f3ed775e
DG
1317 goto error;
1318 }
0cda4f28 1319
0cda4f28 1320 /* kernel loglevels not implemented */
91744e14 1321 ev->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
f3ed775e 1322 } else if (opt_userspace) { /* User-space tracer action */
300b8fd5 1323 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
85076754 1324 print_channel_name(channel_name), opt_loglevel ? : "<all>");
2bdd86d4
MD
1325
1326 switch (opt_event_type) {
1327 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
2bdd86d4
MD
1328 /* Fall-through */
1329 case LTTNG_EVENT_TRACEPOINT:
e4baff1e 1330 /* Copy name and type of the event */
91744e14
FD
1331 ev->type = LTTNG_EVENT_TRACEPOINT;
1332 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
1333 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2bdd86d4
MD
1334 break;
1335 case LTTNG_EVENT_PROBE:
1336 case LTTNG_EVENT_FUNCTION:
2bdd86d4 1337 case LTTNG_EVENT_SYSCALL:
dcabc190 1338 case LTTNG_EVENT_USERSPACE_PROBE:
2bdd86d4 1339 default:
cc62c0c0 1340 ERR("Event type not available for user-space tracing");
4ce78777 1341 ret = CMD_UNSUPPORTED;
2bdd86d4
MD
1342 goto error;
1343 }
0cda4f28 1344
7ed70bc9 1345 if (opt_exclude) {
91744e14 1346 ev->exclusion = 1;
d5dd17fd
JI
1347 if (opt_event_type != LTTNG_EVENT_ALL && opt_event_type != LTTNG_EVENT_TRACEPOINT) {
1348 ERR("Exclusion option can only be used with tracepoint events");
1349 ret = CMD_ERROR;
1350 goto error;
1351 }
7ed70bc9 1352 /* Free previously allocated items */
9f449915
PP
1353 strutils_free_null_terminated_array_of_strings(
1354 exclusion_list);
1355 exclusion_list = NULL;
1356 ret = create_exclusion_list_and_validate(
1357 event_name, opt_exclude,
1358 &exclusion_list);
1359 if (ret) {
1360 ret = CMD_ERROR;
7ed70bc9
JI
1361 goto error;
1362 }
502bbe89
PP
1363
1364 warn_on_truncated_exclusion_names(
9f449915 1365 exclusion_list, &warn);
7ed70bc9
JI
1366 }
1367
91744e14 1368 ev->loglevel_type = opt_loglevel_type;
ed7f4083 1369 if (opt_loglevel) {
91744e14
FD
1370 ev->loglevel = loglevel_str_to_value(opt_loglevel);
1371 if (ev->loglevel == -1) {
8005f29a 1372 ERR("Unknown loglevel %s", opt_loglevel);
2f70b271 1373 ret = -LTTNG_ERR_INVALID;
8005f29a
MD
1374 goto error;
1375 }
22e25b71 1376 } else {
91744e14 1377 ev->loglevel = -1;
ed7f4083 1378 }
0e115563 1379 } else if (opt_jul || opt_log4j || opt_python) {
b9dfb167
DG
1380 if (opt_event_type != LTTNG_EVENT_ALL &&
1381 opt_event_type != LTTNG_EVENT_TRACEPOINT) {
5cdb6027 1382 ERR("Event type not supported for domain.");
b9dfb167
DG
1383 ret = CMD_UNSUPPORTED;
1384 goto error;
1385 }
b2064f54 1386
91744e14 1387 ev->loglevel_type = opt_loglevel_type;
b2064f54 1388 if (opt_loglevel) {
5cdb6027 1389 if (opt_jul) {
91744e14 1390 ev->loglevel = loglevel_jul_str_to_value(opt_loglevel);
5cdb6027 1391 } else if (opt_log4j) {
91744e14 1392 ev->loglevel = loglevel_log4j_str_to_value(opt_loglevel);
0e115563 1393 } else if (opt_python) {
91744e14 1394 ev->loglevel = loglevel_python_str_to_value(opt_loglevel);
5cdb6027 1395 }
91744e14 1396 if (ev->loglevel == -1) {
b2064f54
DG
1397 ERR("Unknown loglevel %s", opt_loglevel);
1398 ret = -LTTNG_ERR_INVALID;
1399 goto error;
1400 }
1401 } else {
5cdb6027 1402 if (opt_jul) {
91744e14 1403 ev->loglevel = LTTNG_LOGLEVEL_JUL_ALL;
5cdb6027 1404 } else if (opt_log4j) {
91744e14 1405 ev->loglevel = LTTNG_LOGLEVEL_LOG4J_ALL;
0e115563 1406 } else if (opt_python) {
91744e14 1407 ev->loglevel = LTTNG_LOGLEVEL_PYTHON_DEBUG;
5cdb6027 1408 }
b2064f54 1409 }
91744e14
FD
1410 ev->type = LTTNG_EVENT_TRACEPOINT;
1411 strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
1412 ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 1413 } else {
3ecec76a 1414 assert(0);
f3ed775e
DG
1415 }
1416
025faf73 1417 if (!opt_filter) {
9c48cab3
JI
1418 char *exclusion_string;
1419
89476427 1420 command_ret = lttng_enable_event_with_exclusions(handle,
91744e14 1421 ev, channel_name,
9f449915
PP
1422 NULL,
1423 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1424 exclusion_list);
1425 exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1426 if (!exclusion_string) {
1427 PERROR("Cannot allocate exclusion_string");
1428 error = 1;
1429 goto end;
1430 }
89476427 1431 if (command_ret < 0) {
025faf73 1432 /* Turn ret to positive value to handle the positive error code */
89476427 1433 switch (-command_ret) {
025faf73 1434 case LTTNG_ERR_KERN_EVENT_EXIST:
9c48cab3 1435 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
85076754 1436 event_name,
9c48cab3 1437 exclusion_string,
85076754 1438 print_channel_name(channel_name), session_name);
89476427 1439 warn = 1;
025faf73 1440 break;
45d5d421
CB
1441 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1442 {
1443 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
1444 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1445 exclusion_string,
1446 msg,
1447 print_channel_name(channel_name),
1448 session_name);
1449 error = 1;
1450 break;
1451 }
dcabc190
FD
1452 case LTTNG_ERR_SDT_PROBE_SEMAPHORE:
1453 ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
1454 event_name, print_channel_name(channel_name),
1455 session_name);
1456 error = 1;
1457 break;
025faf73 1458 default:
9c48cab3
JI
1459 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
1460 exclusion_string,
da3d7d0e
JRJ
1461 lttng_strerror(command_ret),
1462 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
1463 ? print_raw_channel_name(channel_name)
1464 : print_channel_name(channel_name),
1465 session_name);
89476427 1466 error = 1;
025faf73
DG
1467 break;
1468 }
89476427 1469 error_holder = command_ret;
025faf73 1470 } else {
8274eeba
AB
1471 switch (dom.type) {
1472 case LTTNG_DOMAIN_KERNEL:
1473 case LTTNG_DOMAIN_UST:
49ceaa70 1474 MSG("%s event %s%s created in channel %s",
8274eeba
AB
1475 get_domain_str(dom.type),
1476 event_name,
1477 exclusion_string,
1478 print_channel_name(channel_name));
1479 break;
1480 case LTTNG_DOMAIN_JUL:
1481 case LTTNG_DOMAIN_LOG4J:
1482 case LTTNG_DOMAIN_PYTHON:
1483 /*
1484 * Don't print the default channel
1485 * name for agent domains.
1486 */
895707da 1487 MSG("%s event %s%s enabled",
8274eeba
AB
1488 get_domain_str(dom.type),
1489 event_name,
1490 exclusion_string);
1491 break;
1492 default:
1493 assert(0);
49ceaa70 1494 }
42224349 1495 }
9c48cab3 1496 free(exclusion_string);
6181537c 1497 }
025faf73
DG
1498
1499 if (opt_filter) {
9c48cab3
JI
1500 char *exclusion_string;
1501
89476427 1502 /* Filter present */
91744e14 1503 ev->filter = 1;
89476427 1504
91744e14 1505 command_ret = lttng_enable_event_with_exclusions(handle, ev, channel_name,
9f449915
PP
1506 opt_filter,
1507 exclusion_list ? strutils_array_of_strings_len(exclusion_list) : 0,
1508 exclusion_list);
1509 exclusion_string = print_exclusions(exclusion_list);
5ef79758
MD
1510 if (!exclusion_string) {
1511 PERROR("Cannot allocate exclusion_string");
1512 error = 1;
1513 goto end;
1514 }
89476427
JRJ
1515 if (command_ret < 0) {
1516 switch (-command_ret) {
7671f53c 1517 case LTTNG_ERR_FILTER_EXIST:
9c48cab3 1518 WARN("Filter on event %s%s is already enabled"
7671f53c 1519 " (channel %s, session %s)",
85076754 1520 event_name,
9c48cab3 1521 exclusion_string,
85076754 1522 print_channel_name(channel_name), session_name);
89476427 1523 warn = 1;
7671f53c 1524 break;
45d5d421
CB
1525 case LTTNG_ERR_TRACE_ALREADY_STARTED:
1526 {
1527 const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
91744e14 1528 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev->name,
45d5d421
CB
1529 exclusion_string,
1530 msg,
1531 print_channel_name(channel_name),
1532 session_name, opt_filter);
1533 error = 1;
1534 break;
1535 }
7671f53c 1536 default:
91744e14 1537 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev->name,
9c48cab3 1538 exclusion_string,
da3d7d0e
JRJ
1539 lttng_strerror(command_ret),
1540 command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME
85076754
MD
1541 ? print_raw_channel_name(channel_name)
1542 : print_channel_name(channel_name),
1543 session_name, opt_filter);
89476427 1544 error = 1;
7671f53c
CB
1545 break;
1546 }
89476427
JRJ
1547 error_holder = command_ret;
1548
16363652 1549 } else {
9c48cab3
JI
1550 MSG("Event %s%s: Filter '%s' successfully set",
1551 event_name, exclusion_string,
1552 opt_filter);
53a80697 1553 }
9c48cab3 1554 free(exclusion_string);
53a80697 1555 }
6181537c 1556
89476427
JRJ
1557 if (lttng_opt_mi) {
1558 if (command_ret) {
1559 success = 0;
91744e14 1560 ev->enabled = 0;
89476427 1561 } else {
91744e14 1562 ev->enabled = 1;
89476427
JRJ
1563 }
1564
91744e14 1565 ret = mi_lttng_event(writer, ev, 1, handle->domain.type);
89476427
JRJ
1566 if (ret) {
1567 ret = CMD_ERROR;
1568 goto error;
1569 }
1570
1571 /* print exclusion */
9f449915 1572 ret = mi_print_exclusion(exclusion_list);
89476427
JRJ
1573 if (ret) {
1574 ret = CMD_ERROR;
1575 goto error;
1576 }
1577
1578 /* Success ? */
1579 ret = mi_lttng_writer_write_element_bool(writer,
1580 mi_lttng_element_command_success, success);
1581 if (ret) {
1582 ret = CMD_ERROR;
1583 goto end;
1584 }
1585
1586 /* Close event element */
1587 ret = mi_lttng_writer_close_element(writer);
1588 if (ret) {
1589 ret = CMD_ERROR;
1590 goto end;
1591 }
1592 }
1593
f3ed775e
DG
1594 /* Next event */
1595 event_name = strtok(NULL, ",");
89476427
JRJ
1596 /* Reset warn, error and success */
1597 success = 1;
f3ed775e
DG
1598 }
1599
8c9ae521 1600end:
89476427
JRJ
1601 /* Close Mi */
1602 if (lttng_opt_mi) {
1603 /* Close events element */
1604 ret = mi_lttng_writer_close_element(writer);
1605 if (ret) {
1606 ret = CMD_ERROR;
1607 goto error;
1608 }
1609 }
f3ed775e 1610error:
ae856491
DG
1611 if (warn) {
1612 ret = CMD_WARNING;
1613 }
89476427
JRJ
1614 if (error) {
1615 ret = CMD_ERROR;
1616 }
cd80958d 1617 lttng_destroy_handle(handle);
9f449915 1618 strutils_free_null_terminated_array_of_strings(exclusion_list);
7ed70bc9 1619
89476427
JRJ
1620 /* Overwrite ret with error_holder if there was an actual error with
1621 * enabling an event.
1622 */
1623 ret = error_holder ? error_holder : ret;
1624
91744e14 1625 lttng_event_destroy(ev);
f3ed775e
DG
1626 return ret;
1627}
1628
1629/*
6181537c 1630 * Add event to trace session
f3ed775e
DG
1631 */
1632int cmd_enable_events(int argc, const char **argv)
1633{
89476427 1634 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
f3ed775e 1635 static poptContext pc;
cd80958d 1636 char *session_name = NULL;
68c7f6e5 1637 const char *leftover = NULL;
de044b7a 1638 int event_type = -1;
f3ed775e
DG
1639
1640 pc = poptGetContext(NULL, argc, argv, long_options, 0);
1641 poptReadDefaultConfig(pc, 0);
1642
1643 /* Default event type */
7a3d1328 1644 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
1645
1646 while ((opt = poptGetNextOpt(pc)) != -1) {
1647 switch (opt) {
1648 case OPT_HELP:
4ba92f18 1649 SHOW_HELP();
f3ed775e 1650 goto end;
f3ed775e 1651 case OPT_TRACEPOINT:
e6ddca71 1652 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e 1653 break;
cf0e5467 1654 case OPT_PROBE:
7d29a247 1655 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e 1656 break;
dcabc190
FD
1657 case OPT_USERSPACE_PROBE:
1658 opt_event_type = LTTNG_EVENT_USERSPACE_PROBE;
1659 break;
f3ed775e
DG
1660 case OPT_FUNCTION:
1661 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b 1662 break;
a54bd42d
MD
1663 case OPT_SYSCALL:
1664 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 1665 break;
eeac7d46
MD
1666 case OPT_USERSPACE:
1667 opt_userspace = 1;
eeac7d46 1668 break;
0cda4f28 1669 case OPT_LOGLEVEL:
8005f29a 1670 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
0cda4f28
MD
1671 opt_loglevel = poptGetOptArg(pc);
1672 break;
1673 case OPT_LOGLEVEL_ONLY:
8005f29a 1674 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
0cda4f28 1675 opt_loglevel = poptGetOptArg(pc);
13dce3b7 1676 break;
679b4943
SM
1677 case OPT_LIST_OPTIONS:
1678 list_cmd_options(stdout, long_options);
679b4943 1679 goto end;
53a80697
MD
1680 case OPT_FILTER:
1681 break;
fac3366c
JI
1682 case OPT_EXCLUDE:
1683 break;
f3ed775e 1684 default:
f3ed775e
DG
1685 ret = CMD_UNDEFINED;
1686 goto end;
1687 }
de044b7a
DG
1688
1689 /* Validate event type. Multiple event type are not supported. */
1690 if (event_type == -1) {
1691 event_type = opt_event_type;
1692 } else {
1693 if (event_type != opt_event_type) {
1694 ERR("Multiple event type not supported.");
1695 ret = CMD_ERROR;
1696 goto end;
1697 }
1698 }
f3ed775e
DG
1699 }
1700
3ecec76a
PP
1701 ret = print_missing_or_multiple_domains(
1702 opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python);
1703 if (ret) {
1704 ret = CMD_ERROR;
1705 goto end;
1706 }
1707
89476427
JRJ
1708 /* Mi check */
1709 if (lttng_opt_mi) {
1710 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
1711 if (!writer) {
1712 ret = -LTTNG_ERR_NOMEM;
1713 goto end;
1714 }
1715
1716 /* Open command element */
1717 ret = mi_lttng_writer_command_open(writer,
1718 mi_lttng_element_command_enable_event);
1719 if (ret) {
1720 ret = CMD_ERROR;
1721 goto end;
1722 }
1723
1724 /* Open output element */
1725 ret = mi_lttng_writer_open_element(writer,
1726 mi_lttng_element_command_output);
1727 if (ret) {
1728 ret = CMD_ERROR;
1729 goto end;
1730 }
1731 }
1732
f3ed775e
DG
1733 opt_event_list = (char*) poptGetArg(pc);
1734 if (opt_event_list == NULL && opt_enable_all == 0) {
1735 ERR("Missing event name(s).\n");
ca1c3607 1736 ret = CMD_ERROR;
f3ed775e
DG
1737 goto end;
1738 }
1739
68c7f6e5
JD
1740 leftover = poptGetArg(pc);
1741 if (leftover) {
1742 ERR("Unknown argument: %s", leftover);
1743 ret = CMD_ERROR;
1744 goto end;
1745 }
1746
cd80958d
DG
1747 if (!opt_session_name) {
1748 session_name = get_session_name();
1749 if (session_name == NULL) {
89476427
JRJ
1750 command_ret = CMD_ERROR;
1751 success = 0;
1752 goto mi_closing;
cd80958d
DG
1753 }
1754 } else {
1755 session_name = opt_session_name;
1756 }
1757
89476427
JRJ
1758 command_ret = enable_events(session_name);
1759 if (command_ret) {
1760 success = 0;
1761 goto mi_closing;
1762 }
1763
1764mi_closing:
1765 /* Mi closing */
1766 if (lttng_opt_mi) {
1767 /* Close output element */
1768 ret = mi_lttng_writer_close_element(writer);
1769 if (ret) {
1770 ret = CMD_ERROR;
1771 goto end;
1772 }
1773
1774 ret = mi_lttng_writer_write_element_bool(writer,
1775 mi_lttng_element_command_success, success);
1776 if (ret) {
1777 ret = CMD_ERROR;
1778 goto end;
1779 }
1780
1781 /* Command element close */
1782 ret = mi_lttng_writer_command_close(writer);
1783 if (ret) {
1784 ret = CMD_ERROR;
1785 goto end;
1786 }
1787 }
f3ed775e
DG
1788
1789end:
89476427
JRJ
1790 /* Mi clean-up */
1791 if (writer && mi_lttng_writer_destroy(writer)) {
1792 /* Preserve original error code */
1793 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
1794 }
1795
cd80958d
DG
1796 if (opt_session_name == NULL) {
1797 free(session_name);
1798 }
1799
89476427
JRJ
1800 /* Overwrite ret if an error occurred in enable_events */
1801 ret = command_ret ? command_ret : ret;
1802
ca1c3607 1803 poptFreeContext(pc);
f3ed775e
DG
1804 return ret;
1805}
9f449915 1806
This page took 0.144464 seconds and 4 git commands to generate.