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