1a9ad6c3a3868978467563b791cff34a133f6b35
2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include "../command.h"
19 static char *opt_session_name
;
20 static char *opt_viewer
;
21 static char *opt_trace_path
;
22 static const char *babeltrace_bin
= CONFIG_BABELTRACE_BIN
;
23 static const char *babeltrace2_bin
= CONFIG_BABELTRACE2_BIN
;
25 #ifdef LTTNG_EMBED_HELP
26 static const char help_msg
[] =
27 #include <lttng-view.1.h>
36 static struct poptOption long_options
[] = {
37 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
38 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
39 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
40 {"viewer", 'e', POPT_ARG_STRING
, &opt_viewer
, 0, 0, 0},
41 {"trace-path", 't', POPT_ARG_STRING
, &opt_trace_path
, 0, 0, 0},
46 * This is needed for each viewer since we are using execvp().
48 static const char *babeltrace_opts
[] = { "babeltrace" };
49 static const char *babeltrace2_opts
[] = { "babeltrace2" };
52 * Type is also use as the index in the viewers array. So please, make sure
53 * your enum value is in the right order in the array below.
56 VIEWER_BABELTRACE
= 0,
57 VIEWER_BABELTRACE2
= 1,
58 VIEWER_USER_DEFINED
= 2,
61 static const struct viewers
{
62 const char *exec_name
;
63 enum viewer_type type
;
65 { "babeltrace", VIEWER_BABELTRACE
},
66 { "babeltrace2", VIEWER_BABELTRACE2
},
67 { NULL
, VIEWER_USER_DEFINED
},
70 /* Is the session we are trying to view is in live mode. */
71 static int session_live_mode
;
73 static const struct viewers
*parse_options(void)
75 if (opt_viewer
== NULL
) {
76 /* Default is babeltrace */
77 return &(viewers
[VIEWER_BABELTRACE2
]);
80 return &(viewers
[VIEWER_USER_DEFINED
]);
84 * Alloc an array of string pointer from a simple string having all options
85 * seperated by spaces. Also adds the trace path to the arguments.
87 * The returning pointer is ready to be passed to execvp().
89 static char **alloc_argv_from_user_opts(char *opts
, const char *trace_path
)
91 int i
= 0, ignore_space
= 0;
92 unsigned int num_opts
= 1;
93 char **argv
, *token
= opts
;
95 /* Count number of arguments. */
98 /* Use to ignore consecutive spaces */
107 } while (*token
!= '\0');
109 /* Add two here for the NULL terminating element and trace path */
110 argv
= zmalloc(sizeof(char *) * (num_opts
+ 2));
115 token
= strtok(opts
, " ");
116 while (token
!= NULL
) {
117 argv
[i
] = strdup(token
);
118 if (argv
[i
] == NULL
) {
121 token
= strtok(NULL
, " ");
125 argv
[num_opts
] = (char *) trace_path
;
126 argv
[num_opts
+ 1] = NULL
;
132 for (i
= 0; i
< num_opts
+ 2; i
++) {
142 * Alloc an array of string pointer from an array of strings. It also adds
143 * the trace path to the argv.
145 * The returning pointer is ready to be passed to execvp().
147 static char **alloc_argv_from_local_opts(const char **opts
, size_t opts_len
,
148 const char *trace_path
)
151 size_t size
, mem_len
;
153 /* Add one for the NULL terminating element. */
154 mem_len
= opts_len
+ 1;
155 if (session_live_mode
) {
156 /* Add 3 option for the live mode being "-i lttng-live URL". */
159 /* Add option for the trace path. */
163 size
= sizeof(char *) * mem_len
;
165 /* Add two here for the trace_path and the NULL terminating element. */
166 argv
= zmalloc(size
);
171 memcpy(argv
, opts
, sizeof(char *) * opts_len
);
173 if (session_live_mode
) {
174 argv
[opts_len
] = (char *) "-i";
175 argv
[opts_len
+ 1] = (char *) "lttng-live";
176 argv
[opts_len
+ 2] = (char *) trace_path
;
177 argv
[opts_len
+ 3] = NULL
;
179 argv
[opts_len
] = (char *) trace_path
;
180 argv
[opts_len
+ 1] = NULL
;
188 * Spawn viewer with the trace directory path.
190 static int spawn_viewer(const char *trace_path
)
194 const char *viewer_bin
= NULL
;
195 const struct viewers
*viewer
;
198 /* Check for --viewer options */
199 viewer
= parse_options();
200 if (viewer
== NULL
) {
206 switch (viewer
->type
) {
207 case VIEWER_BABELTRACE2
:
208 if (stat(babeltrace2_bin
, &status
) == 0) {
209 viewer_bin
= babeltrace2_bin
;
211 viewer_bin
= viewer
->exec_name
;
213 argv
= alloc_argv_from_local_opts(babeltrace2_opts
,
214 ARRAY_SIZE(babeltrace2_opts
), trace_path
);
216 case VIEWER_BABELTRACE
:
217 if (stat(babeltrace_bin
, &status
) == 0) {
218 viewer_bin
= babeltrace_bin
;
220 viewer_bin
= viewer
->exec_name
;
222 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
223 ARRAY_SIZE(babeltrace_opts
), trace_path
);
225 case VIEWER_USER_DEFINED
:
226 argv
= alloc_argv_from_user_opts(opt_viewer
, trace_path
);
228 viewer_bin
= argv
[0];
232 viewer_bin
= viewers
[VIEWER_BABELTRACE
].exec_name
;
233 argv
= alloc_argv_from_local_opts(babeltrace_opts
,
234 ARRAY_SIZE(babeltrace_opts
), trace_path
);
238 if (argv
== NULL
|| !viewer_bin
) {
243 DBG("Using %s viewer", viewer_bin
);
245 ret
= execvp(viewer_bin
, argv
);
247 if (errno
== ENOENT
&& viewer
->exec_name
) {
248 if (viewer
->type
== VIEWER_BABELTRACE2
) {
249 /* Fallback to legacy babeltrace. */
250 DBG("babeltrace2 not installed on the system, falling back to babeltrace 1.x");
251 viewer
= &viewers
[VIEWER_BABELTRACE
];
256 ERR("Viewer \"%s\" not found on the system",
260 PERROR("Failed to launch \"%s\" viewer", viewer_bin
);
272 * Build the live path we need for the lttng live view.
274 static char *build_live_path(char *session_name
)
278 char hostname
[HOST_NAME_MAX
];
280 ret
= gethostname(hostname
, sizeof(hostname
));
282 PERROR("gethostname");
286 ret
= asprintf(&path
, "net://localhost/host/%s/%s", hostname
,
289 PERROR("asprintf live path");
298 * Exec viewer if found and use session name path.
300 static int view_trace(void)
303 char *session_name
, *trace_path
= NULL
;
304 struct lttng_session
*sessions
= NULL
;
305 bool free_trace_path
= false;
308 * Safety net. If lttng is suid at some point for *any* useless reasons,
309 * this prevent any bad execution of binaries.
312 if (getuid() != geteuid()) {
313 ERR("UID does not match effective UID.");
316 } else if (getgid() != getegid()) {
317 ERR("GID does not match effective GID.");
323 /* User define trace path override the session name */
324 if (opt_trace_path
) {
326 } else if(opt_session_name
== NULL
) {
327 session_name
= get_session_name();
328 if (session_name
== NULL
) {
333 session_name
= opt_session_name
;
336 DBG("Viewing trace for session %s", session_name
);
339 int i
, count
, found
= 0;
341 /* Getting all sessions */
342 count
= lttng_list_sessions(&sessions
);
344 ERR("Unable to list sessions. Session name %s not found.",
346 MSG("Is there a session daemon running?");
351 /* Find our session listed by the session daemon */
352 for (i
= 0; i
< count
; i
++) {
353 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
360 MSG("Session name %s not found", session_name
);
365 session_live_mode
= sessions
[i
].live_timer_interval
;
367 DBG("Session live mode set to %d", session_live_mode
);
369 if (sessions
[i
].enabled
&& !session_live_mode
) {
370 WARN("Session %s is running. Please stop it before reading it.",
376 /* If the timer interval is set we are in live mode. */
377 if (session_live_mode
) {
378 trace_path
= build_live_path(session_name
);
383 free_trace_path
= true;
385 /* Get file system session path. */
386 trace_path
= sessions
[i
].path
;
389 trace_path
= opt_trace_path
;
392 MSG("Trace directory: %s\n", trace_path
);
394 ret
= spawn_viewer(trace_path
);
396 /* Don't set ret so lttng can interpret the sessiond error. */
401 if (session_live_mode
&& free_trace_path
) {
406 if (opt_session_name
== NULL
) {
414 * The 'view <options>' first level command
416 int cmd_view(int argc
, const char **argv
)
418 int opt
, ret
= CMD_SUCCESS
;
419 static poptContext pc
;
420 const char *leftover
= NULL
;
422 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
423 poptReadDefaultConfig(pc
, 0);
426 WARN("mi does not apply to view command");
429 while ((opt
= poptGetNextOpt(pc
)) != -1) {
434 case OPT_LIST_OPTIONS
:
435 list_cmd_options(stdout
, long_options
);
443 opt_session_name
= (char*) poptGetArg(pc
);
445 leftover
= poptGetArg(pc
);
447 ERR("Unknown argument: %s", leftover
);
This page took 0.057026 seconds and 4 git commands to generate.