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