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