| 1 | /* Copyright (C) 2009 Pierre-Marc Fournier |
| 2 | * |
| 3 | * This library is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of the GNU Lesser General Public |
| 5 | * License as published by the Free Software Foundation; either |
| 6 | * version 2.1 of the License, or (at your option) any later version. |
| 7 | * |
| 8 | * This library 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 GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public |
| 14 | * License along with this library; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <stdio.h> |
| 20 | #include <unistd.h> |
| 21 | #include <getopt.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <fcntl.h> |
| 24 | |
| 25 | #include "ustcomm.h" |
| 26 | #include "ustcmd.h" |
| 27 | #include "usterr.h" |
| 28 | |
| 29 | enum command { |
| 30 | CREATE_TRACE=1000, |
| 31 | ALLOC_TRACE, |
| 32 | START_TRACE, |
| 33 | STOP_TRACE, |
| 34 | DESTROY_TRACE, |
| 35 | LIST_MARKERS, |
| 36 | ENABLE_MARKER, |
| 37 | DISABLE_MARKER, |
| 38 | GET_ONLINE_PIDS, |
| 39 | SET_SUBBUF_SIZE, |
| 40 | SET_SUBBUF_NUM, |
| 41 | GET_SUBBUF_SIZE, |
| 42 | GET_SUBBUF_NUM, |
| 43 | GET_SOCK_PATH, |
| 44 | SET_SOCK_PATH, |
| 45 | FORCE_SWITCH, |
| 46 | UNKNOWN |
| 47 | }; |
| 48 | |
| 49 | struct ust_opts { |
| 50 | enum command cmd; |
| 51 | pid_t *pids; |
| 52 | char *regex; |
| 53 | }; |
| 54 | |
| 55 | char *progname = NULL; |
| 56 | |
| 57 | void usage(void) |
| 58 | { |
| 59 | fprintf(stderr, "usage: %s COMMAND PIDs...\n", progname); |
| 60 | fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\ |
| 61 | \n\ |
| 62 | Commands:\n\ |
| 63 | --create-trace\t\t\tCreate trace\n\ |
| 64 | --alloc-trace\t\t\tAlloc trace\n\ |
| 65 | --start-trace\t\t\tStart tracing\n\ |
| 66 | --stop-trace\t\t\tStop tracing\n\ |
| 67 | --destroy-trace\t\t\tDestroy the trace\n\ |
| 68 | --set-subbuf-size \"CHANNEL/bytes\"\tSet the size of subbuffers per channel\n\ |
| 69 | --set-subbuf-num \"CHANNEL/n\"\tSet the number of subbuffers per channel\n\ |
| 70 | --set-sock-path\t\t\tSet the path of the daemon socket\n\ |
| 71 | --get-subbuf-size \"CHANNEL\"\t\tGet the size of subbuffers per channel\n\ |
| 72 | --get-subbuf-num \"CHANNEL\"\t\tGet the number of subbuffers per channel\n\ |
| 73 | --get-sock-path\t\t\tGet the path of the daemon socket\n\ |
| 74 | --enable-marker \"CHANNEL/MARKER\"\tEnable a marker\n\ |
| 75 | --disable-marker \"CHANNEL/MARKER\"\tDisable a marker\n\ |
| 76 | --list-markers\t\t\tList the markers of the process, their\n\t\t\t\t\t state and format string\n\ |
| 77 | --force-switch\t\t\tForce a subbuffer switch\n\ |
| 78 | \ |
| 79 | "); |
| 80 | } |
| 81 | |
| 82 | int parse_opts_long(int argc, char **argv, struct ust_opts *opts) |
| 83 | { |
| 84 | int c; |
| 85 | |
| 86 | opts->pids = NULL; |
| 87 | opts->regex = NULL; |
| 88 | |
| 89 | while (1) { |
| 90 | int option_index = 0; |
| 91 | static struct option long_options[] = { |
| 92 | { "create-trace", 0, 0, CREATE_TRACE }, |
| 93 | { "alloc-trace", 0, 0, ALLOC_TRACE }, |
| 94 | { "start-trace", 0, 0, START_TRACE }, |
| 95 | { "stop-trace", 0, 0, STOP_TRACE }, |
| 96 | { "destroy-trace", 0, 0, DESTROY_TRACE }, |
| 97 | { "list-markers", 0, 0, LIST_MARKERS }, |
| 98 | { "enable-marker", 1, 0, ENABLE_MARKER }, |
| 99 | { "disable-marker", 1, 0, DISABLE_MARKER }, |
| 100 | { "help", 0, 0, 'h' }, |
| 101 | { "online-pids", 0, 0, GET_ONLINE_PIDS }, |
| 102 | { "set-subbuf-size", 1, 0, SET_SUBBUF_SIZE }, |
| 103 | { "set-subbuf-num", 1, 0, SET_SUBBUF_NUM }, |
| 104 | { "get-subbuf-size", 1, 0, GET_SUBBUF_SIZE }, |
| 105 | { "get-subbuf-num", 1, 0, GET_SUBBUF_NUM }, |
| 106 | { "get-sock-path", 0, 0, GET_SOCK_PATH }, |
| 107 | { "set-sock-path", 1, 0, SET_SOCK_PATH }, |
| 108 | { "force-switch", 0, 0, FORCE_SWITCH }, |
| 109 | { 0, 0, 0, 0 } |
| 110 | }; |
| 111 | |
| 112 | c = getopt_long(argc, argv, "h", long_options, &option_index); |
| 113 | if (c == -1) |
| 114 | break; |
| 115 | |
| 116 | if(c >= 1000) |
| 117 | opts->cmd = c; |
| 118 | |
| 119 | switch (c) { |
| 120 | case 0: |
| 121 | printf("option %s", long_options[option_index].name); |
| 122 | if (optarg) |
| 123 | printf(" with arg %s", optarg); |
| 124 | printf("\n"); |
| 125 | break; |
| 126 | |
| 127 | case ENABLE_MARKER: |
| 128 | case DISABLE_MARKER: |
| 129 | case SET_SUBBUF_SIZE: |
| 130 | case SET_SUBBUF_NUM: |
| 131 | case GET_SUBBUF_SIZE: |
| 132 | case GET_SUBBUF_NUM: |
| 133 | case SET_SOCK_PATH: |
| 134 | opts->regex = strdup(optarg); |
| 135 | break; |
| 136 | |
| 137 | case 'h': |
| 138 | usage(); |
| 139 | exit(0); |
| 140 | |
| 141 | case '?': |
| 142 | fprintf(stderr, "Invalid argument\n\n"); |
| 143 | usage(); |
| 144 | exit(1); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (argc - optind > 0 && opts->cmd != GET_ONLINE_PIDS) { |
| 149 | int i; |
| 150 | int pididx=0; |
| 151 | opts->pids = malloc((argc-optind+1) * sizeof(pid_t)); |
| 152 | |
| 153 | for(i=optind; i<argc; i++) { |
| 154 | /* don't take any chances, use a long long */ |
| 155 | long long tmp; |
| 156 | char *endptr; |
| 157 | tmp = strtoull(argv[i], &endptr, 10); |
| 158 | if(*endptr != '\0') { |
| 159 | ERR("The pid \"%s\" is invalid.", argv[i]); |
| 160 | return 1; |
| 161 | } |
| 162 | opts->pids[pididx++] = (pid_t) tmp; |
| 163 | } |
| 164 | opts->pids[pididx] = -1; |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | int main(int argc, char *argv[]) |
| 171 | { |
| 172 | pid_t *pidit; |
| 173 | int result; |
| 174 | char *tmp; |
| 175 | struct ust_opts opts; |
| 176 | |
| 177 | progname = argv[0]; |
| 178 | |
| 179 | if(argc <= 1) { |
| 180 | fprintf(stderr, "No operation specified.\n"); |
| 181 | usage(); |
| 182 | exit(EXIT_FAILURE); |
| 183 | } |
| 184 | |
| 185 | result = parse_opts_long(argc, argv, &opts); |
| 186 | if(result) { |
| 187 | fprintf(stderr, "\n"); |
| 188 | usage(); |
| 189 | exit(EXIT_FAILURE); |
| 190 | } |
| 191 | |
| 192 | if(opts.pids == NULL && opts.cmd != GET_ONLINE_PIDS) { |
| 193 | fprintf(stderr, "No pid specified.\n"); |
| 194 | usage(); |
| 195 | exit(EXIT_FAILURE); |
| 196 | } |
| 197 | if(opts.cmd == UNKNOWN) { |
| 198 | fprintf(stderr, "No command specified.\n"); |
| 199 | usage(); |
| 200 | exit(EXIT_FAILURE); |
| 201 | } |
| 202 | if (opts.cmd == GET_ONLINE_PIDS) { |
| 203 | pid_t *pp = ustcmd_get_online_pids(); |
| 204 | unsigned int i = 0; |
| 205 | |
| 206 | if (pp) { |
| 207 | while (pp[i] != 0) { |
| 208 | printf("%u\n", (unsigned int) pp[i]); |
| 209 | ++i; |
| 210 | } |
| 211 | free(pp); |
| 212 | } |
| 213 | |
| 214 | exit(EXIT_SUCCESS); |
| 215 | } |
| 216 | |
| 217 | pidit = opts.pids; |
| 218 | struct marker_status *cmsf = NULL; |
| 219 | |
| 220 | while(*pidit != -1) { |
| 221 | switch (opts.cmd) { |
| 222 | case CREATE_TRACE: |
| 223 | result = ustcmd_create_trace(*pidit); |
| 224 | if (result) { |
| 225 | ERR("error while trying to create trace with PID %u\n", (unsigned int) *pidit); |
| 226 | break; |
| 227 | } |
| 228 | break; |
| 229 | |
| 230 | case START_TRACE: |
| 231 | result = ustcmd_start_trace(*pidit); |
| 232 | if (result) { |
| 233 | ERR("error while trying to for trace with PID %u\n", (unsigned int) *pidit); |
| 234 | break; |
| 235 | } |
| 236 | break; |
| 237 | |
| 238 | case STOP_TRACE: |
| 239 | result = ustcmd_stop_trace(*pidit); |
| 240 | if (result) { |
| 241 | ERR("error while trying to stop trace for PID %u\n", (unsigned int) *pidit); |
| 242 | break; |
| 243 | } |
| 244 | break; |
| 245 | |
| 246 | case DESTROY_TRACE: |
| 247 | result = ustcmd_destroy_trace(*pidit); |
| 248 | if (result) { |
| 249 | ERR("error while trying to destroy trace with PID %u\n", (unsigned int) *pidit); |
| 250 | break; |
| 251 | } |
| 252 | break; |
| 253 | |
| 254 | case LIST_MARKERS: |
| 255 | cmsf = NULL; |
| 256 | if (ustcmd_get_cmsf(&cmsf, *pidit)) { |
| 257 | fprintf(stderr, |
| 258 | "error while trying to list markers for" |
| 259 | " PID %u\n", (unsigned int) *pidit); |
| 260 | break; |
| 261 | } |
| 262 | unsigned int i = 0; |
| 263 | while (cmsf[i].channel != NULL) { |
| 264 | printf("{PID: %u, channel/marker: %s/%s, " |
| 265 | "state: %u, fmt: %s}\n", |
| 266 | (unsigned int) *pidit, |
| 267 | cmsf[i].channel, |
| 268 | cmsf[i].marker, |
| 269 | cmsf[i].state, |
| 270 | cmsf[i].fs); |
| 271 | ++i; |
| 272 | } |
| 273 | ustcmd_free_cmsf(cmsf); |
| 274 | break; |
| 275 | |
| 276 | case ENABLE_MARKER: |
| 277 | if(opts.regex) |
| 278 | ustcmd_set_marker_state(opts.regex, 1, *pidit); |
| 279 | break; |
| 280 | case DISABLE_MARKER: |
| 281 | if(opts.regex) |
| 282 | ustcmd_set_marker_state(opts.regex, 0, *pidit); |
| 283 | break; |
| 284 | |
| 285 | case SET_SUBBUF_SIZE: |
| 286 | ustcmd_set_subbuf_size(opts.regex, *pidit); |
| 287 | break; |
| 288 | |
| 289 | case SET_SUBBUF_NUM: |
| 290 | ustcmd_set_subbuf_num(opts.regex, *pidit); |
| 291 | break; |
| 292 | |
| 293 | case GET_SUBBUF_SIZE: |
| 294 | result = ustcmd_get_subbuf_size(opts.regex, *pidit); |
| 295 | if (result == -1) { |
| 296 | ERR("error while trying to get_subuf_size with PID %u\n", (unsigned int) *pidit); |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | printf("the size of subbufers is %d\n", result); |
| 301 | break; |
| 302 | |
| 303 | case GET_SUBBUF_NUM: |
| 304 | result = ustcmd_get_subbuf_num(opts.regex, *pidit); |
| 305 | if (result == -1) { |
| 306 | ERR("error while trying to get_subuf_num with PID %u\n", (unsigned int) *pidit); |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | printf("the number of subbufers is %d\n", result); |
| 311 | break; |
| 312 | |
| 313 | case ALLOC_TRACE: |
| 314 | result = ustcmd_alloc_trace(*pidit); |
| 315 | if (result) { |
| 316 | ERR("error while trying to alloc trace with PID %u\n", (unsigned int) *pidit); |
| 317 | break; |
| 318 | } |
| 319 | break; |
| 320 | |
| 321 | case GET_SOCK_PATH: |
| 322 | result = ustcmd_get_sock_path(&tmp, *pidit); |
| 323 | if (result) { |
| 324 | ERR("error while trying to get sock path for PID %u\n", (unsigned int) *pidit); |
| 325 | break; |
| 326 | } |
| 327 | printf("the socket path is %s\n", tmp); |
| 328 | free(tmp); |
| 329 | break; |
| 330 | |
| 331 | case SET_SOCK_PATH: |
| 332 | result = ustcmd_set_sock_path(opts.regex, *pidit); |
| 333 | if (result) { |
| 334 | ERR("error while trying to set sock path for PID %u\n", (unsigned int) *pidit); |
| 335 | } |
| 336 | break; |
| 337 | |
| 338 | case FORCE_SWITCH: |
| 339 | result = ustcmd_force_switch(*pidit); |
| 340 | if (result) { |
| 341 | ERR("error while trying to force switch for PID %u\n", (unsigned int) *pidit); |
| 342 | } |
| 343 | break; |
| 344 | |
| 345 | default: |
| 346 | ERR("unknown command\n"); |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | pidit++; |
| 351 | } |
| 352 | |
| 353 | if (opts.pids != NULL) { |
| 354 | free(opts.pids); |
| 355 | } |
| 356 | if (opts.regex != NULL) { |
| 357 | free(opts.regex); |
| 358 | } |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |