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