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