Add new kernel consumer name to gitignore
[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
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"
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;
41static char *opt_kprobe_addr;
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");
89 fprintf(ofp, " --kprobe ADDR Kernel Kprobe\n");
90 fprintf(ofp, " --function SYMBOL Function tracer event\n");
91 fprintf(ofp, " --marker User-space marker (deprecated)\n");
92 fprintf(ofp, "\n");
93}
94
95/*
96 * enable_events
97 *
98 * Enabling event using the lttng API.
99 */
100static int enable_events(void)
101{
102 int err, ret = CMD_SUCCESS;
103 char *event_name, *channel_name;
104 struct lttng_event ev;
105
5440dc42 106 if (set_session_name(opt_session_name) < 0) {
f3ed775e
DG
107 ret = CMD_ERROR;
108 goto error;
109 }
110
111 if (opt_channel_name == NULL) {
112 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
113 if (err < 0) {
114 ret = CMD_FATAL;
115 goto error;
116 }
117 } else {
118 channel_name = opt_channel_name;
119 }
120
121 if (opt_enable_all) {
122 if (opt_kernel) {
123 ret = lttng_kernel_enable_event(NULL, channel_name);
124 goto error;
125 }
126
127 /* TODO: User-space tracer */
128 }
129
130 /* Strip event list */
131 event_name = strtok(opt_event_list, ",");
132 while (event_name != NULL) {
133 /* Kernel tracer action */
134 if (opt_kernel) {
135 DBG("Enabling kernel event %s for channel %s",
136 event_name, channel_name);
137 /* Copy name and type of the event */
138 strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
139 ev.type = opt_event_type;
140
141 switch (opt_event_type) {
142 case LTTNG_EVENT_TRACEPOINTS:
143 ret = lttng_kernel_enable_event(&ev, channel_name);
144 break;
145 case LTTNG_EVENT_KPROBES:
146 /* FIXME: check addr format */
147 ev.attr.kprobe.addr = atoll(opt_kprobe_addr);
148 ret = lttng_kernel_enable_event(&ev, channel_name);
149 break;
150 case LTTNG_EVENT_FUNCTION:
151 strncpy(ev.attr.ftrace.symbol_name, opt_function_symbol, LTTNG_SYMBOL_NAME_LEN);
152 ret = lttng_kernel_enable_event(&ev, channel_name);
153 break;
154 default:
155 ret = CMD_NOT_IMPLEMENTED;
156 goto error;
157 }
158
159 if (ret > 0) {
160 MSG("Kernel event %s created in channel %s", event_name, channel_name);
161 }
162 } else if (opt_userspace) { /* User-space tracer action */
163 /*
164 * TODO: Waiting on lttng UST 2.0
165 */
166 if (opt_pid_all) {
167 } else if (opt_pid != 0) {
168 }
169 ret = CMD_NOT_IMPLEMENTED;
170 goto error;
171 } else {
172 ERR("Please specify a tracer (kernel or user-space)");
173 goto error;
174 }
175
176 /* Next event */
177 event_name = strtok(NULL, ",");
178 }
179
180error:
181 return ret;
182}
183
184/*
185 * cmd_enable_events
186 *
187 * Add event to trace session
188 */
189int cmd_enable_events(int argc, const char **argv)
190{
191 int opt, ret;
192 static poptContext pc;
193
194 pc = poptGetContext(NULL, argc, argv, long_options, 0);
195 poptReadDefaultConfig(pc, 0);
196
197 /* Default event type */
198 opt_event_type = LTTNG_KERNEL_TRACEPOINTS;
199
200 while ((opt = poptGetNextOpt(pc)) != -1) {
201 switch (opt) {
202 case OPT_HELP:
203 usage(stderr);
204 ret = CMD_SUCCESS;
205 goto end;
206 case OPT_USERSPACE:
207 opt_userspace = 1;
208 opt_cmd_name = poptGetOptArg(pc);
209 break;
210 case OPT_TRACEPOINT:
211 opt_event_type = LTTNG_EVENT_TRACEPOINTS;
212 break;
213 case OPT_MARKER:
214 ret = CMD_NOT_IMPLEMENTED;
215 goto end;
216 case OPT_KPROBE:
217 opt_event_type = LTTNG_EVENT_KPROBES;
218 opt_kprobe_addr = poptGetOptArg(pc);
219 break;
220 case OPT_FUNCTION:
221 opt_event_type = LTTNG_EVENT_FUNCTION;
222 opt_function_symbol = poptGetOptArg(pc);
223 break;
224 default:
225 usage(stderr);
226 ret = CMD_UNDEFINED;
227 goto end;
228 }
229 }
230
231 opt_event_list = (char*) poptGetArg(pc);
232 if (opt_event_list == NULL && opt_enable_all == 0) {
233 ERR("Missing event name(s).\n");
234 usage(stderr);
235 ret = CMD_SUCCESS;
236 goto end;
237 }
238
239 ret = enable_events();
240
241end:
242 return ret;
243}
This page took 0.031136 seconds and 4 git commands to generate.