Add loglevel to event list
[lttng-tools.git] / lttng / commands / enable_events.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
f3ed775e
DG
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#define _GNU_SOURCE
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>
5a0de755 27#include <inttypes.h>
8f0d098b 28#include <ctype.h>
f3ed775e 29
6e2d116c
DG
30#include "../cmd.h"
31#include "../conf.h"
32#include "../utils.h"
f3ed775e
DG
33
34static char *opt_event_list;
35static int opt_event_type;
36static char *opt_kernel;
5440dc42 37static char *opt_session_name;
f3ed775e
DG
38static int opt_pid_all;
39static int opt_userspace;
eeac7d46 40static char *opt_cmd_name;
f3ed775e
DG
41static int opt_enable_all;
42static pid_t opt_pid;
cf0e5467 43static char *opt_probe;
8f0d098b
MD
44static char *opt_function;
45static char *opt_function_entry_symbol;
f3ed775e
DG
46static char *opt_channel_name;
47
48enum {
49 OPT_HELP = 1,
f3ed775e
DG
50 OPT_TRACEPOINT,
51 OPT_MARKER,
cf0e5467 52 OPT_PROBE,
f3ed775e 53 OPT_FUNCTION,
8f0d098b 54 OPT_FUNCTION_ENTRY,
a54bd42d 55 OPT_SYSCALL,
eeac7d46 56 OPT_USERSPACE,
f3ed775e
DG
57};
58
cd80958d
DG
59static struct lttng_handle *handle;
60
f3ed775e
DG
61static struct poptOption long_options[] = {
62 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
63 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 64 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
f3ed775e
DG
65 {"all-events", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
66 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
67 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
40e9d5d3 68 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
f3ed775e
DG
69 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
70 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
71 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
72 {"marker", 0, POPT_ARG_NONE, 0, OPT_MARKER, 0, 0},
7ebae521 73 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
40e9d5d3 74 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
b213d387
MD
75#if 0
76 /*
77 * Currently removed from lttng kernel tracer. Removed from
78 * lttng UI to discourage its use.
79 */
40e9d5d3 80 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
b213d387 81#endif
7ebae521 82 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
f3ed775e
DG
83 {0, 0, 0, 0, 0, 0, 0}
84};
85
86/*
87 * usage
88 */
89static void usage(FILE *ofp)
90{
91 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
92 fprintf(ofp, "\n");
93 fprintf(ofp, " -h, --help Show this help\n");
5440dc42 94 fprintf(ofp, " -s, --session Apply on session name\n");
f3ed775e
DG
95 fprintf(ofp, " -c, --channel Apply on this channel\n");
96 fprintf(ofp, " -a, --all-events Enable all tracepoints\n");
97 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
98 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
99 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
100 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
101 fprintf(ofp, "\n");
102 fprintf(ofp, "Event options:\n");
103 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
8f0d098b 104 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
cf0e5467
MD
105 fprintf(ofp, " Dynamic probe.\n");
106 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
107 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
8f0d098b
MD
108 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
109 fprintf(ofp, " Dynamic function entry/return probe.\n");
110 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
111 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
b213d387 112#if 0
8f0d098b
MD
113 fprintf(ofp, " --function:entry symbol\n");
114 fprintf(ofp, " Function tracer event\n");
b213d387 115#endif
a54bd42d 116 fprintf(ofp, " --syscall System call event\n");
f3ed775e
DG
117 fprintf(ofp, " --marker User-space marker (deprecated)\n");
118 fprintf(ofp, "\n");
119}
120
0d63dd19 121/*
cf0e5467 122 * parse_probe_addr
0d63dd19 123 *
cf0e5467 124 * Parse probe options.
0d63dd19 125 */
cf0e5467 126static int parse_probe_opts(struct lttng_event *ev, char *opt)
0d63dd19
DG
127{
128 int ret;
8ff0bbd0 129 char s_hex[19];
0d63dd19
DG
130 char name[LTTNG_SYMBOL_NAME_LEN];
131
132 if (opt == NULL) {
133 ret = -1;
8f0d098b 134 goto end;
0d63dd19
DG
135 }
136
137 /* Check for symbol+offset */
8ff0bbd0 138 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
0d63dd19 139 if (ret == 2) {
7d29a247 140 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 141 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
cf0e5467 142 DBG("probe symbol %s", ev->attr.probe.symbol_name);
8ff0bbd0
DG
143 if (strlen(s_hex) == 0) {
144 ERR("Invalid probe offset %s", s_hex);
0d63dd19 145 ret = -1;
8f0d098b 146 goto end;
0d63dd19 147 }
8ff0bbd0 148 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
cf0e5467 149 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
3000dc78 150 ev->attr.probe.addr = 0;
8f0d098b
MD
151 goto end;
152 }
153
154 /* Check for symbol */
155 if (isalpha(name[0])) {
156 ret = sscanf(opt, "%s", name);
157 if (ret == 1) {
158 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 159 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
8f0d098b
MD
160 DBG("probe symbol %s", ev->attr.probe.symbol_name);
161 ev->attr.probe.offset = 0;
162 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
163 ev->attr.probe.addr = 0;
164 goto end;
165 }
0d63dd19
DG
166 }
167
168 /* Check for address */
8ff0bbd0 169 ret = sscanf(opt, "%s", s_hex);
0d63dd19 170 if (ret > 0) {
8ff0bbd0
DG
171 if (strlen(s_hex) == 0) {
172 ERR("Invalid probe address %s", s_hex);
0d63dd19 173 ret = -1;
8f0d098b 174 goto end;
0d63dd19 175 }
8ff0bbd0 176 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
cf0e5467 177 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
3000dc78
DG
178 ev->attr.probe.offset = 0;
179 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
8f0d098b 180 goto end;
0d63dd19
DG
181 }
182
183 /* No match */
184 ret = -1;
185
8f0d098b 186end:
0d63dd19
DG
187 return ret;
188}
189
f3ed775e
DG
190/*
191 * enable_events
192 *
193 * Enabling event using the lttng API.
194 */
cd80958d 195static int enable_events(char *session_name)
f3ed775e
DG
196{
197 int err, ret = CMD_SUCCESS;
b73d0b29 198 char *event_name, *channel_name = NULL;
f3ed775e 199 struct lttng_event ev;
7d29a247 200 struct lttng_domain dom;
f3ed775e 201
f3ed775e
DG
202 if (opt_channel_name == NULL) {
203 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
204 if (err < 0) {
205 ret = CMD_FATAL;
206 goto error;
207 }
208 } else {
209 channel_name = opt_channel_name;
210 }
211
2bdd86d4
MD
212 if (opt_kernel && opt_userspace) {
213 MSG("Choose only one of --kernel or --userspace");
214 ret = CMD_FATAL;
215 goto error;
216 }
7d29a247
DG
217 /* Create lttng domain */
218 if (opt_kernel) {
219 dom.type = LTTNG_DOMAIN_KERNEL;
220 }
2bdd86d4
MD
221 if (opt_userspace) {
222 /* TODO
223 * LTTNG_DOMAIN_UST_EXEC_NAME,
224 * LTTNG_DOMAIN_UST_PID,
225 * LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
226 */
227 dom.type = LTTNG_DOMAIN_UST;
228 }
7d29a247 229
cd80958d
DG
230 handle = lttng_create_handle(session_name, &dom);
231 if (handle == NULL) {
232 ret = -1;
233 goto error;
234 }
1aef21b6 235
cd80958d 236 if (opt_enable_all) {
8c9ae521
DG
237 /* Default setup for enable all */
238 ev.name[0] = '\0';
239 ev.type = opt_event_type;
240
241 ret = lttng_enable_event(handle, &ev, channel_name);
242 if (ret < 0) {
243 goto error;
244 }
245
0133c199
MD
246 switch (opt_event_type) {
247 case LTTNG_EVENT_TRACEPOINT:
2bdd86d4
MD
248 MSG("All %s tracepoints are enabled in channel %s",
249 opt_kernel ? "kernel" : "UST", channel_name);
0133c199 250 break;
a54bd42d 251 case LTTNG_EVENT_SYSCALL:
0133c199 252 if (opt_kernel) {
8c9ae521
DG
253 MSG("All kernel system calls are enabled in channel %s",
254 channel_name);
c93d9daf 255 }
0133c199 256 break;
7a3d1328 257 case LTTNG_EVENT_ALL:
2bdd86d4
MD
258 MSG("All %s events are enabled in channel %s",
259 opt_kernel ? "kernel" : "UST", channel_name);
7a3d1328 260 break;
0133c199 261 default:
8c9ae521 262 /*
2bdd86d4
MD
263 * We should not be here since
264 * lttng_enable_event should have failed on the
265 * event type.
8c9ae521 266 */
f3ed775e
DG
267 goto error;
268 }
8c9ae521 269 goto end;
f3ed775e
DG
270 }
271
272 /* Strip event list */
273 event_name = strtok(opt_event_list, ",");
274 while (event_name != NULL) {
275 /* Kernel tracer action */
276 if (opt_kernel) {
277 DBG("Enabling kernel event %s for channel %s",
278 event_name, channel_name);
279 /* Copy name and type of the event */
280 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 281 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e
DG
282 ev.type = opt_event_type;
283
284 switch (opt_event_type) {
7a3d1328
MD
285 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
286 ev.type = LTTNG_EVENT_TRACEPOINT;
287 /* Fall-through */
e6ddca71 288 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 289 break;
7d29a247 290 case LTTNG_EVENT_PROBE:
cf0e5467 291 ret = parse_probe_opts(&ev, opt_probe);
0d63dd19 292 if (ret < 0) {
cf0e5467 293 ERR("Unable to parse probe options");
0d63dd19
DG
294 ret = 0;
295 goto error;
296 }
f3ed775e
DG
297 break;
298 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
299 ret = parse_probe_opts(&ev, opt_function);
300 if (ret < 0) {
301 ERR("Unable to parse function probe options");
302 ret = 0;
303 goto error;
304 }
305 break;
306 case LTTNG_EVENT_FUNCTION_ENTRY:
307 strncpy(ev.attr.ftrace.symbol_name,
308 opt_function_entry_symbol,
309 LTTNG_SYMBOL_NAME_LEN);
99497cd0 310 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e 311 break;
a54bd42d 312 case LTTNG_EVENT_SYSCALL:
0133c199
MD
313 MSG("per-syscall selection not supported yet. Use \"-a\" for all syscalls.");
314 ret = CMD_NOT_IMPLEMENTED;
315 goto error;
f3ed775e
DG
316 default:
317 ret = CMD_NOT_IMPLEMENTED;
318 goto error;
319 }
320
cd80958d 321 ret = lttng_enable_event(handle, &ev, channel_name);
7d29a247 322 if (ret == 0) {
f3ed775e
DG
323 MSG("Kernel event %s created in channel %s", event_name, channel_name);
324 }
325 } else if (opt_userspace) { /* User-space tracer action */
326 /*
2bdd86d4
MD
327 * TODO: only supporting pid_all tracing for
328 * now. Should have different domain based on
329 * opt_pid.
f3ed775e 330 */
2bdd86d4
MD
331 if (!opt_pid_all) {
332 MSG("Only supporting tracing all UST processes (-u --all) for now.");
333 ret = CMD_NOT_IMPLEMENTED;
334 goto error;
335 }
336 DBG("Enabling UST event %s for channel %s",
337 event_name, channel_name);
338 /* Copy name and type of the event */
339 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
340 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
341 ev.type = opt_event_type;
342
343 switch (opt_event_type) {
344 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
345 ev.type = LTTNG_EVENT_TRACEPOINT;
346 /* Fall-through */
347 case LTTNG_EVENT_TRACEPOINT:
348 break;
349 case LTTNG_EVENT_PROBE:
350 case LTTNG_EVENT_FUNCTION:
351 case LTTNG_EVENT_FUNCTION_ENTRY:
352 case LTTNG_EVENT_SYSCALL:
353 default:
354 ret = CMD_NOT_IMPLEMENTED;
355 goto error;
356 }
357
358 ret = lttng_enable_event(handle, &ev, channel_name);
359 if (ret == 0) {
360 MSG("UST event %s created in channel %s", event_name, channel_name);
f3ed775e 361 }
f3ed775e 362 } else {
048e2159 363 ERR("Please specify a tracer (--kernel or --userspace)");
f3ed775e
DG
364 goto error;
365 }
366
367 /* Next event */
368 event_name = strtok(NULL, ",");
369 }
370
8c9ae521 371end:
f3ed775e 372error:
b73d0b29
MD
373 if (opt_channel_name == NULL) {
374 free(channel_name);
375 }
cd80958d
DG
376 lttng_destroy_handle(handle);
377
f3ed775e
DG
378 return ret;
379}
380
381/*
382 * cmd_enable_events
383 *
384 * Add event to trace session
385 */
386int cmd_enable_events(int argc, const char **argv)
387{
388 int opt, ret;
389 static poptContext pc;
cd80958d 390 char *session_name = NULL;
f3ed775e
DG
391
392 pc = poptGetContext(NULL, argc, argv, long_options, 0);
393 poptReadDefaultConfig(pc, 0);
394
395 /* Default event type */
7a3d1328 396 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
397
398 while ((opt = poptGetNextOpt(pc)) != -1) {
399 switch (opt) {
400 case OPT_HELP:
401 usage(stderr);
402 ret = CMD_SUCCESS;
403 goto end;
f3ed775e 404 case OPT_TRACEPOINT:
e6ddca71 405 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e
DG
406 break;
407 case OPT_MARKER:
408 ret = CMD_NOT_IMPLEMENTED;
409 goto end;
cf0e5467 410 case OPT_PROBE:
7d29a247 411 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e
DG
412 break;
413 case OPT_FUNCTION:
414 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b
MD
415 break;
416 case OPT_FUNCTION_ENTRY:
417 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
f3ed775e 418 break;
a54bd42d
MD
419 case OPT_SYSCALL:
420 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 421 break;
eeac7d46
MD
422 case OPT_USERSPACE:
423 opt_userspace = 1;
eeac7d46 424 break;
f3ed775e
DG
425 default:
426 usage(stderr);
427 ret = CMD_UNDEFINED;
428 goto end;
429 }
430 }
431
432 opt_event_list = (char*) poptGetArg(pc);
433 if (opt_event_list == NULL && opt_enable_all == 0) {
434 ERR("Missing event name(s).\n");
435 usage(stderr);
436 ret = CMD_SUCCESS;
437 goto end;
438 }
439
cd80958d
DG
440 if (!opt_session_name) {
441 session_name = get_session_name();
442 if (session_name == NULL) {
443 ret = -1;
444 goto end;
445 }
446 } else {
447 session_name = opt_session_name;
448 }
449
450 ret = enable_events(session_name);
f3ed775e
DG
451
452end:
cd80958d
DG
453 if (opt_session_name == NULL) {
454 free(session_name);
455 }
456
f3ed775e
DG
457 return ret;
458}
This page took 0.073945 seconds and 4 git commands to generate.