2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
25 #include <sys/types.h>
30 #include <src/common/sessiond-comm/sessiond-comm.h>
33 #include <common/mi-lttng.h>
35 #include "../command.h"
37 #if (LTTNG_SYMBOL_NAME_LEN == 256)
38 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
41 static char *opt_event_list
;
42 static int opt_event_type
;
43 static const char *opt_loglevel
;
44 static int opt_loglevel_type
;
45 static int opt_kernel
;
46 static char *opt_session_name
;
47 static int opt_userspace
;
50 static int opt_enable_all
;
51 static char *opt_probe
;
52 static char *opt_function
;
53 static char *opt_function_entry_symbol
;
54 static char *opt_channel_name
;
55 static char *opt_filter
;
56 static char *opt_exclude
;
58 /* Not implemented yet */
59 static char *opt_cmd_name
;
78 static struct lttng_handle
*handle
;
79 static struct mi_writer
*writer
;
81 static struct poptOption long_options
[] = {
82 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
83 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
84 {"session", 's', POPT_ARG_STRING
, &opt_session_name
, 0, 0, 0},
85 {"all", 'a', POPT_ARG_VAL
, &opt_enable_all
, 1, 0, 0},
86 {"channel", 'c', POPT_ARG_STRING
, &opt_channel_name
, 0, 0, 0},
87 {"kernel", 'k', POPT_ARG_VAL
, &opt_kernel
, 1, 0, 0},
88 {"userspace", 'u', POPT_ARG_NONE
, 0, OPT_USERSPACE
, 0, 0},
89 {"jul", 'j', POPT_ARG_VAL
, &opt_jul
, 1, 0, 0},
90 {"log4j", 'l', POPT_ARG_VAL
, &opt_log4j
, 1, 0, 0},
91 {"tracepoint", 0, POPT_ARG_NONE
, 0, OPT_TRACEPOINT
, 0, 0},
92 {"probe", 0, POPT_ARG_STRING
, &opt_probe
, OPT_PROBE
, 0, 0},
93 {"function", 0, POPT_ARG_STRING
, &opt_function
, OPT_FUNCTION
, 0, 0},
96 * Currently removed from lttng kernel tracer. Removed from
97 * lttng UI to discourage its use.
99 {"function:entry", 0, POPT_ARG_STRING
, &opt_function_entry_symbol
, OPT_FUNCTION_ENTRY
, 0, 0},
101 {"syscall", 0, POPT_ARG_NONE
, 0, OPT_SYSCALL
, 0, 0},
102 {"loglevel", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL
, 0, 0},
103 {"loglevel-only", 0, POPT_ARG_STRING
, 0, OPT_LOGLEVEL_ONLY
, 0, 0},
104 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
105 {"filter", 'f', POPT_ARG_STRING
, &opt_filter
, OPT_FILTER
, 0, 0},
106 {"exclude", 'x', POPT_ARG_STRING
, &opt_exclude
, OPT_EXCLUDE
, 0, 0},
107 {0, 0, 0, 0, 0, 0, 0}
113 static void usage(FILE *ofp
)
115 fprintf(ofp
, "usage: lttng enable-event NAME[,NAME2,...] (-k | -u | -j | -l | -p) [OPTIONS] \n");
117 fprintf(ofp
, "Options:\n");
118 fprintf(ofp
, " -h, --help Show this help\n");
119 fprintf(ofp
, " --list-options Simple listing of options\n");
120 fprintf(ofp
, " -s, --session NAME Apply to session name\n");
121 fprintf(ofp
, " -c, --channel NAME Apply to this channel\n");
122 fprintf(ofp
, " -a, --all Enable all tracepoints and syscalls\n");
123 fprintf(ofp
, " -k, --kernel Apply for the kernel tracer\n");
124 fprintf(ofp
, " -u, --userspace Apply to the user-space tracer\n");
125 fprintf(ofp
, " -j, --jul Apply for Java application using JUL\n");
126 fprintf(ofp
, " -l, --log4j Apply for Java application using LOG4j\n");
128 fprintf(ofp
, "Event options:\n");
129 fprintf(ofp
, " --tracepoint Tracepoint event (default)\n");
130 fprintf(ofp
, " - userspace tracer supports wildcards at end of string.\n");
131 fprintf(ofp
, " Don't forget to quote to deal with bash expansion.\n");
132 fprintf(ofp
, " e.g.:\n");
133 fprintf(ofp
, " \"*\"\n");
134 fprintf(ofp
, " \"app_component:na*\"\n");
135 fprintf(ofp
, " --probe (addr | symbol | symbol+offset)\n");
136 fprintf(ofp
, " Dynamic probe.\n");
137 fprintf(ofp
, " Addr and offset can be octal (0NNN...),\n");
138 fprintf(ofp
, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
139 fprintf(ofp
, " --function (addr | symbol | symbol+offset)\n");
140 fprintf(ofp
, " Dynamic function entry/return probe.\n");
141 fprintf(ofp
, " Addr and offset can be octal (0NNN...),\n");
142 fprintf(ofp
, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
144 fprintf(ofp
, " --function:entry symbol\n");
145 fprintf(ofp
, " Function tracer event\n");
147 fprintf(ofp
, " --syscall System call event\n");
149 fprintf(ofp
, " --loglevel name\n");
150 fprintf(ofp
, " Tracepoint loglevel range from 0 to loglevel.\n");
151 fprintf(ofp
, " For JUL/LOG4j domain, see the table below for the range values.\n");
152 fprintf(ofp
, " --loglevel-only name\n");
153 fprintf(ofp
, " Tracepoint loglevel (only this loglevel)\n");
155 fprintf(ofp
, " The loglevel or loglevel-only options should be\n");
156 fprintf(ofp
, " combined with a tracepoint name or tracepoint\n");
157 fprintf(ofp
, " wildcard.\n");
158 fprintf(ofp
, " Available loglevels:\n");
159 fprintf(ofp
, " (higher value is more verbose)\n");
160 fprintf(ofp
, " TRACE_EMERG = 0\n");
161 fprintf(ofp
, " TRACE_ALERT = 1\n");
162 fprintf(ofp
, " TRACE_CRIT = 2\n");
163 fprintf(ofp
, " TRACE_ERR = 3\n");
164 fprintf(ofp
, " TRACE_WARNING = 4\n");
165 fprintf(ofp
, " TRACE_NOTICE = 5\n");
166 fprintf(ofp
, " TRACE_INFO = 6\n");
167 fprintf(ofp
, " TRACE_DEBUG_SYSTEM = 7\n");
168 fprintf(ofp
, " TRACE_DEBUG_PROGRAM = 8\n");
169 fprintf(ofp
, " TRACE_DEBUG_PROCESS = 9\n");
170 fprintf(ofp
, " TRACE_DEBUG_MODULE = 10\n");
171 fprintf(ofp
, " TRACE_DEBUG_UNIT = 11\n");
172 fprintf(ofp
, " TRACE_DEBUG_FUNCTION = 12\n");
173 fprintf(ofp
, " TRACE_DEBUG_LINE = 13\n");
174 fprintf(ofp
, " TRACE_DEBUG = 14\n");
175 fprintf(ofp
, " (shortcuts such as \"system\" are allowed)\n");
177 fprintf(ofp
, " Available JUL domain loglevels:\n");
178 fprintf(ofp
, " JUL_OFF = INT32_MAX\n");
179 fprintf(ofp
, " JUL_SEVERE = %d\n", LTTNG_LOGLEVEL_JUL_SEVERE
);
180 fprintf(ofp
, " JUL_WARNING = %d\n", LTTNG_LOGLEVEL_JUL_WARNING
);
181 fprintf(ofp
, " JUL_INFO = %d\n", LTTNG_LOGLEVEL_JUL_INFO
);
182 fprintf(ofp
, " JUL_CONFIG = %d\n", LTTNG_LOGLEVEL_JUL_CONFIG
);
183 fprintf(ofp
, " JUL_FINE = %d\n", LTTNG_LOGLEVEL_JUL_FINE
);
184 fprintf(ofp
, " JUL_FINER = %d\n", LTTNG_LOGLEVEL_JUL_FINER
);
185 fprintf(ofp
, " JUL_FINEST = %d\n", LTTNG_LOGLEVEL_JUL_FINEST
);
186 fprintf(ofp
, " JUL_ALL = INT32_MIN\n");
187 fprintf(ofp
, " (shortcuts such as \"severe\" are allowed)\n");
189 fprintf(ofp
, " Available LOG4j domain loglevels:\n");
190 fprintf(ofp
, " LOG4J_OFF = INT32_MAX\n");
191 fprintf(ofp
, " LOG4J_FATAL = %d\n", LTTNG_LOGLEVEL_LOG4J_FATAL
);
192 fprintf(ofp
, " LOG4J_ERROR = %d\n", LTTNG_LOGLEVEL_LOG4J_ERROR
);
193 fprintf(ofp
, " LOG4J_WARN = %d\n", LTTNG_LOGLEVEL_LOG4J_WARN
);
194 fprintf(ofp
, " LOG4J_INFO = %d\n", LTTNG_LOGLEVEL_LOG4J_INFO
);
195 fprintf(ofp
, " LOG4J_DEBUG = %d\n", LTTNG_LOGLEVEL_LOG4J_DEBUG
);
196 fprintf(ofp
, " LOG4J_TRACE = %d\n", LTTNG_LOGLEVEL_LOG4J_TRACE
);
197 fprintf(ofp
, " LOG4J_ALL = INT32_MIN\n");
198 fprintf(ofp
, " (shortcuts such as \"severe\" are allowed)\n");
200 fprintf(ofp
, " -f, --filter \'expression\'\n");
201 fprintf(ofp
, " Filter expression on event fields and context.\n");
202 fprintf(ofp
, " Event recording depends on evaluation.\n");
203 fprintf(ofp
, " Only specify on first activation of\n");
204 fprintf(ofp
, " a given event within a session.\n");
205 fprintf(ofp
, " Filter only allowed when enabling\n");
206 fprintf(ofp
, " events within a session before tracing\n");
207 fprintf(ofp
, " is started. If the filter fails to link\n");
208 fprintf(ofp
, " with the event within the traced domain,\n");
209 fprintf(ofp
, " the event will be discarded. Currently,\n");
210 fprintf(ofp
, " filter is only implemented for the user-space\n");
211 fprintf(ofp
, " tracer.\n");
212 fprintf(ofp
, " Expression examples:.\n");
214 fprintf(ofp
, " 'intfield > 500 && intfield < 503'\n");
215 fprintf(ofp
, " '(strfield == \"test\" || intfield != 10) && intfield > 33'\n");
216 fprintf(ofp
, " 'doublefield > 1.1 && intfield < 5.3'\n");
218 fprintf(ofp
, " Wildcards are allowed at the end of strings:\n");
219 fprintf(ofp
, " 'seqfield1 == \"te*\"'\n");
220 fprintf(ofp
, " In string literals, the escape character is '\\'.\n");
221 fprintf(ofp
, " Use '\\*' for the '*' character, and '\\\\' for\n");
222 fprintf(ofp
, " the '\\' character. Wildcard match any sequence of,\n");
223 fprintf(ofp
, " characters including an empty sub-string (match 0 or\n");
224 fprintf(ofp
, " more characters).\n");
226 fprintf(ofp
, " Context information can be used for filtering. The\n");
227 fprintf(ofp
, " examples below show usage of context filtering on\n");
228 fprintf(ofp
, " process name (with a wildcard), process ID range, and\n");
229 fprintf(ofp
, " unique thread ID for filtering. The process and\n");
230 fprintf(ofp
, " thread ID of running applications can be found under\n");
231 fprintf(ofp
, " columns \"PID\" and \"LWP\" of the \"ps -eLf\" command.\n");
233 fprintf(ofp
, " '$ctx.procname == \"demo*\"'\n");
234 fprintf(ofp
, " '$ctx.vpid >= 4433 && $ctx.vpid < 4455'\n");
235 fprintf(ofp
, " '$ctx.vtid == 1234'\n");
236 fprintf(ofp
, " -x, --exclude LIST\n");
237 fprintf(ofp
, " Add exclusions to UST tracepoints:\n");
238 fprintf(ofp
, " Events that match any of the items\n");
239 fprintf(ofp
, " in the comma-separated LIST are not\n");
240 fprintf(ofp
, " enabled, even if they match a wildcard\n");
241 fprintf(ofp
, " definition of the event.\n");
246 * Parse probe options.
248 static int parse_probe_opts(struct lttng_event
*ev
, char *opt
)
250 int ret
= CMD_SUCCESS
;
253 #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18" /* 18 is (19 - 1) (\0 is extra) */
254 char name
[LTTNG_SYMBOL_NAME_LEN
];
261 /* Check for symbol+offset */
262 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
263 "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", name
, s_hex
);
265 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
266 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
267 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
268 if (*s_hex
== '\0') {
269 ERR("Invalid probe offset %s", s_hex
);
273 ev
->attr
.probe
.offset
= strtoul(s_hex
, NULL
, 0);
274 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
275 ev
->attr
.probe
.addr
= 0;
279 /* Check for symbol */
280 if (isalpha(name
[0])) {
281 match
= sscanf(opt
, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
"s",
284 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
285 ev
->attr
.probe
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
286 DBG("probe symbol %s", ev
->attr
.probe
.symbol_name
);
287 ev
->attr
.probe
.offset
= 0;
288 DBG("probe offset %" PRIu64
, ev
->attr
.probe
.offset
);
289 ev
->attr
.probe
.addr
= 0;
294 /* Check for address */
295 match
= sscanf(opt
, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API
"s", s_hex
);
297 if (*s_hex
== '\0') {
298 ERR("Invalid probe address %s", s_hex
);
302 ev
->attr
.probe
.addr
= strtoul(s_hex
, NULL
, 0);
303 DBG("probe addr %" PRIu64
, ev
->attr
.probe
.addr
);
304 ev
->attr
.probe
.offset
= 0;
305 memset(ev
->attr
.probe
.symbol_name
, 0, LTTNG_SYMBOL_NAME_LEN
);
317 * Maps LOG4j loglevel from string to value
319 static int loglevel_log4j_str_to_value(const char *inputstr
)
322 char str
[LTTNG_SYMBOL_NAME_LEN
];
325 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
326 * added at the end of the loop so a the upper bound we avoid the overflow.
328 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
329 str
[i
] = toupper(inputstr
[i
]);
334 if (!strcmp(str
, "LOG4J_OFF") || !strcmp(str
, "OFF")) {
335 return LTTNG_LOGLEVEL_LOG4J_OFF
;
336 } else if (!strcmp(str
, "LOG4J_FATAL") || !strcmp(str
, "FATAL")) {
337 return LTTNG_LOGLEVEL_LOG4J_FATAL
;
338 } else if (!strcmp(str
, "LOG4J_ERROR") || !strcmp(str
, "ERROR")) {
339 return LTTNG_LOGLEVEL_LOG4J_ERROR
;
340 } else if (!strcmp(str
, "LOG4J_WARN") || !strcmp(str
, "WARN")) {
341 return LTTNG_LOGLEVEL_LOG4J_WARN
;
342 } else if (!strcmp(str
, "LOG4J_INFO") || !strcmp(str
, "INFO")) {
343 return LTTNG_LOGLEVEL_LOG4J_INFO
;
344 } else if (!strcmp(str
, "LOG4J_DEBUG") || !strcmp(str
, "DEBUG")) {
345 return LTTNG_LOGLEVEL_LOG4J_DEBUG
;
346 } else if (!strcmp(str
, "LOG4J_TRACE") || !strcmp(str
, "TRACE")) {
347 return LTTNG_LOGLEVEL_LOG4J_TRACE
;
348 } else if (!strcmp(str
, "LOG4J_ALL") || !strcmp(str
, "ALL")) {
349 return LTTNG_LOGLEVEL_LOG4J_ALL
;
356 * Maps JUL loglevel from string to value
358 static int loglevel_jul_str_to_value(const char *inputstr
)
361 char str
[LTTNG_SYMBOL_NAME_LEN
];
364 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
365 * added at the end of the loop so a the upper bound we avoid the overflow.
367 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
368 str
[i
] = toupper(inputstr
[i
]);
373 if (!strcmp(str
, "JUL_OFF") || !strcmp(str
, "OFF")) {
374 return LTTNG_LOGLEVEL_JUL_OFF
;
375 } else if (!strcmp(str
, "JUL_SEVERE") || !strcmp(str
, "SEVERE")) {
376 return LTTNG_LOGLEVEL_JUL_SEVERE
;
377 } else if (!strcmp(str
, "JUL_WARNING") || !strcmp(str
, "WARNING")) {
378 return LTTNG_LOGLEVEL_JUL_WARNING
;
379 } else if (!strcmp(str
, "JUL_INFO") || !strcmp(str
, "INFO")) {
380 return LTTNG_LOGLEVEL_JUL_INFO
;
381 } else if (!strcmp(str
, "JUL_CONFIG") || !strcmp(str
, "CONFIG")) {
382 return LTTNG_LOGLEVEL_JUL_CONFIG
;
383 } else if (!strcmp(str
, "JUL_FINE") || !strcmp(str
, "FINE")) {
384 return LTTNG_LOGLEVEL_JUL_FINE
;
385 } else if (!strcmp(str
, "JUL_FINER") || !strcmp(str
, "FINER")) {
386 return LTTNG_LOGLEVEL_JUL_FINER
;
387 } else if (!strcmp(str
, "JUL_FINEST") || !strcmp(str
, "FINEST")) {
388 return LTTNG_LOGLEVEL_JUL_FINEST
;
389 } else if (!strcmp(str
, "JUL_ALL") || !strcmp(str
, "ALL")) {
390 return LTTNG_LOGLEVEL_JUL_ALL
;
397 * Maps loglevel from string to value
400 int loglevel_str_to_value(const char *inputstr
)
403 char str
[LTTNG_SYMBOL_NAME_LEN
];
406 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
407 * added at the end of the loop so a the upper bound we avoid the overflow.
409 while (i
< (LTTNG_SYMBOL_NAME_LEN
- 1) && inputstr
[i
] != '\0') {
410 str
[i
] = toupper(inputstr
[i
]);
414 if (!strcmp(str
, "TRACE_EMERG") || !strcmp(str
, "EMERG")) {
415 return LTTNG_LOGLEVEL_EMERG
;
416 } else if (!strcmp(str
, "TRACE_ALERT") || !strcmp(str
, "ALERT")) {
417 return LTTNG_LOGLEVEL_ALERT
;
418 } else if (!strcmp(str
, "TRACE_CRIT") || !strcmp(str
, "CRIT")) {
419 return LTTNG_LOGLEVEL_CRIT
;
420 } else if (!strcmp(str
, "TRACE_ERR") || !strcmp(str
, "ERR")) {
421 return LTTNG_LOGLEVEL_ERR
;
422 } else if (!strcmp(str
, "TRACE_WARNING") || !strcmp(str
, "WARNING")) {
423 return LTTNG_LOGLEVEL_WARNING
;
424 } else if (!strcmp(str
, "TRACE_NOTICE") || !strcmp(str
, "NOTICE")) {
425 return LTTNG_LOGLEVEL_NOTICE
;
426 } else if (!strcmp(str
, "TRACE_INFO") || !strcmp(str
, "INFO")) {
427 return LTTNG_LOGLEVEL_INFO
;
428 } else if (!strcmp(str
, "TRACE_DEBUG_SYSTEM") || !strcmp(str
, "DEBUG_SYSTEM") || !strcmp(str
, "SYSTEM")) {
429 return LTTNG_LOGLEVEL_DEBUG_SYSTEM
;
430 } else if (!strcmp(str
, "TRACE_DEBUG_PROGRAM") || !strcmp(str
, "DEBUG_PROGRAM") || !strcmp(str
, "PROGRAM")) {
431 return LTTNG_LOGLEVEL_DEBUG_PROGRAM
;
432 } else if (!strcmp(str
, "TRACE_DEBUG_PROCESS") || !strcmp(str
, "DEBUG_PROCESS") || !strcmp(str
, "PROCESS")) {
433 return LTTNG_LOGLEVEL_DEBUG_PROCESS
;
434 } else if (!strcmp(str
, "TRACE_DEBUG_MODULE") || !strcmp(str
, "DEBUG_MODULE") || !strcmp(str
, "MODULE")) {
435 return LTTNG_LOGLEVEL_DEBUG_MODULE
;
436 } else if (!strcmp(str
, "TRACE_DEBUG_UNIT") || !strcmp(str
, "DEBUG_UNIT") || !strcmp(str
, "UNIT")) {
437 return LTTNG_LOGLEVEL_DEBUG_UNIT
;
438 } else if (!strcmp(str
, "TRACE_DEBUG_FUNCTION") || !strcmp(str
, "DEBUG_FUNCTION") || !strcmp(str
, "FUNCTION")) {
439 return LTTNG_LOGLEVEL_DEBUG_FUNCTION
;
440 } else if (!strcmp(str
, "TRACE_DEBUG_LINE") || !strcmp(str
, "DEBUG_LINE") || !strcmp(str
, "LINE")) {
441 return LTTNG_LOGLEVEL_DEBUG_LINE
;
442 } else if (!strcmp(str
, "TRACE_DEBUG") || !strcmp(str
, "DEBUG")) {
443 return LTTNG_LOGLEVEL_DEBUG
;
450 const char *print_channel_name(const char *name
)
452 return name
? : DEFAULT_CHANNEL_NAME
;
456 const char *print_raw_channel_name(const char *name
)
458 return name
? : "<default>";
462 * Mi print exlcusion list
465 int mi_print_exclusion(int count
, char **names
)
475 ret
= mi_lttng_writer_open_element(writer
, config_element_exclusions
);
480 for (i
= 0; i
< count
; i
++) {
481 ret
= mi_lttng_writer_write_element_string(writer
,
482 config_element_exclusion
, names
[i
]);
488 /* Close exclusions element */
489 ret
= mi_lttng_writer_close_element(writer
);
496 * Return allocated string for pretty-printing exclusion names.
499 char *print_exclusions(int count
, char **names
)
503 const char *preamble
= " excluding ";
510 /* calculate total required length */
511 for (i
= 0; i
< count
; i
++) {
512 length
+= strlen(names
[i
]) + 1;
515 /* add length of preamble + one for NUL - one for last (missing) comma */
516 length
+= strlen(preamble
);
517 ret
= zmalloc(length
);
521 strncpy(ret
, preamble
, length
);
522 for (i
= 0; i
< count
; i
++) {
523 strcat(ret
, names
[i
]);
524 if (i
!= count
- 1) {
533 * Compare list of exclusions against an event name.
534 * Return a list of legal exclusion names.
535 * Produce an error or a warning about others (depending on the situation)
538 int check_exclusion_subsets(const char *event_name
,
539 const char *exclusions
,
540 int *exclusion_count_ptr
,
541 char ***exclusion_list_ptr
)
543 const char *excluder_ptr
;
544 const char *event_ptr
;
545 const char *next_excluder
;
547 int exclusion_count
= 0;
548 char **exclusion_list
= NULL
;
549 int ret
= CMD_SUCCESS
;
551 if (event_name
[strlen(event_name
) - 1] != '*') {
552 ERR("Event %s: Excluders can only be used with wildcarded events", event_name
);
556 next_excluder
= exclusions
;
557 while (*next_excluder
!= 0) {
558 event_ptr
= event_name
;
559 excluder_ptr
= next_excluder
;
560 excluder_length
= strcspn(next_excluder
, ",");
562 /* Scan both the excluder and the event letter by letter */
570 /* Event is a subset of the excluder */
571 ERR("Event %s: %.*s excludes all events from %s",
580 char **new_exclusion_list
;
582 /* Excluder is a proper subset of event */
583 string
= strndup(next_excluder
, excluder_length
);
585 PERROR("strndup error");
588 new_exclusion_list
= realloc(exclusion_list
,
589 sizeof(char *) * (exclusion_count
+ 1));
590 if (!new_exclusion_list
) {
595 exclusion_list
= new_exclusion_list
;
597 exclusion_list
[exclusion_count
- 1] = string
;
601 /* Excluder and event sets have no common elements */
602 WARN("Event %s: %.*s does not exclude any events from %s",
613 next_excluder
+= excluder_length
;
614 if (*next_excluder
== ',') {
620 while (exclusion_count
--) {
621 free(exclusion_list
[exclusion_count
]);
623 if (exclusion_list
!= NULL
) {
624 free(exclusion_list
);
626 exclusion_list
= NULL
;
630 *exclusion_count_ptr
= exclusion_count
;
631 *exclusion_list_ptr
= exclusion_list
;
635 * Enabling event using the lttng API.
636 * Note: in case of error only the last error code will be return.
638 static int enable_events(char *session_name
)
640 int ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
;
641 int error_holder
= CMD_SUCCESS
, warn
= 0, error
= 0, success
= 1;
642 char *event_name
, *channel_name
= NULL
;
643 struct lttng_event ev
;
644 struct lttng_domain dom
;
645 int exclusion_count
= 0;
646 char **exclusion_list
= NULL
;
648 memset(&ev
, 0, sizeof(ev
));
649 memset(&dom
, 0, sizeof(dom
));
653 ERR("Filter not implement for kernel tracing yet");
658 WARN("Kernel loglevels are not supported.");
662 /* Create lttng domain */
664 dom
.type
= LTTNG_DOMAIN_KERNEL
;
665 dom
.buf_type
= LTTNG_BUFFER_GLOBAL
;
666 } else if (opt_userspace
) {
667 dom
.type
= LTTNG_DOMAIN_UST
;
669 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
670 } else if (opt_jul
) {
671 dom
.type
= LTTNG_DOMAIN_JUL
;
673 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
674 } else if (opt_log4j
) {
675 dom
.type
= LTTNG_DOMAIN_LOG4J
;
677 dom
.buf_type
= LTTNG_BUFFER_PER_UID
;
679 /* Checked by the caller. */
685 case LTTNG_DOMAIN_KERNEL
:
686 case LTTNG_DOMAIN_JUL
:
687 case LTTNG_DOMAIN_LOG4J
:
688 ERR("Event name exclusions are not yet implemented for %s events",
689 get_domain_str(dom
.type
));
692 case LTTNG_DOMAIN_UST
:
693 /* Exclusions supported */
700 channel_name
= opt_channel_name
;
702 handle
= lttng_create_handle(session_name
, &dom
);
703 if (handle
== NULL
) {
710 /* Open a events element */
711 ret
= mi_lttng_writer_open_element(writer
, config_element_events
);
718 if (opt_enable_all
) {
719 /* Default setup for enable all */
721 ev
.type
= opt_event_type
;
723 /* kernel loglevels not implemented */
724 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
726 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
727 strcpy(ev
.name
, "*");
728 ev
.loglevel_type
= opt_loglevel_type
;
730 assert(opt_userspace
|| opt_jul
|| opt_log4j
);
732 ev
.loglevel
= loglevel_str_to_value(opt_loglevel
);
733 } else if (opt_jul
) {
734 ev
.loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
735 } else if (opt_log4j
) {
736 ev
.loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
738 if (ev
.loglevel
== -1) {
739 ERR("Unknown loglevel %s", opt_loglevel
);
740 ret
= -LTTNG_ERR_INVALID
;
744 assert(opt_userspace
|| opt_jul
|| opt_log4j
);
747 } else if (opt_jul
|| opt_log4j
) {
748 ev
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
754 ret
= check_exclusion_subsets("*", opt_exclude
,
755 &exclusion_count
, &exclusion_list
);
756 if (ret
== CMD_ERROR
) {
762 ret
= lttng_enable_event_with_exclusions(handle
,
765 exclusion_count
, exclusion_list
);
768 case LTTNG_ERR_KERN_EVENT_EXIST
:
769 WARN("Kernel events already enabled (channel %s, session %s)",
770 print_channel_name(channel_name
), session_name
);
773 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
775 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
776 ERR("Events: %s (channel %s, session %s)",
778 print_channel_name(channel_name
),
784 ERR("Events: %s (channel %s, session %s)",
786 ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
787 ? print_raw_channel_name(channel_name
)
788 : print_channel_name(channel_name
),
796 switch (opt_event_type
) {
797 case LTTNG_EVENT_TRACEPOINT
:
798 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
799 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
801 if (!exclusion_string
) {
802 PERROR("Cannot allocate exclusion_string");
806 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
807 get_domain_str(dom
.type
),
809 print_channel_name(channel_name
),
811 free(exclusion_string
);
813 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
815 if (!exclusion_string
) {
816 PERROR("Cannot allocate exclusion_string");
820 MSG("All %s tracepoints%s are enabled in channel %s",
821 get_domain_str(dom
.type
),
823 print_channel_name(channel_name
));
824 free(exclusion_string
);
827 case LTTNG_EVENT_SYSCALL
:
829 MSG("All %s system calls are enabled in channel %s",
830 get_domain_str(dom
.type
),
831 print_channel_name(channel_name
));
834 case LTTNG_EVENT_ALL
:
835 if (opt_loglevel
&& dom
.type
!= LTTNG_DOMAIN_KERNEL
) {
836 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
838 if (!exclusion_string
) {
839 PERROR("Cannot allocate exclusion_string");
843 MSG("All %s events%s are enabled in channel %s for loglevel %s",
844 get_domain_str(dom
.type
),
846 print_channel_name(channel_name
),
848 free(exclusion_string
);
850 char *exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
852 if (!exclusion_string
) {
853 PERROR("Cannot allocate exclusion_string");
857 MSG("All %s events%s are enabled in channel %s",
858 get_domain_str(dom
.type
),
860 print_channel_name(channel_name
));
861 free(exclusion_string
);
866 * We should not be here since lttng_enable_event should have
867 * failed on the event type.
874 command_ret
= lttng_enable_event_with_exclusions(handle
, &ev
, channel_name
,
875 opt_filter
, exclusion_count
, exclusion_list
);
876 if (command_ret
< 0) {
877 switch (-command_ret
) {
878 case LTTNG_ERR_FILTER_EXIST
:
879 WARN("Filter on all events is already enabled"
880 " (channel %s, session %s)",
881 print_channel_name(channel_name
), session_name
);
884 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
886 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
887 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
889 print_channel_name(channel_name
),
890 session_name
, opt_filter
);
895 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
896 lttng_strerror(command_ret
),
897 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
898 ? print_raw_channel_name(channel_name
)
899 : print_channel_name(channel_name
),
900 session_name
, opt_filter
);
904 error_holder
= command_ret
;
907 MSG("Filter '%s' successfully set", opt_filter
);
912 /* The wildcard * is used for kernel and ust domain to
913 * represent ALL. We copy * in event name to force the wildcard use
916 * Note: this is strictly for semantic and printing while in
917 * machine interface mode.
919 strcpy(ev
.name
, "*");
921 /* If we reach here the events are enabled */
922 if (!error
&& !warn
) {
928 ret
= mi_lttng_event(writer
, &ev
, 1, handle
->domain
.type
);
934 /* print exclusion */
935 ret
= mi_print_exclusion(exclusion_count
, exclusion_list
);
942 ret
= mi_lttng_writer_write_element_bool(writer
,
943 mi_lttng_element_command_success
, success
);
949 /* Close event element */
950 ret
= mi_lttng_writer_close_element(writer
);
960 /* Strip event list */
961 event_name
= strtok(opt_event_list
, ",");
962 while (event_name
!= NULL
) {
963 /* Copy name and type of the event */
964 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
965 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
966 ev
.type
= opt_event_type
;
968 /* Kernel tracer action */
970 DBG("Enabling kernel event %s for channel %s",
972 print_channel_name(channel_name
));
974 switch (opt_event_type
) {
975 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
976 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
978 case LTTNG_EVENT_TRACEPOINT
:
980 case LTTNG_EVENT_PROBE
:
981 ret
= parse_probe_opts(&ev
, opt_probe
);
983 ERR("Unable to parse probe options");
988 case LTTNG_EVENT_FUNCTION
:
989 ret
= parse_probe_opts(&ev
, opt_function
);
991 ERR("Unable to parse function probe options");
996 case LTTNG_EVENT_FUNCTION_ENTRY
:
997 strncpy(ev
.attr
.ftrace
.symbol_name
, opt_function_entry_symbol
,
998 LTTNG_SYMBOL_NAME_LEN
);
999 ev
.attr
.ftrace
.symbol_name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1001 case LTTNG_EVENT_SYSCALL
:
1002 ev
.type
= LTTNG_EVENT_SYSCALL
;
1005 ret
= CMD_UNDEFINED
;
1009 /* kernel loglevels not implemented */
1010 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1011 } else if (opt_userspace
) { /* User-space tracer action */
1013 if (opt_cmd_name
!= NULL
|| opt_pid
) {
1014 MSG("Only supporting tracing all UST processes (-u) for now.");
1015 ret
= CMD_UNDEFINED
;
1020 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name
,
1021 print_channel_name(channel_name
), opt_loglevel
? : "<all>");
1023 switch (opt_event_type
) {
1024 case LTTNG_EVENT_ALL
: /* Default behavior is tracepoint */
1026 case LTTNG_EVENT_TRACEPOINT
:
1027 /* Copy name and type of the event */
1028 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1029 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1030 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1032 case LTTNG_EVENT_PROBE
:
1033 case LTTNG_EVENT_FUNCTION
:
1034 case LTTNG_EVENT_FUNCTION_ENTRY
:
1035 case LTTNG_EVENT_SYSCALL
:
1037 ERR("Event type not available for user-space tracing");
1038 ret
= CMD_UNSUPPORTED
;
1044 if (opt_event_type
!= LTTNG_EVENT_ALL
&& opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1045 ERR("Exclusion option can only be used with tracepoint events");
1049 /* Free previously allocated items */
1050 if (exclusion_list
!= NULL
) {
1051 while (exclusion_count
--) {
1052 free(exclusion_list
[exclusion_count
]);
1054 free(exclusion_list
);
1055 exclusion_list
= NULL
;
1057 /* Check for proper subsets */
1058 ret
= check_exclusion_subsets(event_name
, opt_exclude
,
1059 &exclusion_count
, &exclusion_list
);
1060 if (ret
== CMD_ERROR
) {
1065 ev
.loglevel_type
= opt_loglevel_type
;
1067 ev
.loglevel
= loglevel_str_to_value(opt_loglevel
);
1068 if (ev
.loglevel
== -1) {
1069 ERR("Unknown loglevel %s", opt_loglevel
);
1070 ret
= -LTTNG_ERR_INVALID
;
1076 } else if (opt_jul
|| opt_log4j
) {
1077 if (opt_event_type
!= LTTNG_EVENT_ALL
&&
1078 opt_event_type
!= LTTNG_EVENT_TRACEPOINT
) {
1079 ERR("Event type not supported for domain.");
1080 ret
= CMD_UNSUPPORTED
;
1084 ev
.loglevel_type
= opt_loglevel_type
;
1087 ev
.loglevel
= loglevel_jul_str_to_value(opt_loglevel
);
1088 } else if (opt_log4j
) {
1089 ev
.loglevel
= loglevel_log4j_str_to_value(opt_loglevel
);
1091 if (ev
.loglevel
== -1) {
1092 ERR("Unknown loglevel %s", opt_loglevel
);
1093 ret
= -LTTNG_ERR_INVALID
;
1098 ev
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1099 } else if (opt_log4j
) {
1100 ev
.loglevel
= LTTNG_LOGLEVEL_LOG4J_ALL
;
1103 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
1104 strncpy(ev
.name
, event_name
, LTTNG_SYMBOL_NAME_LEN
);
1105 ev
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1111 char *exclusion_string
;
1113 command_ret
= lttng_enable_event_with_exclusions(handle
,
1115 NULL
, exclusion_count
, exclusion_list
);
1116 exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
1117 if (!exclusion_string
) {
1118 PERROR("Cannot allocate exclusion_string");
1122 if (command_ret
< 0) {
1123 /* Turn ret to positive value to handle the positive error code */
1124 switch (-command_ret
) {
1125 case LTTNG_ERR_KERN_EVENT_EXIST
:
1126 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
1129 print_channel_name(channel_name
), session_name
);
1132 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1134 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1135 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1138 print_channel_name(channel_name
),
1144 ERR("Event %s%s: %s (channel %s, session %s)", event_name
,
1146 lttng_strerror(command_ret
),
1147 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1148 ? print_raw_channel_name(channel_name
)
1149 : print_channel_name(channel_name
),
1154 error_holder
= command_ret
;
1157 case LTTNG_DOMAIN_KERNEL
:
1158 case LTTNG_DOMAIN_UST
:
1159 MSG("%s event %s%s created in channel %s",
1160 get_domain_str(dom
.type
),
1163 print_channel_name(channel_name
));
1165 case LTTNG_DOMAIN_JUL
:
1166 case LTTNG_DOMAIN_LOG4J
:
1168 * Don't print the default channel
1169 * name for agent domains.
1171 MSG("%s event %s%s enabled",
1172 get_domain_str(dom
.type
),
1180 free(exclusion_string
);
1184 char *exclusion_string
;
1186 /* Filter present */
1189 command_ret
= lttng_enable_event_with_exclusions(handle
, &ev
, channel_name
,
1190 opt_filter
, exclusion_count
, exclusion_list
);
1191 exclusion_string
= print_exclusions(exclusion_count
, exclusion_list
);
1192 if (!exclusion_string
) {
1193 PERROR("Cannot allocate exclusion_string");
1197 if (command_ret
< 0) {
1198 switch (-command_ret
) {
1199 case LTTNG_ERR_FILTER_EXIST
:
1200 WARN("Filter on event %s%s is already enabled"
1201 " (channel %s, session %s)",
1204 print_channel_name(channel_name
), session_name
);
1207 case LTTNG_ERR_TRACE_ALREADY_STARTED
:
1209 const char *msg
= "The command tried to enable an event in a new domain for a session that has already been started once.";
1210 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
.name
,
1213 print_channel_name(channel_name
),
1214 session_name
, opt_filter
);
1219 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev
.name
,
1221 lttng_strerror(command_ret
),
1222 command_ret
== -LTTNG_ERR_NEED_CHANNEL_NAME
1223 ? print_raw_channel_name(channel_name
)
1224 : print_channel_name(channel_name
),
1225 session_name
, opt_filter
);
1229 error_holder
= command_ret
;
1232 MSG("Event %s%s: Filter '%s' successfully set",
1233 event_name
, exclusion_string
,
1236 free(exclusion_string
);
1247 ret
= mi_lttng_event(writer
, &ev
, 1, handle
->domain
.type
);
1253 /* print exclusion */
1254 ret
= mi_print_exclusion(exclusion_count
, exclusion_list
);
1261 ret
= mi_lttng_writer_write_element_bool(writer
,
1262 mi_lttng_element_command_success
, success
);
1268 /* Close event element */
1269 ret
= mi_lttng_writer_close_element(writer
);
1277 event_name
= strtok(NULL
, ",");
1278 /* Reset warn, error and success */
1285 /* Close events element */
1286 ret
= mi_lttng_writer_close_element(writer
);
1299 lttng_destroy_handle(handle
);
1301 if (exclusion_list
!= NULL
) {
1302 while (exclusion_count
--) {
1303 free(exclusion_list
[exclusion_count
]);
1305 free(exclusion_list
);
1308 /* Overwrite ret with error_holder if there was an actual error with
1309 * enabling an event.
1311 ret
= error_holder
? error_holder
: ret
;
1317 * Add event to trace session
1319 int cmd_enable_events(int argc
, const char **argv
)
1321 int opt
, ret
= CMD_SUCCESS
, command_ret
= CMD_SUCCESS
, success
= 1;
1322 static poptContext pc
;
1323 char *session_name
= NULL
;
1324 int event_type
= -1;
1326 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
1327 poptReadDefaultConfig(pc
, 0);
1329 /* Default event type */
1330 opt_event_type
= LTTNG_EVENT_ALL
;
1332 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1337 case OPT_TRACEPOINT
:
1338 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
1341 opt_event_type
= LTTNG_EVENT_PROBE
;
1344 opt_event_type
= LTTNG_EVENT_FUNCTION
;
1346 case OPT_FUNCTION_ENTRY
:
1347 opt_event_type
= LTTNG_EVENT_FUNCTION_ENTRY
;
1350 opt_event_type
= LTTNG_EVENT_SYSCALL
;
1356 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
1357 opt_loglevel
= poptGetOptArg(pc
);
1359 case OPT_LOGLEVEL_ONLY
:
1360 opt_loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
1361 opt_loglevel
= poptGetOptArg(pc
);
1363 case OPT_LIST_OPTIONS
:
1364 list_cmd_options(stdout
, long_options
);
1372 ret
= CMD_UNDEFINED
;
1376 /* Validate event type. Multiple event type are not supported. */
1377 if (event_type
== -1) {
1378 event_type
= opt_event_type
;
1380 if (event_type
!= opt_event_type
) {
1381 ERR("Multiple event type not supported.");
1388 ret
= print_missing_or_multiple_domains(
1389 opt_kernel
+ opt_userspace
+ opt_jul
+ opt_log4j
);
1397 writer
= mi_lttng_writer_create(fileno(stdout
), lttng_opt_mi
);
1399 ret
= -LTTNG_ERR_NOMEM
;
1403 /* Open command element */
1404 ret
= mi_lttng_writer_command_open(writer
,
1405 mi_lttng_element_command_enable_event
);
1411 /* Open output element */
1412 ret
= mi_lttng_writer_open_element(writer
,
1413 mi_lttng_element_command_output
);
1420 opt_event_list
= (char*) poptGetArg(pc
);
1421 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
1422 ERR("Missing event name(s).\n");
1428 if (!opt_session_name
) {
1429 session_name
= get_session_name();
1430 if (session_name
== NULL
) {
1431 command_ret
= CMD_ERROR
;
1436 session_name
= opt_session_name
;
1439 command_ret
= enable_events(session_name
);
1448 /* Close output element */
1449 ret
= mi_lttng_writer_close_element(writer
);
1455 ret
= mi_lttng_writer_write_element_bool(writer
,
1456 mi_lttng_element_command_success
, success
);
1462 /* Command element close */
1463 ret
= mi_lttng_writer_command_close(writer
);
1472 if (writer
&& mi_lttng_writer_destroy(writer
)) {
1473 /* Preserve original error code */
1474 ret
= ret
? ret
: LTTNG_ERR_MI_IO_FAIL
;
1477 if (opt_session_name
== NULL
) {
1481 /* Overwrite ret if an error occurred in enable_events */
1482 ret
= command_ret
? command_ret
: ret
;
1484 poptFreeContext(pc
);