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