| 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_channels; |
| 33 | static char *opt_kernel; |
| 34 | static int opt_pid_all; |
| 35 | static int opt_userspace; |
| 36 | static pid_t opt_pid; |
| 37 | |
| 38 | enum { |
| 39 | OPT_HELP = 1, |
| 40 | OPT_USERSPACE, |
| 41 | }; |
| 42 | |
| 43 | static struct poptOption long_options[] = { |
| 44 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 45 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 46 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 47 | {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, |
| 48 | {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0}, |
| 49 | {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, |
| 50 | {0, 0, 0, 0, 0, 0, 0} |
| 51 | }; |
| 52 | |
| 53 | /* |
| 54 | * usage |
| 55 | */ |
| 56 | static void usage(FILE *ofp) |
| 57 | { |
| 58 | fprintf(ofp, "usage: lttng disable-channel NAME[,NAME2,...] [options]\n"); |
| 59 | fprintf(ofp, "\n"); |
| 60 | fprintf(ofp, " -h, --help Show this help\n"); |
| 61 | fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n"); |
| 62 | fprintf(ofp, " -u, --userspace Apply for the user-space tracer\n"); |
| 63 | fprintf(ofp, " --all If -u, apply on all traceable apps\n"); |
| 64 | fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n"); |
| 65 | fprintf(ofp, "\n"); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * disable_channels |
| 70 | * |
| 71 | * Disabling channel using the lttng API. |
| 72 | */ |
| 73 | static int disable_channels(void) |
| 74 | { |
| 75 | int ret = CMD_SUCCESS; |
| 76 | char *channel_name; |
| 77 | |
| 78 | if (set_session_name() < 0) { |
| 79 | ret = CMD_ERROR; |
| 80 | goto error; |
| 81 | } |
| 82 | |
| 83 | /* Strip channel list */ |
| 84 | channel_name = strtok(opt_channels, ","); |
| 85 | while (channel_name != NULL) { |
| 86 | /* Kernel tracer action */ |
| 87 | if (opt_kernel) { |
| 88 | DBG("Disabling kernel channel %s", channel_name); |
| 89 | ret = lttng_kernel_disable_channel(channel_name); |
| 90 | if (ret < 0) { |
| 91 | goto error; |
| 92 | } else { |
| 93 | MSG("Kernel channel disabled %s", channel_name); |
| 94 | } |
| 95 | } else if (opt_userspace) { /* User-space tracer action */ |
| 96 | /* |
| 97 | * TODO: Waiting on lttng UST 2.0 |
| 98 | */ |
| 99 | if (opt_pid_all) { |
| 100 | } else if (opt_pid != 0) { |
| 101 | } |
| 102 | ret = CMD_NOT_IMPLEMENTED; |
| 103 | goto error; |
| 104 | } else { |
| 105 | ERR("Please specify a tracer (kernel or user-space)"); |
| 106 | goto error; |
| 107 | } |
| 108 | |
| 109 | /* Next channel */ |
| 110 | channel_name = strtok(NULL, ","); |
| 111 | } |
| 112 | |
| 113 | error: |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * cmd_disable_channels |
| 119 | * |
| 120 | * Disable channel to trace session |
| 121 | */ |
| 122 | int cmd_disable_channels(int argc, const char **argv) |
| 123 | { |
| 124 | int opt, ret; |
| 125 | static poptContext pc; |
| 126 | |
| 127 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 128 | poptReadDefaultConfig(pc, 0); |
| 129 | |
| 130 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 131 | switch (opt) { |
| 132 | case OPT_HELP: |
| 133 | usage(stderr); |
| 134 | ret = CMD_SUCCESS; |
| 135 | goto end; |
| 136 | case OPT_USERSPACE: |
| 137 | opt_userspace = 1; |
| 138 | break; |
| 139 | default: |
| 140 | usage(stderr); |
| 141 | ret = CMD_UNDEFINED; |
| 142 | goto end; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | opt_channels = (char*) poptGetArg(pc); |
| 147 | if (opt_channels == NULL) { |
| 148 | ERR("Missing channel name(s).\n"); |
| 149 | usage(stderr); |
| 150 | ret = CMD_SUCCESS; |
| 151 | goto end; |
| 152 | } |
| 153 | |
| 154 | ret = disable_channels(); |
| 155 | |
| 156 | end: |
| 157 | return ret; |
| 158 | } |