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