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