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> |
f3ed775e | 28 | |
6e2d116c DG |
29 | #include "../cmd.h" |
30 | #include "../conf.h" | |
31 | #include "../utils.h" | |
f3ed775e DG |
32 | |
33 | static char *opt_event_list; | |
34 | static int opt_event_type; | |
35 | static char *opt_kernel; | |
36 | static char *opt_cmd_name; | |
5440dc42 | 37 | static char *opt_session_name; |
f3ed775e DG |
38 | static int opt_pid_all; |
39 | static int opt_userspace; | |
40 | static int opt_enable_all; | |
41 | static pid_t opt_pid; | |
0d63dd19 | 42 | static char *opt_kprobe; |
f3ed775e DG |
43 | static char *opt_function_symbol; |
44 | static char *opt_channel_name; | |
45 | ||
46 | enum { | |
47 | OPT_HELP = 1, | |
48 | OPT_USERSPACE, | |
49 | OPT_TRACEPOINT, | |
50 | OPT_MARKER, | |
51 | OPT_KPROBE, | |
52 | OPT_FUNCTION, | |
53 | }; | |
54 | ||
55 | static 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}, | |
67 | {"kprobe", 0, POPT_ARG_STRING, 0, OPT_KPROBE, 0, 0}, | |
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 | */ | |
75 | static 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"); | |
0d63dd19 DG |
90 | fprintf(ofp, " --kprobe [addr | symbol+offset]\n"); |
91 | fprintf(ofp, " Kernel Kprobe. (addr or offset can be base 8,10 and 16)\n"); | |
f3ed775e DG |
92 | fprintf(ofp, " --function SYMBOL Function tracer event\n"); |
93 | fprintf(ofp, " --marker User-space marker (deprecated)\n"); | |
94 | fprintf(ofp, "\n"); | |
95 | } | |
96 | ||
0d63dd19 DG |
97 | /* |
98 | * parse_kprobe_addr | |
99 | * | |
100 | * Parse kprobe options. | |
101 | */ | |
102 | static int parse_kprobe_opts(struct lttng_event *ev, char *opt) | |
103 | { | |
104 | int ret; | |
105 | uint64_t hex; | |
106 | char name[LTTNG_SYMBOL_NAME_LEN]; | |
107 | ||
108 | if (opt == NULL) { | |
109 | ret = -1; | |
110 | goto error; | |
111 | } | |
112 | ||
113 | /* Check for symbol+offset */ | |
5a0de755 | 114 | ret = sscanf(opt, "%[^'+']+%" SCNu64, name, &hex); |
0d63dd19 | 115 | if (ret == 2) { |
7d29a247 DG |
116 | strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN); |
117 | DBG("kprobe symbol %s", ev->attr.probe.symbol_name); | |
0d63dd19 | 118 | if (hex == 0) { |
5a0de755 | 119 | ERR("Invalid kprobe offset %" PRIu64, hex); |
0d63dd19 DG |
120 | ret = -1; |
121 | goto error; | |
122 | } | |
7d29a247 | 123 | ev->attr.probe.offset = hex; |
5a0de755 | 124 | DBG("kprobe offset %" PRIu64, ev->attr.probe.offset); |
0d63dd19 DG |
125 | goto error; |
126 | } | |
127 | ||
128 | /* Check for address */ | |
5a0de755 | 129 | ret = sscanf(opt, "%" SCNu64, &hex); |
0d63dd19 DG |
130 | if (ret > 0) { |
131 | if (hex == 0) { | |
5a0de755 | 132 | ERR("Invalid kprobe address %" PRIu64, hex); |
0d63dd19 DG |
133 | ret = -1; |
134 | goto error; | |
135 | } | |
7d29a247 | 136 | ev->attr.probe.addr = hex; |
5a0de755 | 137 | DBG("kprobe addr %" PRIu64, ev->attr.probe.addr); |
0d63dd19 DG |
138 | goto error; |
139 | } | |
140 | ||
141 | /* No match */ | |
142 | ret = -1; | |
143 | ||
144 | error: | |
145 | return ret; | |
146 | } | |
147 | ||
f3ed775e DG |
148 | /* |
149 | * enable_events | |
150 | * | |
151 | * Enabling event using the lttng API. | |
152 | */ | |
153 | static int enable_events(void) | |
154 | { | |
155 | int err, ret = CMD_SUCCESS; | |
b73d0b29 | 156 | char *event_name, *channel_name = NULL; |
f3ed775e | 157 | struct lttng_event ev; |
7d29a247 | 158 | struct lttng_domain dom; |
f3ed775e | 159 | |
5440dc42 | 160 | if (set_session_name(opt_session_name) < 0) { |
f3ed775e DG |
161 | ret = CMD_ERROR; |
162 | goto error; | |
163 | } | |
164 | ||
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 DG |
180 | if (opt_enable_all) { |
181 | if (opt_kernel) { | |
7d29a247 | 182 | ret = lttng_enable_event(&dom, NULL, channel_name); |
c93d9daf DG |
183 | if (ret == 0) { |
184 | MSG("All kernel events are enabled in channel %s", channel_name); | |
185 | } | |
f3ed775e DG |
186 | goto error; |
187 | } | |
188 | ||
189 | /* TODO: User-space tracer */ | |
190 | } | |
191 | ||
192 | /* Strip event list */ | |
193 | event_name = strtok(opt_event_list, ","); | |
194 | while (event_name != NULL) { | |
195 | /* Kernel tracer action */ | |
196 | if (opt_kernel) { | |
197 | DBG("Enabling kernel event %s for channel %s", | |
198 | event_name, channel_name); | |
199 | /* Copy name and type of the event */ | |
200 | strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN); | |
201 | ev.type = opt_event_type; | |
202 | ||
203 | switch (opt_event_type) { | |
e6ddca71 | 204 | case LTTNG_EVENT_TRACEPOINT: |
f3ed775e | 205 | break; |
7d29a247 | 206 | case LTTNG_EVENT_PROBE: |
0d63dd19 DG |
207 | ret = parse_kprobe_opts(&ev, opt_kprobe); |
208 | if (ret < 0) { | |
209 | ERR("Unable to parse kprobe options"); | |
210 | ret = 0; | |
211 | goto error; | |
212 | } | |
f3ed775e DG |
213 | break; |
214 | case LTTNG_EVENT_FUNCTION: | |
215 | strncpy(ev.attr.ftrace.symbol_name, opt_function_symbol, LTTNG_SYMBOL_NAME_LEN); | |
f3ed775e DG |
216 | break; |
217 | default: | |
218 | ret = CMD_NOT_IMPLEMENTED; | |
219 | goto error; | |
220 | } | |
221 | ||
7d29a247 DG |
222 | ret = lttng_enable_event(&dom, &ev, channel_name); |
223 | if (ret == 0) { | |
f3ed775e | 224 | MSG("Kernel event %s created in channel %s", event_name, channel_name); |
7d29a247 DG |
225 | } else if (ret < 0) { |
226 | ERR("Unable to find event %s", ev.name); | |
f3ed775e DG |
227 | } |
228 | } else if (opt_userspace) { /* User-space tracer action */ | |
229 | /* | |
230 | * TODO: Waiting on lttng UST 2.0 | |
231 | */ | |
232 | if (opt_pid_all) { | |
233 | } else if (opt_pid != 0) { | |
234 | } | |
235 | ret = CMD_NOT_IMPLEMENTED; | |
236 | goto error; | |
237 | } else { | |
238 | ERR("Please specify a tracer (kernel or user-space)"); | |
239 | goto error; | |
240 | } | |
241 | ||
242 | /* Next event */ | |
243 | event_name = strtok(NULL, ","); | |
244 | } | |
245 | ||
246 | error: | |
b73d0b29 MD |
247 | if (opt_channel_name == NULL) { |
248 | free(channel_name); | |
249 | } | |
f3ed775e DG |
250 | return ret; |
251 | } | |
252 | ||
253 | /* | |
254 | * cmd_enable_events | |
255 | * | |
256 | * Add event to trace session | |
257 | */ | |
258 | int cmd_enable_events(int argc, const char **argv) | |
259 | { | |
260 | int opt, ret; | |
261 | static poptContext pc; | |
262 | ||
263 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
264 | poptReadDefaultConfig(pc, 0); | |
265 | ||
266 | /* Default event type */ | |
e6ddca71 | 267 | opt_event_type = LTTNG_EVENT_TRACEPOINT; |
f3ed775e DG |
268 | |
269 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
270 | switch (opt) { | |
271 | case OPT_HELP: | |
272 | usage(stderr); | |
273 | ret = CMD_SUCCESS; | |
274 | goto end; | |
275 | case OPT_USERSPACE: | |
276 | opt_userspace = 1; | |
277 | opt_cmd_name = poptGetOptArg(pc); | |
278 | break; | |
279 | case OPT_TRACEPOINT: | |
e6ddca71 | 280 | opt_event_type = LTTNG_EVENT_TRACEPOINT; |
f3ed775e DG |
281 | break; |
282 | case OPT_MARKER: | |
283 | ret = CMD_NOT_IMPLEMENTED; | |
284 | goto end; | |
285 | case OPT_KPROBE: | |
7d29a247 | 286 | opt_event_type = LTTNG_EVENT_PROBE; |
0d63dd19 | 287 | opt_kprobe = poptGetOptArg(pc); |
f3ed775e DG |
288 | break; |
289 | case OPT_FUNCTION: | |
290 | opt_event_type = LTTNG_EVENT_FUNCTION; | |
291 | opt_function_symbol = poptGetOptArg(pc); | |
292 | break; | |
293 | default: | |
294 | usage(stderr); | |
295 | ret = CMD_UNDEFINED; | |
296 | goto end; | |
297 | } | |
298 | } | |
299 | ||
300 | opt_event_list = (char*) poptGetArg(pc); | |
301 | if (opt_event_list == NULL && opt_enable_all == 0) { | |
302 | ERR("Missing event name(s).\n"); | |
303 | usage(stderr); | |
304 | ret = CMD_SUCCESS; | |
305 | goto end; | |
306 | } | |
307 | ||
308 | ret = enable_events(); | |
309 | ||
310 | end: | |
311 | return ret; | |
312 | } |