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