Fix memory leak in lttng cli
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.c
CommitLineData
26cc6b4e
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.
26cc6b4e
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
c399183f 28#include "../command.h"
26cc6b4e
DG
29
30static char *opt_channels;
e14f64a8 31static int opt_kernel;
5440dc42 32static char *opt_session_name;
26cc6b4e 33static int opt_userspace;
d78d6610
DG
34#if 0
35/* Not implemented yet */
eeac7d46 36static char *opt_cmd_name;
26cc6b4e 37static pid_t opt_pid;
d78d6610 38#endif
26cc6b4e
DG
39
40enum {
41 OPT_HELP = 1,
42 OPT_USERSPACE,
679b4943 43 OPT_LIST_OPTIONS,
26cc6b4e
DG
44};
45
cd80958d
DG
46static struct lttng_handle *handle;
47
26cc6b4e
DG
48static struct poptOption long_options[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
5440dc42 51 {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0},
26cc6b4e 52 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
d78d6610
DG
53#if 0
54 /* Not implemented yet */
6caa2bcc 55 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
26cc6b4e 56 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
d78d6610
DG
57#else
58 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
59#endif
679b4943 60 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
26cc6b4e
DG
61 {0, 0, 0, 0, 0, 0, 0}
62};
63
64/*
65 * usage
66 */
67static void usage(FILE *ofp)
68{
69 fprintf(ofp, "usage: lttng disable-channel NAME[,NAME2,...] [options]\n");
70 fprintf(ofp, "\n");
78f0bacd 71 fprintf(ofp, " -h, --help Show this help\n");
679b4943 72 fprintf(ofp, " --list-options Simple listing of options\n");
27221701
DG
73 fprintf(ofp, " -s, --session Apply to session name\n");
74 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
d78d6610 75#if 0
27221701 76 fprintf(ofp, " -u, --userspace [CMD] Apply to the user-space tracer\n");
e14f64a8
DG
77 fprintf(ofp, " If no CMD, the domain used is UST global\n");
78 fprintf(ofp, " or else the domain is UST EXEC_NAME\n");
79 fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
d78d6610 80#else
27221701 81 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
d78d6610 82#endif
26cc6b4e
DG
83 fprintf(ofp, "\n");
84}
85
86/*
cd80958d 87 * Disabling channel using the lttng API.
26cc6b4e 88 */
cd80958d 89static int disable_channels(char *session_name)
26cc6b4e 90{
ae856491 91 int ret = CMD_SUCCESS, warn = 0;
26cc6b4e 92 char *channel_name;
7d29a247 93 struct lttng_domain dom;
26cc6b4e 94
d78d6610 95 /* Create lttng domain */
7d29a247
DG
96 if (opt_kernel) {
97 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 98 } else if (opt_userspace) {
78f0bacd 99 dom.type = LTTNG_DOMAIN_UST;
78f0bacd 100 } else {
e14f64a8 101 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
d16c1a4c 102 ret = CMD_ERROR;
78f0bacd 103 goto error;
7d29a247
DG
104 }
105
cd80958d
DG
106 handle = lttng_create_handle(session_name, &dom);
107 if (handle == NULL) {
108 ret = -1;
109 goto error;
110 }
111
26cc6b4e
DG
112 /* Strip channel list */
113 channel_name = strtok(opt_channels, ",");
114 while (channel_name != NULL) {
78f0bacd
DG
115 DBG("Disabling channel %s", channel_name);
116
117 ret = lttng_disable_channel(handle, channel_name);
118 if (ret < 0) {
ae856491
DG
119 ERR("Channel %s: %s (session %s)", channel_name,
120 lttng_strerror(ret), session_name);
121 warn = 1;
26cc6b4e 122 } else {
7885e399 123 MSG("%s channel %s disabled for session %s",
ae856491 124 opt_kernel ? "Kernel" : "UST", channel_name, session_name);
26cc6b4e
DG
125 }
126
127 /* Next channel */
128 channel_name = strtok(NULL, ",");
129 }
130
ae856491
DG
131 ret = CMD_SUCCESS;
132
26cc6b4e 133error:
ae856491
DG
134 if (warn) {
135 ret = CMD_WARNING;
136 }
137
cd80958d
DG
138 lttng_destroy_handle(handle);
139
26cc6b4e
DG
140 return ret;
141}
142
143/*
144 * cmd_disable_channels
145 *
146 * Disable channel to trace session
147 */
148int cmd_disable_channels(int argc, const char **argv)
149{
ca1c3607 150 int opt, ret = CMD_SUCCESS;
26cc6b4e 151 static poptContext pc;
cd80958d 152 char *session_name = NULL;
26cc6b4e
DG
153
154 pc = poptGetContext(NULL, argc, argv, long_options, 0);
155 poptReadDefaultConfig(pc, 0);
156
157 while ((opt = poptGetNextOpt(pc)) != -1) {
158 switch (opt) {
159 case OPT_HELP:
ca1c3607 160 usage(stdout);
26cc6b4e
DG
161 goto end;
162 case OPT_USERSPACE:
163 opt_userspace = 1;
164 break;
679b4943
SM
165 case OPT_LIST_OPTIONS:
166 list_cmd_options(stdout, long_options);
679b4943 167 goto end;
26cc6b4e
DG
168 default:
169 usage(stderr);
170 ret = CMD_UNDEFINED;
171 goto end;
172 }
173 }
174
175 opt_channels = (char*) poptGetArg(pc);
176 if (opt_channels == NULL) {
177 ERR("Missing channel name(s).\n");
178 usage(stderr);
ca1c3607 179 ret = CMD_ERROR;
26cc6b4e
DG
180 goto end;
181 }
182
cd80958d
DG
183 if (!opt_session_name) {
184 session_name = get_session_name();
185 if (session_name == NULL) {
ca1c3607 186 ret = CMD_ERROR;
cd80958d
DG
187 goto end;
188 }
189 } else {
190 session_name = opt_session_name;
191 }
192
193 ret = disable_channels(session_name);
26cc6b4e
DG
194
195end:
5853fd43
DG
196 if (!opt_session_name && session_name) {
197 free(session_name);
198 }
ca1c3607 199 poptFreeContext(pc);
26cc6b4e
DG
200 return ret;
201}
This page took 0.054455 seconds and 4 git commands to generate.