a13cb352510da0e3f2b60cc9a8c74f2b62f66870
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.
24 #include <sys/types.h>
27 #include "../command.h"
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
;
34 #ifdef LTTNG_EMBED_HELP
35 static const char help_msg
[] =
36 #include <lttng-view.1.h>
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},
55 * This is needed for each viewer since we are using execvp().
57 static const char *babeltrace_opts
[] = { "babeltrace" };
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.
64 VIEWER_BABELTRACE
= 0,
65 VIEWER_USER_DEFINED
= 1,
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.
72 static const struct viewers
{
73 const char *exec_name
;
74 enum viewer_type type
;
76 { "babeltrace", VIEWER_BABELTRACE
},
77 { NULL
, VIEWER_USER_DEFINED
},
80 /* Is the session we are trying to view is in live mode. */
81 static int session_live_mode
;
83 static const struct viewers
*parse_options(void)
85 if (opt_viewer
== NULL
) {
86 /* Default is babeltrace */
87 return &(viewers
[VIEWER_BABELTRACE
]);
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.
95 return &(viewers
[VIEWER_USER_DEFINED
]);
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.
102 * The returning pointer is ready to be passed to execvp().
104 static char **alloc_argv_from_user_opts(char *opts
, const char *trace_path
)
106 int i
= 0, ignore_space
= 0;
107 unsigned int num_opts
= 1;
108 char **argv
, *token
= opts
;
110 /* Count number of arguments. */
113 /* Use to ignore consecutive spaces */
122 } while (*token
!= '\0');
124 /* Add two here for the NULL terminating element and trace path */
125 argv
= zmalloc(sizeof(char *) * (num_opts
+ 2));
130 token
= strtok(opts
, " ");
131 while (token
!= NULL
) {
132 argv
[i
] = strdup(token
);
133 if (argv
[i
] == NULL
) {
136 token
= strtok(NULL
, " ");
140 argv
[num_opts
] = (char *) trace_path
;
141 argv
[num_opts
+ 1] = NULL
;
147 for (i
= 0; i
< num_opts
+ 2; i
++) {
157 * Alloc an array of string pointer from an array of strings. It also adds
158 * the trace path to the argv.
160 * The returning pointer is ready to be passed to execvp().
162 static char **alloc_argv_from_local_opts(const char **opts
, size_t opts_len
,
163 const char *trace_path
)
166 size_t size
, mem_len
;
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". */
174 /* Add option for the trace path. */
178 size
= sizeof(char *) * mem_len
;
180 /* Add two here for the trace_path and the NULL terminating element. */
181 argv
= zmalloc(size
);
186 memcpy(argv
, opts
, sizeof(char *) * opts_len
);
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
;
194 argv
[opts_len
] = (char *) trace_path
;
195 argv
[opts_len
+ 1] = NULL
;
203 * Spawn viewer with the trace directory path.
205 static int spawn_viewer(const char *trace_path
)
209 const char *viewer_bin
= NULL
;
210 const struct viewers
*viewer
;
213 /* Check for --viewer options */
214 viewer
= parse_options();
215 if (viewer
== NULL
) {
220 switch (viewer
->type
) {
221 case VIEWER_BABELTRACE
:
222 if (stat(babeltrace_bin
, &status
) == 0) {
223 viewer_bin
= babeltrace_bin
;
225 viewer_bin
= viewer
->exec_name
;
227 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
228 ARRAY_SIZE(babeltrace_opts
), trace_path
);
230 case VIEWER_USER_DEFINED
:
231 argv
= alloc_argv_from_user_opts(opt_viewer
, trace_path
);
233 viewer_bin
= argv
[0];
237 viewer_bin
= viewers
[VIEWER_BABELTRACE
].exec_name
;
238 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
239 ARRAY_SIZE(babeltrace_opts
), trace_path
);
243 if (argv
== NULL
|| !viewer_bin
) {
248 DBG("Using %s viewer", viewer_bin
);
250 ret
= execvp(viewer_bin
, argv
);
252 if (errno
== ENOENT
) {
253 ERR("%s not found on the system", viewer_bin
);
255 PERROR("exec: %s", viewer_bin
);
267 * Build the live path we need for the lttng live view.
269 static char *build_live_path(char *session_name
)
273 char hostname
[HOST_NAME_MAX
];
275 ret
= gethostname(hostname
, sizeof(hostname
));
277 PERROR("gethostname");
281 ret
= asprintf(&path
, "net://localhost/host/%s/%s", hostname
,
284 PERROR("asprintf live path");
293 * Exec viewer if found and use session name path.
295 static int view_trace(void)
298 char *session_name
, *trace_path
= NULL
;
299 struct lttng_session
*sessions
= NULL
;
300 bool free_trace_path
= false;
303 * Safety net. If lttng is suid at some point for *any* useless reasons,
304 * this prevent any bad execution of binaries.
307 if (getuid() != geteuid()) {
308 ERR("UID does not match effective UID.");
311 } else if (getgid() != getegid()) {
312 ERR("GID does not match effective GID.");
318 /* User define trace path override the session name */
319 if (opt_trace_path
) {
321 } else if(opt_session_name
== NULL
) {
322 session_name
= get_session_name();
323 if (session_name
== NULL
) {
328 session_name
= opt_session_name
;
331 DBG("Viewing trace for session %s", session_name
);
334 int i
, count
, found
= 0;
336 /* Getting all sessions */
337 count
= lttng_list_sessions(&sessions
);
339 ERR("Unable to list sessions. Session name %s not found.",
341 MSG("Is there a session daemon running?");
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) {
355 MSG("Session name %s not found", session_name
);
360 session_live_mode
= sessions
[i
].live_timer_interval
;
362 DBG("Session live mode set to %d", session_live_mode
);
364 if (sessions
[i
].enabled
&& !session_live_mode
) {
365 WARN("Session %s is running. Please stop it before reading it.",
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
);
378 free_trace_path
= true;
380 /* Get file system session path. */
381 trace_path
= sessions
[i
].path
;
384 trace_path
= opt_trace_path
;
387 MSG("Trace directory: %s\n", trace_path
);
389 ret
= spawn_viewer(trace_path
);
391 /* Don't set ret so lttng can interpret the sessiond error. */
396 if (session_live_mode
&& free_trace_path
) {
401 if (opt_session_name
== NULL
) {
409 * The 'view <options>' first level command
411 int cmd_view(int argc
, const char **argv
)
413 int opt
, ret
= CMD_SUCCESS
;
414 static poptContext pc
;
415 const char *leftover
= NULL
;
417 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
418 poptReadDefaultConfig(pc
, 0);
421 WARN("mi does not apply to view command");
424 while ((opt
= poptGetNextOpt(pc
)) != -1) {
429 case OPT_LIST_OPTIONS
:
430 list_cmd_options(stdout
, long_options
);
438 opt_session_name
= (char*) poptGetArg(pc
);
440 leftover
= poptGetArg(pc
);
442 ERR("Unknown argument: %s", leftover
);
This page took 0.046211 seconds and 4 git commands to generate.