2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library 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 Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <lttng/lttng-error.h>
24 #include <lttng/snapshot.h>
25 #include <lttng/snapshot-internal.h>
27 #include "lttng-ctl-helper.h"
30 * Add an output object to a session identified by name.
32 * Return 0 on success or else a negative LTTNG_ERR code.
34 int lttng_snapshot_add_output(const char *session_name
,
35 struct lttng_snapshot_output
*output
)
38 struct lttcomm_session_msg lsm
;
39 struct lttcomm_lttng_output_id
*reply
;
41 if (!session_name
|| !output
) {
42 return -LTTNG_ERR_INVALID
;
45 memset(&lsm
, 0, sizeof(lsm
));
46 lsm
.cmd_type
= LTTNG_SNAPSHOT_ADD_OUTPUT
;
48 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
49 sizeof(lsm
.session
.name
));
50 memcpy(&lsm
.u
.snapshot_output
.output
, output
,
51 sizeof(lsm
.u
.snapshot_output
.output
));
53 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &reply
);
58 output
->id
= reply
->id
;
65 * Delete an output object to a session identified by name.
67 * Return 0 on success or else a negative LTTNG_ERR code.
69 int lttng_snapshot_del_output(const char *session_name
,
70 struct lttng_snapshot_output
*output
)
72 struct lttcomm_session_msg lsm
;
74 if (!session_name
|| !output
) {
75 return -LTTNG_ERR_INVALID
;
78 memset(&lsm
, 0, sizeof(lsm
));
79 lsm
.cmd_type
= LTTNG_SNAPSHOT_DEL_OUTPUT
;
81 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
82 sizeof(lsm
.session
.name
));
83 memcpy(&lsm
.u
.snapshot_output
.output
, output
,
84 sizeof(lsm
.u
.snapshot_output
.output
));
86 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
90 * List all snapshot output(s) of a session identified by name. The output list
91 * object is populated and can be iterated over with the get_next call below.
93 * Return 0 on success or else a negative LTTNG_ERR code and the list pointer
96 int lttng_snapshot_list_output(const char *session_name
,
97 struct lttng_snapshot_output_list
**list
)
100 struct lttcomm_session_msg lsm
;
101 struct lttng_snapshot_output_list
*new_list
= NULL
;
103 if (!session_name
|| !list
) {
104 ret
= -LTTNG_ERR_INVALID
;
108 memset(&lsm
, 0, sizeof(lsm
));
109 lsm
.cmd_type
= LTTNG_SNAPSHOT_LIST_OUTPUT
;
111 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
112 sizeof(lsm
.session
.name
));
114 new_list
= zmalloc(sizeof(*new_list
));
116 ret
= -LTTNG_ERR_NOMEM
;
120 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &new_list
->array
);
125 new_list
->count
= ret
/ sizeof(struct lttng_snapshot_output
);
136 * Return the next available snapshot output object in the given list. A list
137 * output command MUST have been done before.
139 * Return the next object on success or else NULL indicating the end of the
142 struct lttng_snapshot_output
*lttng_snapshot_output_list_get_next(
143 struct lttng_snapshot_output_list
*list
)
145 struct lttng_snapshot_output
*output
= NULL
;
151 /* We've reached the end. */
152 if (list
->index
== list
->count
) {
156 output
= &list
->array
[list
->index
];
165 * Free an output list object.
167 void lttng_snapshot_output_list_destroy(struct lttng_snapshot_output_list
*list
)
178 * Snapshot a trace for the given session.
180 * The output object can be NULL but an add output MUST be done prior to this
181 * call. If it's not NULL, it will be used to snapshot a trace.
183 * The wait parameter is ignored for now. The snapshot record command will
184 * ALWAYS wait for the snapshot to complete before returning meaning the
185 * snapshot has been written on disk or streamed over the network to a relayd.
187 * Return 0 on success or else a negative LTTNG_ERR value.
189 int lttng_snapshot_record(const char *session_name
,
190 struct lttng_snapshot_output
*output
, int wait
)
192 struct lttcomm_session_msg lsm
;
195 return -LTTNG_ERR_INVALID
;
198 memset(&lsm
, 0, sizeof(lsm
));
199 lsm
.cmd_type
= LTTNG_SNAPSHOT_RECORD
;
201 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
202 sizeof(lsm
.session
.name
));
205 * Not having an output object will use the default one of the session that
206 * would need to be set by a call to add output prior to calling snapshot
210 memcpy(&lsm
.u
.snapshot_record
.output
, output
,
211 sizeof(lsm
.u
.snapshot_record
.output
));
214 /* The wait param is ignored. */
216 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
220 * Return an newly allocated snapshot output object or NULL on error.
222 struct lttng_snapshot_output
*lttng_snapshot_output_create(void)
224 struct lttng_snapshot_output
*output
;
226 output
= zmalloc(sizeof(struct lttng_snapshot_output
));
231 output
->max_size
= (uint64_t) -1ULL;
238 * Free a given snapshot output object.
240 void lttng_snapshot_output_destroy(struct lttng_snapshot_output
*obj
)
248 * Getter family functions of snapshot output.
251 uint32_t lttng_snapshot_output_get_id(struct lttng_snapshot_output
*output
)
256 const char *lttng_snapshot_output_get_name(
257 struct lttng_snapshot_output
*output
)
262 const char *lttng_snapshot_output_get_data_url(struct lttng_snapshot_output
*output
)
264 return output
->data_url
;
267 const char *lttng_snapshot_output_get_ctrl_url(struct lttng_snapshot_output
*output
)
269 return output
->ctrl_url
;
272 uint64_t lttng_snapshot_output_get_maxsize(
273 struct lttng_snapshot_output
*output
)
275 return output
->max_size
;
279 * Setter family functions for snapshot output.
282 int lttng_snapshot_output_set_id(uint32_t id
,
283 struct lttng_snapshot_output
*output
)
285 if (!output
|| id
== 0) {
286 return -LTTNG_ERR_INVALID
;
293 int lttng_snapshot_output_set_size(uint64_t size
,
294 struct lttng_snapshot_output
*output
)
297 return -LTTNG_ERR_INVALID
;
300 output
->max_size
= size
;
304 int lttng_snapshot_output_set_name(const char *name
,
305 struct lttng_snapshot_output
*output
)
307 if (!output
|| !name
) {
308 return -LTTNG_ERR_INVALID
;
311 lttng_ctl_copy_string(output
->name
, name
, sizeof(output
->name
));
315 int lttng_snapshot_output_set_ctrl_url(const char *url
,
316 struct lttng_snapshot_output
*output
)
318 if (!output
|| !url
) {
319 return -LTTNG_ERR_INVALID
;
322 lttng_ctl_copy_string(output
->ctrl_url
, url
, sizeof(output
->ctrl_url
));
326 int lttng_snapshot_output_set_data_url(const char *url
,
327 struct lttng_snapshot_output
*output
)
329 if (!output
|| !url
) {
330 return -LTTNG_ERR_INVALID
;
333 lttng_ctl_copy_string(output
->data_url
, url
, sizeof(output
->data_url
));