Add the lttng view command
[lttng-tools.git] / src / bin / lttng / commands / view.c
CommitLineData
0c95f5b2
DG
1/*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
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;
32static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN;
33//static const char *lttv_gui_bin = CONFIG_LTTV_GUI_BIN;
34
35enum {
36 OPT_HELP = 1,
37 OPT_LIST_OPTIONS,
38};
39
40static struct poptOption long_options[] = {
41 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
42 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
43 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
44 {"viewer", 'e', POPT_ARG_STRING, &opt_viewer, 0, 0, 0},
45 {0, 0, 0, 0, 0, 0, 0}
46};
47
48/*
49 * This is needed for each viewer since we are using execvp().
50 */
51static const char *babeltrace_opts[] = { "babeltrace", "-n", "all", };
52//static const char *lttv_gui_opts[] = { "lttv-gui", "-t", };
53
54/*
55 * Type is also use as the index in the viewers array. So please, make sure
56 * your enum value is in the right order in the array below.
57 */
58enum viewer_type {
59 VIEWER_BABELTRACE = 0,
60 VIEWER_LTTV_GUI = 1,
61 VIEWER_USER_DEFINED = 2,
62};
63
64/*
65 * NOTE: "lttv" is a shell command and it's not working for exec() family
66 * functions so we might think of removing this wrapper or using bash.
67 */
68static struct viewers {
69 const char *exec_name;
70 enum viewer_type type;
71} viewers[] = {
72 { "babeltrace", VIEWER_BABELTRACE },
73 { "lttv-gui", VIEWER_LTTV_GUI },
74 { NULL, VIEWER_USER_DEFINED },
75};
76
77/*
78 * usage
79 */
80static void usage(FILE *ofp)
81{
82 fprintf(ofp, "usage: lttng view [SESSION_NAME] [OPTIONS]\n");
83 fprintf(ofp, "\n");
84 fprintf(ofp, "By default, the babeltrace viewer will be used for text viewing\n");
85 fprintf(ofp, "\n");
86 fprintf(ofp, "Where SESSION_NAME is an optional session name. If not specified, lttng will\n");
87 fprintf(ofp, "get it from the configuration file (.lttngrc).\n");
88 fprintf(ofp, "\n");
89 fprintf(ofp, " -h, --help Show this help\n");
90 fprintf(ofp, " --list-options Simple listing of options\n");
91 fprintf(ofp, " -e, --viewer CMD Specify viewer and/or options to use\n");
92 fprintf(ofp, " This will completely override the default viewers so\n");
93 fprintf(ofp, " please make sure to specify the full command.\n");
94 fprintf(ofp, "\n");
95}
96
97static struct viewers *parse_options(void)
98{
99 if (opt_viewer == NULL) {
100 /* Default is babeltrace */
101 return &(viewers[VIEWER_BABELTRACE]);
102 }
103
104#if 0
105 if (strstr(opt_viewer, viewers[VIEWER_LTTV_GUI].exec_name) == 0) {
106 return &(viewers[VIEWER_LTTV_GUI]);
107 }
108#endif
109
110 /*
111 * This means that if -e, --viewers is used, we just override everything
112 * with it. For supported viewers like lttv, we could simply detect if "-t"
113 * is passed and if not, add the trace directory to it.
114 */
115 return &(viewers[VIEWER_USER_DEFINED]);
116}
117
118/*
119 * Alloc an array of string pointer from a simple string having all options
120 * seperated by spaces. Also adds the trace path to the arguments.
121 *
122 * The returning pointer is ready to be passed to execvp().
123 */
124static char **alloc_argv_from_user_opts(char *opts, const char *trace_path)
125{
126 int i = 0, ignore_space = 0;
127 unsigned int num_opts = 1;
128 char **argv, *token = opts;
129
130 /* Count number of arguments. */
131 do {
132 if (*token == ' ') {
133 /* Use to ignore consecutive spaces */
134 if (!ignore_space) {
135 num_opts++;
136 }
137 ignore_space = 1;
138 } else {
139 ignore_space = 0;
140 }
141 token++;
142 } while (*token != '\0');
143
144 /* Add two here for the NULL terminating element and trace path */
145 argv = malloc(sizeof(char *) * (num_opts + 2));
146 if (argv == NULL) {
147 goto error;
148 }
149
150 token = strtok(opts, " ");
151 while (token != NULL) {
152 argv[i] = strdup(token);
153 token = strtok(NULL, " ");
154 i++;
155 }
156
157 argv[num_opts] = (char *) trace_path;
158 argv[num_opts + 1] = NULL;
159
160 return argv;
161
162error:
163 return NULL;
164}
165
166/*
167 * Alloc an array of string pointer from an array of strings. It also adds
168 * the trace path to the argv.
169 *
170 * The returning pointer is ready to be passed to execvp().
171 */
172static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
173 const char *trace_path)
174{
175 char **argv;
176 size_t size;
177
178 size = sizeof(char *) * opts_len;
179
180 /* Add two here for the trace_path and the NULL terminating element. */
181 argv = malloc(size + 2);
182 if (argv == NULL) {
183 goto error;
184 }
185
186 memcpy(argv, opts, size);
187
188 argv[opts_len] = (char *)trace_path;
189 argv[opts_len + 1] = NULL;
190
191error:
192 return argv;
193}
194
195/*
196 * Spawn viewer with the trace directory path.
197 */
198static int spawn_viewer(const char *trace_path)
199{
200 int ret = 0;
201 pid_t pid;
202 struct stat status;
203 const char *viewer_bin = NULL;
204 struct viewers *viewer;
205 char **argv = NULL;
206
207 /* Check for --viewer options */
208 viewer = parse_options();
209 if (viewer == NULL) {
210 ret = CMD_ERROR;
211 goto error;
212 }
213
214 pid = fork();
215 if (pid == 0) {
216 switch (viewer->type) {
217 case VIEWER_BABELTRACE:
218 if (stat(babeltrace_bin, &status) == 0) {
219 viewer_bin = babeltrace_bin;
220 } else {
221 viewer_bin = viewer->exec_name;
222 }
223 argv = alloc_argv_from_local_opts(babeltrace_opts,
224 ARRAY_SIZE(babeltrace_opts), trace_path);
225 break;
226#if 0
227 case VIEWER_LTTV_GUI:
228 if (stat(lttv_gui_bin, &status) == 0) {
229 viewer_bin = lttv_gui_bin;
230 } else {
231 viewer_bin = viewer->exec_name;
232 }
233 argv = alloc_argv_from_local_opts(lttv_gui_opts,
234 ARRAY_SIZE(lttv_gui_opts), trace_path);
235 break;
236#endif
237 case VIEWER_USER_DEFINED:
238 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
239 if (argv) {
240 viewer_bin = argv[0];
241 }
242 break;
243 default:
244 viewer_bin = viewers[VIEWER_BABELTRACE].exec_name;
245 argv = alloc_argv_from_local_opts(babeltrace_opts,
246 ARRAY_SIZE(babeltrace_opts), trace_path);
247 break;
248 }
249
250 if (argv == NULL) {
251 ret = CMD_FATAL;
252 goto error;
253 }
254
255 DBG("Using %s viewer", viewer_bin);
256
257 ret = execvp(viewer_bin, argv);
258 if (ret) {
259 PERROR("exec: %s", viewer_bin);
260 free(argv);
261 ret = CMD_FATAL;
262 goto error;
263 }
264 } else if (pid > 0) {
265 ret = CMD_SUCCESS;
266 } else {
267 PERROR("Fork trace viewer");
268 ret = CMD_FATAL;
269 }
270
271error:
272 return ret;
273}
274
275/*
276 * Exec viewer if found and use session name path.
277 */
278static int view_trace(void)
279{
280 int ret, count, i, found = 0;
281 char *session_name;
282 struct lttng_session *sessions = NULL;
283
284 /*
285 * Safety net. If lttng is suid at some point for *any* useless reasons,
286 * this prevent any bad execution of binraries.
287 */
288 if (getuid() != 0) {
289 if (getuid() != geteuid()) {
290 ERR("UID does not match effective UID.");
291 goto error;
292 } else if (getgid() != getegid()) {
293 ERR("GID does not match effective GID.");
294 goto error;
295 }
296 }
297
298 if (opt_session_name == NULL) {
299 session_name = get_session_name();
300 if (session_name == NULL) {
301 ret = CMD_ERROR;
302 goto error;
303 }
304 } else {
305 session_name = opt_session_name;
306 }
307
308 DBG("Viewing trace for session %s", session_name);
309
310 /* Getting all sessions */
311 count = lttng_list_sessions(&sessions);
312 if (count < 0) {
313 ERR("Unable to list sessions. Session name %s not found.",
314 session_name);
315 MSG("Is there a session daemon running?");
316 ret = CMD_ERROR;
317 goto free_error;
318 }
319
320 /* Find our session listed by the session daemon */
321 for (i = 0; i < count; i++) {
322 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
323 found = 1;
324 break;
325 }
326 }
327
328 if (!found) {
329 MSG("Session name %s not found", session_name);
330 goto free_sessions;
331 }
332
333 MSG("Trace directory: %s\n", sessions[i].path);
334
335 ret = spawn_viewer(sessions[i].path);
336 if (ret < 0) {
337 /* Don't set ret so lttng can interpret the sessiond error. */
338 goto free_sessions;
339 }
340
341 ret = CMD_SUCCESS;
342
343free_sessions:
344 if (sessions) {
345 free(sessions);
346 }
347free_error:
348 if (opt_session_name == NULL) {
349 free(session_name);
350 }
351error:
352 return ret;
353}
354
355/*
356 * The 'view <options>' first level command
357 */
358int cmd_view(int argc, const char **argv)
359{
360 int opt, ret = CMD_SUCCESS;
361 static poptContext pc;
362
363 pc = poptGetContext(NULL, argc, argv, long_options, 0);
364 poptReadDefaultConfig(pc, 0);
365
366 while ((opt = poptGetNextOpt(pc)) != -1) {
367 switch (opt) {
368 case OPT_HELP:
369 usage(stdout);
370 goto end;
371 case OPT_LIST_OPTIONS:
372 list_cmd_options(stdout, long_options);
373 goto end;
374 default:
375 usage(stderr);
376 ret = CMD_UNDEFINED;
377 goto end;
378 }
379 }
380
381 opt_session_name = (char*) poptGetArg(pc);
382
383 ret = view_trace();
384
385end:
386 poptFreeContext(pc);
387 return ret;
388}
This page took 0.036504 seconds and 4 git commands to generate.