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