a13cb352510da0e3f2b60cc9a8c74f2b62f66870
[lttng-tools.git] / src / bin / lttng / commands / view.c
1 /*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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.
12 *
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.
16 */
17
18 #define _LGPL_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
29 static char *opt_session_name;
30 static char *opt_viewer;
31 static char *opt_trace_path;
32 static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN;
33
34 #ifdef LTTNG_EMBED_HELP
35 static const char help_msg[] =
36 #include <lttng-view.1.h>
37 ;
38 #endif
39
40 enum {
41 OPT_HELP = 1,
42 OPT_LIST_OPTIONS,
43 };
44
45 static struct poptOption long_options[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
48 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
49 {"viewer", 'e', POPT_ARG_STRING, &opt_viewer, 0, 0, 0},
50 {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0},
51 {0, 0, 0, 0, 0, 0, 0}
52 };
53
54 /*
55 * This is needed for each viewer since we are using execvp().
56 */
57 static const char *babeltrace_opts[] = { "babeltrace" };
58
59 /*
60 * Type is also use as the index in the viewers array. So please, make sure
61 * your enum value is in the right order in the array below.
62 */
63 enum viewer_type {
64 VIEWER_BABELTRACE = 0,
65 VIEWER_USER_DEFINED = 1,
66 };
67
68 /*
69 * NOTE: "lttv" is a shell command and it's not working for exec() family
70 * functions so we might think of removing this wrapper or using bash.
71 */
72 static const struct viewers {
73 const char *exec_name;
74 enum viewer_type type;
75 } viewers[] = {
76 { "babeltrace", VIEWER_BABELTRACE },
77 { NULL, VIEWER_USER_DEFINED },
78 };
79
80 /* Is the session we are trying to view is in live mode. */
81 static int session_live_mode;
82
83 static const struct viewers *parse_options(void)
84 {
85 if (opt_viewer == NULL) {
86 /* Default is babeltrace */
87 return &(viewers[VIEWER_BABELTRACE]);
88 }
89
90 /*
91 * This means that if -e, --viewers is used, we just override everything
92 * with it. For supported viewers like lttv, we could simply detect if "-t"
93 * is passed and if not, add the trace directory to it.
94 */
95 return &(viewers[VIEWER_USER_DEFINED]);
96 }
97
98 /*
99 * Alloc an array of string pointer from a simple string having all options
100 * seperated by spaces. Also adds the trace path to the arguments.
101 *
102 * The returning pointer is ready to be passed to execvp().
103 */
104 static char **alloc_argv_from_user_opts(char *opts, const char *trace_path)
105 {
106 int i = 0, ignore_space = 0;
107 unsigned int num_opts = 1;
108 char **argv, *token = opts;
109
110 /* Count number of arguments. */
111 do {
112 if (*token == ' ') {
113 /* Use to ignore consecutive spaces */
114 if (!ignore_space) {
115 num_opts++;
116 }
117 ignore_space = 1;
118 } else {
119 ignore_space = 0;
120 }
121 token++;
122 } while (*token != '\0');
123
124 /* Add two here for the NULL terminating element and trace path */
125 argv = zmalloc(sizeof(char *) * (num_opts + 2));
126 if (argv == NULL) {
127 goto error;
128 }
129
130 token = strtok(opts, " ");
131 while (token != NULL) {
132 argv[i] = strdup(token);
133 if (argv[i] == NULL) {
134 goto error;
135 }
136 token = strtok(NULL, " ");
137 i++;
138 }
139
140 argv[num_opts] = (char *) trace_path;
141 argv[num_opts + 1] = NULL;
142
143 return argv;
144
145 error:
146 if (argv) {
147 for (i = 0; i < num_opts + 2; i++) {
148 free(argv[i]);
149 }
150 free(argv);
151 }
152
153 return NULL;
154 }
155
156 /*
157 * Alloc an array of string pointer from an array of strings. It also adds
158 * the trace path to the argv.
159 *
160 * The returning pointer is ready to be passed to execvp().
161 */
162 static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
163 const char *trace_path)
164 {
165 char **argv;
166 size_t size, mem_len;
167
168 /* Add one for the NULL terminating element. */
169 mem_len = opts_len + 1;
170 if (session_live_mode) {
171 /* Add 3 option for the live mode being "-i lttng-live URL". */
172 mem_len += 3;
173 } else {
174 /* Add option for the trace path. */
175 mem_len += 1;
176 }
177
178 size = sizeof(char *) * mem_len;
179
180 /* Add two here for the trace_path and the NULL terminating element. */
181 argv = zmalloc(size);
182 if (argv == NULL) {
183 goto error;
184 }
185
186 memcpy(argv, opts, sizeof(char *) * opts_len);
187
188 if (session_live_mode) {
189 argv[opts_len] = "-i";
190 argv[opts_len + 1] = "lttng-live";
191 argv[opts_len + 2] = (char *) trace_path;
192 argv[opts_len + 3] = NULL;
193 } else {
194 argv[opts_len] = (char *) trace_path;
195 argv[opts_len + 1] = NULL;
196 }
197
198 error:
199 return argv;
200 }
201
202 /*
203 * Spawn viewer with the trace directory path.
204 */
205 static int spawn_viewer(const char *trace_path)
206 {
207 int ret = 0;
208 struct stat status;
209 const char *viewer_bin = NULL;
210 const struct viewers *viewer;
211 char **argv = NULL;
212
213 /* Check for --viewer options */
214 viewer = parse_options();
215 if (viewer == NULL) {
216 ret = CMD_ERROR;
217 goto error;
218 }
219
220 switch (viewer->type) {
221 case VIEWER_BABELTRACE:
222 if (stat(babeltrace_bin, &status) == 0) {
223 viewer_bin = babeltrace_bin;
224 } else {
225 viewer_bin = viewer->exec_name;
226 }
227 argv = alloc_argv_from_local_opts(babeltrace_opts,
228 ARRAY_SIZE(babeltrace_opts), trace_path);
229 break;
230 case VIEWER_USER_DEFINED:
231 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
232 if (argv) {
233 viewer_bin = argv[0];
234 }
235 break;
236 default:
237 viewer_bin = viewers[VIEWER_BABELTRACE].exec_name;
238 argv = alloc_argv_from_local_opts(babeltrace_opts,
239 ARRAY_SIZE(babeltrace_opts), trace_path);
240 break;
241 }
242
243 if (argv == NULL || !viewer_bin) {
244 ret = CMD_FATAL;
245 goto error;
246 }
247
248 DBG("Using %s viewer", viewer_bin);
249
250 ret = execvp(viewer_bin, argv);
251 if (ret) {
252 if (errno == ENOENT) {
253 ERR("%s not found on the system", viewer_bin);
254 } else {
255 PERROR("exec: %s", viewer_bin);
256 }
257 ret = CMD_FATAL;
258 goto error;
259 }
260
261 error:
262 free(argv);
263 return ret;
264 }
265
266 /*
267 * Build the live path we need for the lttng live view.
268 */
269 static char *build_live_path(char *session_name)
270 {
271 int ret;
272 char *path = NULL;
273 char hostname[HOST_NAME_MAX];
274
275 ret = gethostname(hostname, sizeof(hostname));
276 if (ret < 0) {
277 PERROR("gethostname");
278 goto error;
279 }
280
281 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
282 session_name);
283 if (ret < 0) {
284 PERROR("asprintf live path");
285 goto error;
286 }
287
288 error:
289 return path;
290 }
291
292 /*
293 * Exec viewer if found and use session name path.
294 */
295 static int view_trace(void)
296 {
297 int ret;
298 char *session_name, *trace_path = NULL;
299 struct lttng_session *sessions = NULL;
300 bool free_trace_path = false;
301
302 /*
303 * Safety net. If lttng is suid at some point for *any* useless reasons,
304 * this prevent any bad execution of binaries.
305 */
306 if (getuid() != 0) {
307 if (getuid() != geteuid()) {
308 ERR("UID does not match effective UID.");
309 ret = CMD_ERROR;
310 goto error;
311 } else if (getgid() != getegid()) {
312 ERR("GID does not match effective GID.");
313 ret = CMD_ERROR;
314 goto error;
315 }
316 }
317
318 /* User define trace path override the session name */
319 if (opt_trace_path) {
320 session_name = NULL;
321 } else if(opt_session_name == NULL) {
322 session_name = get_session_name();
323 if (session_name == NULL) {
324 ret = CMD_ERROR;
325 goto error;
326 }
327 } else {
328 session_name = opt_session_name;
329 }
330
331 DBG("Viewing trace for session %s", session_name);
332
333 if (session_name) {
334 int i, count, found = 0;
335
336 /* Getting all sessions */
337 count = lttng_list_sessions(&sessions);
338 if (count < 0) {
339 ERR("Unable to list sessions. Session name %s not found.",
340 session_name);
341 MSG("Is there a session daemon running?");
342 ret = CMD_ERROR;
343 goto free_error;
344 }
345
346 /* Find our session listed by the session daemon */
347 for (i = 0; i < count; i++) {
348 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
349 found = 1;
350 break;
351 }
352 }
353
354 if (!found) {
355 MSG("Session name %s not found", session_name);
356 ret = CMD_ERROR;
357 goto free_sessions;
358 }
359
360 session_live_mode = sessions[i].live_timer_interval;
361
362 DBG("Session live mode set to %d", session_live_mode);
363
364 if (sessions[i].enabled && !session_live_mode) {
365 WARN("Session %s is running. Please stop it before reading it.",
366 session_name);
367 ret = CMD_ERROR;
368 goto free_sessions;
369 }
370
371 /* If the timer interval is set we are in live mode. */
372 if (session_live_mode) {
373 trace_path = build_live_path(session_name);
374 if (!trace_path) {
375 ret = CMD_ERROR;
376 goto free_sessions;
377 }
378 free_trace_path = true;
379 } else {
380 /* Get file system session path. */
381 trace_path = sessions[i].path;
382 }
383 } else {
384 trace_path = opt_trace_path;
385 }
386
387 MSG("Trace directory: %s\n", trace_path);
388
389 ret = spawn_viewer(trace_path);
390 if (ret < 0) {
391 /* Don't set ret so lttng can interpret the sessiond error. */
392 goto free_sessions;
393 }
394
395 free_sessions:
396 if (session_live_mode && free_trace_path) {
397 free(trace_path);
398 }
399 free(sessions);
400 free_error:
401 if (opt_session_name == NULL) {
402 free(session_name);
403 }
404 error:
405 return ret;
406 }
407
408 /*
409 * The 'view <options>' first level command
410 */
411 int cmd_view(int argc, const char **argv)
412 {
413 int opt, ret = CMD_SUCCESS;
414 static poptContext pc;
415 const char *leftover = NULL;
416
417 pc = poptGetContext(NULL, argc, argv, long_options, 0);
418 poptReadDefaultConfig(pc, 0);
419
420 if (lttng_opt_mi) {
421 WARN("mi does not apply to view command");
422 }
423
424 while ((opt = poptGetNextOpt(pc)) != -1) {
425 switch (opt) {
426 case OPT_HELP:
427 SHOW_HELP();
428 goto end;
429 case OPT_LIST_OPTIONS:
430 list_cmd_options(stdout, long_options);
431 goto end;
432 default:
433 ret = CMD_UNDEFINED;
434 goto end;
435 }
436 }
437
438 opt_session_name = (char*) poptGetArg(pc);
439
440 leftover = poptGetArg(pc);
441 if (leftover) {
442 ERR("Unknown argument: %s", leftover);
443 ret = CMD_ERROR;
444 goto end;
445 }
446
447 ret = view_trace();
448
449 end:
450 poptFreeContext(pc);
451 return ret;
452 }
This page took 0.037952 seconds and 4 git commands to generate.