2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
6 * SPDX-License-Identifier: LGPL-2.1-only
12 #include "spawn-viewer.hpp"
14 #include <common/compat/errno.hpp>
16 #include <lttng/constant.h>
20 #include <sys/types.h>
24 * Type is also use as the index in the viewers array. So please, make sure
25 * your enum value is in the right order in the array below.
28 VIEWER_BABELTRACE
= 0,
29 VIEWER_BABELTRACE2
= 1,
30 VIEWER_USER_DEFINED
= 2,
34 const char *babeltrace_bin
= CONFIG_BABELTRACE_BIN
;
35 const char *babeltrace2_bin
= CONFIG_BABELTRACE2_BIN
;
38 * This is needed for each viewer since we are using execvp().
40 const char *babeltrace_opts
[] = { "babeltrace" };
41 const char *babeltrace2_opts
[] = { "babeltrace2" };
44 const char *exec_name
;
45 enum viewer_type type
;
47 { "babeltrace", VIEWER_BABELTRACE
},
48 { "babeltrace2", VIEWER_BABELTRACE2
},
49 { nullptr, VIEWER_USER_DEFINED
},
53 static const struct viewer
*parse_viewer_option(const char *opt_viewer
)
55 if (opt_viewer
== nullptr) {
56 /* Default is babeltrace2 */
57 return &(viewers
[VIEWER_BABELTRACE2
]);
60 return &(viewers
[VIEWER_USER_DEFINED
]);
64 * Alloc an array of string pointer from a simple string having all options
65 * seperated by spaces. Also adds the trace path to the arguments.
67 * The returning pointer is ready to be passed to execvp().
69 static char **alloc_argv_from_user_opts(char *opts
, const char *trace_path
)
71 int i
= 0, ignore_space
= 0;
72 unsigned int num_opts
= 1;
73 char **argv
, *token
= opts
, *saveptr
= nullptr;
75 /* Count number of arguments. */
78 /* Use to ignore consecutive spaces */
87 } while (*token
!= '\0');
89 /* Add two here for the NULL terminating element and trace path */
90 argv
= calloc
<char *>(num_opts
+ 2);
91 if (argv
== nullptr) {
95 token
= strtok_r(opts
, " ", &saveptr
);
96 while (token
!= nullptr) {
97 argv
[i
] = strdup(token
);
98 if (argv
[i
] == nullptr) {
101 token
= strtok_r(nullptr, " ", &saveptr
);
105 argv
[num_opts
] = (char *) trace_path
;
106 argv
[num_opts
+ 1] = nullptr;
112 for (i
= 0; i
< num_opts
+ 2; i
++) {
122 * Alloc an array of string pointer from an array of strings. It also adds
123 * the trace path to the argv.
125 * The returning pointer is ready to be passed to execvp().
127 static char **alloc_argv_from_local_opts(const char **opts
,
129 const char *trace_path
,
135 /* Add one for the NULL terminating element. */
136 mem_len
= opts_len
+ 1;
138 /* Add 3 option for the live mode being "-i lttng-live URL". */
141 /* Add option for the trace path. */
145 argv
= calloc
<char *>(mem_len
);
146 if (argv
== nullptr) {
150 memcpy(argv
, opts
, sizeof(char *) * opts_len
);
153 argv
[opts_len
] = (char *) "-i";
154 argv
[opts_len
+ 1] = (char *) "lttng-live";
155 argv
[opts_len
+ 2] = (char *) trace_path
;
156 argv
[opts_len
+ 3] = nullptr;
158 argv
[opts_len
] = (char *) trace_path
;
159 argv
[opts_len
+ 1] = nullptr;
167 * Spawn viewer with the trace directory path.
169 int spawn_viewer(const char *trace_path
, char *opt_viewer
, bool opt_live_mode
)
173 const char *viewer_bin
= nullptr;
174 const struct viewer
*viewer
;
175 char **argv
= nullptr;
177 /* Check for --viewer option. */
178 viewer
= parse_viewer_option(opt_viewer
);
179 if (viewer
== nullptr) {
185 switch (viewer
->type
) {
186 case VIEWER_BABELTRACE2
:
187 if (stat(babeltrace2_bin
, &status
) == 0) {
188 viewer_bin
= babeltrace2_bin
;
190 viewer_bin
= viewer
->exec_name
;
192 argv
= alloc_argv_from_local_opts(
193 babeltrace2_opts
, ARRAY_SIZE(babeltrace2_opts
), trace_path
, opt_live_mode
);
195 case VIEWER_BABELTRACE
:
196 if (stat(babeltrace_bin
, &status
) == 0) {
197 viewer_bin
= babeltrace_bin
;
199 viewer_bin
= viewer
->exec_name
;
201 argv
= alloc_argv_from_local_opts(
202 babeltrace_opts
, ARRAY_SIZE(babeltrace_opts
), trace_path
, opt_live_mode
);
204 case VIEWER_USER_DEFINED
:
205 argv
= alloc_argv_from_user_opts(opt_viewer
, trace_path
);
207 viewer_bin
= argv
[0];
214 if (argv
== nullptr || !viewer_bin
) {
219 DBG("Using %s viewer", viewer_bin
);
221 ret
= execvp(viewer_bin
, argv
);
223 if (errno
== ENOENT
&& viewer
->exec_name
) {
224 if (viewer
->type
== VIEWER_BABELTRACE2
) {
225 /* Fallback to legacy babeltrace. */
226 DBG("Default viewer \"%s\" not installed on the system, falling back to \"%s\"",
227 viewers
[VIEWER_BABELTRACE2
].exec_name
,
228 viewers
[VIEWER_BABELTRACE
].exec_name
);
229 viewer
= &viewers
[VIEWER_BABELTRACE
];
234 ERR("Default viewer \"%s\" (and fallback \"%s\") not found on the system",
235 viewers
[VIEWER_BABELTRACE2
].exec_name
,
236 viewers
[VIEWER_BABELTRACE
].exec_name
);
239 PERROR("Failed to launch \"%s\" viewer", viewer_bin
);
246 * This function should never return if successfull because `execvp(3)`
247 * onle returns if an error has occurred.
249 LTTNG_ASSERT(ret
!= 0);
This page took 0.052885 seconds and 4 git commands to generate.