2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
12 #include <common/sessiond-comm/sessiond-comm.h>
13 #include <lttng/lttng-error.h>
14 #include <lttng/snapshot.h>
15 #include <lttng/snapshot-internal.h>
17 #include "lttng-ctl-helper.h"
20 * Add an output object to a session identified by name.
22 * Return 0 on success or else a negative LTTNG_ERR code.
24 int lttng_snapshot_add_output(const char *session_name
,
25 struct lttng_snapshot_output
*output
)
28 struct lttcomm_session_msg lsm
;
29 struct lttcomm_lttng_output_id
*reply
;
31 if (!session_name
|| !output
) {
32 return -LTTNG_ERR_INVALID
;
35 memset(&lsm
, 0, sizeof(lsm
));
36 lsm
.cmd_type
= LTTNG_SNAPSHOT_ADD_OUTPUT
;
38 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
39 sizeof(lsm
.session
.name
));
40 memcpy(&lsm
.u
.snapshot_output
.output
, output
,
41 sizeof(lsm
.u
.snapshot_output
.output
));
43 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &reply
);
48 output
->id
= reply
->id
;
55 * Delete an output object to a session identified by name.
57 * Return 0 on success or else a negative LTTNG_ERR code.
59 int lttng_snapshot_del_output(const char *session_name
,
60 struct lttng_snapshot_output
*output
)
62 struct lttcomm_session_msg lsm
;
64 if (!session_name
|| !output
) {
65 return -LTTNG_ERR_INVALID
;
68 memset(&lsm
, 0, sizeof(lsm
));
69 lsm
.cmd_type
= LTTNG_SNAPSHOT_DEL_OUTPUT
;
71 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
72 sizeof(lsm
.session
.name
));
73 memcpy(&lsm
.u
.snapshot_output
.output
, output
,
74 sizeof(lsm
.u
.snapshot_output
.output
));
76 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
80 * List all snapshot output(s) of a session identified by name. The output list
81 * object is populated and can be iterated over with the get_next call below.
83 * Return 0 on success or else a negative LTTNG_ERR code and the list pointer
86 int lttng_snapshot_list_output(const char *session_name
,
87 struct lttng_snapshot_output_list
**list
)
90 struct lttcomm_session_msg lsm
;
91 struct lttng_snapshot_output_list
*new_list
= NULL
;
93 if (!session_name
|| !list
) {
94 ret
= -LTTNG_ERR_INVALID
;
98 memset(&lsm
, 0, sizeof(lsm
));
99 lsm
.cmd_type
= LTTNG_SNAPSHOT_LIST_OUTPUT
;
101 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
102 sizeof(lsm
.session
.name
));
104 new_list
= zmalloc(sizeof(*new_list
));
106 ret
= -LTTNG_ERR_NOMEM
;
110 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &new_list
->array
);
115 new_list
->count
= ret
/ sizeof(struct lttng_snapshot_output
);
126 * Return the next available snapshot output object in the given list. A list
127 * output command MUST have been done before.
129 * Return the next object on success or else NULL indicating the end of the
132 struct lttng_snapshot_output
*lttng_snapshot_output_list_get_next(
133 struct lttng_snapshot_output_list
*list
)
135 struct lttng_snapshot_output
*output
= NULL
;
141 /* We've reached the end. */
142 if (list
->index
== list
->count
) {
146 output
= &list
->array
[list
->index
];
155 * Free an output list object.
157 void lttng_snapshot_output_list_destroy(struct lttng_snapshot_output_list
*list
)
168 * Snapshot a trace for the given session.
170 * The output object can be NULL but an add output MUST be done prior to this
171 * call. If it's not NULL, it will be used to snapshot a trace.
173 * The wait parameter is ignored for now. The snapshot record command will
174 * ALWAYS wait for the snapshot to complete before returning meaning the
175 * snapshot has been written on disk or streamed over the network to a relayd.
177 * Return 0 on success or else a negative LTTNG_ERR value.
179 int lttng_snapshot_record(const char *session_name
,
180 struct lttng_snapshot_output
*output
, int wait
)
182 struct lttcomm_session_msg lsm
;
185 return -LTTNG_ERR_INVALID
;
188 memset(&lsm
, 0, sizeof(lsm
));
189 lsm
.cmd_type
= LTTNG_SNAPSHOT_RECORD
;
191 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
192 sizeof(lsm
.session
.name
));
195 * Not having an output object will use the default one of the session that
196 * would need to be set by a call to add output prior to calling snapshot
200 memcpy(&lsm
.u
.snapshot_record
.output
, output
,
201 sizeof(lsm
.u
.snapshot_record
.output
));
204 /* The wait param is ignored. */
206 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
210 * Return an newly allocated snapshot output object or NULL on error.
212 struct lttng_snapshot_output
*lttng_snapshot_output_create(void)
214 struct lttng_snapshot_output
*output
;
216 output
= zmalloc(sizeof(struct lttng_snapshot_output
));
221 output
->max_size
= (uint64_t) -1ULL;
228 * Free a given snapshot output object.
230 void lttng_snapshot_output_destroy(struct lttng_snapshot_output
*obj
)
238 * Getter family functions of snapshot output.
241 uint32_t lttng_snapshot_output_get_id(struct lttng_snapshot_output
*output
)
246 const char *lttng_snapshot_output_get_name(
247 struct lttng_snapshot_output
*output
)
252 const char *lttng_snapshot_output_get_data_url(struct lttng_snapshot_output
*output
)
254 return output
->data_url
;
257 const char *lttng_snapshot_output_get_ctrl_url(struct lttng_snapshot_output
*output
)
259 return output
->ctrl_url
;
262 uint64_t lttng_snapshot_output_get_maxsize(
263 struct lttng_snapshot_output
*output
)
265 return output
->max_size
;
269 * Setter family functions for snapshot output.
272 int lttng_snapshot_output_set_id(uint32_t id
,
273 struct lttng_snapshot_output
*output
)
275 if (!output
|| id
== 0) {
276 return -LTTNG_ERR_INVALID
;
283 int lttng_snapshot_output_set_size(uint64_t size
,
284 struct lttng_snapshot_output
*output
)
287 return -LTTNG_ERR_INVALID
;
290 output
->max_size
= size
;
294 int lttng_snapshot_output_set_name(const char *name
,
295 struct lttng_snapshot_output
*output
)
297 if (!output
|| !name
) {
298 return -LTTNG_ERR_INVALID
;
301 lttng_ctl_copy_string(output
->name
, name
, sizeof(output
->name
));
305 int lttng_snapshot_output_set_ctrl_url(const char *url
,
306 struct lttng_snapshot_output
*output
)
308 if (!output
|| !url
) {
309 return -LTTNG_ERR_INVALID
;
312 lttng_ctl_copy_string(output
->ctrl_url
, url
, sizeof(output
->ctrl_url
));
316 int lttng_snapshot_output_set_data_url(const char *url
,
317 struct lttng_snapshot_output
*output
)
319 if (!output
|| !url
) {
320 return -LTTNG_ERR_INVALID
;
323 lttng_ctl_copy_string(output
->data_url
, url
, sizeof(output
->data_url
));