lttng disable_events: ensure end of string is set to \0
[lttng-tools.git] / lttng / commands / disable_events.c
CommitLineData
e953ef25
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.
e953ef25
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
6e2d116c
DG
28#include "../cmd.h"
29#include "../conf.h"
30#include "../utils.h"
e953ef25
DG
31
32static char *opt_event_list;
9730260e 33static int opt_kernel;
e953ef25 34static char *opt_channel_name;
5440dc42 35static char *opt_session_name;
e953ef25
DG
36static int opt_pid_all;
37static int opt_userspace;
eeac7d46 38static char *opt_cmd_name;
e953ef25
DG
39static int opt_disable_all;
40static pid_t opt_pid;
41
42enum {
43 OPT_HELP = 1,
44 OPT_USERSPACE,
45};
46
cd80958d
DG
47static struct lttng_handle *handle;
48
e953ef25
DG
49static struct poptOption long_options[] = {
50 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
51 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 52 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
e953ef25
DG
53 {"all-events", 'a', POPT_ARG_VAL, &opt_disable_all, 1, 0, 0},
54 {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0},
55 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
6caa2bcc 56 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
e953ef25
DG
57 {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
58 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
59 {0, 0, 0, 0, 0, 0, 0}
60};
61
62/*
63 * usage
64 */
65static void usage(FILE *ofp)
66{
67 fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] [options]\n");
68 fprintf(ofp, "\n");
69 fprintf(ofp, " -h, --help Show this help\n");
5440dc42 70 fprintf(ofp, " -s, --session Apply on session name\n");
e953ef25 71 fprintf(ofp, " -c, --channel Apply on this channel\n");
9730260e 72 fprintf(ofp, " -a, --all-events Disable all tracepoints\n");
e953ef25
DG
73 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
74 fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n");
75 fprintf(ofp, " --all If -u, apply on all traceable apps\n");
76 fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n");
77 fprintf(ofp, "\n");
78}
79
80/*
81 * disable_events
82 *
83 * Disabling event using the lttng API.
84 */
cd80958d 85static int disable_events(char *session_name)
e953ef25
DG
86{
87 int err, ret = CMD_SUCCESS;
b73d0b29 88 char *event_name, *channel_name = NULL;
7d29a247 89 struct lttng_domain dom;
e953ef25 90
e953ef25
DG
91 if (opt_channel_name == NULL) {
92 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
93 if (err < 0) {
94 ret = CMD_FATAL;
95 goto error;
96 }
97 } else {
98 channel_name = opt_channel_name;
99 }
100
7d29a247
DG
101 if (opt_kernel) {
102 dom.type = LTTNG_DOMAIN_KERNEL;
9730260e
DG
103 } else if (opt_pid != 0) {
104 dom.type = LTTNG_DOMAIN_UST_PID;
105 dom.attr.pid = opt_pid;
106 DBG("PID %d set to lttng handle", opt_pid);
107 } else if (opt_userspace && opt_cmd_name == NULL) {
108 dom.type = LTTNG_DOMAIN_UST;
109 DBG("UST global domain selected");
110 } else if (opt_userspace && opt_cmd_name != NULL) {
111 dom.type = LTTNG_DOMAIN_UST_EXEC_NAME;
112 strncpy(dom.attr.exec_name, opt_cmd_name, NAME_MAX);
422138c8 113 dom.attr.exec_name[NAME_MAX - 1] = '\0';
9730260e
DG
114 } else {
115 ERR("Please specify a tracer (--kernel or --userspace)");
116 ret = CMD_NOT_IMPLEMENTED;
117 goto error;
7d29a247
DG
118 }
119
cd80958d
DG
120 handle = lttng_create_handle(session_name, &dom);
121 if (handle == NULL) {
122 ret = -1;
123 goto error;
124 }
125
e953ef25 126 if (opt_disable_all) {
9730260e
DG
127 ret = lttng_disable_event(handle, NULL, channel_name);
128 if (ret < 0) {
e953ef25
DG
129 goto error;
130 }
131
9730260e
DG
132 MSG("All %s events are disabled in channel %s",
133 opt_kernel ? "kernel" : "UST", channel_name);
134 goto end;
e953ef25
DG
135 }
136
137 /* Strip event list */
138 event_name = strtok(opt_event_list, ",");
139 while (event_name != NULL) {
140 /* Kernel tracer action */
141 if (opt_kernel) {
9730260e 142 DBG("Disabling kernel event %s in channel %s",
e953ef25 143 event_name, channel_name);
e953ef25 144 } else if (opt_userspace) { /* User-space tracer action */
9730260e
DG
145 if (!opt_pid_all) {
146 MSG("Only supporting tracing all UST processes (-u --all) for now.");
147 ret = CMD_NOT_IMPLEMENTED;
148 goto error;
e953ef25 149 }
9730260e
DG
150 DBG("Disabling UST event %s in channel %s",
151 event_name, channel_name);
e953ef25 152 } else {
048e2159 153 ERR("Please specify a tracer (--kernel or --userspace)");
e953ef25
DG
154 goto error;
155 }
156
9730260e
DG
157 ret = lttng_disable_event(handle, event_name, channel_name);
158 if (ret < 0) {
159 MSG("Unable to disable %s event %s in channel %s",
160 opt_kernel ? "kernel" : "UST", event_name,
161 channel_name);
162 } else {
163 MSG("%s event %s disabled in channel %s",
164 opt_kernel ? "kernel" : "UST", event_name,
165 channel_name);
166 }
167
e953ef25
DG
168 /* Next event */
169 event_name = strtok(NULL, ",");
170 }
171
9730260e 172end:
e953ef25 173error:
b73d0b29
MD
174 if (opt_channel_name == NULL) {
175 free(channel_name);
176 }
cd80958d
DG
177 lttng_destroy_handle(handle);
178
e953ef25
DG
179 return ret;
180}
181
182/*
183 * cmd_disable_events
184 *
185 * Disable event to trace session
186 */
187int cmd_disable_events(int argc, const char **argv)
188{
189 int opt, ret;
190 static poptContext pc;
cd80958d 191 char *session_name = NULL;
e953ef25
DG
192
193 pc = poptGetContext(NULL, argc, argv, long_options, 0);
194 poptReadDefaultConfig(pc, 0);
195
196 while ((opt = poptGetNextOpt(pc)) != -1) {
197 switch (opt) {
198 case OPT_HELP:
199 usage(stderr);
200 ret = CMD_SUCCESS;
201 goto end;
202 case OPT_USERSPACE:
203 opt_userspace = 1;
e953ef25
DG
204 break;
205 default:
206 usage(stderr);
207 ret = CMD_UNDEFINED;
208 goto end;
209 }
210 }
211
212 opt_event_list = (char*) poptGetArg(pc);
213 if (opt_event_list == NULL && opt_disable_all == 0) {
214 ERR("Missing event name(s).\n");
215 usage(stderr);
216 ret = CMD_SUCCESS;
217 goto end;
218 }
219
cd80958d
DG
220 if (!opt_session_name) {
221 session_name = get_session_name();
222 if (session_name == NULL) {
223 ret = -1;
224 goto end;
225 }
226 } else {
227 session_name = opt_session_name;
228 }
229
230 ret = disable_events(session_name);
e953ef25
DG
231
232end:
233 return ret;
234}
This page took 0.033534 seconds and 4 git commands to generate.