Update version to 2.0-pre13
[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;
37static char *opt_cmd_name;
5440dc42 38static char *opt_session_name;
f3ed775e
DG
39static int opt_pid_all;
40static int opt_userspace;
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,
50 OPT_USERSPACE,
51 OPT_TRACEPOINT,
52 OPT_MARKER,
cf0e5467 53 OPT_PROBE,
f3ed775e 54 OPT_FUNCTION,
8f0d098b 55 OPT_FUNCTION_ENTRY,
f3ed775e
DG
56};
57
cd80958d
DG
58static struct lttng_handle *handle;
59
f3ed775e
DG
60static struct poptOption long_options[] = {
61 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
62 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 63 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
f3ed775e
DG
64 {"all-events", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
65 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
66 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
67 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, 0, OPT_USERSPACE, 0, 0},
68 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
69 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
70 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
71 {"marker", 0, POPT_ARG_NONE, 0, OPT_MARKER, 0, 0},
cf0e5467 72 {"probe", 0, POPT_ARG_STRING, 0, OPT_PROBE, 0, 0},
f3ed775e 73 {"function", 0, POPT_ARG_STRING, 0, OPT_FUNCTION, 0, 0},
8f0d098b 74 {"function:entry", 0, POPT_ARG_STRING, 0, OPT_FUNCTION_ENTRY, 0, 0},
f3ed775e
DG
75 {0, 0, 0, 0, 0, 0, 0}
76};
77
78/*
79 * usage
80 */
81static void usage(FILE *ofp)
82{
83 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
84 fprintf(ofp, "\n");
85 fprintf(ofp, " -h, --help Show this help\n");
5440dc42 86 fprintf(ofp, " -s, --session Apply on session name\n");
f3ed775e
DG
87 fprintf(ofp, " -c, --channel Apply on this channel\n");
88 fprintf(ofp, " -a, --all-events Enable all tracepoints\n");
89 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
90 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
91 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
92 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
93 fprintf(ofp, "\n");
94 fprintf(ofp, "Event options:\n");
95 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
8f0d098b 96 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
cf0e5467
MD
97 fprintf(ofp, " Dynamic probe.\n");
98 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
99 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
8f0d098b
MD
100 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
101 fprintf(ofp, " Dynamic function entry/return probe.\n");
102 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
103 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
104 fprintf(ofp, " --function:entry symbol\n");
105 fprintf(ofp, " Function tracer event\n");
f3ed775e
DG
106 fprintf(ofp, " --marker User-space marker (deprecated)\n");
107 fprintf(ofp, "\n");
108}
109
0d63dd19 110/*
cf0e5467 111 * parse_probe_addr
0d63dd19 112 *
cf0e5467 113 * Parse probe options.
0d63dd19 114 */
cf0e5467 115static int parse_probe_opts(struct lttng_event *ev, char *opt)
0d63dd19
DG
116{
117 int ret;
8ff0bbd0 118 char s_hex[19];
0d63dd19
DG
119 char name[LTTNG_SYMBOL_NAME_LEN];
120
121 if (opt == NULL) {
122 ret = -1;
8f0d098b 123 goto end;
0d63dd19
DG
124 }
125
126 /* Check for symbol+offset */
8ff0bbd0 127 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
0d63dd19 128 if (ret == 2) {
7d29a247 129 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 130 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
cf0e5467 131 DBG("probe symbol %s", ev->attr.probe.symbol_name);
8ff0bbd0
DG
132 if (strlen(s_hex) == 0) {
133 ERR("Invalid probe offset %s", s_hex);
0d63dd19 134 ret = -1;
8f0d098b 135 goto end;
0d63dd19 136 }
8ff0bbd0 137 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
cf0e5467 138 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
3000dc78 139 ev->attr.probe.addr = 0;
8f0d098b
MD
140 goto end;
141 }
142
143 /* Check for symbol */
144 if (isalpha(name[0])) {
145 ret = sscanf(opt, "%s", name);
146 if (ret == 1) {
147 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 148 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
8f0d098b
MD
149 DBG("probe symbol %s", ev->attr.probe.symbol_name);
150 ev->attr.probe.offset = 0;
151 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
152 ev->attr.probe.addr = 0;
153 goto end;
154 }
0d63dd19
DG
155 }
156
157 /* Check for address */
8ff0bbd0 158 ret = sscanf(opt, "%s", s_hex);
0d63dd19 159 if (ret > 0) {
8ff0bbd0
DG
160 if (strlen(s_hex) == 0) {
161 ERR("Invalid probe address %s", s_hex);
0d63dd19 162 ret = -1;
8f0d098b 163 goto end;
0d63dd19 164 }
8ff0bbd0 165 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
cf0e5467 166 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
3000dc78
DG
167 ev->attr.probe.offset = 0;
168 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
8f0d098b 169 goto end;
0d63dd19
DG
170 }
171
172 /* No match */
173 ret = -1;
174
8f0d098b 175end:
0d63dd19
DG
176 return ret;
177}
178
f3ed775e
DG
179/*
180 * enable_events
181 *
182 * Enabling event using the lttng API.
183 */
cd80958d 184static int enable_events(char *session_name)
f3ed775e
DG
185{
186 int err, ret = CMD_SUCCESS;
b73d0b29 187 char *event_name, *channel_name = NULL;
f3ed775e 188 struct lttng_event ev;
7d29a247 189 struct lttng_domain dom;
f3ed775e 190
f3ed775e
DG
191 if (opt_channel_name == NULL) {
192 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
193 if (err < 0) {
194 ret = CMD_FATAL;
195 goto error;
196 }
197 } else {
198 channel_name = opt_channel_name;
199 }
200
7d29a247
DG
201 /* Create lttng domain */
202 if (opt_kernel) {
203 dom.type = LTTNG_DOMAIN_KERNEL;
204 }
205
cd80958d
DG
206 handle = lttng_create_handle(session_name, &dom);
207 if (handle == NULL) {
208 ret = -1;
209 goto error;
210 }
1aef21b6 211
cd80958d 212 if (opt_enable_all) {
f3ed775e 213 if (opt_kernel) {
cd80958d 214 ret = lttng_enable_event(handle, NULL, channel_name);
c93d9daf
DG
215 if (ret == 0) {
216 MSG("All kernel events are enabled in channel %s", channel_name);
217 }
f3ed775e
DG
218 goto error;
219 }
220
221 /* TODO: User-space tracer */
222 }
223
224 /* Strip event list */
225 event_name = strtok(opt_event_list, ",");
226 while (event_name != NULL) {
227 /* Kernel tracer action */
228 if (opt_kernel) {
229 DBG("Enabling kernel event %s for channel %s",
230 event_name, channel_name);
231 /* Copy name and type of the event */
232 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
99497cd0 233 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e
DG
234 ev.type = opt_event_type;
235
236 switch (opt_event_type) {
e6ddca71 237 case LTTNG_EVENT_TRACEPOINT:
f3ed775e 238 break;
7d29a247 239 case LTTNG_EVENT_PROBE:
cf0e5467 240 ret = parse_probe_opts(&ev, opt_probe);
0d63dd19 241 if (ret < 0) {
cf0e5467 242 ERR("Unable to parse probe options");
0d63dd19
DG
243 ret = 0;
244 goto error;
245 }
f3ed775e
DG
246 break;
247 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
248 ret = parse_probe_opts(&ev, opt_function);
249 if (ret < 0) {
250 ERR("Unable to parse function probe options");
251 ret = 0;
252 goto error;
253 }
254 break;
255 case LTTNG_EVENT_FUNCTION_ENTRY:
256 strncpy(ev.attr.ftrace.symbol_name,
257 opt_function_entry_symbol,
258 LTTNG_SYMBOL_NAME_LEN);
99497cd0 259 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
f3ed775e
DG
260 break;
261 default:
262 ret = CMD_NOT_IMPLEMENTED;
263 goto error;
264 }
265
cd80958d 266 ret = lttng_enable_event(handle, &ev, channel_name);
7d29a247 267 if (ret == 0) {
f3ed775e
DG
268 MSG("Kernel event %s created in channel %s", event_name, channel_name);
269 }
270 } else if (opt_userspace) { /* User-space tracer action */
271 /*
272 * TODO: Waiting on lttng UST 2.0
273 */
274 if (opt_pid_all) {
275 } else if (opt_pid != 0) {
276 }
277 ret = CMD_NOT_IMPLEMENTED;
278 goto error;
279 } else {
048e2159 280 ERR("Please specify a tracer (--kernel or --userspace)");
f3ed775e
DG
281 goto error;
282 }
283
284 /* Next event */
285 event_name = strtok(NULL, ",");
286 }
287
288error:
b73d0b29
MD
289 if (opt_channel_name == NULL) {
290 free(channel_name);
291 }
cd80958d
DG
292 lttng_destroy_handle(handle);
293
f3ed775e
DG
294 return ret;
295}
296
297/*
298 * cmd_enable_events
299 *
300 * Add event to trace session
301 */
302int cmd_enable_events(int argc, const char **argv)
303{
304 int opt, ret;
305 static poptContext pc;
cd80958d 306 char *session_name = NULL;
f3ed775e
DG
307
308 pc = poptGetContext(NULL, argc, argv, long_options, 0);
309 poptReadDefaultConfig(pc, 0);
310
311 /* Default event type */
e6ddca71 312 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e
DG
313
314 while ((opt = poptGetNextOpt(pc)) != -1) {
315 switch (opt) {
316 case OPT_HELP:
317 usage(stderr);
318 ret = CMD_SUCCESS;
319 goto end;
320 case OPT_USERSPACE:
321 opt_userspace = 1;
322 opt_cmd_name = poptGetOptArg(pc);
323 break;
324 case OPT_TRACEPOINT:
e6ddca71 325 opt_event_type = LTTNG_EVENT_TRACEPOINT;
f3ed775e
DG
326 break;
327 case OPT_MARKER:
328 ret = CMD_NOT_IMPLEMENTED;
329 goto end;
cf0e5467 330 case OPT_PROBE:
7d29a247 331 opt_event_type = LTTNG_EVENT_PROBE;
cf0e5467 332 opt_probe = poptGetOptArg(pc);
f3ed775e
DG
333 break;
334 case OPT_FUNCTION:
335 opt_event_type = LTTNG_EVENT_FUNCTION;
8f0d098b
MD
336 opt_function = poptGetOptArg(pc);
337 break;
338 case OPT_FUNCTION_ENTRY:
339 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
340 opt_function_entry_symbol = poptGetOptArg(pc);
f3ed775e
DG
341 break;
342 default:
343 usage(stderr);
344 ret = CMD_UNDEFINED;
345 goto end;
346 }
347 }
348
349 opt_event_list = (char*) poptGetArg(pc);
350 if (opt_event_list == NULL && opt_enable_all == 0) {
351 ERR("Missing event name(s).\n");
352 usage(stderr);
353 ret = CMD_SUCCESS;
354 goto end;
355 }
356
cd80958d
DG
357 if (!opt_session_name) {
358 session_name = get_session_name();
359 if (session_name == NULL) {
360 ret = -1;
361 goto end;
362 }
363 } else {
364 session_name = opt_session_name;
365 }
366
367 ret = enable_events(session_name);
f3ed775e
DG
368
369end:
cd80958d
DG
370 if (opt_session_name == NULL) {
371 free(session_name);
372 }
373
f3ed775e
DG
374 return ret;
375}
This page took 0.06303 seconds and 4 git commands to generate.