Commit | Line | Data |
---|---|---|
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 | |
34 | static char *opt_event_list; | |
35 | static int opt_event_type; | |
36 | static char *opt_kernel; | |
37 | static char *opt_cmd_name; | |
5440dc42 | 38 | static char *opt_session_name; |
f3ed775e DG |
39 | static int opt_pid_all; |
40 | static int opt_userspace; | |
41 | static int opt_enable_all; | |
42 | static pid_t opt_pid; | |
cf0e5467 | 43 | static char *opt_probe; |
8f0d098b MD |
44 | static char *opt_function; |
45 | static char *opt_function_entry_symbol; | |
f3ed775e DG |
46 | static char *opt_channel_name; |
47 | ||
48 | enum { | |
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, |
a54bd42d | 56 | OPT_SYSCALL, |
f3ed775e DG |
57 | }; |
58 | ||
cd80958d DG |
59 | static struct lttng_handle *handle; |
60 | ||
f3ed775e DG |
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}, | |
5440dc42 | 64 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
f3ed775e DG |
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}, | |
cf0e5467 | 73 | {"probe", 0, POPT_ARG_STRING, 0, OPT_PROBE, 0, 0}, |
f3ed775e | 74 | {"function", 0, POPT_ARG_STRING, 0, OPT_FUNCTION, 0, 0}, |
8f0d098b | 75 | {"function:entry", 0, POPT_ARG_STRING, 0, OPT_FUNCTION_ENTRY, 0, 0}, |
a54bd42d | 76 | {"syscall", 0, POPT_ARG_NONE, 0, OPT_SYSCALL, 0, 0}, |
f3ed775e DG |
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"); | |
5440dc42 | 88 | fprintf(ofp, " -s, --session Apply on session name\n"); |
f3ed775e DG |
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"); | |
8f0d098b | 98 | fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n"); |
cf0e5467 MD |
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"); | |
8f0d098b MD |
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"); | |
a54bd42d | 108 | fprintf(ofp, " --syscall System call event\n"); |
f3ed775e DG |
109 | fprintf(ofp, " --marker User-space marker (deprecated)\n"); |
110 | fprintf(ofp, "\n"); | |
111 | } | |
112 | ||
0d63dd19 | 113 | /* |
cf0e5467 | 114 | * parse_probe_addr |
0d63dd19 | 115 | * |
cf0e5467 | 116 | * Parse probe options. |
0d63dd19 | 117 | */ |
cf0e5467 | 118 | static int parse_probe_opts(struct lttng_event *ev, char *opt) |
0d63dd19 DG |
119 | { |
120 | int ret; | |
8ff0bbd0 | 121 | char s_hex[19]; |
0d63dd19 DG |
122 | char name[LTTNG_SYMBOL_NAME_LEN]; |
123 | ||
124 | if (opt == NULL) { | |
125 | ret = -1; | |
8f0d098b | 126 | goto end; |
0d63dd19 DG |
127 | } |
128 | ||
129 | /* Check for symbol+offset */ | |
8ff0bbd0 | 130 | ret = sscanf(opt, "%[^'+']+%s", name, s_hex); |
0d63dd19 | 131 | if (ret == 2) { |
7d29a247 | 132 | strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN); |
99497cd0 | 133 | ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
cf0e5467 | 134 | DBG("probe symbol %s", ev->attr.probe.symbol_name); |
8ff0bbd0 DG |
135 | if (strlen(s_hex) == 0) { |
136 | ERR("Invalid probe offset %s", s_hex); | |
0d63dd19 | 137 | ret = -1; |
8f0d098b | 138 | goto end; |
0d63dd19 | 139 | } |
8ff0bbd0 | 140 | ev->attr.probe.offset = strtoul(s_hex, NULL, 0); |
cf0e5467 | 141 | DBG("probe offset %" PRIu64, ev->attr.probe.offset); |
3000dc78 | 142 | ev->attr.probe.addr = 0; |
8f0d098b MD |
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); | |
99497cd0 | 151 | ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
8f0d098b MD |
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 | } | |
0d63dd19 DG |
158 | } |
159 | ||
160 | /* Check for address */ | |
8ff0bbd0 | 161 | ret = sscanf(opt, "%s", s_hex); |
0d63dd19 | 162 | if (ret > 0) { |
8ff0bbd0 DG |
163 | if (strlen(s_hex) == 0) { |
164 | ERR("Invalid probe address %s", s_hex); | |
0d63dd19 | 165 | ret = -1; |
8f0d098b | 166 | goto end; |
0d63dd19 | 167 | } |
8ff0bbd0 | 168 | ev->attr.probe.addr = strtoul(s_hex, NULL, 0); |
cf0e5467 | 169 | DBG("probe addr %" PRIu64, ev->attr.probe.addr); |
3000dc78 DG |
170 | ev->attr.probe.offset = 0; |
171 | memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN); | |
8f0d098b | 172 | goto end; |
0d63dd19 DG |
173 | } |
174 | ||
175 | /* No match */ | |
176 | ret = -1; | |
177 | ||
8f0d098b | 178 | end: |
0d63dd19 DG |
179 | return ret; |
180 | } | |
181 | ||
f3ed775e DG |
182 | /* |
183 | * enable_events | |
184 | * | |
185 | * Enabling event using the lttng API. | |
186 | */ | |
cd80958d | 187 | static int enable_events(char *session_name) |
f3ed775e DG |
188 | { |
189 | int err, ret = CMD_SUCCESS; | |
b73d0b29 | 190 | char *event_name, *channel_name = NULL; |
f3ed775e | 191 | struct lttng_event ev; |
7d29a247 | 192 | struct lttng_domain dom; |
f3ed775e | 193 | |
f3ed775e DG |
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 | ||
7d29a247 DG |
204 | /* Create lttng domain */ |
205 | if (opt_kernel) { | |
206 | dom.type = LTTNG_DOMAIN_KERNEL; | |
207 | } | |
208 | ||
cd80958d DG |
209 | handle = lttng_create_handle(session_name, &dom); |
210 | if (handle == NULL) { | |
211 | ret = -1; | |
212 | goto error; | |
213 | } | |
1aef21b6 | 214 | |
cd80958d | 215 | if (opt_enable_all) { |
8c9ae521 DG |
216 | /* Default setup for enable all */ |
217 | ev.name[0] = '\0'; | |
218 | ev.type = opt_event_type; | |
219 | ||
220 | ret = lttng_enable_event(handle, &ev, channel_name); | |
221 | if (ret < 0) { | |
222 | goto error; | |
223 | } | |
224 | ||
0133c199 MD |
225 | switch (opt_event_type) { |
226 | case LTTNG_EVENT_TRACEPOINT: | |
227 | if (opt_kernel) { | |
8c9ae521 DG |
228 | MSG("All kernel events are enabled in channel %s", |
229 | channel_name); | |
0133c199 | 230 | } |
0133c199 | 231 | break; |
a54bd42d | 232 | case LTTNG_EVENT_SYSCALL: |
0133c199 | 233 | if (opt_kernel) { |
8c9ae521 DG |
234 | MSG("All kernel system calls are enabled in channel %s", |
235 | channel_name); | |
c93d9daf | 236 | } |
0133c199 MD |
237 | break; |
238 | default: | |
8c9ae521 DG |
239 | /* |
240 | * We should not be here since lttng_enable_event should had failed | |
241 | * on the event type. | |
242 | */ | |
f3ed775e | 243 | goto error; |
8c9ae521 | 244 | |
f3ed775e | 245 | } |
8c9ae521 DG |
246 | |
247 | goto end; | |
f3ed775e DG |
248 | } |
249 | ||
250 | /* Strip event list */ | |
251 | event_name = strtok(opt_event_list, ","); | |
252 | while (event_name != NULL) { | |
253 | /* Kernel tracer action */ | |
254 | if (opt_kernel) { | |
255 | DBG("Enabling kernel event %s for channel %s", | |
256 | event_name, channel_name); | |
257 | /* Copy name and type of the event */ | |
258 | strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN); | |
99497cd0 | 259 | ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
f3ed775e DG |
260 | ev.type = opt_event_type; |
261 | ||
262 | switch (opt_event_type) { | |
e6ddca71 | 263 | case LTTNG_EVENT_TRACEPOINT: |
f3ed775e | 264 | break; |
7d29a247 | 265 | case LTTNG_EVENT_PROBE: |
cf0e5467 | 266 | ret = parse_probe_opts(&ev, opt_probe); |
0d63dd19 | 267 | if (ret < 0) { |
cf0e5467 | 268 | ERR("Unable to parse probe options"); |
0d63dd19 DG |
269 | ret = 0; |
270 | goto error; | |
271 | } | |
f3ed775e DG |
272 | break; |
273 | case LTTNG_EVENT_FUNCTION: | |
8f0d098b MD |
274 | ret = parse_probe_opts(&ev, opt_function); |
275 | if (ret < 0) { | |
276 | ERR("Unable to parse function probe options"); | |
277 | ret = 0; | |
278 | goto error; | |
279 | } | |
280 | break; | |
281 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
282 | strncpy(ev.attr.ftrace.symbol_name, | |
283 | opt_function_entry_symbol, | |
284 | LTTNG_SYMBOL_NAME_LEN); | |
99497cd0 | 285 | ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
f3ed775e | 286 | break; |
a54bd42d | 287 | case LTTNG_EVENT_SYSCALL: |
0133c199 MD |
288 | MSG("per-syscall selection not supported yet. Use \"-a\" for all syscalls."); |
289 | ret = CMD_NOT_IMPLEMENTED; | |
290 | goto error; | |
f3ed775e DG |
291 | default: |
292 | ret = CMD_NOT_IMPLEMENTED; | |
293 | goto error; | |
294 | } | |
295 | ||
cd80958d | 296 | ret = lttng_enable_event(handle, &ev, channel_name); |
7d29a247 | 297 | if (ret == 0) { |
f3ed775e DG |
298 | MSG("Kernel event %s created in channel %s", event_name, channel_name); |
299 | } | |
300 | } else if (opt_userspace) { /* User-space tracer action */ | |
301 | /* | |
302 | * TODO: Waiting on lttng UST 2.0 | |
303 | */ | |
304 | if (opt_pid_all) { | |
305 | } else if (opt_pid != 0) { | |
306 | } | |
307 | ret = CMD_NOT_IMPLEMENTED; | |
308 | goto error; | |
309 | } else { | |
048e2159 | 310 | ERR("Please specify a tracer (--kernel or --userspace)"); |
f3ed775e DG |
311 | goto error; |
312 | } | |
313 | ||
314 | /* Next event */ | |
315 | event_name = strtok(NULL, ","); | |
316 | } | |
317 | ||
8c9ae521 | 318 | end: |
f3ed775e | 319 | error: |
b73d0b29 MD |
320 | if (opt_channel_name == NULL) { |
321 | free(channel_name); | |
322 | } | |
cd80958d DG |
323 | lttng_destroy_handle(handle); |
324 | ||
f3ed775e DG |
325 | return ret; |
326 | } | |
327 | ||
328 | /* | |
329 | * cmd_enable_events | |
330 | * | |
331 | * Add event to trace session | |
332 | */ | |
333 | int cmd_enable_events(int argc, const char **argv) | |
334 | { | |
335 | int opt, ret; | |
336 | static poptContext pc; | |
cd80958d | 337 | char *session_name = NULL; |
f3ed775e DG |
338 | |
339 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
340 | poptReadDefaultConfig(pc, 0); | |
341 | ||
342 | /* Default event type */ | |
e6ddca71 | 343 | opt_event_type = LTTNG_EVENT_TRACEPOINT; |
f3ed775e DG |
344 | |
345 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
346 | switch (opt) { | |
347 | case OPT_HELP: | |
348 | usage(stderr); | |
349 | ret = CMD_SUCCESS; | |
350 | goto end; | |
351 | case OPT_USERSPACE: | |
352 | opt_userspace = 1; | |
353 | opt_cmd_name = poptGetOptArg(pc); | |
354 | break; | |
355 | case OPT_TRACEPOINT: | |
e6ddca71 | 356 | opt_event_type = LTTNG_EVENT_TRACEPOINT; |
f3ed775e DG |
357 | break; |
358 | case OPT_MARKER: | |
359 | ret = CMD_NOT_IMPLEMENTED; | |
360 | goto end; | |
cf0e5467 | 361 | case OPT_PROBE: |
7d29a247 | 362 | opt_event_type = LTTNG_EVENT_PROBE; |
cf0e5467 | 363 | opt_probe = poptGetOptArg(pc); |
f3ed775e DG |
364 | break; |
365 | case OPT_FUNCTION: | |
366 | opt_event_type = LTTNG_EVENT_FUNCTION; | |
8f0d098b MD |
367 | opt_function = poptGetOptArg(pc); |
368 | break; | |
369 | case OPT_FUNCTION_ENTRY: | |
370 | opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY; | |
371 | opt_function_entry_symbol = poptGetOptArg(pc); | |
f3ed775e | 372 | break; |
a54bd42d MD |
373 | case OPT_SYSCALL: |
374 | opt_event_type = LTTNG_EVENT_SYSCALL; | |
0133c199 | 375 | break; |
f3ed775e DG |
376 | default: |
377 | usage(stderr); | |
378 | ret = CMD_UNDEFINED; | |
379 | goto end; | |
380 | } | |
381 | } | |
382 | ||
383 | opt_event_list = (char*) poptGetArg(pc); | |
384 | if (opt_event_list == NULL && opt_enable_all == 0) { | |
385 | ERR("Missing event name(s).\n"); | |
386 | usage(stderr); | |
387 | ret = CMD_SUCCESS; | |
388 | goto end; | |
389 | } | |
390 | ||
cd80958d DG |
391 | if (!opt_session_name) { |
392 | session_name = get_session_name(); | |
393 | if (session_name == NULL) { | |
394 | ret = -1; | |
395 | goto end; | |
396 | } | |
397 | } else { | |
398 | session_name = opt_session_name; | |
399 | } | |
400 | ||
401 | ret = enable_events(session_name); | |
f3ed775e DG |
402 | |
403 | end: | |
cd80958d DG |
404 | if (opt_session_name == NULL) { |
405 | free(session_name); | |
406 | } | |
407 | ||
f3ed775e DG |
408 | return ret; |
409 | } |