Fix: lttng view.c missing strdup OOM check
[lttng-tools.git] / src / bin / lttng / commands / view.c
CommitLineData
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
30static char *opt_session_name;
31static char *opt_viewer;
f1f887c0 32static char *opt_trace_path;
0c95f5b2
DG
33static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN;
34//static const char *lttv_gui_bin = CONFIG_LTTV_GUI_BIN;
35
36enum {
37 OPT_HELP = 1,
38 OPT_LIST_OPTIONS,
39};
40
41static 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 53static 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 */
60enum 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 */
70static 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
8960e9cd
DG
79/* Is the session we are trying to view is in live mode. */
80static int session_live_mode;
81
0c95f5b2
DG
82/*
83 * usage
84 */
85static void usage(FILE *ofp)
86{
87 fprintf(ofp, "usage: lttng view [SESSION_NAME] [OPTIONS]\n");
88 fprintf(ofp, "\n");
89 fprintf(ofp, "By default, the babeltrace viewer will be used for text viewing\n");
90 fprintf(ofp, "\n");
91 fprintf(ofp, "Where SESSION_NAME is an optional session name. If not specified, lttng will\n");
92 fprintf(ofp, "get it from the configuration file (.lttngrc).\n");
93 fprintf(ofp, "\n");
32a6298d 94 fprintf(ofp, "Options:\n");
0c95f5b2
DG
95 fprintf(ofp, " -h, --help Show this help\n");
96 fprintf(ofp, " --list-options Simple listing of options\n");
f1f887c0 97 fprintf(ofp, " -t, --trace-path PATH Trace directory path for the viewer\n");
0c95f5b2
DG
98 fprintf(ofp, " -e, --viewer CMD Specify viewer and/or options to use\n");
99 fprintf(ofp, " This will completely override the default viewers so\n");
f1f887c0
DG
100 fprintf(ofp, " please make sure to specify the full command. The trace\n");
101 fprintf(ofp, " directory path of the session will be appended at the end\n");
102 fprintf(ofp, " to the arguments\n");
0c95f5b2
DG
103 fprintf(ofp, "\n");
104}
105
106static struct viewers *parse_options(void)
107{
108 if (opt_viewer == NULL) {
109 /* Default is babeltrace */
110 return &(viewers[VIEWER_BABELTRACE]);
111 }
112
113#if 0
114 if (strstr(opt_viewer, viewers[VIEWER_LTTV_GUI].exec_name) == 0) {
115 return &(viewers[VIEWER_LTTV_GUI]);
116 }
117#endif
118
119 /*
120 * This means that if -e, --viewers is used, we just override everything
121 * with it. For supported viewers like lttv, we could simply detect if "-t"
122 * is passed and if not, add the trace directory to it.
123 */
124 return &(viewers[VIEWER_USER_DEFINED]);
125}
126
127/*
128 * Alloc an array of string pointer from a simple string having all options
129 * seperated by spaces. Also adds the trace path to the arguments.
130 *
131 * The returning pointer is ready to be passed to execvp().
132 */
133static char **alloc_argv_from_user_opts(char *opts, const char *trace_path)
134{
135 int i = 0, ignore_space = 0;
136 unsigned int num_opts = 1;
137 char **argv, *token = opts;
138
139 /* Count number of arguments. */
140 do {
141 if (*token == ' ') {
142 /* Use to ignore consecutive spaces */
143 if (!ignore_space) {
144 num_opts++;
145 }
146 ignore_space = 1;
147 } else {
148 ignore_space = 0;
149 }
150 token++;
151 } while (*token != '\0');
152
153 /* Add two here for the NULL terminating element and trace path */
80ca125c 154 argv = zmalloc(sizeof(char *) * (num_opts + 2));
0c95f5b2
DG
155 if (argv == NULL) {
156 goto error;
157 }
158
159 token = strtok(opts, " ");
160 while (token != NULL) {
161 argv[i] = strdup(token);
6314111d
MD
162 if (argv[i] == NULL) {
163 goto error;
164 }
0c95f5b2
DG
165 token = strtok(NULL, " ");
166 i++;
167 }
168
169 argv[num_opts] = (char *) trace_path;
170 argv[num_opts + 1] = NULL;
171
172 return argv;
173
174error:
175 return NULL;
176}
177
178/*
179 * Alloc an array of string pointer from an array of strings. It also adds
180 * the trace path to the argv.
181 *
182 * The returning pointer is ready to be passed to execvp().
183 */
184static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
185 const char *trace_path)
186{
187 char **argv;
8960e9cd
DG
188 size_t size, mem_len;
189
190
191 /* Add one for the NULL terminating element. */
192 mem_len = opts_len + 1;
193 if (session_live_mode) {
194 /* Add 3 option for the live mode being "-i lttng-live URL". */
195 mem_len += 3;
196 } else {
197 /* Add option for the trace path. */
198 mem_len += 1;
199 }
0c95f5b2 200
8960e9cd 201 size = sizeof(char *) * mem_len;
0c95f5b2
DG
202
203 /* Add two here for the trace_path and the NULL terminating element. */
80ca125c 204 argv = zmalloc(size);
0c95f5b2
DG
205 if (argv == NULL) {
206 goto error;
207 }
208
209 memcpy(argv, opts, size);
210
8960e9cd
DG
211 if (session_live_mode) {
212 argv[opts_len] = "-i";
213 argv[opts_len + 1] = "lttng-live";
214 argv[opts_len + 2] = (char *) trace_path;
215 argv[opts_len + 3] = NULL;
216 } else {
217 argv[opts_len] = (char *) trace_path;
218 argv[opts_len + 1] = NULL;
219 }
0c95f5b2
DG
220
221error:
222 return argv;
223}
224
225/*
226 * Spawn viewer with the trace directory path.
227 */
228static int spawn_viewer(const char *trace_path)
229{
230 int ret = 0;
0c95f5b2
DG
231 struct stat status;
232 const char *viewer_bin = NULL;
233 struct viewers *viewer;
234 char **argv = NULL;
235
236 /* Check for --viewer options */
237 viewer = parse_options();
238 if (viewer == NULL) {
239 ret = CMD_ERROR;
240 goto error;
241 }
242
85a68078
DG
243 switch (viewer->type) {
244 case VIEWER_BABELTRACE:
245 if (stat(babeltrace_bin, &status) == 0) {
246 viewer_bin = babeltrace_bin;
247 } else {
248 viewer_bin = viewer->exec_name;
249 }
250 argv = alloc_argv_from_local_opts(babeltrace_opts,
251 ARRAY_SIZE(babeltrace_opts), trace_path);
252 break;
0c95f5b2 253#if 0
85a68078
DG
254 case VIEWER_LTTV_GUI:
255 if (stat(lttv_gui_bin, &status) == 0) {
256 viewer_bin = lttv_gui_bin;
257 } else {
258 viewer_bin = viewer->exec_name;
259 }
260 argv = alloc_argv_from_local_opts(lttv_gui_opts,
261 ARRAY_SIZE(lttv_gui_opts), trace_path);
262 break;
0c95f5b2 263#endif
85a68078
DG
264 case VIEWER_USER_DEFINED:
265 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
266 if (argv) {
267 viewer_bin = argv[0];
0c95f5b2 268 }
85a68078
DG
269 break;
270 default:
271 viewer_bin = viewers[VIEWER_BABELTRACE].exec_name;
272 argv = alloc_argv_from_local_opts(babeltrace_opts,
273 ARRAY_SIZE(babeltrace_opts), trace_path);
274 break;
275 }
0c95f5b2 276
85a68078
DG
277 if (argv == NULL) {
278 ret = CMD_FATAL;
279 goto error;
280 }
0c95f5b2 281
85a68078 282 DBG("Using %s viewer", viewer_bin);
0c95f5b2 283
85a68078
DG
284 ret = execvp(viewer_bin, argv);
285 if (ret) {
0c687325
JD
286 if (errno == ENOENT) {
287 ERR("%s not found on the system", viewer_bin);
288 } else {
289 PERROR("exec: %s", viewer_bin);
290 }
85a68078 291 free(argv);
0c95f5b2 292 ret = CMD_FATAL;
85a68078 293 goto error;
0c95f5b2
DG
294 }
295
296error:
297 return ret;
298}
299
8960e9cd
DG
300/*
301 * Build the live path we need for the lttng live view.
302 */
303static char *build_live_path(char *session_name)
304{
305 int ret;
306 char *path = NULL;
307 char hostname[HOST_NAME_MAX];
308
309 ret = gethostname(hostname, sizeof(hostname));
310 if (ret < 0) {
311 perror("gethostname");
312 goto error;
313 }
314
315 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
316 session_name);
317 if (ret < 0) {
318 perror("asprintf live path");
319 goto error;
320 }
321
322error:
323 return path;
324}
325
0c95f5b2
DG
326/*
327 * Exec viewer if found and use session name path.
328 */
329static int view_trace(void)
330{
c617c0c6 331 int ret;
8960e9cd 332 char *session_name, *trace_path = NULL;
0c95f5b2
DG
333 struct lttng_session *sessions = NULL;
334
335 /*
336 * Safety net. If lttng is suid at some point for *any* useless reasons,
f1f887c0 337 * this prevent any bad execution of binaries.
0c95f5b2
DG
338 */
339 if (getuid() != 0) {
340 if (getuid() != geteuid()) {
341 ERR("UID does not match effective UID.");
6e9261f4 342 ret = CMD_ERROR;
0c95f5b2
DG
343 goto error;
344 } else if (getgid() != getegid()) {
345 ERR("GID does not match effective GID.");
6e9261f4 346 ret = CMD_ERROR;
0c95f5b2
DG
347 goto error;
348 }
349 }
350
f1f887c0
DG
351 /* User define trace path override the session name */
352 if (opt_trace_path) {
353 session_name = NULL;
354 } else if(opt_session_name == NULL) {
0c95f5b2
DG
355 session_name = get_session_name();
356 if (session_name == NULL) {
357 ret = CMD_ERROR;
358 goto error;
359 }
360 } else {
361 session_name = opt_session_name;
362 }
363
364 DBG("Viewing trace for session %s", session_name);
365
f1f887c0 366 if (session_name) {
c617c0c6
MD
367 int i, count, found = 0;
368
f1f887c0
DG
369 /* Getting all sessions */
370 count = lttng_list_sessions(&sessions);
371 if (count < 0) {
372 ERR("Unable to list sessions. Session name %s not found.",
373 session_name);
374 MSG("Is there a session daemon running?");
375 ret = CMD_ERROR;
376 goto free_error;
377 }
0c95f5b2 378
f1f887c0
DG
379 /* Find our session listed by the session daemon */
380 for (i = 0; i < count; i++) {
381 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
382 found = 1;
383 break;
384 }
0c95f5b2 385 }
0c95f5b2 386
f1f887c0
DG
387 if (!found) {
388 MSG("Session name %s not found", session_name);
389 ret = CMD_ERROR;
390 goto free_sessions;
391 }
392
8960e9cd
DG
393 session_live_mode = sessions[i].live_timer_interval;
394
395 DBG("Session live mode set to %d", session_live_mode);
3822545d 396
8960e9cd 397 if (sessions[i].enabled && !session_live_mode) {
3822545d
DG
398 WARN("Session %s is running. Please stop it before reading it.",
399 session_name);
400 ret = CMD_ERROR;
401 goto free_sessions;
402 }
8960e9cd
DG
403
404 /* If the timer interval is set we are in live mode. */
405 if (session_live_mode) {
406 trace_path = build_live_path(session_name);
407 if (!trace_path) {
408 ret = CMD_ERROR;
409 goto free_sessions;
410 }
411 } else {
412 /* Get file system session path. */
413 trace_path = sessions[i].path;
414 }
f1f887c0
DG
415 } else {
416 trace_path = opt_trace_path;
0c95f5b2
DG
417 }
418
f1f887c0 419 MSG("Trace directory: %s\n", trace_path);
0c95f5b2 420
f1f887c0 421 ret = spawn_viewer(trace_path);
0c95f5b2
DG
422 if (ret < 0) {
423 /* Don't set ret so lttng can interpret the sessiond error. */
424 goto free_sessions;
425 }
426
0c95f5b2 427free_sessions:
8960e9cd
DG
428 if (session_live_mode) {
429 free(trace_path);
430 }
0e428499 431 free(sessions);
0c95f5b2
DG
432free_error:
433 if (opt_session_name == NULL) {
434 free(session_name);
435 }
436error:
437 return ret;
438}
439
440/*
441 * The 'view <options>' first level command
442 */
443int cmd_view(int argc, const char **argv)
444{
445 int opt, ret = CMD_SUCCESS;
446 static poptContext pc;
447
448 pc = poptGetContext(NULL, argc, argv, long_options, 0);
449 poptReadDefaultConfig(pc, 0);
450
451 while ((opt = poptGetNextOpt(pc)) != -1) {
452 switch (opt) {
453 case OPT_HELP:
454 usage(stdout);
455 goto end;
456 case OPT_LIST_OPTIONS:
457 list_cmd_options(stdout, long_options);
458 goto end;
459 default:
460 usage(stderr);
461 ret = CMD_UNDEFINED;
462 goto end;
463 }
464 }
465
466 opt_session_name = (char*) poptGetArg(pc);
467
468 ret = view_trace();
469
470end:
471 poptFreeContext(pc);
472 return ret;
473}
This page took 0.051651 seconds and 4 git commands to generate.