Commit | Line | Data |
---|---|---|
0c95f5b2 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
0c95f5b2 | 7 | * |
d14d33bf AM |
8 | * This program 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 | |
11 | * GNU General Public License for more details. | |
0c95f5b2 | 12 | * |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
0c95f5b2 DG |
16 | */ |
17 | ||
18 | #define _GNU_SOURCE | |
19 | #include <popt.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <sys/stat.h> | |
24 | #include <sys/types.h> | |
25 | #include <unistd.h> | |
26 | ||
27 | #include "../command.h" | |
28 | #include <config.h> | |
29 | ||
30 | static char *opt_session_name; | |
31 | static char *opt_viewer; | |
f1f887c0 | 32 | static char *opt_trace_path; |
0c95f5b2 DG |
33 | static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN; |
34 | //static const char *lttv_gui_bin = CONFIG_LTTV_GUI_BIN; | |
35 | ||
36 | enum { | |
37 | OPT_HELP = 1, | |
38 | OPT_LIST_OPTIONS, | |
39 | }; | |
40 | ||
41 | static struct poptOption long_options[] = { | |
42 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
43 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
44 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, | |
45 | {"viewer", 'e', POPT_ARG_STRING, &opt_viewer, 0, 0, 0}, | |
f1f887c0 | 46 | {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0}, |
0c95f5b2 DG |
47 | {0, 0, 0, 0, 0, 0, 0} |
48 | }; | |
49 | ||
50 | /* | |
51 | * This is needed for each viewer since we are using execvp(). | |
52 | */ | |
36ef520f | 53 | static const char *babeltrace_opts[] = { "babeltrace" }; |
0c95f5b2 DG |
54 | //static const char *lttv_gui_opts[] = { "lttv-gui", "-t", }; |
55 | ||
56 | /* | |
57 | * Type is also use as the index in the viewers array. So please, make sure | |
58 | * your enum value is in the right order in the array below. | |
59 | */ | |
60 | enum viewer_type { | |
61 | VIEWER_BABELTRACE = 0, | |
62 | VIEWER_LTTV_GUI = 1, | |
63 | VIEWER_USER_DEFINED = 2, | |
64 | }; | |
65 | ||
66 | /* | |
67 | * NOTE: "lttv" is a shell command and it's not working for exec() family | |
68 | * functions so we might think of removing this wrapper or using bash. | |
69 | */ | |
70 | static struct viewers { | |
71 | const char *exec_name; | |
72 | enum viewer_type type; | |
73 | } viewers[] = { | |
74 | { "babeltrace", VIEWER_BABELTRACE }, | |
75 | { "lttv-gui", VIEWER_LTTV_GUI }, | |
76 | { NULL, VIEWER_USER_DEFINED }, | |
77 | }; | |
78 | ||
79 | /* | |
80 | * usage | |
81 | */ | |
82 | static void usage(FILE *ofp) | |
83 | { | |
84 | fprintf(ofp, "usage: lttng view [SESSION_NAME] [OPTIONS]\n"); | |
85 | fprintf(ofp, "\n"); | |
86 | fprintf(ofp, "By default, the babeltrace viewer will be used for text viewing\n"); | |
87 | fprintf(ofp, "\n"); | |
88 | fprintf(ofp, "Where SESSION_NAME is an optional session name. If not specified, lttng will\n"); | |
89 | fprintf(ofp, "get it from the configuration file (.lttngrc).\n"); | |
90 | fprintf(ofp, "\n"); | |
91 | fprintf(ofp, " -h, --help Show this help\n"); | |
92 | fprintf(ofp, " --list-options Simple listing of options\n"); | |
f1f887c0 | 93 | fprintf(ofp, " -t, --trace-path PATH Trace directory path for the viewer\n"); |
0c95f5b2 DG |
94 | fprintf(ofp, " -e, --viewer CMD Specify viewer and/or options to use\n"); |
95 | fprintf(ofp, " This will completely override the default viewers so\n"); | |
f1f887c0 DG |
96 | fprintf(ofp, " please make sure to specify the full command. The trace\n"); |
97 | fprintf(ofp, " directory path of the session will be appended at the end\n"); | |
98 | fprintf(ofp, " to the arguments\n"); | |
0c95f5b2 DG |
99 | fprintf(ofp, "\n"); |
100 | } | |
101 | ||
102 | static struct viewers *parse_options(void) | |
103 | { | |
104 | if (opt_viewer == NULL) { | |
105 | /* Default is babeltrace */ | |
106 | return &(viewers[VIEWER_BABELTRACE]); | |
107 | } | |
108 | ||
109 | #if 0 | |
110 | if (strstr(opt_viewer, viewers[VIEWER_LTTV_GUI].exec_name) == 0) { | |
111 | return &(viewers[VIEWER_LTTV_GUI]); | |
112 | } | |
113 | #endif | |
114 | ||
115 | /* | |
116 | * This means that if -e, --viewers is used, we just override everything | |
117 | * with it. For supported viewers like lttv, we could simply detect if "-t" | |
118 | * is passed and if not, add the trace directory to it. | |
119 | */ | |
120 | return &(viewers[VIEWER_USER_DEFINED]); | |
121 | } | |
122 | ||
123 | /* | |
124 | * Alloc an array of string pointer from a simple string having all options | |
125 | * seperated by spaces. Also adds the trace path to the arguments. | |
126 | * | |
127 | * The returning pointer is ready to be passed to execvp(). | |
128 | */ | |
129 | static char **alloc_argv_from_user_opts(char *opts, const char *trace_path) | |
130 | { | |
131 | int i = 0, ignore_space = 0; | |
132 | unsigned int num_opts = 1; | |
133 | char **argv, *token = opts; | |
134 | ||
135 | /* Count number of arguments. */ | |
136 | do { | |
137 | if (*token == ' ') { | |
138 | /* Use to ignore consecutive spaces */ | |
139 | if (!ignore_space) { | |
140 | num_opts++; | |
141 | } | |
142 | ignore_space = 1; | |
143 | } else { | |
144 | ignore_space = 0; | |
145 | } | |
146 | token++; | |
147 | } while (*token != '\0'); | |
148 | ||
149 | /* Add two here for the NULL terminating element and trace path */ | |
150 | argv = malloc(sizeof(char *) * (num_opts + 2)); | |
151 | if (argv == NULL) { | |
152 | goto error; | |
153 | } | |
154 | ||
155 | token = strtok(opts, " "); | |
156 | while (token != NULL) { | |
157 | argv[i] = strdup(token); | |
158 | token = strtok(NULL, " "); | |
159 | i++; | |
160 | } | |
161 | ||
162 | argv[num_opts] = (char *) trace_path; | |
163 | argv[num_opts + 1] = NULL; | |
164 | ||
165 | return argv; | |
166 | ||
167 | error: | |
168 | return NULL; | |
169 | } | |
170 | ||
171 | /* | |
172 | * Alloc an array of string pointer from an array of strings. It also adds | |
173 | * the trace path to the argv. | |
174 | * | |
175 | * The returning pointer is ready to be passed to execvp(). | |
176 | */ | |
177 | static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len, | |
178 | const char *trace_path) | |
179 | { | |
180 | char **argv; | |
181 | size_t size; | |
182 | ||
183 | size = sizeof(char *) * opts_len; | |
184 | ||
185 | /* Add two here for the trace_path and the NULL terminating element. */ | |
186 | argv = malloc(size + 2); | |
187 | if (argv == NULL) { | |
188 | goto error; | |
189 | } | |
190 | ||
191 | memcpy(argv, opts, size); | |
192 | ||
193 | argv[opts_len] = (char *)trace_path; | |
194 | argv[opts_len + 1] = NULL; | |
195 | ||
196 | error: | |
197 | return argv; | |
198 | } | |
199 | ||
200 | /* | |
201 | * Spawn viewer with the trace directory path. | |
202 | */ | |
203 | static int spawn_viewer(const char *trace_path) | |
204 | { | |
205 | int ret = 0; | |
0c95f5b2 DG |
206 | struct stat status; |
207 | const char *viewer_bin = NULL; | |
208 | struct viewers *viewer; | |
209 | char **argv = NULL; | |
210 | ||
211 | /* Check for --viewer options */ | |
212 | viewer = parse_options(); | |
213 | if (viewer == NULL) { | |
214 | ret = CMD_ERROR; | |
215 | goto error; | |
216 | } | |
217 | ||
85a68078 DG |
218 | switch (viewer->type) { |
219 | case VIEWER_BABELTRACE: | |
220 | if (stat(babeltrace_bin, &status) == 0) { | |
221 | viewer_bin = babeltrace_bin; | |
222 | } else { | |
223 | viewer_bin = viewer->exec_name; | |
224 | } | |
225 | argv = alloc_argv_from_local_opts(babeltrace_opts, | |
226 | ARRAY_SIZE(babeltrace_opts), trace_path); | |
227 | break; | |
0c95f5b2 | 228 | #if 0 |
85a68078 DG |
229 | case VIEWER_LTTV_GUI: |
230 | if (stat(lttv_gui_bin, &status) == 0) { | |
231 | viewer_bin = lttv_gui_bin; | |
232 | } else { | |
233 | viewer_bin = viewer->exec_name; | |
234 | } | |
235 | argv = alloc_argv_from_local_opts(lttv_gui_opts, | |
236 | ARRAY_SIZE(lttv_gui_opts), trace_path); | |
237 | break; | |
0c95f5b2 | 238 | #endif |
85a68078 DG |
239 | case VIEWER_USER_DEFINED: |
240 | argv = alloc_argv_from_user_opts(opt_viewer, trace_path); | |
241 | if (argv) { | |
242 | viewer_bin = argv[0]; | |
0c95f5b2 | 243 | } |
85a68078 DG |
244 | break; |
245 | default: | |
246 | viewer_bin = viewers[VIEWER_BABELTRACE].exec_name; | |
247 | argv = alloc_argv_from_local_opts(babeltrace_opts, | |
248 | ARRAY_SIZE(babeltrace_opts), trace_path); | |
249 | break; | |
250 | } | |
0c95f5b2 | 251 | |
85a68078 DG |
252 | if (argv == NULL) { |
253 | ret = CMD_FATAL; | |
254 | goto error; | |
255 | } | |
0c95f5b2 | 256 | |
85a68078 | 257 | DBG("Using %s viewer", viewer_bin); |
0c95f5b2 | 258 | |
85a68078 DG |
259 | ret = execvp(viewer_bin, argv); |
260 | if (ret) { | |
0c687325 JD |
261 | if (errno == ENOENT) { |
262 | ERR("%s not found on the system", viewer_bin); | |
263 | } else { | |
264 | PERROR("exec: %s", viewer_bin); | |
265 | } | |
85a68078 | 266 | free(argv); |
0c95f5b2 | 267 | ret = CMD_FATAL; |
85a68078 | 268 | goto error; |
0c95f5b2 DG |
269 | } |
270 | ||
271 | error: | |
272 | return ret; | |
273 | } | |
274 | ||
275 | /* | |
276 | * Exec viewer if found and use session name path. | |
277 | */ | |
278 | static int view_trace(void) | |
279 | { | |
280 | int ret, count, i, found = 0; | |
f1f887c0 | 281 | char *session_name, *trace_path; |
0c95f5b2 DG |
282 | struct lttng_session *sessions = NULL; |
283 | ||
284 | /* | |
285 | * Safety net. If lttng is suid at some point for *any* useless reasons, | |
f1f887c0 | 286 | * this prevent any bad execution of binaries. |
0c95f5b2 DG |
287 | */ |
288 | if (getuid() != 0) { | |
289 | if (getuid() != geteuid()) { | |
290 | ERR("UID does not match effective UID."); | |
6e9261f4 | 291 | ret = CMD_ERROR; |
0c95f5b2 DG |
292 | goto error; |
293 | } else if (getgid() != getegid()) { | |
294 | ERR("GID does not match effective GID."); | |
6e9261f4 | 295 | ret = CMD_ERROR; |
0c95f5b2 DG |
296 | goto error; |
297 | } | |
298 | } | |
299 | ||
f1f887c0 DG |
300 | /* User define trace path override the session name */ |
301 | if (opt_trace_path) { | |
302 | session_name = NULL; | |
303 | } else if(opt_session_name == NULL) { | |
0c95f5b2 DG |
304 | session_name = get_session_name(); |
305 | if (session_name == NULL) { | |
306 | ret = CMD_ERROR; | |
307 | goto error; | |
308 | } | |
309 | } else { | |
310 | session_name = opt_session_name; | |
311 | } | |
312 | ||
313 | DBG("Viewing trace for session %s", session_name); | |
314 | ||
f1f887c0 DG |
315 | if (session_name) { |
316 | /* Getting all sessions */ | |
317 | count = lttng_list_sessions(&sessions); | |
318 | if (count < 0) { | |
319 | ERR("Unable to list sessions. Session name %s not found.", | |
320 | session_name); | |
321 | MSG("Is there a session daemon running?"); | |
322 | ret = CMD_ERROR; | |
323 | goto free_error; | |
324 | } | |
0c95f5b2 | 325 | |
f1f887c0 DG |
326 | /* Find our session listed by the session daemon */ |
327 | for (i = 0; i < count; i++) { | |
328 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { | |
329 | found = 1; | |
330 | break; | |
331 | } | |
0c95f5b2 | 332 | } |
0c95f5b2 | 333 | |
f1f887c0 DG |
334 | if (!found) { |
335 | MSG("Session name %s not found", session_name); | |
336 | ret = CMD_ERROR; | |
337 | goto free_sessions; | |
338 | } | |
339 | ||
340 | trace_path = sessions[i].path; | |
341 | } else { | |
342 | trace_path = opt_trace_path; | |
0c95f5b2 DG |
343 | } |
344 | ||
f1f887c0 | 345 | MSG("Trace directory: %s\n", trace_path); |
0c95f5b2 | 346 | |
f1f887c0 | 347 | ret = spawn_viewer(trace_path); |
0c95f5b2 DG |
348 | if (ret < 0) { |
349 | /* Don't set ret so lttng can interpret the sessiond error. */ | |
350 | goto free_sessions; | |
351 | } | |
352 | ||
0c95f5b2 DG |
353 | free_sessions: |
354 | if (sessions) { | |
355 | free(sessions); | |
356 | } | |
357 | free_error: | |
358 | if (opt_session_name == NULL) { | |
359 | free(session_name); | |
360 | } | |
361 | error: | |
362 | return ret; | |
363 | } | |
364 | ||
365 | /* | |
366 | * The 'view <options>' first level command | |
367 | */ | |
368 | int cmd_view(int argc, const char **argv) | |
369 | { | |
370 | int opt, ret = CMD_SUCCESS; | |
371 | static poptContext pc; | |
372 | ||
373 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
374 | poptReadDefaultConfig(pc, 0); | |
375 | ||
376 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
377 | switch (opt) { | |
378 | case OPT_HELP: | |
379 | usage(stdout); | |
380 | goto end; | |
381 | case OPT_LIST_OPTIONS: | |
382 | list_cmd_options(stdout, long_options); | |
383 | goto end; | |
384 | default: | |
385 | usage(stderr); | |
386 | ret = CMD_UNDEFINED; | |
387 | goto end; | |
388 | } | |
389 | } | |
390 | ||
391 | opt_session_name = (char*) poptGetArg(pc); | |
392 | ||
393 | ret = view_trace(); | |
394 | ||
395 | end: | |
396 | poptFreeContext(pc); | |
397 | return ret; | |
398 | } |