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