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