Add lttng-error.h containing every API err. code
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
f3ed775e
DG
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
f3ed775e
DG
16 */
17
18#define _GNU_SOURCE
19#include <popt.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
5a0de755 26#include <inttypes.h>
8f0d098b 27#include <ctype.h>
f3ed775e 28
c399183f 29#include "../command.h"
42224349 30#include <src/common/sessiond-comm/sessiond-comm.h>
f3ed775e
DG
31
32static char *opt_event_list;
33static int opt_event_type;
0cda4f28
MD
34static const char *opt_loglevel;
35static int opt_loglevel_type;
6181537c 36static int opt_kernel;
5440dc42 37static char *opt_session_name;
f3ed775e
DG
38static int opt_userspace;
39static int opt_enable_all;
cf0e5467 40static char *opt_probe;
8f0d098b
MD
41static char *opt_function;
42static char *opt_function_entry_symbol;
f3ed775e 43static char *opt_channel_name;
53a80697 44static char *opt_filter;
d78d6610
DG
45#if 0
46/* Not implemented yet */
47static char *opt_cmd_name;
48static pid_t opt_pid;
49#endif
f3ed775e
DG
50
51enum {
52 OPT_HELP = 1,
f3ed775e 53 OPT_TRACEPOINT,
cf0e5467 54 OPT_PROBE,
f3ed775e 55 OPT_FUNCTION,
8f0d098b 56 OPT_FUNCTION_ENTRY,
a54bd42d 57 OPT_SYSCALL,
eeac7d46 58 OPT_USERSPACE,
0cda4f28
MD
59 OPT_LOGLEVEL,
60 OPT_LOGLEVEL_ONLY,
679b4943 61 OPT_LIST_OPTIONS,
53a80697 62 OPT_FILTER,
f3ed775e
DG
63};
64
cd80958d
DG
65static struct lttng_handle *handle;
66
f3ed775e
DG
67static struct poptOption long_options[] = {
68 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
69 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 70 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e14f64a8 71 {"all", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
f3ed775e
DG
72 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
73 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610 74 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
f3ed775e 75 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
7ebae521 76 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
40e9d5d3 77 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
b213d387
MD
78#if 0
79 /*
80 * Currently removed from lttng kernel tracer. Removed from
81 * lttng UI to discourage its use.
82 */
40e9d5d3 83 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
b213d387 84#endif
7ebae521 85 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
0cda4f28
MD
86 {"loglevel", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL, 0, 0},
87 {"loglevel-only", 0, POPT_ARG_STRING, 0, OPT_LOGLEVEL_ONLY, 0, 0},
679b4943 88 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
53a80697 89 {"filter", 'f', POPT_ARG_STRING, &opt_filter, OPT_FILTER, 0, 0},
f3ed775e
DG
90 {0, 0, 0, 0, 0, 0, 0}
91};
92
93/*
94 * usage
95 */
96static void usage(FILE *ofp)
97{
32a6298d 98 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [-k|-u] [OPTIONS] \n");
f3ed775e 99 fprintf(ofp, "\n");
32a6298d 100 fprintf(ofp, "Options:\n");
f3ed775e 101 fprintf(ofp, " -h, --help Show this help\n");
679b4943 102 fprintf(ofp, " --list-options Simple listing of options\n");
32a6298d
DG
103 fprintf(ofp, " -s, --session NAME Apply to session name\n");
104 fprintf(ofp, " -c, --channel NAME Apply to this channel\n");
e08bff8d 105 fprintf(ofp, " -a, --all Enable all tracepoints and syscalls\n");
f3ed775e 106 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
27221701 107 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
f3ed775e
DG
108 fprintf(ofp, "\n");
109 fprintf(ofp, "Event options:\n");
110 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
be49af1e
MD
111 fprintf(ofp, " - userspace tracer supports wildcards at end of string.\n");
112 fprintf(ofp, " Don't forget to quote to deal with bash expansion.\n");
113 fprintf(ofp, " e.g.:\n");
114 fprintf(ofp, " \"*\"\n");
115 fprintf(ofp, " \"app_component:na*\"\n");
8f0d098b 116 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
cf0e5467
MD
117 fprintf(ofp, " Dynamic probe.\n");
118 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
119 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
8f0d098b
MD
120 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
121 fprintf(ofp, " Dynamic function entry/return probe.\n");
122 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
123 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
b213d387 124#if 0
8f0d098b
MD
125 fprintf(ofp, " --function:entry symbol\n");
126 fprintf(ofp, " Function tracer event\n");
b213d387 127#endif
a54bd42d 128 fprintf(ofp, " --syscall System call event\n");
f3ed775e 129 fprintf(ofp, "\n");
300b8fd5
MD
130 fprintf(ofp, " --loglevel name\n");
131 fprintf(ofp, " Tracepoint loglevel range from 0 to loglevel\n");
132 fprintf(ofp, " --loglevel-only name\n");
133 fprintf(ofp, " Tracepoint loglevel (only this loglevel)\n");
134 fprintf(ofp, "\n");
0c8477c8
MD
135 fprintf(ofp, " The loglevel or loglevel-only options should be\n");
136 fprintf(ofp, " combined with a tracepoint name or tracepoint\n");
137 fprintf(ofp, " wildcard.\n");
138 fprintf(ofp, " Available loglevels:\n");
139 fprintf(ofp, " (higher value is more verbose)\n");
46839cc2
MD
140 fprintf(ofp, " TRACE_EMERG = 0\n");
141 fprintf(ofp, " TRACE_ALERT = 1\n");
142 fprintf(ofp, " TRACE_CRIT = 2\n");
143 fprintf(ofp, " TRACE_ERR = 3\n");
144 fprintf(ofp, " TRACE_WARNING = 4\n");
145 fprintf(ofp, " TRACE_NOTICE = 5\n");
146 fprintf(ofp, " TRACE_INFO = 6\n");
147 fprintf(ofp, " TRACE_DEBUG_SYSTEM = 7\n");
148 fprintf(ofp, " TRACE_DEBUG_PROGRAM = 8\n");
149 fprintf(ofp, " TRACE_DEBUG_PROCESS = 9\n");
150 fprintf(ofp, " TRACE_DEBUG_MODULE = 10\n");
151 fprintf(ofp, " TRACE_DEBUG_UNIT = 11\n");
152 fprintf(ofp, " TRACE_DEBUG_FUNCTION = 12\n");
153 fprintf(ofp, " TRACE_DEBUG_LINE = 13\n");
154 fprintf(ofp, " TRACE_DEBUG = 14\n");
155 fprintf(ofp, " (shortcuts such as \"system\" are allowed)\n");
53a80697
MD
156 fprintf(ofp, " --filter \'expression\'\n");
157 fprintf(ofp, " Filter expression on event fields,\n");
158 fprintf(ofp, " event recording depends on evaluation.\n");
919e300c
MD
159 fprintf(ofp, " Only specify on first activation of\n");
160 fprintf(ofp, " a given event within a session.\n");
161 fprintf(ofp, " Filter only allowed when enabling\n");
162 fprintf(ofp, " events within a session before tracing\n");
9bd578f5
MD
163 fprintf(ofp, " is started. If the filter fails to link\n");
164 fprintf(ofp, " with the event within the traced domain,\n");
165 fprintf(ofp, " the event will be discarded. Currently,\n");
166 fprintf(ofp, " filter is only implemented for the user-space\n");
167 fprintf(ofp, " tracer.\n");
168 fprintf(ofp, " Expression examples:.\n");
169 fprintf(ofp, " \n");
170 fprintf(ofp, " 'intfield > 500 && intfield < 503'\n");
171 fprintf(ofp, " '(stringfield == \"test\" || intfield != 10) && intfield > 33'\n");
172 fprintf(ofp, " 'doublefield > 1.1 && intfield < 5.3'\n");
173 fprintf(ofp, " \n");
174 fprintf(ofp, " Wildcards are allowed at the end of strings:\n");
175 fprintf(ofp, " 'seqfield1 == \"te*\"'\n");
176 fprintf(ofp, " In string literals, the escape character is '\\'.\n");
177 fprintf(ofp, " Use '\\*' for the '*' character, and '\\\\' for\n");
178 fprintf(ofp, " the '\\' character.\n");
300b8fd5 179 fprintf(ofp, "\n");
f3ed775e
DG
180}
181
0d63dd19 182/*
6181537c 183 * Parse probe options.
0d63dd19 184 */
cf0e5467 185static int parse_probe_opts(struct lttng_event *ev, char *opt)
0d63dd19
DG
186{
187 int ret;
8ff0bbd0 188 char s_hex[19];
0d63dd19
DG
189 char name[LTTNG_SYMBOL_NAME_LEN];
190
191 if (opt == NULL) {
192 ret = -1;
8f0d098b 193 goto end;
0d63dd19
DG
194 }
195
196 /* Check for symbol+offset */
8ff0bbd0 197 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
0d63dd19 198 if (ret == 2) {
7d29a247 199 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 200 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
cf0e5467 201 DBG("probe symbol %s", ev->attr.probe.symbol_name);
8ff0bbd0
DG
202 if (strlen(s_hex) == 0) {
203 ERR("Invalid probe offset %s", s_hex);
0d63dd19 204 ret = -1;
8f0d098b 205 goto end;
0d63dd19 206 }
8ff0bbd0 207 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
cf0e5467 208 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
3000dc78 209 ev->attr.probe.addr = 0;
8f0d098b
MD
210 goto end;
211 }
212
213 /* Check for symbol */
214 if (isalpha(name[0])) {
215 ret = sscanf(opt, "%s", name);
216 if (ret == 1) {
217 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 218 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
8f0d098b
MD
219 DBG("probe symbol %s", ev->attr.probe.symbol_name);
220 ev->attr.probe.offset = 0;
221 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
222 ev->attr.probe.addr = 0;
223 goto end;
224 }
0d63dd19
DG
225 }
226
227 /* Check for address */
8ff0bbd0 228 ret = sscanf(opt, "%s", s_hex);
0d63dd19 229 if (ret > 0) {
8ff0bbd0
DG
230 if (strlen(s_hex) == 0) {
231 ERR("Invalid probe address %s", s_hex);
0d63dd19 232 ret = -1;
8f0d098b 233 goto end;
0d63dd19 234 }
8ff0bbd0 235 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
cf0e5467 236 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
3000dc78
DG
237 ev->attr.probe.offset = 0;
238 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
8f0d098b 239 goto end;
0d63dd19
DG
240 }
241
242 /* No match */
243 ret = -1;
244
8f0d098b 245end:
0d63dd19
DG
246 return ret;
247}
248
8005f29a 249/*
0c8477c8 250 * Maps loglevel from string to value
8005f29a
MD
251 */
252static
46839cc2 253int loglevel_str_to_value(const char *inputstr)
8005f29a 254{
46839cc2
MD
255 int i = 0;
256 char str[LTTNG_SYMBOL_NAME_LEN];
257
258 while (inputstr[i] != '\0' && i < LTTNG_SYMBOL_NAME_LEN) {
259 str[i] = toupper(inputstr[i]);
260 i++;
261 }
262 str[i] = '\0';
263 if (!strcmp(str, "TRACE_EMERG") || !strcmp(str, "EMERG")) {
264 return LTTNG_LOGLEVEL_EMERG;
265 } else if (!strcmp(str, "TRACE_ALERT") || !strcmp(str, "ALERT")) {
266 return LTTNG_LOGLEVEL_ALERT;
267 } else if (!strcmp(str, "TRACE_CRIT") || !strcmp(str, "CRIT")) {
268 return LTTNG_LOGLEVEL_CRIT;
269 } else if (!strcmp(str, "TRACE_ERR") || !strcmp(str, "ERR")) {
270 return LTTNG_LOGLEVEL_ERR;
271 } else if (!strcmp(str, "TRACE_WARNING") || !strcmp(str, "WARNING")) {
272 return LTTNG_LOGLEVEL_WARNING;
273 } else if (!strcmp(str, "TRACE_NOTICE") || !strcmp(str, "NOTICE")) {
274 return LTTNG_LOGLEVEL_NOTICE;
275 } else if (!strcmp(str, "TRACE_INFO") || !strcmp(str, "INFO")) {
276 return LTTNG_LOGLEVEL_INFO;
277 } else if (!strcmp(str, "TRACE_DEBUG_SYSTEM") || !strcmp(str, "DEBUG_SYSTEM") || !strcmp(str, "SYSTEM")) {
278 return LTTNG_LOGLEVEL_DEBUG_SYSTEM;
279 } else if (!strcmp(str, "TRACE_DEBUG_PROGRAM") || !strcmp(str, "DEBUG_PROGRAM") || !strcmp(str, "PROGRAM")) {
280 return LTTNG_LOGLEVEL_DEBUG_PROGRAM;
281 } else if (!strcmp(str, "TRACE_DEBUG_PROCESS") || !strcmp(str, "DEBUG_PROCESS") || !strcmp(str, "PROCESS")) {
282 return LTTNG_LOGLEVEL_DEBUG_PROCESS;
283 } else if (!strcmp(str, "TRACE_DEBUG_MODULE") || !strcmp(str, "DEBUG_MODULE") || !strcmp(str, "MODULE")) {
284 return LTTNG_LOGLEVEL_DEBUG_MODULE;
285 } else if (!strcmp(str, "TRACE_DEBUG_UNIT") || !strcmp(str, "DEBUG_UNIT") || !strcmp(str, "UNIT")) {
286 return LTTNG_LOGLEVEL_DEBUG_UNIT;
287 } else if (!strcmp(str, "TRACE_DEBUG_FUNCTION") || !strcmp(str, "DEBUG_FUNCTION") || !strcmp(str, "FUNCTION")) {
288 return LTTNG_LOGLEVEL_DEBUG_FUNCTION;
289 } else if (!strcmp(str, "TRACE_DEBUG_LINE") || !strcmp(str, "DEBUG_LINE") || !strcmp(str, "LINE")) {
290 return LTTNG_LOGLEVEL_DEBUG_LINE;
291 } else if (!strcmp(str, "TRACE_DEBUG") || !strcmp(str, "DEBUG")) {
292 return LTTNG_LOGLEVEL_DEBUG;
8005f29a
MD
293 } else {
294 return -1;
295 }
296}
297
f3ed775e 298/*
6181537c 299 * Enabling event using the lttng API.
f3ed775e 300 */
cd80958d 301static int enable_events(char *session_name)
f3ed775e 302{
ae856491 303 int err, ret = CMD_SUCCESS, warn = 0;
b73d0b29 304 char *event_name, *channel_name = NULL;
f3ed775e 305 struct lttng_event ev;
7d29a247 306 struct lttng_domain dom;
f3ed775e 307
441c16a7
MD
308 memset(&ev, 0, sizeof(ev));
309 memset(&dom, 0, sizeof(dom));
310
53a80697
MD
311 if (opt_kernel) {
312 if (opt_filter) {
313 ERR("Filter not implement for kernel tracing yet");
314 ret = CMD_ERROR;
315 goto error;
316 }
317 }
318
7d29a247
DG
319 /* Create lttng domain */
320 if (opt_kernel) {
321 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 322 } else if (opt_userspace) {
2bdd86d4 323 dom.type = LTTNG_DOMAIN_UST;
6181537c 324 } else {
e14f64a8 325 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
d16c1a4c 326 ret = CMD_ERROR;
6181537c 327 goto error;
2bdd86d4 328 }
7d29a247 329
ae856491
DG
330 if (opt_channel_name == NULL) {
331 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
332 if (err < 0) {
333 ret = CMD_FATAL;
334 goto error;
335 }
336 } else {
337 channel_name = opt_channel_name;
338 }
339
cd80958d
DG
340 handle = lttng_create_handle(session_name, &dom);
341 if (handle == NULL) {
342 ret = -1;
343 goto error;
344 }
1aef21b6 345
cd80958d 346 if (opt_enable_all) {
8c9ae521 347 /* Default setup for enable all */
75e8c5ab
MD
348 if (opt_kernel) {
349 ev.type = opt_event_type;
350 ev.name[0] = '\0';
300b8fd5
MD
351 /* kernel loglevels not implemented */
352 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
75e8c5ab
MD
353 } else {
354 ev.type = LTTNG_EVENT_TRACEPOINT;
355 strcpy(ev.name, "*");
300b8fd5
MD
356 ev.loglevel_type = opt_loglevel_type;
357 if (opt_loglevel) {
358 ev.loglevel = loglevel_str_to_value(opt_loglevel);
359 if (ev.loglevel == -1) {
360 ERR("Unknown loglevel %s", opt_loglevel);
361 ret = -1;
362 goto error;
363 }
22e25b71
MD
364 } else {
365 ev.loglevel = -1;
300b8fd5 366 }
75e8c5ab 367 }
8c9ae521
DG
368
369 ret = lttng_enable_event(handle, &ev, channel_name);
370 if (ret < 0) {
42224349 371 switch (-ret) {
f73fabfd 372 case LTTNG_ERR_KERN_EVENT_EXIST:
42224349
DG
373 WARN("Kernel events already enabled (channel %s, session %s)",
374 channel_name, session_name);
6d805133 375 break;
42224349 376 default:
a6de644c 377 ERR("Events: %s (channel %s, session %s)",
42224349
DG
378 lttng_strerror(ret), channel_name, session_name);
379 break;
380 }
6d805133 381 goto end;
8c9ae521 382 }
53a80697
MD
383 if (opt_filter) {
384 ret = lttng_set_event_filter(handle, ev.name, channel_name,
385 opt_filter);
386 if (ret < 0) {
387 ERR("Error setting filter");
388 ret = -1;
389 goto error;
390 }
391 }
8c9ae521 392
0133c199
MD
393 switch (opt_event_type) {
394 case LTTNG_EVENT_TRACEPOINT:
57064ada
MD
395 if (opt_loglevel) {
396 MSG("All %s tracepoints are enabled in channel %s for loglevel %s",
397 opt_kernel ? "kernel" : "UST", channel_name,
398 opt_loglevel);
399 } else {
400 MSG("All %s tracepoints are enabled in channel %s",
401 opt_kernel ? "kernel" : "UST", channel_name);
402
403 }
0133c199 404 break;
a54bd42d 405 case LTTNG_EVENT_SYSCALL:
0133c199 406 if (opt_kernel) {
8c9ae521
DG
407 MSG("All kernel system calls are enabled in channel %s",
408 channel_name);
c93d9daf 409 }
0133c199 410 break;
7a3d1328 411 case LTTNG_EVENT_ALL:
57064ada
MD
412 if (opt_loglevel) {
413 MSG("All %s events are enabled in channel %s for loglevel %s",
414 opt_kernel ? "kernel" : "UST", channel_name,
415 opt_loglevel);
416 } else {
417 MSG("All %s events are enabled in channel %s",
418 opt_kernel ? "kernel" : "UST", channel_name);
419 }
7a3d1328 420 break;
0133c199 421 default:
8c9ae521 422 /*
6181537c
DG
423 * We should not be here since lttng_enable_event should have
424 * failed on the event type.
8c9ae521 425 */
f3ed775e
DG
426 goto error;
427 }
8c9ae521 428 goto end;
f3ed775e
DG
429 }
430
431 /* Strip event list */
432 event_name = strtok(opt_event_list, ",");
433 while (event_name != NULL) {
6181537c
DG
434 /* Copy name and type of the event */
435 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
436 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
437 ev.type = opt_event_type;
438
f3ed775e
DG
439 /* Kernel tracer action */
440 if (opt_kernel) {
441 DBG("Enabling kernel event %s for channel %s",
442 event_name, channel_name);
f3ed775e
DG
443
444 switch (opt_event_type) {
7a3d1328
MD
445 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
446 ev.type = LTTNG_EVENT_TRACEPOINT;
447 /* Fall-through */
e6ddca71 448 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 449 break;
7d29a247 450 case LTTNG_EVENT_PROBE:
cf0e5467 451 ret = parse_probe_opts(&ev, opt_probe);
0d63dd19 452 if (ret < 0) {
cf0e5467 453 ERR("Unable to parse probe options");
0d63dd19
DG
454 ret = 0;
455 goto error;
456 }
f3ed775e
DG
457 break;
458 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
459 ret = parse_probe_opts(&ev, opt_function);
460 if (ret < 0) {
461 ERR("Unable to parse function probe options");
462 ret = 0;
463 goto error;
464 }
465 break;
466 case LTTNG_EVENT_FUNCTION_ENTRY:
6181537c
DG
467 strncpy(ev.attr.ftrace.symbol_name, opt_function_entry_symbol,
468 LTTNG_SYMBOL_NAME_LEN);
99497cd0 469 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 470 break;
a54bd42d 471 case LTTNG_EVENT_SYSCALL:
6181537c
DG
472 MSG("per-syscall selection not supported yet. Use \"-a\" "
473 "for all syscalls.");
f3ed775e 474 default:
1ab1ea0b 475 ret = CMD_UNDEFINED;
f3ed775e
DG
476 goto error;
477 }
0cda4f28 478
ed7f4083 479 if (opt_loglevel) {
0cda4f28
MD
480 MSG("Kernel loglevels are not supported.");
481 ret = CMD_UNDEFINED;
482 goto error;
483 }
484
485 /* kernel loglevels not implemented */
8005f29a 486 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
f3ed775e 487 } else if (opt_userspace) { /* User-space tracer action */
d78d6610 488#if 0
e14f64a8
DG
489 if (opt_cmd_name != NULL || opt_pid) {
490 MSG("Only supporting tracing all UST processes (-u) for now.");
1ab1ea0b 491 ret = CMD_UNDEFINED;
2bdd86d4
MD
492 goto error;
493 }
d78d6610 494#endif
6181537c 495
300b8fd5
MD
496 DBG("Enabling UST event %s for channel %s, loglevel %s", event_name,
497 channel_name, opt_loglevel ? : "<all>");
2bdd86d4
MD
498
499 switch (opt_event_type) {
500 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
2bdd86d4
MD
501 /* Fall-through */
502 case LTTNG_EVENT_TRACEPOINT:
e4baff1e
MD
503 /* Copy name and type of the event */
504 ev.type = LTTNG_EVENT_TRACEPOINT;
505 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
506 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2bdd86d4
MD
507 break;
508 case LTTNG_EVENT_PROBE:
509 case LTTNG_EVENT_FUNCTION:
510 case LTTNG_EVENT_FUNCTION_ENTRY:
511 case LTTNG_EVENT_SYSCALL:
512 default:
cc62c0c0 513 ERR("Event type not available for user-space tracing");
1ab1ea0b 514 ret = CMD_UNDEFINED;
2bdd86d4
MD
515 goto error;
516 }
0cda4f28
MD
517
518 ev.loglevel_type = opt_loglevel_type;
ed7f4083 519 if (opt_loglevel) {
8005f29a
MD
520 ev.loglevel = loglevel_str_to_value(opt_loglevel);
521 if (ev.loglevel == -1) {
522 ERR("Unknown loglevel %s", opt_loglevel);
300b8fd5 523 ret = -1;
8005f29a
MD
524 goto error;
525 }
22e25b71
MD
526 } else {
527 ev.loglevel = -1;
ed7f4083 528 }
f3ed775e 529 } else {
e14f64a8 530 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
300b8fd5 531 ret = CMD_ERROR;
f3ed775e
DG
532 goto error;
533 }
534
6181537c 535 ret = lttng_enable_event(handle, &ev, channel_name);
ae856491 536 if (ret < 0) {
42224349
DG
537 /* Turn ret to positive value to handle the positive error code */
538 switch (-ret) {
f73fabfd 539 case LTTNG_ERR_KERN_EVENT_EXIST:
42224349
DG
540 WARN("Kernel event %s already enabled (channel %s, session %s)",
541 event_name, channel_name, session_name);
542 break;
543 default:
544 ERR("Event %s: %s (channel %s, session %s)", event_name,
545 lttng_strerror(ret), channel_name, session_name);
546 break;
547 }
ae856491
DG
548 warn = 1;
549 } else {
6181537c
DG
550 MSG("%s event %s created in channel %s",
551 opt_kernel ? "kernel": "UST", event_name, channel_name);
552 }
53a80697
MD
553 if (opt_filter) {
554 ret = lttng_set_event_filter(handle, ev.name,
555 channel_name, opt_filter);
556 if (ret < 0) {
557 ERR("Error setting filter");
558 ret = -1;
559 goto error;
560 }
561 }
6181537c 562
f3ed775e
DG
563 /* Next event */
564 event_name = strtok(NULL, ",");
565 }
566
8c9ae521 567end:
f3ed775e 568error:
ae856491
DG
569 if (warn) {
570 ret = CMD_WARNING;
571 }
b73d0b29
MD
572 if (opt_channel_name == NULL) {
573 free(channel_name);
574 }
cd80958d
DG
575 lttng_destroy_handle(handle);
576
f3ed775e
DG
577 return ret;
578}
579
580/*
6181537c 581 * Add event to trace session
f3ed775e
DG
582 */
583int cmd_enable_events(int argc, const char **argv)
584{
ca1c3607 585 int opt, ret = CMD_SUCCESS;
f3ed775e 586 static poptContext pc;
cd80958d 587 char *session_name = NULL;
f3ed775e
DG
588
589 pc = poptGetContext(NULL, argc, argv, long_options, 0);
590 poptReadDefaultConfig(pc, 0);
591
592 /* Default event type */
7a3d1328 593 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
594
595 while ((opt = poptGetNextOpt(pc)) != -1) {
596 switch (opt) {
597 case OPT_HELP:
ca1c3607 598 usage(stdout);
f3ed775e 599 goto end;
f3ed775e 600 case OPT_TRACEPOINT:
e6ddca71 601 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e 602 break;
cf0e5467 603 case OPT_PROBE:
7d29a247 604 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e
DG
605 break;
606 case OPT_FUNCTION:
607 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b
MD
608 break;
609 case OPT_FUNCTION_ENTRY:
610 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
f3ed775e 611 break;
a54bd42d
MD
612 case OPT_SYSCALL:
613 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 614 break;
eeac7d46
MD
615 case OPT_USERSPACE:
616 opt_userspace = 1;
eeac7d46 617 break;
0cda4f28 618 case OPT_LOGLEVEL:
8005f29a 619 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
0cda4f28
MD
620 opt_loglevel = poptGetOptArg(pc);
621 break;
622 case OPT_LOGLEVEL_ONLY:
8005f29a 623 opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
0cda4f28 624 opt_loglevel = poptGetOptArg(pc);
13dce3b7 625 break;
679b4943
SM
626 case OPT_LIST_OPTIONS:
627 list_cmd_options(stdout, long_options);
679b4943 628 goto end;
53a80697
MD
629 case OPT_FILTER:
630 break;
f3ed775e
DG
631 default:
632 usage(stderr);
633 ret = CMD_UNDEFINED;
634 goto end;
635 }
636 }
637
638 opt_event_list = (char*) poptGetArg(pc);
639 if (opt_event_list == NULL && opt_enable_all == 0) {
640 ERR("Missing event name(s).\n");
641 usage(stderr);
ca1c3607 642 ret = CMD_ERROR;
f3ed775e
DG
643 goto end;
644 }
645
cd80958d
DG
646 if (!opt_session_name) {
647 session_name = get_session_name();
648 if (session_name == NULL) {
ca1c3607 649 ret = CMD_ERROR;
cd80958d
DG
650 goto end;
651 }
652 } else {
653 session_name = opt_session_name;
654 }
655
656 ret = enable_events(session_name);
f3ed775e
DG
657
658end:
cd80958d
DG
659 if (opt_session_name == NULL) {
660 free(session_name);
661 }
662
ca1c3607 663 poptFreeContext(pc);
f3ed775e
DG
664 return ret;
665}
This page took 0.112032 seconds and 4 git commands to generate.