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