Small fixes to lttng man pages (v2)
[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
79/*
80 * usage
81 */
82static 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
102static 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 */
129static 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
167error:
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 */
177static 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
196error:
197 return argv;
198}
199
200/*
201 * Spawn viewer with the trace directory path.
202 */
203static 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) {
261 PERROR("exec: %s", viewer_bin);
262 free(argv);
0c95f5b2 263 ret = CMD_FATAL;
85a68078 264 goto error;
0c95f5b2
DG
265 }
266
267error:
268 return ret;
269}
270
271/*
272 * Exec viewer if found and use session name path.
273 */
274static int view_trace(void)
275{
276 int ret, count, i, found = 0;
f1f887c0 277 char *session_name, *trace_path;
0c95f5b2
DG
278 struct lttng_session *sessions = NULL;
279
280 /*
281 * Safety net. If lttng is suid at some point for *any* useless reasons,
f1f887c0 282 * this prevent any bad execution of binaries.
0c95f5b2
DG
283 */
284 if (getuid() != 0) {
285 if (getuid() != geteuid()) {
286 ERR("UID does not match effective UID.");
6e9261f4 287 ret = CMD_ERROR;
0c95f5b2
DG
288 goto error;
289 } else if (getgid() != getegid()) {
290 ERR("GID does not match effective GID.");
6e9261f4 291 ret = CMD_ERROR;
0c95f5b2
DG
292 goto error;
293 }
294 }
295
f1f887c0
DG
296 /* User define trace path override the session name */
297 if (opt_trace_path) {
298 session_name = NULL;
299 } else if(opt_session_name == NULL) {
0c95f5b2
DG
300 session_name = get_session_name();
301 if (session_name == NULL) {
302 ret = CMD_ERROR;
303 goto error;
304 }
305 } else {
306 session_name = opt_session_name;
307 }
308
309 DBG("Viewing trace for session %s", session_name);
310
f1f887c0
DG
311 if (session_name) {
312 /* Getting all sessions */
313 count = lttng_list_sessions(&sessions);
314 if (count < 0) {
315 ERR("Unable to list sessions. Session name %s not found.",
316 session_name);
317 MSG("Is there a session daemon running?");
318 ret = CMD_ERROR;
319 goto free_error;
320 }
0c95f5b2 321
f1f887c0
DG
322 /* Find our session listed by the session daemon */
323 for (i = 0; i < count; i++) {
324 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
325 found = 1;
326 break;
327 }
0c95f5b2 328 }
0c95f5b2 329
f1f887c0
DG
330 if (!found) {
331 MSG("Session name %s not found", session_name);
332 ret = CMD_ERROR;
333 goto free_sessions;
334 }
335
336 trace_path = sessions[i].path;
337 } else {
338 trace_path = opt_trace_path;
0c95f5b2
DG
339 }
340
f1f887c0 341 MSG("Trace directory: %s\n", trace_path);
0c95f5b2 342
f1f887c0 343 ret = spawn_viewer(trace_path);
0c95f5b2
DG
344 if (ret < 0) {
345 /* Don't set ret so lttng can interpret the sessiond error. */
346 goto free_sessions;
347 }
348
349 ret = CMD_SUCCESS;
350
351free_sessions:
352 if (sessions) {
353 free(sessions);
354 }
355free_error:
356 if (opt_session_name == NULL) {
357 free(session_name);
358 }
359error:
360 return ret;
361}
362
363/*
364 * The 'view <options>' first level command
365 */
366int cmd_view(int argc, const char **argv)
367{
368 int opt, ret = CMD_SUCCESS;
369 static poptContext pc;
370
371 pc = poptGetContext(NULL, argc, argv, long_options, 0);
372 poptReadDefaultConfig(pc, 0);
373
374 while ((opt = poptGetNextOpt(pc)) != -1) {
375 switch (opt) {
376 case OPT_HELP:
377 usage(stdout);
378 goto end;
379 case OPT_LIST_OPTIONS:
380 list_cmd_options(stdout, long_options);
381 goto end;
382 default:
383 usage(stderr);
384 ret = CMD_UNDEFINED;
385 goto end;
386 }
387 }
388
389 opt_session_name = (char*) poptGetArg(pc);
390
391 ret = view_trace();
392
393end:
394 poptFreeContext(pc);
395 return ret;
396}
This page took 0.042359 seconds and 5 git commands to generate.