2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
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.
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.
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.
25 #include <sys/types.h>
28 #include "../command.h"
31 static char *opt_session_name
;
32 static char *opt_viewer
;
33 static char *opt_trace_path
;
34 static const char *babeltrace_bin
= CONFIG_BABELTRACE_BIN
;
35 //static const char *lttv_gui_bin = CONFIG_LTTV_GUI_BIN;
42 static 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},
47 {"trace-path", 't', POPT_ARG_STRING
, &opt_trace_path
, 0, 0, 0},
52 * This is needed for each viewer since we are using execvp().
54 static const char *babeltrace_opts
[] = { "babeltrace" };
55 //static const char *lttv_gui_opts[] = { "lttv-gui", "-t", };
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.
62 VIEWER_BABELTRACE
= 0,
64 VIEWER_USER_DEFINED
= 2,
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.
71 static struct viewers
{
72 const char *exec_name
;
73 enum viewer_type type
;
75 { "babeltrace", VIEWER_BABELTRACE
},
76 { "lttv-gui", VIEWER_LTTV_GUI
},
77 { NULL
, VIEWER_USER_DEFINED
},
80 /* Is the session we are trying to view is in live mode. */
81 static int session_live_mode
;
86 static void usage(FILE *ofp
)
88 fprintf(ofp
, "usage: lttng view [SESSION_NAME] [OPTIONS]\n");
90 fprintf(ofp
, "By default, the babeltrace viewer will be used for text viewing\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");
95 fprintf(ofp
, "Options:\n");
96 fprintf(ofp
, " -h, --help Show this help\n");
97 fprintf(ofp
, " --list-options Simple listing of options\n");
98 fprintf(ofp
, " -t, --trace-path PATH Trace directory path for the viewer\n");
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");
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");
107 static struct viewers
*parse_options(void)
109 if (opt_viewer
== NULL
) {
110 /* Default is babeltrace */
111 return &(viewers
[VIEWER_BABELTRACE
]);
115 * This means that if -e, --viewers is used, we just override everything
116 * with it. For supported viewers like lttv, we could simply detect if "-t"
117 * is passed and if not, add the trace directory to it.
119 return &(viewers
[VIEWER_USER_DEFINED
]);
123 * Alloc an array of string pointer from a simple string having all options
124 * seperated by spaces. Also adds the trace path to the arguments.
126 * The returning pointer is ready to be passed to execvp().
128 static char **alloc_argv_from_user_opts(char *opts
, const char *trace_path
)
130 int i
= 0, ignore_space
= 0;
131 unsigned int num_opts
= 1;
132 char **argv
, *token
= opts
;
134 /* Count number of arguments. */
137 /* Use to ignore consecutive spaces */
146 } while (*token
!= '\0');
148 /* Add two here for the NULL terminating element and trace path */
149 argv
= zmalloc(sizeof(char *) * (num_opts
+ 2));
154 token
= strtok(opts
, " ");
155 while (token
!= NULL
) {
156 argv
[i
] = strdup(token
);
157 if (argv
[i
] == NULL
) {
160 token
= strtok(NULL
, " ");
164 argv
[num_opts
] = (char *) trace_path
;
165 argv
[num_opts
+ 1] = NULL
;
171 for (i
= 0; i
< num_opts
+ 2; i
++) {
181 * Alloc an array of string pointer from an array of strings. It also adds
182 * the trace path to the argv.
184 * The returning pointer is ready to be passed to execvp().
186 static char **alloc_argv_from_local_opts(const char **opts
, size_t opts_len
,
187 const char *trace_path
)
190 size_t size
, mem_len
;
193 /* Add one for the NULL terminating element. */
194 mem_len
= opts_len
+ 1;
195 if (session_live_mode
) {
196 /* Add 3 option for the live mode being "-i lttng-live URL". */
199 /* Add option for the trace path. */
203 size
= sizeof(char *) * mem_len
;
205 /* Add two here for the trace_path and the NULL terminating element. */
206 argv
= zmalloc(size
);
211 memcpy(argv
, opts
, size
);
213 if (session_live_mode
) {
214 argv
[opts_len
] = "-i";
215 argv
[opts_len
+ 1] = "lttng-live";
216 argv
[opts_len
+ 2] = (char *) trace_path
;
217 argv
[opts_len
+ 3] = NULL
;
219 argv
[opts_len
] = (char *) trace_path
;
220 argv
[opts_len
+ 1] = NULL
;
228 * Spawn viewer with the trace directory path.
230 static int spawn_viewer(const char *trace_path
)
234 const char *viewer_bin
= NULL
;
235 struct viewers
*viewer
;
238 /* Check for --viewer options */
239 viewer
= parse_options();
240 if (viewer
== NULL
) {
245 switch (viewer
->type
) {
246 case VIEWER_BABELTRACE
:
247 if (stat(babeltrace_bin
, &status
) == 0) {
248 viewer_bin
= babeltrace_bin
;
250 viewer_bin
= viewer
->exec_name
;
252 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
253 ARRAY_SIZE(babeltrace_opts
), trace_path
);
255 case VIEWER_USER_DEFINED
:
256 argv
= alloc_argv_from_user_opts(opt_viewer
, trace_path
);
258 viewer_bin
= argv
[0];
262 viewer_bin
= viewers
[VIEWER_BABELTRACE
].exec_name
;
263 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
264 ARRAY_SIZE(babeltrace_opts
), trace_path
);
268 if (argv
== NULL
|| !viewer_bin
) {
273 DBG("Using %s viewer", viewer_bin
);
275 ret
= execvp(viewer_bin
, argv
);
277 if (errno
== ENOENT
) {
278 ERR("%s not found on the system", viewer_bin
);
280 PERROR("exec: %s", viewer_bin
);
292 * Build the live path we need for the lttng live view.
294 static char *build_live_path(char *session_name
)
298 char hostname
[HOST_NAME_MAX
];
300 ret
= gethostname(hostname
, sizeof(hostname
));
302 PERROR("gethostname");
306 ret
= asprintf(&path
, "net://localhost/host/%s/%s", hostname
,
309 PERROR("asprintf live path");
318 * Exec viewer if found and use session name path.
320 static int view_trace(void)
323 char *session_name
, *trace_path
= NULL
;
324 struct lttng_session
*sessions
= NULL
;
327 * Safety net. If lttng is suid at some point for *any* useless reasons,
328 * this prevent any bad execution of binaries.
331 if (getuid() != geteuid()) {
332 ERR("UID does not match effective UID.");
335 } else if (getgid() != getegid()) {
336 ERR("GID does not match effective GID.");
342 /* User define trace path override the session name */
343 if (opt_trace_path
) {
345 } else if(opt_session_name
== NULL
) {
346 session_name
= get_session_name();
347 if (session_name
== NULL
) {
352 session_name
= opt_session_name
;
355 DBG("Viewing trace for session %s", session_name
);
358 int i
, count
, found
= 0;
360 /* Getting all sessions */
361 count
= lttng_list_sessions(&sessions
);
363 ERR("Unable to list sessions. Session name %s not found.",
365 MSG("Is there a session daemon running?");
370 /* Find our session listed by the session daemon */
371 for (i
= 0; i
< count
; i
++) {
372 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
379 MSG("Session name %s not found", session_name
);
384 session_live_mode
= sessions
[i
].live_timer_interval
;
386 DBG("Session live mode set to %d", session_live_mode
);
388 if (sessions
[i
].enabled
&& !session_live_mode
) {
389 WARN("Session %s is running. Please stop it before reading it.",
395 /* If the timer interval is set we are in live mode. */
396 if (session_live_mode
) {
397 trace_path
= build_live_path(session_name
);
403 /* Get file system session path. */
404 trace_path
= sessions
[i
].path
;
407 trace_path
= opt_trace_path
;
410 MSG("Trace directory: %s\n", trace_path
);
412 ret
= spawn_viewer(trace_path
);
414 /* Don't set ret so lttng can interpret the sessiond error. */
419 if (session_live_mode
) {
424 if (opt_session_name
== NULL
) {
432 * The 'view <options>' first level command
434 int cmd_view(int argc
, const char **argv
)
436 int opt
, ret
= CMD_SUCCESS
;
437 static poptContext pc
;
439 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
440 poptReadDefaultConfig(pc
, 0);
443 WARN("mi does not apply to view command");
446 while ((opt
= poptGetNextOpt(pc
)) != -1) {
451 case OPT_LIST_OPTIONS
:
452 list_cmd_options(stdout
, long_options
);
461 opt_session_name
= (char*) poptGetArg(pc
);
This page took 0.038274 seconds and 4 git commands to generate.