2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
11 #include <common/sessiond-comm/sessiond-comm.hpp>
12 #include <lttng/lttng-error.h>
13 #include <lttng/snapshot.h>
14 #include <lttng/snapshot-internal.hpp>
16 #include "lttng-ctl-helper.hpp"
19 * Add an output object to a session identified by name.
21 * Return 0 on success or else a negative LTTNG_ERR code.
23 int lttng_snapshot_add_output(const char *session_name
,
24 struct lttng_snapshot_output
*output
)
27 struct lttcomm_session_msg lsm
;
28 struct lttcomm_lttng_output_id
*reply
;
30 if (!session_name
|| !output
) {
31 ret
= -LTTNG_ERR_INVALID
;
35 memset(&lsm
, 0, sizeof(lsm
));
36 lsm
.cmd_type
= LTTNG_SNAPSHOT_ADD_OUTPUT
;
38 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
39 sizeof(lsm
.session
.name
));
41 ret
= -LTTNG_ERR_INVALID
;
45 memcpy(&lsm
.u
.snapshot_output
.output
, output
,
46 sizeof(lsm
.u
.snapshot_output
.output
));
48 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &reply
);
53 output
->id
= reply
->id
;
61 * Delete an output object to a session identified by name.
63 * Return 0 on success or else a negative LTTNG_ERR code.
65 int lttng_snapshot_del_output(const char *session_name
,
66 struct lttng_snapshot_output
*output
)
69 struct lttcomm_session_msg lsm
;
71 if (!session_name
|| !output
) {
72 ret
= -LTTNG_ERR_INVALID
;
76 memset(&lsm
, 0, sizeof(lsm
));
77 lsm
.cmd_type
= LTTNG_SNAPSHOT_DEL_OUTPUT
;
79 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
80 sizeof(lsm
.session
.name
));
82 ret
= -LTTNG_ERR_INVALID
;
86 memcpy(&lsm
.u
.snapshot_output
.output
, output
,
87 sizeof(lsm
.u
.snapshot_output
.output
));
89 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
95 * List all snapshot output(s) of a session identified by name. The output list
96 * object is populated and can be iterated over with the get_next call below.
98 * Return 0 on success or else a negative LTTNG_ERR code and the list pointer
101 int lttng_snapshot_list_output(const char *session_name
,
102 struct lttng_snapshot_output_list
**list
)
105 struct lttcomm_session_msg lsm
;
106 struct lttng_snapshot_output_list
*new_list
= NULL
;
108 if (!session_name
|| !list
) {
109 ret
= -LTTNG_ERR_INVALID
;
113 memset(&lsm
, 0, sizeof(lsm
));
114 lsm
.cmd_type
= LTTNG_SNAPSHOT_LIST_OUTPUT
;
116 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
117 sizeof(lsm
.session
.name
));
119 ret
= -LTTNG_ERR_INVALID
;
123 new_list
= zmalloc
<lttng_snapshot_output_list
>();
125 ret
= -LTTNG_ERR_NOMEM
;
129 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &new_list
->array
);
134 new_list
->count
= ret
/ sizeof(struct lttng_snapshot_output
);
145 * Return the next available snapshot output object in the given list. A list
146 * output command MUST have been done before.
148 * Return the next object on success or else NULL indicating the end of the
151 struct lttng_snapshot_output
*lttng_snapshot_output_list_get_next(
152 struct lttng_snapshot_output_list
*list
)
154 struct lttng_snapshot_output
*output
= NULL
;
160 /* We've reached the end. */
161 if (list
->index
== list
->count
) {
165 output
= &list
->array
[list
->index
];
174 * Free an output list object.
176 void lttng_snapshot_output_list_destroy(struct lttng_snapshot_output_list
*list
)
187 * Snapshot a trace for the given session.
189 * The output object can be NULL but an add output MUST be done prior to this
190 * call. If it's not NULL, it will be used to snapshot a trace.
192 * The wait parameter is ignored for now. The snapshot record command will
193 * ALWAYS wait for the snapshot to complete before returning meaning the
194 * snapshot has been written on disk or streamed over the network to a relayd.
196 * Return 0 on success or else a negative LTTNG_ERR value.
198 int lttng_snapshot_record(const char *session_name
,
199 struct lttng_snapshot_output
*output
,
200 int wait
__attribute__((unused
)))
203 struct lttcomm_session_msg lsm
;
206 ret
= -LTTNG_ERR_INVALID
;
210 memset(&lsm
, 0, sizeof(lsm
));
211 lsm
.cmd_type
= LTTNG_SNAPSHOT_RECORD
;
213 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
214 sizeof(lsm
.session
.name
));
216 ret
= -LTTNG_ERR_INVALID
;
221 * Not having an output object will use the default one of the session that
222 * would need to be set by a call to add output prior to calling snapshot
226 memcpy(&lsm
.u
.snapshot_record
.output
, output
,
227 sizeof(lsm
.u
.snapshot_record
.output
));
230 /* The wait param is ignored. */
231 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
237 * Return an newly allocated snapshot output object or NULL on error.
239 struct lttng_snapshot_output
*lttng_snapshot_output_create(void)
241 struct lttng_snapshot_output
*output
;
243 output
= zmalloc
<lttng_snapshot_output
>();
248 output
->max_size
= (uint64_t) -1ULL;
255 * Free a given snapshot output object.
257 void lttng_snapshot_output_destroy(struct lttng_snapshot_output
*obj
)
265 * Getter family functions of snapshot output.
268 uint32_t lttng_snapshot_output_get_id(const struct lttng_snapshot_output
*output
)
273 const char *lttng_snapshot_output_get_name(
274 const struct lttng_snapshot_output
*output
)
279 const char *lttng_snapshot_output_get_data_url(const struct lttng_snapshot_output
*output
)
281 return output
->data_url
;
284 const char *lttng_snapshot_output_get_ctrl_url(const struct lttng_snapshot_output
*output
)
286 return output
->ctrl_url
;
289 uint64_t lttng_snapshot_output_get_maxsize(
290 const struct lttng_snapshot_output
*output
)
292 return output
->max_size
;
296 * Setter family functions for snapshot output.
299 int lttng_snapshot_output_set_id(uint32_t id
,
300 struct lttng_snapshot_output
*output
)
302 if (!output
|| id
== 0) {
303 return -LTTNG_ERR_INVALID
;
310 int lttng_snapshot_output_set_size(uint64_t size
,
311 struct lttng_snapshot_output
*output
)
314 return -LTTNG_ERR_INVALID
;
317 output
->max_size
= size
;
321 int lttng_snapshot_output_set_name(const char *name
,
322 struct lttng_snapshot_output
*output
)
326 if (!output
|| !name
) {
327 ret
= -LTTNG_ERR_INVALID
;
331 ret
= lttng_strncpy(output
->name
, name
, sizeof(output
->name
));
333 ret
= -LTTNG_ERR_INVALID
;
341 int lttng_snapshot_output_set_ctrl_url(const char *url
,
342 struct lttng_snapshot_output
*output
)
346 if (!output
|| !url
) {
347 ret
= -LTTNG_ERR_INVALID
;
351 ret
= lttng_strncpy(output
->ctrl_url
, url
, sizeof(output
->ctrl_url
));
353 ret
= -LTTNG_ERR_INVALID
;
361 int lttng_snapshot_output_set_data_url(const char *url
,
362 struct lttng_snapshot_output
*output
)
366 if (!output
|| !url
) {
367 ret
= -LTTNG_ERR_INVALID
;
371 ret
= lttng_strncpy(output
->data_url
, url
, sizeof(output
->data_url
));
373 ret
= -LTTNG_ERR_INVALID
;
381 int lttng_snapshot_output_set_local_path(const char *path
,
382 struct lttng_snapshot_output
*output
)
385 struct lttng_uri
*uris
= NULL
;
388 if (!path
|| !output
) {
389 ret
= -LTTNG_ERR_INVALID
;
393 num_uris
= uri_parse_str_urls(path
, NULL
, &uris
);
395 ret
= -LTTNG_ERR_INVALID
;
399 if (uris
[0].dtype
!= LTTNG_DST_PATH
) {
400 ret
= -LTTNG_ERR_INVALID
;
404 ret
= lttng_strncpy(output
->ctrl_url
, path
, sizeof(output
->ctrl_url
));
406 ret
= -LTTNG_ERR_INVALID
;
415 int lttng_snapshot_output_set_network_url(const char *url
,
416 struct lttng_snapshot_output
*output
)
419 struct lttng_uri
*uris
= NULL
;
422 if (!url
|| !output
) {
423 ret
= -LTTNG_ERR_INVALID
;
427 num_uris
= uri_parse_str_urls(url
, NULL
, &uris
);
429 ret
= -LTTNG_ERR_INVALID
;
433 if (uris
[0].dtype
!= LTTNG_DST_IPV4
&&
434 uris
[0].dtype
!= LTTNG_DST_IPV6
) {
435 ret
= -LTTNG_ERR_INVALID
;
439 if (uris
[1].dtype
!= LTTNG_DST_IPV4
&&
440 uris
[1].dtype
!= LTTNG_DST_IPV6
) {
441 ret
= -LTTNG_ERR_INVALID
;
445 ret
= lttng_strncpy(output
->ctrl_url
, url
, sizeof(output
->ctrl_url
));
447 ret
= -LTTNG_ERR_INVALID
;
456 int lttng_snapshot_output_set_network_urls(
457 const char *ctrl_url
, const char *data_url
,
458 struct lttng_snapshot_output
*output
)
461 struct lttng_uri
*uris
= NULL
;
464 if (!ctrl_url
|| !data_url
|| !output
) {
465 ret
= -LTTNG_ERR_INVALID
;
469 num_uris
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
471 ret
= -LTTNG_ERR_INVALID
;
475 if (uris
[0].dtype
!= LTTNG_DST_IPV4
&&
476 uris
[0].dtype
!= LTTNG_DST_IPV6
) {
477 ret
= -LTTNG_ERR_INVALID
;
481 if (uris
[1].dtype
!= LTTNG_DST_IPV4
&&
482 uris
[1].dtype
!= LTTNG_DST_IPV6
) {
483 ret
= -LTTNG_ERR_INVALID
;
487 ret
= lttng_strncpy(output
->ctrl_url
, ctrl_url
, sizeof(output
->ctrl_url
));
489 ret
= -LTTNG_ERR_INVALID
;
493 ret
= lttng_strncpy(output
->data_url
, data_url
, sizeof(output
->data_url
));
495 ret
= -LTTNG_ERR_INVALID
;