Fix: add missing JUL loglevel handling
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
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 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #include <assert.h>
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <inttypes.h>
28 #include <ctype.h>
29
30 #include "../command.h"
31 #include <src/common/sessiond-comm/sessiond-comm.h>
32
33 static char *opt_event_list;
34 static int opt_event_type;
35 static const char *opt_loglevel;
36 static int opt_loglevel_type;
37 static int opt_kernel;
38 static char *opt_session_name;
39 static int opt_userspace;
40 static int opt_jul;
41 static int opt_enable_all;
42 static char *opt_probe;
43 static char *opt_function;
44 static char *opt_function_entry_symbol;
45 static char *opt_channel_name;
46 static char *opt_filter;
47 static char *opt_exclude;
48 #if 0
49 /* Not implemented yet */
50 static char *opt_cmd_name;
51 static pid_t opt_pid;
52 #endif
53
54 enum {
55 OPT_HELP = 1,
56 OPT_TRACEPOINT,
57 OPT_PROBE,
58 OPT_FUNCTION,
59 OPT_FUNCTION_ENTRY,
60 OPT_SYSCALL,
61 OPT_USERSPACE,
62 OPT_LOGLEVEL,
63 OPT_LOGLEVEL_ONLY,
64 OPT_LIST_OPTIONS,
65 OPT_FILTER,
66 OPT_EXCLUDE,
67 };
68
69 static struct lttng_handle *handle;
70
71 static struct poptOption long_options[] = {
72 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
73 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
74 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
75 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
76 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
77 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
78 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
79 {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0},
80 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
81 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
82 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
83 #if 0
84 /*
85 * Currently removed from lttng kernel tracer. Removed from
86 * lttng UI to discourage its use.
87 */
88 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
89 #endif
90 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
91 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
92 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
93 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
94 {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0},
95 {"exclude", 'x', POPT_ARG_STRING, &opt_exclude, OPT_EXCLUDE, 0, 0},
96 {0, 0, 0, 0, 0, 0, 0}
97 };
98
99 /*
100 * usage
101 */
102 static void usage(FILE *ofp)
103 {
104 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [-k|-u] [OPTIONS] \n");
105 fprintf(ofp, "\n");
106 fprintf(ofp, "Options:\n");
107 fprintf(ofp, " -h, --help Show this help\n");
108 fprintf(ofp, " --list-options Simple listing of options\n");
109 fprintf(ofp, " -s, --session NAME Apply to session name\n");
110 fprintf(ofp, " -c, --channel NAME Apply to this channel\n");
111 fprintf(ofp, " -a, --all Enable all tracepoints and syscalls\n");
112 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
113 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
114 fprintf(ofp, " -j, --jul Apply for Java application using JUL\n");
115 fprintf(ofp, "\n");
116 fprintf(ofp, "Event options:\n");
117 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
118 fprintf(ofp, " - userspace tracer supports wildcards at end of string.\n");
119 fprintf(ofp, " Don't forget to quote to deal with bash expansion.\n");
120 fprintf(ofp, " e.g.:\n");
121 fprintf(ofp, " \"*\"\n");
122 fprintf(ofp, " \"app_component:na*\"\n");
123 fprintf(ofp, " --probe (addr | symbol | symbol+offset)\n");
124 fprintf(ofp, " Dynamic probe.\n");
125 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
126 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
127 fprintf(ofp, " --function (addr | symbol | symbol+offset)\n");
128 fprintf(ofp, " Dynamic function entry/return probe.\n");
129 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
130 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
131 #if 0
132 fprintf(ofp, " --function:entry symbol\n");
133 fprintf(ofp, " Function tracer event\n");
134 #endif
135 fprintf(ofp, " --syscall System call event\n");
136 fprintf(ofp, "\n");
137 fprintf(ofp, " --loglevel name\n");
138 fprintf(ofp, " Tracepoint loglevel range from 0 to loglevel\n");
139 fprintf(ofp, " --loglevel-only name\n");
140 fprintf(ofp, " Tracepoint loglevel (only this loglevel)\n");
141 fprintf(ofp, "\n");
142 fprintf(ofp, " The loglevel or loglevel-only options should be\n");
143 fprintf(ofp, " combined with a tracepoint name or tracepoint\n");
144 fprintf(ofp, " wildcard.\n");
145 fprintf(ofp, " Available loglevels:\n");
146 fprintf(ofp, " (higher value is more verbose)\n");
147 fprintf(ofp, " TRACE_EMERG = 0\n");
148 fprintf(ofp, " TRACE_ALERT = 1\n");
149 fprintf(ofp, " TRACE_CRIT = 2\n");
150 fprintf(ofp, " TRACE_ERR = 3\n");
151 fprintf(ofp, " TRACE_WARNING = 4\n");
152 fprintf(ofp, " TRACE_NOTICE = 5\n");
153 fprintf(ofp, " TRACE_INFO = 6\n");
154 fprintf(ofp, " TRACE_DEBUG_SYSTEM = 7\n");
155 fprintf(ofp, " TRACE_DEBUG_PROGRAM = 8\n");
156 fprintf(ofp, " TRACE_DEBUG_PROCESS = 9\n");
157 fprintf(ofp, " TRACE_DEBUG_MODULE = 10\n");
158 fprintf(ofp, " TRACE_DEBUG_UNIT = 11\n");
159 fprintf(ofp, " TRACE_DEBUG_FUNCTION = 12\n");
160 fprintf(ofp, " TRACE_DEBUG_LINE = 13\n");
161 fprintf(ofp, " TRACE_DEBUG = 14\n");
162 fprintf(ofp, " (shortcuts such as \"system\" are allowed)\n");
163 fprintf(ofp, "\n");
164 fprintf(ofp, " Available JUL domain loglevels:\n");
165 fprintf(ofp, " JUL_OFF = INT32_MAX\n");
166 fprintf(ofp, " JUL_SEVERE = %d\n", LTTNG_LOGLEVEL_JUL_SEVERE);
167 fprintf(ofp, " JUL_WARNING = %d\n", LTTNG_LOGLEVEL_JUL_WARNING);
168 fprintf(ofp, " JUL_INFO = %d\n", LTTNG_LOGLEVEL_JUL_INFO);
169 fprintf(ofp, " JUL_CONFIG = %d\n", LTTNG_LOGLEVEL_JUL_CONFIG);
170 fprintf(ofp, " JUL_FINE = %d\n", LTTNG_LOGLEVEL_JUL_FINE);
171 fprintf(ofp, " JUL_FINER = %d\n", LTTNG_LOGLEVEL_JUL_FINER);
172 fprintf(ofp, " JUL_FINEST = %d\n", LTTNG_LOGLEVEL_JUL_FINEST);
173 fprintf(ofp, " JUL_ALL = INT32_MIN\n");
174 fprintf(ofp, " (shortcuts such as \"severe\" are allowed)\n");
175 fprintf(ofp, "\n");
176 fprintf(ofp, " -f, --filter \'expression\'\n");
177 fprintf(ofp, " Filter expression on event fields and context.\n");
178 fprintf(ofp, " Event recording depends on evaluation.\n");
179 fprintf(ofp, " Only specify on first activation of\n");
180 fprintf(ofp, " a given event within a session.\n");
181 fprintf(ofp, " Filter only allowed when enabling\n");
182 fprintf(ofp, " events within a session before tracing\n");
183 fprintf(ofp, " is started. If the filter fails to link\n");
184 fprintf(ofp, " with the event within the traced domain,\n");
185 fprintf(ofp, " the event will be discarded. Currently,\n");
186 fprintf(ofp, " filter is only implemented for the user-space\n");
187 fprintf(ofp, " tracer.\n");
188 fprintf(ofp, " Expression examples:.\n");
189 fprintf(ofp, " \n");
190 fprintf(ofp, " 'intfield > 500 && intfield < 503'\n");
191 fprintf(ofp, " '(strfield == \"test\" || intfield != 10) && intfield > 33'\n");
192 fprintf(ofp, " 'doublefield > 1.1 && intfield < 5.3'\n");
193 fprintf(ofp, " \n");
194 fprintf(ofp, " Wildcards are allowed at the end of strings:\n");
195 fprintf(ofp, " 'seqfield1 == \"te*\"'\n");
196 fprintf(ofp, " In string literals, the escape character is '\\'.\n");
197 fprintf(ofp, " Use '\\*' for the '*' character, and '\\\\' for\n");
198 fprintf(ofp, " the '\\' character. Wildcard match any sequence of,\n");
199 fprintf(ofp, " characters including an empty sub-string (match 0 or\n");
200 fprintf(ofp, " more characters).\n");
201 fprintf(ofp, "\n");
202 fprintf(ofp, " Context information can be used for filtering. The\n");
203 fprintf(ofp, " examples below show usage of context filtering on\n");
204 fprintf(ofp, " process name (with a wildcard), process ID range, and\n");
205 fprintf(ofp, " unique thread ID for filtering. The process and\n");
206 fprintf(ofp, " thread ID of running applications can be found under\n");
207 fprintf(ofp, " columns \"PID\" and \"LWP\" of the \"ps -eLf\" command.\n");
208 fprintf(ofp, "\n");
209 fprintf(ofp, " '$ctx.procname == \"demo*\"'\n");
210 fprintf(ofp, " '$ctx.vpid >= 4433 && $ctx.vpid < 4455'\n");
211 fprintf(ofp, " '$ctx.vtid == 1234'\n");
212 fprintf(ofp, " -x, --exclude LIST\n");
213 fprintf(ofp, " Add exclusions to UST tracepoints:\n");
214 fprintf(ofp, " Events that match any of the items\n");
215 fprintf(ofp, " in the comma-separated LIST are not\n");
216 fprintf(ofp, " enabled, even if they match a wildcard\n");
217 fprintf(ofp, " definition of the event.\n");
218 fprintf(ofp, "\n");
219 }
220
221 /*
222 * Parse probe options.
223 */
224 static int parse_probe_opts(struct lttng_event *ev, char *opt)
225 {
226 int ret;
227 char s_hex[19];
228 char name[LTTNG_SYMBOL_NAME_LEN];
229
230 if (opt == NULL) {
231 ret = -1;
232 goto end;
233 }
234
235 /* Check for symbol+offset */
236 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
237 if (ret == 2) {
238 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
239 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
240 DBG("probe symbol %s", ev->attr.probe.symbol_name);
241 if (*s_hex == '\0') {
242 ERR("Invalid probe offset %s", s_hex);
243 ret = -1;
244 goto end;
245 }
246 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
247 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
248 ev->attr.probe.addr = 0;
249 goto end;
250 }
251
252 /* Check for symbol */
253 if (isalpha(name[0])) {
254 ret = sscanf(opt, "%s", name);
255 if (ret == 1) {
256 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
257 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
258 DBG("probe symbol %s", ev->attr.probe.symbol_name);
259 ev->attr.probe.offset = 0;
260 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
261 ev->attr.probe.addr = 0;
262 goto end;
263 }
264 }
265
266 /* Check for address */
267 ret = sscanf(opt, "%s", s_hex);
268 if (ret > 0) {
269 if (*s_hex == '\0') {
270 ERR("Invalid probe address %s", s_hex);
271 ret = -1;
272 goto end;
273 }
274 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
275 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
276 ev->attr.probe.offset = 0;
277 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
278 goto end;
279 }
280
281 /* No match */
282 ret = -1;
283
284 end:
285 return ret;
286 }
287
288 /*
289 * Maps JUL loglevel from string to value
290 */
291 static int loglevel_jul_str_to_value(const char *inputstr)
292 {
293 int i = 0;
294 char str[LTTNG_SYMBOL_NAME_LEN];
295
296 /*
297 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
298 * added at the end of the loop so a the upper bound we avoid the overflow.
299 */
300 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
301 str[i] = toupper(inputstr[i]);
302 i++;
303 }
304 str[i] = '\0';
305
306 if (!strcmp(str, "JUL_OFF") || !strcmp(str, "OFF")) {
307 return LTTNG_LOGLEVEL_JUL_OFF;
308 } else if (!strcmp(str, "JUL_SEVERE") || !strcmp(str, "SEVERE")) {
309 return LTTNG_LOGLEVEL_JUL_SEVERE;
310 } else if (!strcmp(str, "JUL_WARNING") || !strcmp(str, "WARNING")) {
311 return LTTNG_LOGLEVEL_JUL_WARNING;
312 } else if (!strcmp(str, "JUL_INFO") || !strcmp(str, "INFO")) {
313 return LTTNG_LOGLEVEL_JUL_INFO;
314 } else if (!strcmp(str, "JUL_CONFIG") || !strcmp(str, "CONFIG")) {
315 return LTTNG_LOGLEVEL_JUL_CONFIG;
316 } else if (!strcmp(str, "JUL_FINE") || !strcmp(str, "FINE")) {
317 return LTTNG_LOGLEVEL_JUL_FINE;
318 } else if (!strcmp(str, "JUL_FINER") || !strcmp(str, "FINER")) {
319 return LTTNG_LOGLEVEL_JUL_FINER;
320 } else if (!strcmp(str, "JUL_FINEST") || !strcmp(str, "FINEST")) {
321 return LTTNG_LOGLEVEL_JUL_FINEST;
322 } else if (!strcmp(str, "JUL_ALL") || !strcmp(str, "ALL")) {
323 return LTTNG_LOGLEVEL_JUL_ALL;
324 } else {
325 return -1;
326 }
327 }
328
329 /*
330 * Maps loglevel from string to value
331 */
332 static
333 int loglevel_str_to_value(const char *inputstr)
334 {
335 int i = 0;
336 char str[LTTNG_SYMBOL_NAME_LEN];
337
338 /*
339 * Loop up to LTTNG_SYMBOL_NAME_LEN minus one because the NULL bytes is
340 * added at the end of the loop so a the upper bound we avoid the overflow.
341 */
342 while (i < (LTTNG_SYMBOL_NAME_LEN - 1) && inputstr[i] != '\0') {
343 str[i] = toupper(inputstr[i]);
344 i++;
345 }
346 str[i] = '\0';
347 if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) {
348 return LTTNG_LOGLEVEL_EMERG;
349 } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) {
350 return LTTNG_LOGLEVEL_ALERT;
351 } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) {
352 return LTTNG_LOGLEVEL_CRIT;
353 } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) {
354 return LTTNG_LOGLEVEL_ERR;
355 } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) {
356 return LTTNG_LOGLEVEL_WARNING;
357 } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) {
358 return LTTNG_LOGLEVEL_NOTICE;
359 } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) {
360 return LTTNG_LOGLEVEL_INFO;
361 } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) {
362 return LTTNG_LOGLEVEL_DEBUG_SYSTEM;
363 } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) {
364 return LTTNG_LOGLEVEL_DEBUG_PROGRAM;
365 } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) {
366 return LTTNG_LOGLEVEL_DEBUG_PROCESS;
367 } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) {
368 return LTTNG_LOGLEVEL_DEBUG_MODULE;
369 } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) {
370 return LTTNG_LOGLEVEL_DEBUG_UNIT;
371 } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) {
372 return LTTNG_LOGLEVEL_DEBUG_FUNCTION;
373 } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) {
374 return LTTNG_LOGLEVEL_DEBUG_LINE;
375 } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) {
376 return LTTNG_LOGLEVEL_DEBUG;
377 } else {
378 return -1;
379 }
380 }
381
382 static
383 const char *print_channel_name(const char *name)
384 {
385 return name ? : DEFAULT_CHANNEL_NAME;
386 }
387
388 static
389 const char *print_raw_channel_name(const char *name)
390 {
391 return name ? : "<default>";
392 }
393
394 /*
395 * Return allocated string for pretty-printing exclusion names.
396 */
397 static
398 char *print_exclusions(int count, char **names)
399 {
400 int length = 0;
401 int i;
402 const char *preamble = " excluding ";
403 char *ret;
404
405 if (count == 0) {
406 return strdup("");
407 }
408
409 /* calculate total required length */
410 for (i = 0; i < count; i++) {
411 length += strlen(names[i]) + 1;
412 }
413
414 /* add length of preamble + one for NUL - one for last (missing) comma */
415 length += strlen(preamble);
416 ret = malloc(length);
417 strncpy(ret, preamble, length);
418 for (i = 0; i < count; i++) {
419 strcat(ret, names[i]);
420 if (i != count - 1) {
421 strcat(ret, ",");
422 }
423 }
424 return ret;
425 }
426
427 /*
428 * Compare list of exclusions against an event name.
429 * Return a list of legal exclusion names.
430 * Produce an error or a warning about others (depending on the situation)
431 */
432 static
433 int check_exclusion_subsets(const char *event_name,
434 const char *exclusions,
435 int *exclusion_count_ptr,
436 char ***exclusion_list_ptr)
437 {
438 const char *excluder_ptr;
439 const char *event_ptr;
440 const char *next_excluder;
441 int excluder_length;
442 int exclusion_count = 0;
443 char **exclusion_list = NULL;
444 int ret = CMD_SUCCESS;
445
446 if (event_name[strlen(event_name) - 1] != '*') {
447 ERR("Event %s: Excluders can only be used with wildcarded events", event_name);
448 goto error;
449 }
450
451 next_excluder = exclusions;
452 while (*next_excluder != 0) {
453 event_ptr = event_name;
454 excluder_ptr = next_excluder;
455 excluder_length = strcspn(next_excluder, ",");
456
457 /* Scan both the excluder and the event letter by letter */
458 while (1) {
459 char e, x;
460
461 e = *event_ptr;
462 x = *excluder_ptr;
463
464 if (x == '*') {
465 /* Event is a subset of the excluder */
466 ERR("Event %s: %.*s excludes all events from %s",
467 event_name,
468 excluder_length,
469 next_excluder,
470 event_name);
471 goto error;
472 }
473 if (e == '*') {
474 /* Excluder is a proper subset of event */
475 exclusion_count++;
476 exclusion_list = realloc(exclusion_list, sizeof(char **) * exclusion_count);
477 exclusion_list[exclusion_count - 1] = strndup(next_excluder, excluder_length);
478
479 break;
480 }
481 if (x != e) {
482 /* Excluder and event sets have no common elements */
483 WARN("Event %s: %.*s does not exclude any events from %s",
484 event_name,
485 excluder_length,
486 next_excluder,
487 event_name);
488 break;
489 }
490 excluder_ptr++;
491 event_ptr++;
492 }
493 /* next excluder */
494 next_excluder += excluder_length;
495 if (*next_excluder == ',') {
496 next_excluder++;
497 }
498 }
499 goto end;
500 error:
501 while (exclusion_count--) {
502 free(exclusion_list[exclusion_count]);
503 }
504 if (exclusion_list != NULL) {
505 free(exclusion_list);
506 }
507 exclusion_list = NULL;
508 exclusion_count = 0;
509 ret = CMD_ERROR;
510 end:
511 *exclusion_count_ptr = exclusion_count;
512 *exclusion_list_ptr = exclusion_list;
513 return ret;
514 }
515 /*
516 * Enabling event using the lttng API.
517 */
518 static int enable_events(char *session_name)
519 {
520 int ret = CMD_SUCCESS, warn = 0;
521 char *event_name, *channel_name = NULL;
522 struct lttng_event ev;
523 struct lttng_domain dom;
524 int exclusion_count = 0;
525 char **exclusion_list = NULL;
526
527 memset(&ev, 0, sizeof(ev));
528 memset(&dom, 0, sizeof(dom));
529
530 if (opt_kernel) {
531 if (opt_filter) {
532 ERR("Filter not implement for kernel tracing yet");
533 ret = CMD_ERROR;
534 goto error;
535 }
536 if (opt_loglevel) {
537 WARN("Kernel loglevels are not supported.");
538 }
539 }
540
541 /* Create lttng domain */
542 if (opt_kernel) {
543 dom.type = LTTNG_DOMAIN_KERNEL;
544 dom.buf_type = LTTNG_BUFFER_GLOBAL;
545 } else if (opt_userspace) {
546 dom.type = LTTNG_DOMAIN_UST;
547 /* Default. */
548 dom.buf_type = LTTNG_BUFFER_PER_UID;
549 } else if (opt_jul) {
550 dom.type = LTTNG_DOMAIN_JUL;
551 /* Default. */
552 dom.buf_type = LTTNG_BUFFER_PER_UID;
553 } else {
554 print_missing_domain();
555 ret = CMD_ERROR;
556 goto error;
557 }
558
559 if (opt_kernel && opt_exclude) {
560 ERR("Event name exclusions are not yet implemented for kernel events");
561 ret = CMD_ERROR;
562 goto error;
563 }
564
565 channel_name = opt_channel_name;
566
567 handle = lttng_create_handle(session_name, &dom);
568 if (handle == NULL) {
569 ret = -1;
570 goto error;
571 }
572
573 if (opt_enable_all) {
574 /* Default setup for enable all */
575 if (opt_kernel) {
576 ev.type = opt_event_type;
577 ev.name[0] = '\0';
578 /* kernel loglevels not implemented */
579 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
580 } else {
581 ev.type = LTTNG_EVENT_TRACEPOINT;
582 strcpy(ev.name, "*");
583 ev.loglevel_type = opt_loglevel_type;
584 if (opt_loglevel) {
585 assert(opt_userspace || opt_jul);
586 if (opt_userspace) {
587 ev.loglevel = loglevel_str_to_value(opt_loglevel);
588 } else if (opt_jul) {
589 ev.loglevel = loglevel_jul_str_to_value(opt_loglevel);
590 }
591 if (ev.loglevel == -1) {
592 ERR("Unknown loglevel %s", opt_loglevel);
593 ret = -LTTNG_ERR_INVALID;
594 goto error;
595 }
596 } else {
597 assert(opt_userspace || opt_jul);
598 if (opt_userspace) {
599 ev.loglevel = -1;
600 } else if (opt_jul) {
601 ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
602 }
603 }
604 }
605
606 if (opt_exclude) {
607 ret = check_exclusion_subsets("*", opt_exclude,
608 &exclusion_count, &exclusion_list);
609 if (ret == CMD_ERROR) {
610 goto error;
611 }
612 }
613 if (!opt_filter) {
614 ret = lttng_enable_event_with_exclusions(handle,
615 &ev, channel_name,
616 NULL,
617 exclusion_count, exclusion_list);
618 if (ret < 0) {
619 switch (-ret) {
620 case LTTNG_ERR_KERN_EVENT_EXIST:
621 WARN("Kernel events already enabled (channel %s, session %s)",
622 print_channel_name(channel_name), session_name);
623 break;
624 default:
625 ERR("Events: %s (channel %s, session %s)",
626 lttng_strerror(ret),
627 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
628 ? print_raw_channel_name(channel_name)
629 : print_channel_name(channel_name),
630 session_name);
631 break;
632 }
633 goto end;
634 }
635
636 switch (opt_event_type) {
637 case LTTNG_EVENT_TRACEPOINT:
638 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
639 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
640 MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
641 get_domain_str(dom.type),
642 exclusion_string,
643 print_channel_name(channel_name),
644 opt_loglevel);
645 free(exclusion_string);
646 } else {
647 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
648 MSG("All %s tracepoints%s are enabled in channel %s",
649 get_domain_str(dom.type),
650 exclusion_string,
651 print_channel_name(channel_name));
652 free(exclusion_string);
653 }
654 break;
655 case LTTNG_EVENT_SYSCALL:
656 if (opt_kernel) {
657 MSG("All kernel system calls are enabled in channel %s",
658 print_channel_name(channel_name));
659 }
660 break;
661 case LTTNG_EVENT_ALL:
662 if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
663 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
664 MSG("All %s events%s are enabled in channel %s for loglevel %s",
665 get_domain_str(dom.type),
666 exclusion_string,
667 print_channel_name(channel_name),
668 opt_loglevel);
669 free(exclusion_string);
670 } else {
671 char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
672 MSG("All %s events%s are enabled in channel %s",
673 get_domain_str(dom.type),
674 exclusion_string,
675 print_channel_name(channel_name));
676 free(exclusion_string);
677 }
678 break;
679 default:
680 /*
681 * We should not be here since lttng_enable_event should have
682 * failed on the event type.
683 */
684 goto error;
685 }
686 }
687 if (opt_filter) {
688 ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name,
689 opt_filter, exclusion_count, exclusion_list);
690 if (ret < 0) {
691 switch (-ret) {
692 case LTTNG_ERR_FILTER_EXIST:
693 WARN("Filter on all events is already enabled"
694 " (channel %s, session %s)",
695 print_channel_name(channel_name), session_name);
696 break;
697 default:
698 ERR("All events: %s (channel %s, session %s, filter \'%s\')",
699 lttng_strerror(ret),
700 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
701 ? print_raw_channel_name(channel_name)
702 : print_channel_name(channel_name),
703 session_name, opt_filter);
704 break;
705 }
706 goto error;
707 } else {
708 MSG("Filter '%s' successfully set", opt_filter);
709 }
710 }
711 goto end;
712 }
713
714 /* Strip event list */
715 event_name = strtok(opt_event_list, ",");
716 while (event_name != NULL) {
717 /* Copy name and type of the event */
718 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
719 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
720 ev.type = opt_event_type;
721
722 /* Kernel tracer action */
723 if (opt_kernel) {
724 DBG("Enabling kernel event %s for channel %s",
725 event_name,
726 print_channel_name(channel_name));
727
728 switch (opt_event_type) {
729 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
730 ev.type = LTTNG_EVENT_TRACEPOINT;
731 /* Fall-through */
732 case LTTNG_EVENT_TRACEPOINT:
733 break;
734 case LTTNG_EVENT_PROBE:
735 ret = parse_probe_opts(&ev, opt_probe);
736 if (ret < 0) {
737 ERR("Unable to parse probe options");
738 ret = 0;
739 goto error;
740 }
741 break;
742 case LTTNG_EVENT_FUNCTION:
743 ret = parse_probe_opts(&ev, opt_function);
744 if (ret < 0) {
745 ERR("Unable to parse function probe options");
746 ret = 0;
747 goto error;
748 }
749 break;
750 case LTTNG_EVENT_FUNCTION_ENTRY:
751 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
752 LTTNG_SYMBOL_NAME_LEN);
753 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
754 break;
755 case LTTNG_EVENT_SYSCALL:
756 MSG("per-syscall selection not supported yet. Use \"-a\" "
757 "for all syscalls.");
758 default:
759 ret = CMD_UNDEFINED;
760 goto error;
761 }
762
763 /* kernel loglevels not implemented */
764 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
765 } else if (opt_userspace) { /* User-space tracer action */
766 #if 0
767 if (opt_cmd_name != NULL || opt_pid) {
768 MSG("Only supporting tracing all UST processes (-u) for now.");
769 ret = CMD_UNDEFINED;
770 goto error;
771 }
772 #endif
773
774 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
775 print_channel_name(channel_name), opt_loglevel ? : "<all>");
776
777 switch (opt_event_type) {
778 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
779 /* Fall-through */
780 case LTTNG_EVENT_TRACEPOINT:
781 /* Copy name and type of the event */
782 ev.type = LTTNG_EVENT_TRACEPOINT;
783 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
784 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
785 break;
786 case LTTNG_EVENT_PROBE:
787 case LTTNG_EVENT_FUNCTION:
788 case LTTNG_EVENT_FUNCTION_ENTRY:
789 case LTTNG_EVENT_SYSCALL:
790 default:
791 ERR("Event type not available for user-space tracing");
792 ret = CMD_UNSUPPORTED;
793 goto error;
794 }
795
796 if (opt_exclude) {
797 if (opt_event_type != LTTNG_EVENT_ALL && opt_event_type != LTTNG_EVENT_TRACEPOINT) {
798 ERR("Exclusion option can only be used with tracepoint events");
799 ret = CMD_ERROR;
800 goto error;
801 }
802 /* Free previously allocated items */
803 if (exclusion_list != NULL) {
804 while (exclusion_count--) {
805 free(exclusion_list[exclusion_count]);
806 }
807 free(exclusion_list);
808 exclusion_list = NULL;
809 }
810 /* Check for proper subsets */
811 ret = check_exclusion_subsets(event_name, opt_exclude,
812 &exclusion_count, &exclusion_list);
813 if (ret == CMD_ERROR) {
814 goto error;
815 }
816 }
817
818 ev.loglevel_type = opt_loglevel_type;
819 if (opt_loglevel) {
820 ev.loglevel = loglevel_str_to_value(opt_loglevel);
821 if (ev.loglevel == -1) {
822 ERR("Unknown loglevel %s", opt_loglevel);
823 ret = -LTTNG_ERR_INVALID;
824 goto error;
825 }
826 } else {
827 ev.loglevel = -1;
828 }
829 } else if (opt_jul) {
830 if (opt_event_type != LTTNG_EVENT_ALL &&
831 opt_event_type != LTTNG_EVENT_TRACEPOINT) {
832 ERR("Event type not supported for JUL domain.");
833 ret = CMD_UNSUPPORTED;
834 goto error;
835 }
836
837 ev.loglevel_type = opt_loglevel_type;
838 if (opt_loglevel) {
839 ev.loglevel = loglevel_jul_str_to_value(opt_loglevel);
840 if (ev.loglevel == -1) {
841 ERR("Unknown loglevel %s", opt_loglevel);
842 ret = -LTTNG_ERR_INVALID;
843 goto error;
844 }
845 } else {
846 ev.loglevel = LTTNG_LOGLEVEL_JUL_ALL;
847 }
848 ev.type = LTTNG_EVENT_TRACEPOINT;
849 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
850 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
851 } else {
852 print_missing_domain();
853 ret = CMD_ERROR;
854 goto error;
855 }
856
857 if (!opt_filter) {
858 char *exclusion_string;
859
860 ret = lttng_enable_event_with_exclusions(handle,
861 &ev, channel_name,
862 NULL, exclusion_count, exclusion_list);
863 exclusion_string = print_exclusions(exclusion_count, exclusion_list);
864 if (ret < 0) {
865 /* Turn ret to positive value to handle the positive error code */
866 switch (-ret) {
867 case LTTNG_ERR_KERN_EVENT_EXIST:
868 WARN("Kernel event %s%s already enabled (channel %s, session %s)",
869 event_name,
870 exclusion_string,
871 print_channel_name(channel_name), session_name);
872 break;
873 default:
874 ERR("Event %s%s: %s (channel %s, session %s)", event_name,
875 exclusion_string,
876 lttng_strerror(ret),
877 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
878 ? print_raw_channel_name(channel_name)
879 : print_channel_name(channel_name),
880 session_name);
881 break;
882 }
883 warn = 1;
884 } else {
885 MSG("%s event %s%s created in channel %s",
886 get_domain_str(dom.type), event_name,
887 exclusion_string,
888 print_channel_name(channel_name));
889 }
890 free(exclusion_string);
891 }
892
893 if (opt_filter) {
894 char *exclusion_string;
895
896 ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name,
897 opt_filter, exclusion_count, exclusion_list);
898 exclusion_string = print_exclusions(exclusion_count, exclusion_list);
899
900 if (ret < 0) {
901 switch (-ret) {
902 case LTTNG_ERR_FILTER_EXIST:
903 WARN("Filter on event %s%s is already enabled"
904 " (channel %s, session %s)",
905 event_name,
906 exclusion_string,
907 print_channel_name(channel_name), session_name);
908 break;
909 default:
910 ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name,
911 exclusion_string,
912 lttng_strerror(ret),
913 ret == -LTTNG_ERR_NEED_CHANNEL_NAME
914 ? print_raw_channel_name(channel_name)
915 : print_channel_name(channel_name),
916 session_name, opt_filter);
917 break;
918 }
919 free(exclusion_string);
920 goto error;
921 } else {
922 MSG("Event %s%s: Filter '%s' successfully set",
923 event_name, exclusion_string,
924 opt_filter);
925 }
926 free(exclusion_string);
927 }
928
929 /* Next event */
930 event_name = strtok(NULL, ",");
931 }
932
933 end:
934 error:
935 if (warn) {
936 ret = CMD_WARNING;
937 }
938 lttng_destroy_handle(handle);
939
940 if (exclusion_list != NULL) {
941 while (exclusion_count--) {
942 free(exclusion_list[exclusion_count]);
943 }
944 free(exclusion_list);
945 }
946
947 return ret;
948 }
949
950 /*
951 * Add event to trace session
952 */
953 int cmd_enable_events(int argc, const char **argv)
954 {
955 int opt, ret = CMD_SUCCESS;
956 static poptContext pc;
957 char *session_name = NULL;
958 int event_type = -1;
959
960 pc = poptGetContext(NULL, argc, argv, long_options, 0);
961 poptReadDefaultConfig(pc, 0);
962
963 /* Default event type */
964 opt_event_type = LTTNG_EVENT_ALL;
965
966 while ((opt = poptGetNextOpt(pc)) != -1) {
967 switch (opt) {
968 case OPT_HELP:
969 usage(stdout);
970 goto end;
971 case OPT_TRACEPOINT:
972 opt_event_type = LTTNG_EVENT_TRACEPOINT;
973 break;
974 case OPT_PROBE:
975 opt_event_type = LTTNG_EVENT_PROBE;
976 break;
977 case OPT_FUNCTION:
978 opt_event_type = LTTNG_EVENT_FUNCTION;
979 break;
980 case OPT_FUNCTION_ENTRY:
981 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
982 break;
983 case OPT_SYSCALL:
984 opt_event_type = LTTNG_EVENT_SYSCALL;
985 break;
986 case OPT_USERSPACE:
987 opt_userspace = 1;
988 break;
989 case OPT_LOGLEVEL:
990 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
991 opt_loglevel = poptGetOptArg(pc);
992 break;
993 case OPT_LOGLEVEL_ONLY:
994 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
995 opt_loglevel = poptGetOptArg(pc);
996 break;
997 case OPT_LIST_OPTIONS:
998 list_cmd_options(stdout, long_options);
999 goto end;
1000 case OPT_FILTER:
1001 break;
1002 case OPT_EXCLUDE:
1003 break;
1004 default:
1005 usage(stderr);
1006 ret = CMD_UNDEFINED;
1007 goto end;
1008 }
1009
1010 /* Validate event type. Multiple event type are not supported. */
1011 if (event_type == -1) {
1012 event_type = opt_event_type;
1013 } else {
1014 if (event_type != opt_event_type) {
1015 ERR("Multiple event type not supported.");
1016 ret = CMD_ERROR;
1017 goto end;
1018 }
1019 }
1020 }
1021
1022 opt_event_list = (char*) poptGetArg(pc);
1023 if (opt_event_list == NULL && opt_enable_all == 0) {
1024 ERR("Missing event name(s).\n");
1025 usage(stderr);
1026 ret = CMD_ERROR;
1027 goto end;
1028 }
1029
1030 if (!opt_session_name) {
1031 session_name = get_session_name();
1032 if (session_name == NULL) {
1033 ret = CMD_ERROR;
1034 goto end;
1035 }
1036 } else {
1037 session_name = opt_session_name;
1038 }
1039
1040 ret = enable_events(session_name);
1041
1042 end:
1043 if (opt_session_name == NULL) {
1044 free(session_name);
1045 }
1046
1047 poptFreeContext(pc);
1048 return ret;
1049 }
This page took 0.089037 seconds and 6 git commands to generate.