Merge duplicate code in consumer for destroy relayd
[lttng-tools.git] / src / bin / lttng / commands / disable_events.c
CommitLineData
e953ef25
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
e953ef25
DG
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
e953ef25
DG
16 */
17
18#define _GNU_SOURCE
19#include <popt.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26
c399183f 27#include "../command.h"
e953ef25
DG
28
29static char *opt_event_list;
9730260e 30static int opt_kernel;
e953ef25 31static char *opt_channel_name;
5440dc42 32static char *opt_session_name;
e953ef25
DG
33static int opt_userspace;
34static int opt_disable_all;
d78d6610
DG
35#if 0
36/* Not implemented yet */
37static char *opt_cmd_name;
e953ef25 38static pid_t opt_pid;
d78d6610 39#endif
e953ef25
DG
40
41enum {
42 OPT_HELP = 1,
43 OPT_USERSPACE,
679b4943 44 OPT_LIST_OPTIONS,
e953ef25
DG
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},
d78d6610
DG
56#if 0
57 /* Not implemented yet */
6caa2bcc 58 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
e953ef25 59 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
d78d6610
DG
60#else
61 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
62#endif
679b4943 63 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
e953ef25
DG
64 {0, 0, 0, 0, 0, 0, 0}
65};
66
67/*
68 * usage
69 */
70static void usage(FILE *ofp)
71{
72 fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] [options]\n");
73 fprintf(ofp, "\n");
74 fprintf(ofp, " -h, --help Show this help\n");
679b4943 75 fprintf(ofp, " --list-options Simple listing of options\n");
27221701
DG
76 fprintf(ofp, " -s, --session Apply to session name\n");
77 fprintf(ofp, " -c, --channel Apply to this channel\n");
9730260e 78 fprintf(ofp, " -a, --all-events Disable all tracepoints\n");
e953ef25 79 fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n");
d78d6610 80#if 0
27221701 81 fprintf(ofp, " -u, --userspace [CMD] Apply to the user-space tracer\n");
e14f64a8
DG
82 fprintf(ofp, " If no CMD, the domain used is UST global\n");
83 fprintf(ofp, " or else the domain is UST EXEC_NAME\n");
84 fprintf(ofp, " -p, --pid PID If -u, apply to specific PID (domain: UST PID)\n");
d78d6610 85#else
27221701 86 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
d78d6610 87#endif
e953ef25
DG
88 fprintf(ofp, "\n");
89}
90
91/*
92 * disable_events
93 *
94 * Disabling event using the lttng API.
95 */
cd80958d 96static int disable_events(char *session_name)
e953ef25 97{
ae856491 98 int err, ret = CMD_SUCCESS, warn = 0;
b73d0b29 99 char *event_name, *channel_name = NULL;
7d29a247 100 struct lttng_domain dom;
e953ef25 101
441c16a7
MD
102 memset(&dom, 0, sizeof(dom));
103
d78d6610 104 /* Create lttng domain */
7d29a247
DG
105 if (opt_kernel) {
106 dom.type = LTTNG_DOMAIN_KERNEL;
d78d6610 107 } else if (opt_userspace) {
9730260e 108 dom.type = LTTNG_DOMAIN_UST;
9730260e 109 } else {
e14f64a8 110 ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
d16c1a4c 111 ret = CMD_ERROR;
9730260e 112 goto error;
7d29a247
DG
113 }
114
ae856491
DG
115 /* Get channel name */
116 if (opt_channel_name == NULL) {
117 err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME);
118 if (err < 0) {
119 ret = CMD_FATAL;
120 goto error;
121 }
122 } else {
123 channel_name = opt_channel_name;
124 }
125
cd80958d
DG
126 handle = lttng_create_handle(session_name, &dom);
127 if (handle == NULL) {
128 ret = -1;
129 goto error;
130 }
131
e953ef25 132 if (opt_disable_all) {
9730260e
DG
133 ret = lttng_disable_event(handle, NULL, channel_name);
134 if (ret < 0) {
ae856491 135 /* Don't set ret so lttng can interpret the sessiond error. */
e953ef25
DG
136 goto error;
137 }
138
9730260e
DG
139 MSG("All %s events are disabled in channel %s",
140 opt_kernel ? "kernel" : "UST", channel_name);
141 goto end;
e953ef25
DG
142 }
143
144 /* Strip event list */
145 event_name = strtok(opt_event_list, ",");
146 while (event_name != NULL) {
ae856491 147 DBG("Disabling event %s", event_name);
e953ef25 148
9730260e
DG
149 ret = lttng_disable_event(handle, event_name, channel_name);
150 if (ret < 0) {
ae856491
DG
151 ERR("Event %s: %s (channel %s, session %s)", event_name,
152 lttng_strerror(ret), channel_name, session_name);
153 warn = 1;
9730260e 154 } else {
ae856491
DG
155 MSG("%s event %s disabled in channel %s for session %s",
156 opt_kernel ? "kernel" : "UST", event_name, channel_name,
157 session_name);
9730260e
DG
158 }
159
e953ef25
DG
160 /* Next event */
161 event_name = strtok(NULL, ",");
162 }
163
ae856491
DG
164 ret = CMD_SUCCESS;
165
9730260e 166end:
e953ef25 167error:
ae856491
DG
168 if (warn) {
169 ret = CMD_WARNING;
170 }
b73d0b29
MD
171 if (opt_channel_name == NULL) {
172 free(channel_name);
173 }
cd80958d
DG
174 lttng_destroy_handle(handle);
175
e953ef25
DG
176 return ret;
177}
178
179/*
180 * cmd_disable_events
181 *
182 * Disable event to trace session
183 */
184int cmd_disable_events(int argc, const char **argv)
185{
ca1c3607 186 int opt, ret = CMD_SUCCESS;
e953ef25 187 static poptContext pc;
cd80958d 188 char *session_name = NULL;
e953ef25
DG
189
190 pc = poptGetContext(NULL, argc, argv, long_options, 0);
191 poptReadDefaultConfig(pc, 0);
192
193 while ((opt = poptGetNextOpt(pc)) != -1) {
194 switch (opt) {
195 case OPT_HELP:
ca1c3607 196 usage(stdout);
e953ef25
DG
197 goto end;
198 case OPT_USERSPACE:
199 opt_userspace = 1;
e953ef25 200 break;
679b4943
SM
201 case OPT_LIST_OPTIONS:
202 list_cmd_options(stdout, long_options);
679b4943 203 goto end;
e953ef25
DG
204 default:
205 usage(stderr);
206 ret = CMD_UNDEFINED;
207 goto end;
208 }
209 }
210
211 opt_event_list = (char*) poptGetArg(pc);
212 if (opt_event_list == NULL && opt_disable_all == 0) {
213 ERR("Missing event name(s).\n");
214 usage(stderr);
ca1c3607 215 ret = CMD_ERROR;
e953ef25
DG
216 goto end;
217 }
218
cd80958d
DG
219 if (!opt_session_name) {
220 session_name = get_session_name();
221 if (session_name == NULL) {
ca1c3607 222 ret = CMD_ERROR;
cd80958d
DG
223 goto end;
224 }
225 } else {
226 session_name = opt_session_name;
227 }
228
229 ret = disable_events(session_name);
e953ef25
DG
230
231end:
5853fd43
DG
232 if (!opt_session_name && session_name) {
233 free(session_name);
234 }
ca1c3607 235 poptFreeContext(pc);
e953ef25
DG
236 return ret;
237}
This page took 0.03909 seconds and 4 git commands to generate.