| 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 int opt_event_type; |
| 34 | static char *opt_kernel; |
| 35 | static char *opt_cmd_name; |
| 36 | static int opt_pid_all; |
| 37 | static int opt_userspace; |
| 38 | static int opt_enable_all; |
| 39 | static pid_t opt_pid; |
| 40 | static char *opt_kprobe_addr; |
| 41 | static char *opt_function_symbol; |
| 42 | static char *opt_channel_name; |
| 43 | |
| 44 | enum { |
| 45 | OPT_HELP = 1, |
| 46 | OPT_USERSPACE, |
| 47 | OPT_TRACEPOINT, |
| 48 | OPT_MARKER, |
| 49 | OPT_KPROBE, |
| 50 | OPT_FUNCTION, |
| 51 | }; |
| 52 | |
| 53 | static struct poptOption long_options[] = { |
| 54 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 55 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 56 | {"all-events", 'a', POPT_ARG_VAL, &opt_enable_all, 1, 0, 0}, |
| 57 | {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0}, |
| 58 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, |
| 59 | {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, 0, OPT_USERSPACE, 0, 0}, |
| 60 | {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0}, |
| 61 | {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, |
| 62 | {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TRACEPOINT, 0, 0}, |
| 63 | {"marker", 0, POPT_ARG_NONE, 0, OPT_MARKER, 0, 0}, |
| 64 | {"kprobe", 0, POPT_ARG_STRING, 0, OPT_KPROBE, 0, 0}, |
| 65 | {"function", 0, POPT_ARG_STRING, 0, OPT_FUNCTION, 0, 0}, |
| 66 | {0, 0, 0, 0, 0, 0, 0} |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | * usage |
| 71 | */ |
| 72 | static void usage(FILE *ofp) |
| 73 | { |
| 74 | fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [options] [event_options]\n"); |
| 75 | fprintf(ofp, "\n"); |
| 76 | fprintf(ofp, " -h, --help Show this help\n"); |
| 77 | fprintf(ofp, " -c, --channel Apply on this channel\n"); |
| 78 | fprintf(ofp, " -a, --all-events Enable all tracepoints\n"); |
| 79 | fprintf(ofp, " -k, --kernel Apply for the kernel tracer\n"); |
| 80 | fprintf(ofp, " -u, --userspace [CMD] Apply for the user-space tracer\n"); |
| 81 | fprintf(ofp, " --all If -u, apply on all traceable apps\n"); |
| 82 | fprintf(ofp, " -p, --pid PID If -u, apply on a specific PID\n"); |
| 83 | fprintf(ofp, "\n"); |
| 84 | fprintf(ofp, "Event options:\n"); |
| 85 | fprintf(ofp, " --tracepoint Tracepoint event (default)\n"); |
| 86 | fprintf(ofp, " --kprobe ADDR Kernel Kprobe\n"); |
| 87 | fprintf(ofp, " --function SYMBOL Function tracer event\n"); |
| 88 | fprintf(ofp, " --marker User-space marker (deprecated)\n"); |
| 89 | fprintf(ofp, "\n"); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * enable_events |
| 94 | * |
| 95 | * Enabling event using the lttng API. |
| 96 | */ |
| 97 | static int enable_events(void) |
| 98 | { |
| 99 | int err, ret = CMD_SUCCESS; |
| 100 | char *event_name, *channel_name; |
| 101 | struct lttng_event ev; |
| 102 | |
| 103 | if (set_session_name() < 0) { |
| 104 | ret = CMD_ERROR; |
| 105 | goto error; |
| 106 | } |
| 107 | |
| 108 | if (opt_channel_name == NULL) { |
| 109 | err = asprintf(&channel_name, DEFAULT_CHANNEL_NAME); |
| 110 | if (err < 0) { |
| 111 | ret = CMD_FATAL; |
| 112 | goto error; |
| 113 | } |
| 114 | } else { |
| 115 | channel_name = opt_channel_name; |
| 116 | } |
| 117 | |
| 118 | if (opt_enable_all) { |
| 119 | if (opt_kernel) { |
| 120 | ret = lttng_kernel_enable_event(NULL, channel_name); |
| 121 | goto error; |
| 122 | } |
| 123 | |
| 124 | /* TODO: User-space tracer */ |
| 125 | } |
| 126 | |
| 127 | /* Strip event list */ |
| 128 | event_name = strtok(opt_event_list, ","); |
| 129 | while (event_name != NULL) { |
| 130 | /* Kernel tracer action */ |
| 131 | if (opt_kernel) { |
| 132 | DBG("Enabling kernel event %s for channel %s", |
| 133 | event_name, channel_name); |
| 134 | /* Copy name and type of the event */ |
| 135 | strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN); |
| 136 | ev.type = opt_event_type; |
| 137 | |
| 138 | switch (opt_event_type) { |
| 139 | case LTTNG_EVENT_TRACEPOINTS: |
| 140 | ret = lttng_kernel_enable_event(&ev, channel_name); |
| 141 | break; |
| 142 | case LTTNG_EVENT_KPROBES: |
| 143 | /* FIXME: check addr format */ |
| 144 | ev.attr.kprobe.addr = atoll(opt_kprobe_addr); |
| 145 | ret = lttng_kernel_enable_event(&ev, channel_name); |
| 146 | break; |
| 147 | case LTTNG_EVENT_FUNCTION: |
| 148 | strncpy(ev.attr.ftrace.symbol_name, opt_function_symbol, LTTNG_SYMBOL_NAME_LEN); |
| 149 | ret = lttng_kernel_enable_event(&ev, channel_name); |
| 150 | break; |
| 151 | default: |
| 152 | ret = CMD_NOT_IMPLEMENTED; |
| 153 | goto error; |
| 154 | } |
| 155 | |
| 156 | if (ret > 0) { |
| 157 | MSG("Kernel event %s created in channel %s", event_name, channel_name); |
| 158 | } |
| 159 | } else if (opt_userspace) { /* User-space tracer action */ |
| 160 | /* |
| 161 | * TODO: Waiting on lttng UST 2.0 |
| 162 | */ |
| 163 | if (opt_pid_all) { |
| 164 | } else if (opt_pid != 0) { |
| 165 | } |
| 166 | ret = CMD_NOT_IMPLEMENTED; |
| 167 | goto error; |
| 168 | } else { |
| 169 | ERR("Please specify a tracer (kernel or user-space)"); |
| 170 | goto error; |
| 171 | } |
| 172 | |
| 173 | /* Next event */ |
| 174 | event_name = strtok(NULL, ","); |
| 175 | } |
| 176 | |
| 177 | error: |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * cmd_enable_events |
| 183 | * |
| 184 | * Add event to trace session |
| 185 | */ |
| 186 | int cmd_enable_events(int argc, const char **argv) |
| 187 | { |
| 188 | int opt, ret; |
| 189 | static poptContext pc; |
| 190 | |
| 191 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 192 | poptReadDefaultConfig(pc, 0); |
| 193 | |
| 194 | /* Default event type */ |
| 195 | opt_event_type = LTTNG_KERNEL_TRACEPOINTS; |
| 196 | |
| 197 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 198 | switch (opt) { |
| 199 | case OPT_HELP: |
| 200 | usage(stderr); |
| 201 | ret = CMD_SUCCESS; |
| 202 | goto end; |
| 203 | case OPT_USERSPACE: |
| 204 | opt_userspace = 1; |
| 205 | opt_cmd_name = poptGetOptArg(pc); |
| 206 | break; |
| 207 | case OPT_TRACEPOINT: |
| 208 | opt_event_type = LTTNG_EVENT_TRACEPOINTS; |
| 209 | break; |
| 210 | case OPT_MARKER: |
| 211 | ret = CMD_NOT_IMPLEMENTED; |
| 212 | goto end; |
| 213 | case OPT_KPROBE: |
| 214 | opt_event_type = LTTNG_EVENT_KPROBES; |
| 215 | opt_kprobe_addr = poptGetOptArg(pc); |
| 216 | break; |
| 217 | case OPT_FUNCTION: |
| 218 | opt_event_type = LTTNG_EVENT_FUNCTION; |
| 219 | opt_function_symbol = poptGetOptArg(pc); |
| 220 | break; |
| 221 | default: |
| 222 | usage(stderr); |
| 223 | ret = CMD_UNDEFINED; |
| 224 | goto end; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | opt_event_list = (char*) poptGetArg(pc); |
| 229 | if (opt_event_list == NULL && opt_enable_all == 0) { |
| 230 | ERR("Missing event name(s).\n"); |
| 231 | usage(stderr); |
| 232 | ret = CMD_SUCCESS; |
| 233 | goto end; |
| 234 | } |
| 235 | |
| 236 | ret = enable_events(); |
| 237 | |
| 238 | end: |
| 239 | return ret; |
| 240 | } |