Update version to 2.0-pre13
[lttng-tools.git] / lttng / commands / enable_events.c
... / ...
CommitLineData
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#include <ctype.h>
29
30#include "../cmd.h"
31#include "../conf.h"
32#include "../utils.h"
33
34static char *opt_event_list;
35static int opt_event_type;
36static char *opt_kernel;
37static char *opt_cmd_name;
38static char *opt_session_name;
39static int opt_pid_all;
40static int opt_userspace;
41static int opt_enable_all;
42static pid_t opt_pid;
43static char *opt_probe;
44static char *opt_function;
45static char *opt_function_entry_symbol;
46static char *opt_channel_name;
47
48enum {
49 OPT_HELP = 1,
50 OPT_USERSPACE,
51 OPT_TRACEPOINT,
52 OPT_MARKER,
53 OPT_PROBE,
54 OPT_FUNCTION,
55 OPT_FUNCTION_ENTRY,
56};
57
58static struct lttng_handle *handle;
59
60static struct poptOption long_options[] = {
61 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
62 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
63 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
64 {"all-events", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0},
65 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
66 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
67 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, 0, OPT_USERSPACE, 0, 0},
68 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
69 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
70 {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0},
71 {"marker", 0, POPT_ARG_NONE, 0, OPT_MARKER, 0, 0},
72 {"probe", 0, POPT_ARG_STRING, 0, OPT_PROBE, 0, 0},
73 {"function", 0, POPT_ARG_STRING, 0, OPT_FUNCTION, 0, 0},
74 {"function:entry", 0, POPT_ARG_STRING, 0, OPT_FUNCTION_ENTRY, 0, 0},
75 {0, 0, 0, 0, 0, 0, 0}
76};
77
78/*
79 * usage
80 */
81static void usage(FILE *ofp)
82{
83 fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n");
84 fprintf(ofp, "\n");
85 fprintf(ofp, " -h, --help Show this help\n");
86 fprintf(ofp, " -s, --session Apply on session name\n");
87 fprintf(ofp, " -c, --channel Apply on this channel\n");
88 fprintf(ofp, " -a, --all-events Enable all tracepoints\n");
89 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
90 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
91 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
92 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
93 fprintf(ofp, "\n");
94 fprintf(ofp, "Event options:\n");
95 fprintf(ofp, " --tracepoint Tracepoint event (default)\n");
96 fprintf(ofp, " --probe [addr | symbol | symbol+offset]\n");
97 fprintf(ofp, " Dynamic probe.\n");
98 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
99 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
100 fprintf(ofp, " --function [addr | symbol | symbol+offset]\n");
101 fprintf(ofp, " Dynamic function entry/return probe.\n");
102 fprintf(ofp, " Addr and offset can be octal (0NNN...),\n");
103 fprintf(ofp, " decimal (NNN...) or hexadecimal (0xNNN...)\n");
104 fprintf(ofp, " --function:entry symbol\n");
105 fprintf(ofp, " Function tracer event\n");
106 fprintf(ofp, " --marker User-space marker (deprecated)\n");
107 fprintf(ofp, "\n");
108}
109
110/*
111 * parse_probe_addr
112 *
113 * Parse probe options.
114 */
115static int parse_probe_opts(struct lttng_event *ev, char *opt)
116{
117 int ret;
118 char s_hex[19];
119 char name[LTTNG_SYMBOL_NAME_LEN];
120
121 if (opt == NULL) {
122 ret = -1;
123 goto end;
124 }
125
126 /* Check for symbol+offset */
127 ret = sscanf(opt, "%[^'+']+%s", name, s_hex);
128 if (ret == 2) {
129 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
130 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
131 DBG("probe symbol %s", ev->attr.probe.symbol_name);
132 if (strlen(s_hex) == 0) {
133 ERR("Invalid probe offset %s", s_hex);
134 ret = -1;
135 goto end;
136 }
137 ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
138 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
139 ev->attr.probe.addr = 0;
140 goto end;
141 }
142
143 /* Check for symbol */
144 if (isalpha(name[0])) {
145 ret = sscanf(opt, "%s", name);
146 if (ret == 1) {
147 strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
148 ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
149 DBG("probe symbol %s", ev->attr.probe.symbol_name);
150 ev->attr.probe.offset = 0;
151 DBG("probe offset %" PRIu64, ev->attr.probe.offset);
152 ev->attr.probe.addr = 0;
153 goto end;
154 }
155 }
156
157 /* Check for address */
158 ret = sscanf(opt, "%s", s_hex);
159 if (ret > 0) {
160 if (strlen(s_hex) == 0) {
161 ERR("Invalid probe address %s", s_hex);
162 ret = -1;
163 goto end;
164 }
165 ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
166 DBG("probe addr %" PRIu64, ev->attr.probe.addr);
167 ev->attr.probe.offset = 0;
168 memset(ev->attr.probe.symbol_name, 0, LTTNG_SYMBOL_NAME_LEN);
169 goto end;
170 }
171
172 /* No match */
173 ret = -1;
174
175end:
176 return ret;
177}
178
179/*
180 * enable_events
181 *
182 * Enabling event using the lttng API.
183 */
184static int enable_events(char *session_name)
185{
186 int err, ret = CMD_SUCCESS;
187 char *event_name, *channel_name = NULL;
188 struct lttng_event ev;
189 struct lttng_domain dom;
190
191 if (opt_channel_name == NULL) {
192 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
193 if (err < 0) {
194 ret = CMD_FATAL;
195 goto error;
196 }
197 } else {
198 channel_name = opt_channel_name;
199 }
200
201 /* Create lttng domain */
202 if (opt_kernel) {
203 dom.type = LTTNG_DOMAIN_KERNEL;
204 }
205
206 handle = lttng_create_handle(session_name, &dom);
207 if (handle == NULL) {
208 ret = -1;
209 goto error;
210 }
211
212 if (opt_enable_all) {
213 if (opt_kernel) {
214 ret = lttng_enable_event(handle, NULL, channel_name);
215 if (ret == 0) {
216 MSG("All kernel events are enabled in channel %s", channel_name);
217 }
218 goto error;
219 }
220
221 /* TODO: User-space tracer */
222 }
223
224 /* Strip event list */
225 event_name = strtok(opt_event_list, ",");
226 while (event_name != NULL) {
227 /* Kernel tracer action */
228 if (opt_kernel) {
229 DBG("Enabling kernel event %s for channel %s",
230 event_name, channel_name);
231 /* Copy name and type of the event */
232 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
233 ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
234 ev.type = opt_event_type;
235
236 switch (opt_event_type) {
237 case LTTNG_EVENT_TRACEPOINT:
238 break;
239 case LTTNG_EVENT_PROBE:
240 ret = parse_probe_opts(&ev, opt_probe);
241 if (ret < 0) {
242 ERR("Unable to parse probe options");
243 ret = 0;
244 goto error;
245 }
246 break;
247 case LTTNG_EVENT_FUNCTION:
248 ret = parse_probe_opts(&ev, opt_function);
249 if (ret < 0) {
250 ERR("Unable to parse function probe options");
251 ret = 0;
252 goto error;
253 }
254 break;
255 case LTTNG_EVENT_FUNCTION_ENTRY:
256 strncpy(ev.attr.ftrace.symbol_name,
257 opt_function_entry_symbol,
258 LTTNG_SYMBOL_NAME_LEN);
259 ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
260 break;
261 default:
262 ret = CMD_NOT_IMPLEMENTED;
263 goto error;
264 }
265
266 ret = lttng_enable_event(handle, &ev, channel_name);
267 if (ret == 0) {
268 MSG("Kernel event %s created in channel %s", event_name, channel_name);
269 }
270 } else if (opt_userspace) { /* User-space tracer action */
271 /*
272 * TODO: Waiting on lttng UST 2.0
273 */
274 if (opt_pid_all) {
275 } else if (opt_pid != 0) {
276 }
277 ret = CMD_NOT_IMPLEMENTED;
278 goto error;
279 } else {
280 ERR("Please specify a tracer (--kernel or --userspace)");
281 goto error;
282 }
283
284 /* Next event */
285 event_name = strtok(NULL, ",");
286 }
287
288error:
289 if (opt_channel_name == NULL) {
290 free(channel_name);
291 }
292 lttng_destroy_handle(handle);
293
294 return ret;
295}
296
297/*
298 * cmd_enable_events
299 *
300 * Add event to trace session
301 */
302int cmd_enable_events(int argc, const char **argv)
303{
304 int opt, ret;
305 static poptContext pc;
306 char *session_name = NULL;
307
308 pc = poptGetContext(NULL, argc, argv, long_options, 0);
309 poptReadDefaultConfig(pc, 0);
310
311 /* Default event type */
312 opt_event_type = LTTNG_EVENT_TRACEPOINT;
313
314 while ((opt = poptGetNextOpt(pc)) != -1) {
315 switch (opt) {
316 case OPT_HELP:
317 usage(stderr);
318 ret = CMD_SUCCESS;
319 goto end;
320 case OPT_USERSPACE:
321 opt_userspace = 1;
322 opt_cmd_name = poptGetOptArg(pc);
323 break;
324 case OPT_TRACEPOINT:
325 opt_event_type = LTTNG_EVENT_TRACEPOINT;
326 break;
327 case OPT_MARKER:
328 ret = CMD_NOT_IMPLEMENTED;
329 goto end;
330 case OPT_PROBE:
331 opt_event_type = LTTNG_EVENT_PROBE;
332 opt_probe = poptGetOptArg(pc);
333 break;
334 case OPT_FUNCTION:
335 opt_event_type = LTTNG_EVENT_FUNCTION;
336 opt_function = poptGetOptArg(pc);
337 break;
338 case OPT_FUNCTION_ENTRY:
339 opt_event_type = LTTNG_EVENT_FUNCTION_ENTRY;
340 opt_function_entry_symbol = poptGetOptArg(pc);
341 break;
342 default:
343 usage(stderr);
344 ret = CMD_UNDEFINED;
345 goto end;
346 }
347 }
348
349 opt_event_list = (char*) poptGetArg(pc);
350 if (opt_event_list == NULL && opt_enable_all == 0) {
351 ERR("Missing event name(s).\n");
352 usage(stderr);
353 ret = CMD_SUCCESS;
354 goto end;
355 }
356
357 if (!opt_session_name) {
358 session_name = get_session_name();
359 if (session_name == NULL) {
360 ret = -1;
361 goto end;
362 }
363 } else {
364 session_name = opt_session_name;
365 }
366
367 ret = enable_events(session_name);
368
369end:
370 if (opt_session_name == NULL) {
371 free(session_name);
372 }
373
374 return ret;
375}
This page took 0.023117 seconds and 4 git commands to generate.