2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
17 #include <common/spawn-viewer.hpp>
18 #include "../command.hpp"
20 static char *opt_viewer
;
21 static char *opt_trace_path
;
23 #ifdef LTTNG_EMBED_HELP
24 static const char help_msg
[] =
25 #include <lttng-view.1.h>
34 static struct poptOption long_options
[] = {
35 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
36 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
37 {"list-options", 0, POPT_ARG_NONE
, NULL
, OPT_LIST_OPTIONS
, NULL
, NULL
},
38 {"viewer", 'e', POPT_ARG_STRING
, &opt_viewer
, 0, 0, 0},
39 {"trace-path", 't', POPT_ARG_STRING
, &opt_trace_path
, 0, 0, 0},
43 /* Is the session we are trying to view is in live mode. */
44 static int session_live_mode
;
47 * Build the live path we need for the lttng live view.
49 static char *build_live_path(char *session_name
)
53 char hostname
[LTTNG_HOST_NAME_MAX
];
55 ret
= gethostname(hostname
, sizeof(hostname
));
57 PERROR("gethostname");
61 ret
= asprintf(&path
, "net://localhost/host/%s/%s", hostname
,
64 PERROR("asprintf live path");
73 * Exec viewer if found and use session name path.
75 static int view_trace(const char *arg_session_name
)
78 char *session_name
, *trace_path
= NULL
;
79 struct lttng_session
*sessions
= NULL
;
80 bool free_trace_path
= false;
83 * Safety net. If lttng is suid at some point for *any* useless reasons,
84 * this prevent any bad execution of binaries.
87 if (getuid() != geteuid()) {
88 ERR("UID does not match effective UID.");
91 } else if (getgid() != getegid()) {
92 ERR("GID does not match effective GID.");
98 /* User define trace path override the session name */
102 if (arg_session_name
== NULL
) {
103 session_name
= get_session_name();
105 session_name
= strdup(arg_session_name
);
106 if (session_name
== NULL
) {
107 PERROR("Failed to copy session name");
111 if (session_name
== NULL
) {
117 DBG("Viewing trace for session %s", session_name
);
120 int i
, count
, found
= 0;
122 /* Getting all sessions */
123 count
= lttng_list_sessions(&sessions
);
125 ERR("Unable to list sessions. Session name %s not found.",
127 MSG("Is there a session daemon running?");
132 /* Find our session listed by the session daemon */
133 for (i
= 0; i
< count
; i
++) {
134 if (strncmp(sessions
[i
].name
, session_name
, NAME_MAX
) == 0) {
141 MSG("Session name %s not found", session_name
);
146 session_live_mode
= sessions
[i
].live_timer_interval
;
148 DBG("Session live mode set to %d", session_live_mode
);
150 if (sessions
[i
].enabled
&& !session_live_mode
) {
151 WARN("Session %s is running. Please stop it before reading it.",
157 /* If the timer interval is set we are in live mode. */
158 if (session_live_mode
) {
159 trace_path
= build_live_path(session_name
);
164 free_trace_path
= true;
166 /* Get file system session path. */
167 trace_path
= sessions
[i
].path
;
170 trace_path
= opt_trace_path
;
173 MSG("Trace directory: %s\n", trace_path
);
175 ret
= spawn_viewer(trace_path
, opt_viewer
, session_live_mode
);
177 /* Don't set ret so lttng can interpret the sessiond error. */
182 if (session_live_mode
&& free_trace_path
) {
193 * The 'view <options>' first level command
195 int cmd_view(int argc
, const char **argv
)
197 int opt
, ret
= CMD_SUCCESS
;
198 static poptContext pc
;
199 const char *arg_session_name
= NULL
;
200 const char *leftover
= NULL
;
202 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
203 poptReadDefaultConfig(pc
, 0);
206 WARN("mi does not apply to view command");
209 while ((opt
= poptGetNextOpt(pc
)) != -1) {
214 case OPT_LIST_OPTIONS
:
215 list_cmd_options(stdout
, long_options
);
223 arg_session_name
= poptGetArg(pc
);
225 leftover
= poptGetArg(pc
);
227 ERR("Unknown argument: %s", leftover
);
232 ret
= view_trace(arg_session_name
);
This page took 0.083445 seconds and 5 git commands to generate.