| 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" |
| 29 | #include "conf.h" |
| 30 | #include "utils.h" |
| 31 | |
| 32 | static char *opt_event_list; |
| 33 | static char *opt_kernel; |
| 34 | static char *opt_cmd_name; |
| 35 | static char *opt_channel_name; |
| 36 | static char *opt_session_name; |
| 37 | static int opt_pid_all; |
| 38 | static int opt_userspace; |
| 39 | static int opt_disable_all; |
| 40 | static pid_t opt_pid; |
| 41 | |
| 42 | enum { |
| 43 | OPT_HELP = 1, |
| 44 | OPT_USERSPACE, |
| 45 | }; |
| 46 | |
| 47 | static struct poptOption long_options[] = { |
| 48 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 49 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 50 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, |
| 51 | {"all-events", 'a', POPT_ARG_VAL, &opt_disable_all, 1, 0, 0}, |
| 52 | {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0}, |
| 53 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 54 | {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, 0, OPT_USERSPACE, 0, 0}, |
| 55 | {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0}, |
| 56 | {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, |
| 57 | {0, 0, 0, 0, 0, 0, 0} |
| 58 | }; |
| 59 | |
| 60 | /* |
| 61 | * usage |
| 62 | */ |
| 63 | static void usage(FILE *ofp) |
| 64 | { |
| 65 | fprintf(ofp, "usage: lttng disable-event NAME[,NAME2,...] [options]\n"); |
| 66 | fprintf(ofp, "\n"); |
| 67 | fprintf(ofp, " -h, --help Show this help\n"); |
| 68 | fprintf(ofp, " -s, --session Apply on session name\n"); |
| 69 | fprintf(ofp, " -c, --channel Apply on this channel\n"); |
| 70 | fprintf(ofp, " -a, --all-events Enable all tracepoints\n"); |
| 71 | fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n"); |
| 72 | fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n"); |
| 73 | fprintf(ofp, " --all If -u, apply on all traceable apps\n"); |
| 74 | fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n"); |
| 75 | fprintf(ofp, "\n"); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * disable_events |
| 80 | * |
| 81 | * Disabling event using the lttng API. |
| 82 | */ |
| 83 | static int disable_events(void) |
| 84 | { |
| 85 | int err, ret = CMD_SUCCESS; |
| 86 | char *event_name, *channel_name = NULL; |
| 87 | struct lttng_event ev; |
| 88 | |
| 89 | if (set_session_name(opt_session_name) < 0) { |
| 90 | ret = CMD_ERROR; |
| 91 | goto error; |
| 92 | } |
| 93 | |
| 94 | if (opt_channel_name == NULL) { |
| 95 | err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME); |
| 96 | if (err < 0) { |
| 97 | ret = CMD_FATAL; |
| 98 | goto error; |
| 99 | } |
| 100 | } else { |
| 101 | channel_name = opt_channel_name; |
| 102 | } |
| 103 | |
| 104 | if (opt_disable_all) { |
| 105 | if (opt_kernel) { |
| 106 | ret = lttng_kernel_disable_event(NULL, channel_name); |
| 107 | goto error; |
| 108 | } |
| 109 | |
| 110 | /* TODO: User-space tracer */ |
| 111 | } |
| 112 | |
| 113 | /* Strip event list */ |
| 114 | event_name = strtok(opt_event_list, ","); |
| 115 | while (event_name != NULL) { |
| 116 | /* Kernel tracer action */ |
| 117 | if (opt_kernel) { |
| 118 | DBG("Disabling kernel event %s for channel %s", |
| 119 | event_name, channel_name); |
| 120 | |
| 121 | /* Copy name and type of the event */ |
| 122 | strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN); |
| 123 | ret = lttng_kernel_disable_event(event_name, channel_name); |
| 124 | if (ret < 0) { |
| 125 | MSG("Unable to disable event %s for channel %s", |
| 126 | event_name, channel_name); |
| 127 | } else { |
| 128 | MSG("Kernel event %s disabled for channel %s", |
| 129 | event_name, channel_name); |
| 130 | } |
| 131 | } else if (opt_userspace) { /* User-space tracer action */ |
| 132 | /* |
| 133 | * TODO: Waiting on lttng UST 2.0 |
| 134 | */ |
| 135 | if (opt_pid_all) { |
| 136 | } else if (opt_pid != 0) { |
| 137 | } |
| 138 | ret = CMD_NOT_IMPLEMENTED; |
| 139 | goto error; |
| 140 | } else { |
| 141 | ERR("Please specify a tracer (kernel or user-space)"); |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | /* Next event */ |
| 146 | event_name = strtok(NULL, ","); |
| 147 | } |
| 148 | |
| 149 | error: |
| 150 | if (opt_channel_name == NULL) { |
| 151 | free(channel_name); |
| 152 | } |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * cmd_disable_events |
| 158 | * |
| 159 | * Disable event to trace session |
| 160 | */ |
| 161 | int cmd_disable_events(int argc, const char **argv) |
| 162 | { |
| 163 | int opt, ret; |
| 164 | static poptContext pc; |
| 165 | |
| 166 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 167 | poptReadDefaultConfig(pc, 0); |
| 168 | |
| 169 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 170 | switch (opt) { |
| 171 | case OPT_HELP: |
| 172 | usage(stderr); |
| 173 | ret = CMD_SUCCESS; |
| 174 | goto end; |
| 175 | case OPT_USERSPACE: |
| 176 | opt_userspace = 1; |
| 177 | opt_cmd_name = poptGetOptArg(pc); |
| 178 | break; |
| 179 | default: |
| 180 | usage(stderr); |
| 181 | ret = CMD_UNDEFINED; |
| 182 | goto end; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | opt_event_list = (char*) poptGetArg(pc); |
| 187 | if (opt_event_list == NULL && opt_disable_all == 0) { |
| 188 | ERR("Missing event name(s).\n"); |
| 189 | usage(stderr); |
| 190 | ret = CMD_SUCCESS; |
| 191 | goto end; |
| 192 | } |
| 193 | |
| 194 | ret = disable_events(); |
| 195 | |
| 196 | end: |
| 197 | return ret; |
| 198 | } |