| 1 | #include <unistd.h> |
| 2 | #include <getopt.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdio.h> |
| 5 | #include <fcntl.h> |
| 6 | |
| 7 | #include "ustcomm.h" |
| 8 | |
| 9 | struct ust_opts { |
| 10 | char *cmd; |
| 11 | pid_t *pids; |
| 12 | int take_reply; |
| 13 | }; |
| 14 | |
| 15 | char *progname = NULL; |
| 16 | |
| 17 | void usage(void) |
| 18 | { |
| 19 | fprintf(stderr, "usage: %s [OPTIONS] COMMAND PID...\n", progname); |
| 20 | fprintf(stderr, "\nControl the tracing of a process that supports LTTng Userspace Tracing.\n\ |
| 21 | \n\ |
| 22 | Commands:\n\ |
| 23 | --start-trace\t\t\tStart tracing\n\ |
| 24 | --stop-trace\t\t\tStop tracing\n\ |
| 25 | --destroy-trace\t\t\tDestroy the trace\n\ |
| 26 | --enable-marker CHANNEL/MARKER\tEnable a marker\n\ |
| 27 | --disable-marker CHANNEL/MARKER\tDisable a marker\n\ |
| 28 | --list-markers\tList the markers of the process and their state\n\ |
| 29 | \n\ |
| 30 | "); |
| 31 | } |
| 32 | |
| 33 | int parse_opts_long(int argc, char **argv, struct ust_opts *opts) |
| 34 | { |
| 35 | int c; |
| 36 | int digit_optind = 0; |
| 37 | |
| 38 | opts->cmd = NULL; |
| 39 | opts->pids = NULL; |
| 40 | opts->take_reply = 0; |
| 41 | |
| 42 | while (1) { |
| 43 | int this_option_optind = optind ? optind : 1; |
| 44 | int option_index = 0; |
| 45 | static struct option long_options[] = { |
| 46 | {"start-trace", 0, 0, 1000}, |
| 47 | {"stop-trace", 0, 0, 1001}, |
| 48 | {"destroy-trace", 0, 0, 1002}, |
| 49 | {"list-markers", 0, 0, 1004}, |
| 50 | {"print-markers", 0, 0, 1005}, |
| 51 | {"pid", 1, 0, 1006}, |
| 52 | {"enable-marker", 1, 0, 1007}, |
| 53 | {"disable-marker", 1, 0, 1008}, |
| 54 | {"start", 0, 0, 1009}, |
| 55 | {"help", 0, 0, 'h'}, |
| 56 | {"version", 0, 0, 1010}, |
| 57 | {0, 0, 0, 0} |
| 58 | }; |
| 59 | |
| 60 | c = getopt_long(argc, argv, "h", long_options, &option_index); |
| 61 | if (c == -1) |
| 62 | break; |
| 63 | |
| 64 | switch (c) { |
| 65 | case 0: |
| 66 | printf("option %s", long_options[option_index].name); |
| 67 | if (optarg) |
| 68 | printf(" with arg %s", optarg); |
| 69 | printf("\n"); |
| 70 | break; |
| 71 | |
| 72 | case 1000: |
| 73 | opts->cmd = strdup("trace_start"); |
| 74 | break; |
| 75 | case 1001: |
| 76 | opts->cmd = strdup("trace_stop"); |
| 77 | break; |
| 78 | case 1009: |
| 79 | opts->cmd = strdup("start"); |
| 80 | break; |
| 81 | case 1002: |
| 82 | opts->cmd = strdup("trace_destroy"); |
| 83 | break; |
| 84 | case 1004: |
| 85 | opts->cmd = strdup("list_markers"); |
| 86 | opts->take_reply = 1; |
| 87 | break; |
| 88 | case 1007: |
| 89 | asprintf(&opts->cmd, "enable_marker %s", optarg); |
| 90 | break; |
| 91 | case 1008: |
| 92 | asprintf(&opts->cmd, "disable_marker %s", optarg); |
| 93 | break; |
| 94 | case 'h': |
| 95 | usage(); |
| 96 | exit(0); |
| 97 | case 1010: |
| 98 | printf("Version 0\n"); |
| 99 | |
| 100 | default: |
| 101 | /* unknown option or other error; error is printed by getopt, just return */ |
| 102 | return 1; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if(argc - optind > 0) { |
| 107 | int i; |
| 108 | int pididx=0; |
| 109 | opts->pids = malloc((argc-optind+1) * sizeof(pid_t)); |
| 110 | |
| 111 | for(i=optind; i<argc; i++) { |
| 112 | opts->pids[pididx++] = atoi(argv[i]); |
| 113 | } |
| 114 | opts->pids[pididx] = -1; |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | int main(int argc, char *argv[]) |
| 121 | { |
| 122 | pid_t *pidit; |
| 123 | //char *msg = argv[2]; |
| 124 | struct ustcomm_connection conn; |
| 125 | int result; |
| 126 | struct ust_opts opts; |
| 127 | |
| 128 | progname = argv[0]; |
| 129 | |
| 130 | if(argc <= 1) { |
| 131 | fprintf(stderr, "No operation specified.\n"); |
| 132 | usage(); |
| 133 | exit(EXIT_FAILURE); |
| 134 | } |
| 135 | |
| 136 | result = parse_opts_long(argc, argv, &opts); |
| 137 | if(result) { |
| 138 | usage(); |
| 139 | exit(EXIT_FAILURE); |
| 140 | } |
| 141 | |
| 142 | if(opts.pids == NULL) { |
| 143 | fprintf(stderr, "No pid specified.\n"); |
| 144 | usage(); |
| 145 | exit(EXIT_FAILURE); |
| 146 | } |
| 147 | if(opts.cmd == NULL) { |
| 148 | fprintf(stderr, "No command specified.\n"); |
| 149 | usage(); |
| 150 | exit(EXIT_FAILURE); |
| 151 | } |
| 152 | |
| 153 | pidit = opts.pids; |
| 154 | |
| 155 | while(*pidit != -1) { |
| 156 | char *reply; |
| 157 | char **preply; |
| 158 | |
| 159 | if(opts.take_reply) |
| 160 | preply = &reply; |
| 161 | else |
| 162 | preply = NULL; |
| 163 | |
| 164 | result = ustcomm_connect_app(*pidit, &conn); |
| 165 | if(result) { |
| 166 | fprintf(stderr, "error connecting to process\n"); |
| 167 | exit(EXIT_FAILURE); |
| 168 | } |
| 169 | ustcomm_send_request(&conn, opts.cmd, preply); |
| 170 | |
| 171 | if(opts.take_reply) |
| 172 | printf("%s", reply); |
| 173 | pidit++; |
| 174 | } |
| 175 | |
| 176 | free(opts.pids); |
| 177 | free(opts.cmd); |
| 178 | |
| 179 | return 0; |
| 180 | } |