Kernel ioctl to regenerate the statedump
[lttng-tools.git] / src / bin / lttng / commands / load.c
CommitLineData
8c42d845
JG
1/*
2 * Copyright (C) 2014 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
6c1c0768 18#define _LGPL_SOURCE
8c42d845
JG
19#include <inttypes.h>
20#include <popt.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <assert.h>
25
1734c658 26#include <common/mi-lttng.h>
f40ef1d5 27#include <common/config/session-config.h>
1734c658 28
8c42d845
JG
29#include "../command.h"
30
31static char *opt_input_path;
8c42d845
JG
32static int opt_force;
33static int opt_load_all;
34
11143783
DG
35static const char *session_name;
36
8c42d845
JG
37enum {
38 OPT_HELP = 1,
39 OPT_ALL,
40 OPT_FORCE,
13a810d5 41 OPT_LIST_OPTIONS,
8c42d845
JG
42};
43
1734c658
JRJ
44static struct mi_writer *writer;
45
8c42d845
JG
46static struct poptOption load_opts[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
49 {"all", 'a', POPT_ARG_NONE, 0, OPT_ALL, 0, 0},
8c42d845
JG
50 {"input-path", 'i', POPT_ARG_STRING, &opt_input_path, 0, 0, 0},
51 {"force", 'f', POPT_ARG_NONE, 0, OPT_FORCE, 0, 0},
13a810d5 52 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
8c42d845
JG
53 {0, 0, 0, 0, 0, 0, 0}
54};
55
1734c658
JRJ
56static int mi_partial_session(const char *session_name)
57{
58 int ret;
59 assert(writer);
60 assert(session_name);
61
62 /* Open session element */
63 ret = mi_lttng_writer_open_element(writer, config_element_session);
64 if (ret) {
65 goto end;
66 }
67
68 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
69 session_name);
70 if (ret) {
71 goto end;
72 }
73
74 /* Closing session element */
75 ret = mi_lttng_writer_close_element(writer);
76end:
77 return ret;
78}
79
80/*
81 * Mi print of load command
82 */
83static int mi_load_print(const char *session_name)
84{
85 int ret;
86 assert(writer);
87
88 if (opt_load_all) {
89 /* We use a wildcard to represent all sessions */
90 session_name = "*";
91 }
92
93 /* Print load element */
94 ret = mi_lttng_writer_open_element(writer, mi_lttng_element_load);
95 if (ret) {
96 goto end;
97 }
98
99 /* Print session element */
100 ret = mi_partial_session(session_name);
101 if (ret) {
102 goto end;
103 }
104
105 /* Path element */
106 if (opt_input_path) {
107 ret = mi_lttng_writer_write_element_string(writer, config_element_path,
108 opt_input_path);
109 if (ret) {
110 goto end;
111 }
112 }
113
114 /* Close load element */
115 ret = mi_lttng_writer_close_element(writer);
116
117end:
118 return ret;
119}
120
8c42d845
JG
121/*
122 * The 'load <options>' first level command
123 */
124int cmd_load(int argc, const char **argv)
125{
1734c658 126 int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success;
8c42d845
JG
127 int opt;
128 poptContext pc;
129
130 pc = poptGetContext(NULL, argc, argv, load_opts, 0);
131 poptReadDefaultConfig(pc, 0);
132
133 while ((opt = poptGetNextOpt(pc)) != -1) {
134 switch (opt) {
135 case OPT_HELP:
4ba92f18 136 SHOW_HELP();
8c42d845
JG
137 goto end;
138 case OPT_ALL:
139 opt_load_all = 1;
140 break;
13a810d5
DG
141 case OPT_LIST_OPTIONS:
142 list_cmd_options(stdout, load_opts);
143 goto end;
8c42d845
JG
144 case OPT_FORCE:
145 opt_force = 1;
146 break;
147 default:
8c42d845
JG
148 ret = CMD_UNDEFINED;
149 goto end;
150 }
151 }
152
11143783
DG
153 if (!opt_load_all) {
154 session_name = poptGetArg(pc);
732b768a
DG
155 if (session_name) {
156 DBG2("Loading session name: %s", session_name);
1734c658
JRJ
157 } else {
158 /* Default to load_all */
159 opt_load_all = 1;
11143783 160 }
8c42d845
JG
161 }
162
1734c658
JRJ
163 /* Mi check */
164 if (lttng_opt_mi) {
165 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
166 if (!writer) {
167 ret = -LTTNG_ERR_NOMEM;
168 goto end;
169 }
170
171 /* Open command element */
172 ret = mi_lttng_writer_command_open(writer,
173 mi_lttng_element_command_load);
174 if (ret) {
175 ret = CMD_ERROR;
176 goto end;
177 }
178
179 /* Open output element */
180 ret = mi_lttng_writer_open_element(writer,
181 mi_lttng_element_command_output);
182 if (ret) {
183 ret = CMD_ERROR;
184 goto end;
185 }
186 }
187
188 command_ret = config_load_session(opt_input_path, session_name, opt_force, 0);
189 if (command_ret) {
190 ERR("%s", lttng_strerror(command_ret));
191 success = 0;
279d6193
DG
192 } else {
193 if (opt_load_all) {
194 MSG("All sessions have been loaded successfully");
732b768a 195 } else if (session_name) {
8e525b95
DG
196 ret = config_init((char *)session_name);
197 if (ret < 0) {
198 ret = CMD_WARNING;
199 }
279d6193 200 MSG("Session %s has been loaded successfully", session_name);
732b768a
DG
201 } else {
202 MSG("Session has been loaded successfully");
279d6193 203 }
1734c658
JRJ
204 success = 1;
205 }
206
207 /* Mi Printing and closing */
208 if (lttng_opt_mi) {
209 /* Mi print */
210 ret = mi_load_print(session_name);
211 if (ret) {
212 ret = CMD_ERROR;
213 goto end;
214 }
215
216 /* Close output element */
217 ret = mi_lttng_writer_close_element(writer);
218 if (ret) {
219 ret = CMD_ERROR;
220 goto end;
221 }
222
223 /* Success ? */
224 ret = mi_lttng_writer_write_element_bool(writer,
225 mi_lttng_element_command_success, success);
226 if (ret) {
227 ret = CMD_ERROR;
228 goto end;
229 }
230
231 /* Command element close */
232 ret = mi_lttng_writer_command_close(writer);
233 if (ret) {
234 ret = CMD_ERROR;
235 goto end;
236 }
8c42d845
JG
237 }
238end:
1734c658
JRJ
239 if (writer && mi_lttng_writer_destroy(writer)) {
240 /* Preserve original error code */
241 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
242 }
243
244 /* Overwrite ret if the was an error with the load command */
245 ret = command_ret ? -command_ret : ret;
246
8c42d845
JG
247 poptFreeContext(pc);
248 return ret;
249}
This page took 0.039879 seconds and 4 git commands to generate.