2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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
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.
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.
25 #include <sys/types.h>
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
;
42 static char *opt_kprobe
;
43 static char *opt_function_symbol
;
44 static char *opt_channel_name
;
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 {"kprobe", 0, POPT_ARG_STRING
, 0, OPT_KPROBE
, 0, 0},
68 {"function", 0, POPT_ARG_STRING
, 0, OPT_FUNCTION
, 0, 0},
75 static void usage(FILE *ofp
)
77 fprintf(ofp
, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\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");
88 fprintf(ofp
, "Event options:\n");
89 fprintf(ofp
, " --tracepoint Tracepoint event (default)\n");
90 fprintf(ofp
, " --kprobe [addr | symbol+offset]\n");
91 fprintf(ofp
, " Kernel Kprobe. (addr or offset can be base 8,10 and 16)\n");
92 fprintf(ofp
, " --function SYMBOL Function tracer event\n");
93 fprintf(ofp
, " --marker User-space marker (deprecated)\n");
100 * Parse kprobe options.
102 static int parse_kprobe_opts(struct lttng_event
*ev
, char *opt
)
106 char name
[LTTNG_SYMBOL_NAME_LEN
];
113 /* Check for symbol+offset */
114 ret
= sscanf(opt
, "%[^'+']+%" SCNu64
, name
, &hex
);
116 strncpy(ev
->attr
.probe
.symbol_name
, name
, LTTNG_SYMBOL_NAME_LEN
);
117 DBG("kprobe symbol %s", ev
->attr
.probe
.symbol_name
);
119 ERR("Invalid kprobe offset %" PRIu64
, hex
);
123 ev
->attr
.probe
.offset
= hex
;
124 DBG("kprobe offset %" PRIu64
, ev
->attr
.probe
.offset
);
128 /* Check for address */
129 ret
= sscanf(opt
, "%" SCNu64
, &hex
);
132 ERR("Invalid kprobe address %" PRIu64
, hex
);
136 ev
->attr
.probe
.addr
= hex
;
137 DBG("kprobe addr %" PRIu64
, ev
->attr
.probe
.addr
);
151 * Enabling event using the lttng API.
153 static int enable_events(void)
155 int err
, ret
= CMD_SUCCESS
;
156 char *event_name
, *channel_name
= NULL
;
157 struct lttng_event ev
;
158 struct lttng_domain dom
;
160 if (set_session_name(opt_session_name
) < 0) {
165 if (opt_channel_name
== NULL
) {
166 err
= asprintf(&channel_name
, DEFAULT_CHANNEL_NAME
);
172 channel_name
= opt_channel_name
;
175 /* Create lttng domain */
177 dom
.type
= LTTNG_DOMAIN_KERNEL
;
180 if (opt_enable_all
) {
182 ret
= lttng_enable_event(&dom
, NULL
, channel_name
);
184 MSG("All kernel events are enabled in channel %s", channel_name
);
189 /* TODO: User-space tracer */
192 /* Strip event list */
193 event_name
= strtok(opt_event_list
, ",");
194 while (event_name
!= NULL
) {
195 /* Kernel tracer action */
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
;
203 switch (opt_event_type
) {
204 case LTTNG_EVENT_TRACEPOINT
:
206 case LTTNG_EVENT_PROBE
:
207 ret
= parse_kprobe_opts(&ev
, opt_kprobe
);
209 ERR("Unable to parse kprobe options");
214 case LTTNG_EVENT_FUNCTION
:
215 strncpy(ev
.attr
.ftrace
.symbol_name
, opt_function_symbol
, LTTNG_SYMBOL_NAME_LEN
);
218 ret
= CMD_NOT_IMPLEMENTED
;
222 ret
= lttng_enable_event(&dom
, &ev
, channel_name
);
224 MSG("Kernel event %s created in channel %s", event_name
, channel_name
);
225 } else if (ret
< 0) {
226 ERR("Unable to find event %s", ev
.name
);
228 } else if (opt_userspace
) { /* User-space tracer action */
230 * TODO: Waiting on lttng UST 2.0
233 } else if (opt_pid
!= 0) {
235 ret
= CMD_NOT_IMPLEMENTED
;
238 ERR("Please specify a tracer (kernel or user-space)");
243 event_name
= strtok(NULL
, ",");
247 if (opt_channel_name
== NULL
) {
256 * Add event to trace session
258 int cmd_enable_events(int argc
, const char **argv
)
261 static poptContext pc
;
263 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
264 poptReadDefaultConfig(pc
, 0);
266 /* Default event type */
267 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
269 while ((opt
= poptGetNextOpt(pc
)) != -1) {
277 opt_cmd_name
= poptGetOptArg(pc
);
280 opt_event_type
= LTTNG_EVENT_TRACEPOINT
;
283 ret
= CMD_NOT_IMPLEMENTED
;
286 opt_event_type
= LTTNG_EVENT_PROBE
;
287 opt_kprobe
= poptGetOptArg(pc
);
290 opt_event_type
= LTTNG_EVENT_FUNCTION
;
291 opt_function_symbol
= poptGetOptArg(pc
);
300 opt_event_list
= (char*) poptGetArg(pc
);
301 if (opt_event_list
== NULL
&& opt_enable_all
== 0) {
302 ERR("Missing event name(s).\n");
308 ret
= enable_events();
This page took 0.046204 seconds and 4 git commands to generate.