lttng: add-trigger: print generated trigger name
[lttng-tools.git] / src / bin / lttng / commands / load.c
1 /*
2 * Copyright (C) 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <inttypes.h>
10 #include <popt.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <assert.h>
15
16 #include <common/mi-lttng.h>
17 #include <common/config/session-config.h>
18 #include <lttng/lttng.h>
19
20 #include "../command.h"
21
22 static char *the_opt_input_path;
23 static char *the_opt_override_url;
24 static char *the_opt_override_session_name;
25 static int the_opt_force;
26 static int the_opt_load_all;
27
28 static const char *the_session_name;
29
30 #ifdef LTTNG_EMBED_HELP
31 static const char help_msg[] =
32 #include <lttng-load.1.h>
33 ;
34 #endif
35
36 enum {
37 OPT_HELP = 1,
38 OPT_ALL,
39 OPT_FORCE,
40 OPT_LIST_OPTIONS,
41 };
42
43 static struct mi_writer *the_writer;
44
45 static struct poptOption the_load_opts[] = {
46 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
47 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
48 {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0},
49 {"input-path", 'i', POPT_ARG_STRING, &the_opt_input_path, 0, 0, 0},
50 {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
51 {"override-url", 0, POPT_ARG_STRING, &the_opt_override_url, 0, 0, 0},
52 {"override-name", 0, POPT_ARG_STRING, &the_opt_override_session_name, 0, 0, 0},
53 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
54 {0, 0, 0, 0, 0, 0, 0}
55 };
56
57 static int mi_partial_session(const char *session_name)
58 {
59 int ret;
60 assert(the_writer);
61 assert(session_name);
62
63 /* Open session element */
64 ret = mi_lttng_writer_open_element(the_writer, config_element_session);
65 if (ret) {
66 goto end;
67 }
68
69 ret = mi_lttng_writer_write_element_string(the_writer, config_element_name,
70 session_name);
71 if (ret) {
72 goto end;
73 }
74
75 /* Closing session element */
76 ret = mi_lttng_writer_close_element(the_writer);
77 end:
78 return ret;
79 }
80
81 /*
82 * Mi print of load command
83 */
84 static int mi_load_print(const char *session_name)
85 {
86 int ret;
87 assert(the_writer);
88
89 if (the_opt_load_all) {
90 /* We use a wildcard to represent all sessions */
91 session_name = "*";
92 }
93
94 /* Print load element */
95 ret = mi_lttng_writer_open_element(the_writer, mi_lttng_element_load);
96 if (ret) {
97 goto end;
98 }
99
100 /* Print session element */
101 ret = mi_partial_session(session_name);
102 if (ret) {
103 goto end;
104 }
105
106 /* Path element */
107 if (the_opt_input_path) {
108 ret = mi_lttng_writer_write_element_string(the_writer, config_element_path,
109 the_opt_input_path);
110 if (ret) {
111 goto end;
112 }
113 }
114
115 /* Print override elements */
116 ret = mi_lttng_writer_open_element(the_writer, mi_lttng_element_load_overrides);
117 if (ret) {
118 goto end;
119 }
120
121 /* Session name override element */
122 if (the_opt_override_session_name) {
123 ret = mi_lttng_writer_write_element_string(the_writer,
124 config_element_name, the_opt_override_session_name);
125 if (ret) {
126 goto end;
127 }
128 }
129
130 /* Session url override element */
131 if (the_opt_override_url) {
132 ret = mi_lttng_writer_write_element_string(the_writer,
133 mi_lttng_element_load_override_url,
134 the_opt_override_url);
135 if (ret) {
136 goto end;
137 }
138 }
139
140 /* Close override and load element */
141 ret = mi_lttng_close_multi_element(the_writer, 2);
142 end:
143 return ret;
144 }
145
146 /*
147 * The 'load <options>' first level command
148 */
149 int cmd_load(int argc, const char **argv)
150 {
151 int ret, success;
152 int opt;
153 poptContext pc;
154 struct lttng_load_session_attr *session_attr = NULL;
155 char *input_path = NULL;
156 const char *leftover = NULL;
157
158 pc = poptGetContext(NULL, argc, argv, the_load_opts, 0);
159 poptReadDefaultConfig(pc, 0);
160
161 while ((opt = poptGetNextOpt(pc)) != -1) {
162 switch (opt) {
163 case OPT_HELP:
164 SHOW_HELP();
165 ret = CMD_SUCCESS;
166 goto end;
167 case OPT_ALL:
168 the_opt_load_all = 1;
169 break;
170 case OPT_LIST_OPTIONS:
171 list_cmd_options(stdout, the_load_opts);
172 ret = CMD_SUCCESS;
173 goto end;
174 case OPT_FORCE:
175 the_opt_force = 1;
176 break;
177 default:
178 ret = CMD_UNDEFINED;
179 goto end;
180 }
181 }
182
183 ret = lttng_session_daemon_alive();
184 if (!ret) {
185 ERR("No session daemon is available");
186 ret = CMD_ERROR;
187 goto end;
188 }
189
190 if (!the_opt_load_all) {
191 the_session_name = poptGetArg(pc);
192 if (the_session_name) {
193 DBG2("Loading session name: %s", the_session_name);
194 } else {
195 /* Default to load_all */
196 the_opt_load_all = 1;
197 }
198 }
199
200 leftover = poptGetArg(pc);
201 if (leftover) {
202 ERR("Unknown argument: %s", leftover);
203 ret = CMD_ERROR;
204 goto end;
205 }
206
207 /* Mi check */
208 if (lttng_opt_mi) {
209 the_writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
210 if (!the_writer) {
211 ret = CMD_ERROR;
212 goto end;
213 }
214
215 /* Open command element */
216 ret = mi_lttng_writer_command_open(the_writer,
217 mi_lttng_element_command_load);
218 if (ret) {
219 ret = CMD_ERROR;
220 goto end;
221 }
222
223 /* Open output element */
224 ret = mi_lttng_writer_open_element(the_writer,
225 mi_lttng_element_command_output);
226 if (ret) {
227 ret = CMD_ERROR;
228 goto end;
229 }
230 }
231
232 /* Prepare load attributes */
233 session_attr = lttng_load_session_attr_create();
234 if (!session_attr) {
235 ERR("Failed to create load session attributes");
236 ret = CMD_ERROR;
237 goto end;
238 }
239
240 /*
241 * Set the input url
242 * lttng_load_session_attr_set_input_url only suppports absolute path.
243 * Use realpath to resolve any relative path.
244 * */
245 if (the_opt_input_path) {
246 input_path = realpath(the_opt_input_path, NULL);
247 if (!input_path) {
248 PERROR("Invalid input path");
249 ret = CMD_ERROR;
250 goto end;
251 }
252 } else {
253 input_path = NULL;
254 }
255
256 ret = lttng_load_session_attr_set_input_url(session_attr,
257 input_path);
258 if (ret) {
259 ERR("Invalid input path");
260 ret = CMD_ERROR;
261 goto end;
262 }
263
264 /* Set the session name. NULL means all sessions should be loaded */
265 ret = lttng_load_session_attr_set_session_name(session_attr,
266 the_session_name);
267 if (ret) {
268 ERR("Invalid session name");
269 ret = CMD_ERROR;
270 goto end;
271 }
272
273 /* Set the overwrite attribute */
274 ret = lttng_load_session_attr_set_overwrite(session_attr, the_opt_force);
275 if (ret) {
276 ERR("Force argument could not be applied");
277 ret = CMD_ERROR;
278 goto end;
279 }
280
281 /* Set the overrides attributes if any */
282 if (the_opt_override_url) {
283 ret = lttng_load_session_attr_set_override_url(session_attr,
284 the_opt_override_url);
285 if (ret) {
286 ERR("Url override is invalid");
287 goto end;
288 }
289 }
290
291 if (the_opt_override_session_name) {
292 if (the_opt_load_all) {
293 ERR("Options --all and --override-name cannot be used simultaneously");
294 ret = CMD_ERROR;
295 goto end;
296 }
297 ret = lttng_load_session_attr_set_override_session_name(session_attr,
298 the_opt_override_session_name);
299 if (ret) {
300 ERR("Failed to set session name override");
301 ret = CMD_ERROR;
302 goto end;
303 }
304 }
305
306 ret = lttng_load_session(session_attr);
307 if (ret) {
308 ERR("%s", lttng_strerror(ret));
309 success = 0;
310 ret = CMD_ERROR;
311 } else {
312 if (the_opt_load_all) {
313 MSG("All sessions have been loaded successfully");
314 } else if (the_session_name) {
315 ret = config_init((char *) the_session_name);
316 if (ret < 0) {
317 WARN("Could not set %s as the default session",
318 the_session_name);
319 }
320 MSG("Session %s has been loaded successfully", the_session_name);
321 } else {
322 MSG("Session has been loaded successfully");
323 }
324
325 if (the_opt_override_session_name) {
326 MSG("Session name overridden with %s",
327 the_opt_override_session_name);
328 }
329
330 if (the_opt_override_url) {
331 MSG("Session output url overridden with %s", the_opt_override_url);
332 }
333 success = 1;
334 ret = CMD_SUCCESS;
335 }
336
337 /* Mi Printing and closing */
338 if (lttng_opt_mi) {
339 /* Mi print */
340 ret = mi_load_print(the_session_name);
341 if (ret) {
342 ret = CMD_ERROR;
343 goto end;
344 }
345
346 /* Close output element */
347 ret = mi_lttng_writer_close_element(the_writer);
348 if (ret) {
349 ret = CMD_ERROR;
350 goto end;
351 }
352
353 /* Success ? */
354 ret = mi_lttng_writer_write_element_bool(the_writer,
355 mi_lttng_element_command_success, success);
356 if (ret) {
357 ret = CMD_ERROR;
358 goto end;
359 }
360
361 /* Command element close */
362 ret = mi_lttng_writer_command_close(the_writer);
363 if (ret) {
364 ret = CMD_ERROR;
365 goto end;
366 }
367 }
368 end:
369 if (the_writer && mi_lttng_writer_destroy(the_writer)) {
370 ERR("Failed to destroy mi lttng writer");
371 }
372
373 lttng_load_session_attr_destroy(session_attr);
374 free(input_path);
375 poptFreeContext(pc);
376 return ret;
377 }
This page took 0.039774 seconds and 4 git commands to generate.