listing and activation of loglevel by number
[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 50 OPT_TRACEPOINT,
cf0e5467 51 OPT_PROBE,
f3ed775e 52 OPT_FUNCTION,
8f0d098b 53 OPT_FUNCTION_ENTRY,
a54bd42d 54 OPT_SYSCALL,
eeac7d46 55 OPT_USERSPACE,
13dce3b7 56 OPT_TRACEPOINT_LOGLEVEL,
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},
7ebae521 72 {"probe", 0, POPT_ARG_STRING, &opt_probe, OPT_PROBE, 0, 0},
40e9d5d3 73 {"function", 0, POPT_ARG_STRING, &opt_function, OPT_FUNCTION, 0, 0},
b213d387
MD
74#if 0
75 /*
76 * Currently removed from lttng kernel tracer. Removed from
77 * lttng UI to discourage its use.
78 */
40e9d5d3 79 {"function:entry", 0, POPT_ARG_STRING, &opt_function_entry_symbol, OPT_FUNCTION_ENTRY, 0, 0},
b213d387 80#endif
7ebae521 81 {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0},
13dce3b7 82 {"loglevel", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT_LOGLEVEL, 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");
13dce3b7 104 fprintf(ofp, " --loglevel Tracepoint loglevel\n");
8f0d098b 105 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
cf0e5467
MD
106 fprintf(ofp, " Dynamic probe.\n");
107 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
108 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
8f0d098b
MD
109 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
110 fprintf(ofp, " Dynamic function entry/return probe.\n");
111 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
112 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
b213d387 113#if 0
8f0d098b
MD
114 fprintf(ofp, " --function:entry symbol\n");
115 fprintf(ofp, " Function tracer event\n");
b213d387 116#endif
a54bd42d 117 fprintf(ofp, " --syscall System call event\n");
f3ed775e
DG
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);
2bdd86d4
MD
338
339 switch (opt_event_type) {
340 case LTTNG_EVENT_ALL: /* Default behavior is tracepoint */
2bdd86d4
MD
341 /* Fall-through */
342 case LTTNG_EVENT_TRACEPOINT:
e4baff1e
MD
343 /* Copy name and type of the event */
344 ev.type = LTTNG_EVENT_TRACEPOINT;
345 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
346 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
2bdd86d4 347 break;
13dce3b7 348 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL:
e4baff1e 349 /* Copy name and type of the event */
13dce3b7 350 ev.type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
e4baff1e
MD
351 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
352 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
13dce3b7 353 break;
2bdd86d4
MD
354 case LTTNG_EVENT_PROBE:
355 case LTTNG_EVENT_FUNCTION:
356 case LTTNG_EVENT_FUNCTION_ENTRY:
357 case LTTNG_EVENT_SYSCALL:
358 default:
359 ret = CMD_NOT_IMPLEMENTED;
360 goto error;
361 }
362
363 ret = lttng_enable_event(handle, &ev, channel_name);
364 if (ret == 0) {
365 MSG("UST event %s created in channel %s", event_name, channel_name);
f3ed775e 366 }
f3ed775e 367 } else {
048e2159 368 ERR("Please specify a tracer (--kernel or --userspace)");
f3ed775e
DG
369 goto error;
370 }
371
372 /* Next event */
373 event_name = strtok(NULL, ",");
374 }
375
8c9ae521 376end:
f3ed775e 377error:
b73d0b29
MD
378 if (opt_channel_name == NULL) {
379 free(channel_name);
380 }
cd80958d
DG
381 lttng_destroy_handle(handle);
382
f3ed775e
DG
383 return ret;
384}
385
386/*
387 * cmd_enable_events
388 *
389 * Add event to trace session
390 */
391int cmd_enable_events(int argc, const char **argv)
392{
393 int opt, ret;
394 static poptContext pc;
cd80958d 395 char *session_name = NULL;
f3ed775e
DG
396
397 pc = poptGetContext(NULL, argc, argv, long_options, 0);
398 poptReadDefaultConfig(pc, 0);
399
400 /* Default event type */
7a3d1328 401 opt_event_type = LTTNG_EVENT_ALL;
f3ed775e
DG
402
403 while ((opt = poptGetNextOpt(pc)) != -1) {
404 switch (opt) {
405 case OPT_HELP:
406 usage(stderr);
407 ret = CMD_SUCCESS;
408 goto end;
f3ed775e 409 case OPT_TRACEPOINT:
e6ddca71 410 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e 411 break;
cf0e5467 412 case OPT_PROBE:
7d29a247 413 opt_event_type = LTTNG_EVENT_PROBE;
f3ed775e
DG
414 break;
415 case OPT_FUNCTION:
416 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b
MD
417 break;
418 case OPT_FUNCTION_ENTRY:
419 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
f3ed775e 420 break;
a54bd42d
MD
421 case OPT_SYSCALL:
422 opt_event_type = LTTNG_EVENT_SYSCALL;
0133c199 423 break;
eeac7d46
MD
424 case OPT_USERSPACE:
425 opt_userspace = 1;
eeac7d46 426 break;
13dce3b7
MD
427 case OPT_TRACEPOINT_LOGLEVEL:
428 opt_event_type = LTTNG_EVENT_TRACEPOINT_LOGLEVEL;
429 break;
f3ed775e
DG
430 default:
431 usage(stderr);
432 ret = CMD_UNDEFINED;
433 goto end;
434 }
435 }
436
437 opt_event_list = (char*) poptGetArg(pc);
438 if (opt_event_list == NULL && opt_enable_all == 0) {
439 ERR("Missing event name(s).\n");
440 usage(stderr);
441 ret = CMD_SUCCESS;
442 goto end;
443 }
444
cd80958d
DG
445 if (!opt_session_name) {
446 session_name = get_session_name();
447 if (session_name == NULL) {
448 ret = -1;
449 goto end;
450 }
451 } else {
452 session_name = opt_session_name;
453 }
454
455 ret = enable_events(session_name);
f3ed775e
DG
456
457end:
cd80958d
DG
458 if (opt_session_name == NULL) {
459 free(session_name);
460 }
461
f3ed775e
DG
462 return ret;
463}
This page took 0.07444 seconds and 4 git commands to generate.