Make "please specify tracer" comment more helpful
[lttng-tools.git] / lttng / commands / enable_events.c
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
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
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>
27 #include <inttypes.h>
28 #include <ctype.h>
29
30 #include "../cmd.h"
31 #include "../conf.h"
32 #include "../utils.h"
33
34 static char *opt_event_list;
35 static int opt_event_type;
36 static char *opt_kernel;
37 static char *opt_cmd_name;
38 static char *opt_session_name;
39 static int opt_pid_all;
40 static int opt_userspace;
41 static int opt_enable_all;
42 static pid_t opt_pid;
43 static char *opt_probe;
44 static char *opt_function;
45 static char *opt_function_entry_symbol;
46 static char *opt_channel_name;
47
48 enum {
49 OPT_HELP = 1,
50 OPT_USERSPACE,
51 OPT_TRACEPOINT,
52 OPT_MARKER,
53 OPT_PROBE,
54 OPT_FUNCTION,
55 OPT_FUNCTION_ENTRY,
56 };
57
58 static struct poptOption long_options[] = {
59 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
60 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
61 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
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},
70 {"probe", 0, POPT_ARG_STRING, 0, OPT_PROBE, 0, 0},
71 {"function", 0, POPT_ARG_STRING, 0, OPT_FUNCTION, 0, 0},
72 {"function:entry", 0, POPT_ARG_STRING, 0, OPT_FUNCTION_ENTRY, 0, 0},
73 {0, 0, 0, 0, 0, 0, 0}
74 };
75
76 /*
77 * usage
78 */
79 static 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");
84 fprintf(ofp, " -s, --session Apply on session name\n");
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");
94 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
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");
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");
104 fprintf(ofp, " --marker User-space marker (deprecated)\n");
105 fprintf(ofp, "\n");
106 }
107
108 /*
109 * parse_probe_addr
110 *
111 * Parse probe options.
112 */
113 static int parse_probe_opts(struct lttng_event *ev, char *opt)
114 {
115 int ret;
116 char s_hex[19];
117 char name[LTTNG_SYMBOL_NAME_LEN];
118
119 if (opt == NULL) {
120 ret = -1;
121 goto end;
122 }
123
124 /* Check for symbol+offset */
125 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
126 if (ret == 2) {
127 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
128 DBG("probe symbol %s", ev->attr.probe.symbol_name);
129 if (strlen(s_hex) == 0) {
130 ERR("Invalid probe offset %s", s_hex);
131 ret = -1;
132 goto end;
133 }
134 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
135 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
136 ev->attr.probe.addr = 0;
137 goto end;
138 }
139
140 /* Check for symbol */
141 if (isalpha(name[0])) {
142 ret = sscanf(opt, "%s", name);
143 if (ret == 1) {
144 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
145 DBG("probe symbol %s", ev->attr.probe.symbol_name);
146 ev->attr.probe.offset = 0;
147 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
148 ev->attr.probe.addr = 0;
149 goto end;
150 }
151 }
152
153 /* Check for address */
154 ret = sscanf(opt, "%s", s_hex);
155 if (ret > 0) {
156 if (strlen(s_hex) == 0) {
157 ERR("Invalid probe address %s", s_hex);
158 ret = -1;
159 goto end;
160 }
161 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
162 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
163 ev->attr.probe.offset = 0;
164 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
165 goto end;
166 }
167
168 /* No match */
169 ret = -1;
170
171 end:
172 return ret;
173 }
174
175 /*
176 * enable_events
177 *
178 * Enabling event using the lttng API.
179 */
180 static int enable_events(void)
181 {
182 int err, ret = CMD_SUCCESS;
183 char *event_name, *channel_name = NULL;
184 struct lttng_event ev;
185 struct lttng_domain dom;
186
187 if (opt_channel_name == NULL) {
188 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
189 if (err < 0) {
190 ret = CMD_FATAL;
191 goto error;
192 }
193 } else {
194 channel_name = opt_channel_name;
195 }
196
197 /* Create lttng domain */
198 if (opt_kernel) {
199 dom.type = LTTNG_DOMAIN_KERNEL;
200 }
201
202 if (opt_enable_all) {
203 if (set_session_name(opt_session_name) < 0) {
204 ret = CMD_ERROR;
205 goto error;
206 }
207
208 if (opt_kernel) {
209 ret = lttng_enable_event(&dom, NULL, channel_name);
210 if (ret == 0) {
211 MSG("All kernel events are enabled in channel %s", channel_name);
212 }
213 goto error;
214 }
215
216 /* TODO: User-space tracer */
217 }
218
219 /* Strip event list */
220 event_name = strtok(opt_event_list, ",");
221 while (event_name != NULL) {
222 if (set_session_name(opt_session_name) < 0) {
223 ret = CMD_ERROR;
224 goto error;
225 }
226
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);
233 ev.type = opt_event_type;
234
235 switch (opt_event_type) {
236 case LTTNG_EVENT_TRACEPOINT:
237 break;
238 case LTTNG_EVENT_PROBE:
239 ret = parse_probe_opts(&ev, opt_probe);
240 if (ret < 0) {
241 ERR("Unable to parse probe options");
242 ret = 0;
243 goto error;
244 }
245 break;
246 case LTTNG_EVENT_FUNCTION:
247 ret = parse_probe_opts(&ev, opt_function);
248 if (ret < 0) {
249 ERR("Unable to parse function probe options");
250 ret = 0;
251 goto error;
252 }
253 break;
254 case LTTNG_EVENT_FUNCTION_ENTRY:
255 strncpy(ev.attr.ftrace.symbol_name,
256 opt_function_entry_symbol,
257 LTTNG_SYMBOL_NAME_LEN);
258 break;
259 default:
260 ret = CMD_NOT_IMPLEMENTED;
261 goto error;
262 }
263
264 ret = lttng_enable_event(&dom, &ev, channel_name);
265 if (ret == 0) {
266 MSG("Kernel event %s created in channel %s", event_name, channel_name);
267 }
268 } else if (opt_userspace) { /* User-space tracer action */
269 /*
270 * TODO: Waiting on lttng UST 2.0
271 */
272 if (opt_pid_all) {
273 } else if (opt_pid != 0) {
274 }
275 ret = CMD_NOT_IMPLEMENTED;
276 goto error;
277 } else {
278 ERR("Please specify a tracer (--kernel or --userspace)");
279 goto error;
280 }
281
282 /* Next event */
283 event_name = strtok(NULL, ",");
284 }
285
286 error:
287 if (opt_channel_name == NULL) {
288 free(channel_name);
289 }
290 return ret;
291 }
292
293 /*
294 * cmd_enable_events
295 *
296 * Add event to trace session
297 */
298 int cmd_enable_events(int argc, const char **argv)
299 {
300 int opt, ret;
301 static poptContext pc;
302
303 pc = poptGetContext(NULL, argc, argv, long_options, 0);
304 poptReadDefaultConfig(pc, 0);
305
306 /* Default event type */
307 opt_event_type = LTTNG_EVENT_TRACEPOINT;
308
309 while ((opt = poptGetNextOpt(pc)) != -1) {
310 switch (opt) {
311 case OPT_HELP:
312 usage(stderr);
313 ret = CMD_SUCCESS;
314 goto end;
315 case OPT_USERSPACE:
316 opt_userspace = 1;
317 opt_cmd_name = poptGetOptArg(pc);
318 break;
319 case OPT_TRACEPOINT:
320 opt_event_type = LTTNG_EVENT_TRACEPOINT;
321 break;
322 case OPT_MARKER:
323 ret = CMD_NOT_IMPLEMENTED;
324 goto end;
325 case OPT_PROBE:
326 opt_event_type = LTTNG_EVENT_PROBE;
327 opt_probe = poptGetOptArg(pc);
328 break;
329 case OPT_FUNCTION:
330 opt_event_type = LTTNG_EVENT_FUNCTION;
331 opt_function = poptGetOptArg(pc);
332 break;
333 case OPT_FUNCTION_ENTRY:
334 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
335 opt_function_entry_symbol = poptGetOptArg(pc);
336 break;
337 default:
338 usage(stderr);
339 ret = CMD_UNDEFINED;
340 goto end;
341 }
342 }
343
344 opt_event_list = (char*) poptGetArg(pc);
345 if (opt_event_list == NULL && opt_enable_all == 0) {
346 ERR("Missing event name(s).\n");
347 usage(stderr);
348 ret = CMD_SUCCESS;
349 goto end;
350 }
351
352 ret = enable_events();
353
354 end:
355 return ret;
356 }
This page took 0.037321 seconds and 5 git commands to generate.