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 ret
= -LTTNG_ERR_INVALID
;
46 memset(&lsm
, 0, sizeof(lsm
));
47 lsm
.cmd_type
= LTTNG_SNAPSHOT_ADD_OUTPUT
;
49 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
50 sizeof(lsm
.session
.name
));
52 ret
= -LTTNG_ERR_INVALID
;
56 memcpy(&lsm
.u
.snapshot_output
.output
, output
,
57 sizeof(lsm
.u
.snapshot_output
.output
));
59 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &reply
);
64 output
->id
= reply
->id
;
72 * Delete an output object to a session identified by name.
74 * Return 0 on success or else a negative LTTNG_ERR code.
76 int lttng_snapshot_del_output(const char *session_name
,
77 struct lttng_snapshot_output
*output
)
80 struct lttcomm_session_msg lsm
;
82 if (!session_name
|| !output
) {
83 ret
= -LTTNG_ERR_INVALID
;
87 memset(&lsm
, 0, sizeof(lsm
));
88 lsm
.cmd_type
= LTTNG_SNAPSHOT_DEL_OUTPUT
;
90 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
91 sizeof(lsm
.session
.name
));
93 ret
= -LTTNG_ERR_INVALID
;
97 memcpy(&lsm
.u
.snapshot_output
.output
, output
,
98 sizeof(lsm
.u
.snapshot_output
.output
));
100 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
106 * List all snapshot output(s) of a session identified by name. The output list
107 * object is populated and can be iterated over with the get_next call below.
109 * Return 0 on success or else a negative LTTNG_ERR code and the list pointer
112 int lttng_snapshot_list_output(const char *session_name
,
113 struct lttng_snapshot_output_list
**list
)
116 struct lttcomm_session_msg lsm
;
117 struct lttng_snapshot_output_list
*new_list
= NULL
;
119 if (!session_name
|| !list
) {
120 ret
= -LTTNG_ERR_INVALID
;
124 memset(&lsm
, 0, sizeof(lsm
));
125 lsm
.cmd_type
= LTTNG_SNAPSHOT_LIST_OUTPUT
;
127 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
128 sizeof(lsm
.session
.name
));
130 ret
= -LTTNG_ERR_INVALID
;
134 new_list
= zmalloc(sizeof(*new_list
));
136 ret
= -LTTNG_ERR_NOMEM
;
140 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &new_list
->array
);
145 new_list
->count
= ret
/ sizeof(struct lttng_snapshot_output
);
156 * Return the next available snapshot output object in the given list. A list
157 * output command MUST have been done before.
159 * Return the next object on success or else NULL indicating the end of the
162 struct lttng_snapshot_output
*lttng_snapshot_output_list_get_next(
163 struct lttng_snapshot_output_list
*list
)
165 struct lttng_snapshot_output
*output
= NULL
;
171 /* We've reached the end. */
172 if (list
->index
== list
->count
) {
176 output
= &list
->array
[list
->index
];
185 * Free an output list object.
187 void lttng_snapshot_output_list_destroy(struct lttng_snapshot_output_list
*list
)
198 * Snapshot a trace for the given session.
200 * The output object can be NULL but an add output MUST be done prior to this
201 * call. If it's not NULL, it will be used to snapshot a trace.
203 * The wait parameter is ignored for now. The snapshot record command will
204 * ALWAYS wait for the snapshot to complete before returning meaning the
205 * snapshot has been written on disk or streamed over the network to a relayd.
207 * Return 0 on success or else a negative LTTNG_ERR value.
209 int lttng_snapshot_record(const char *session_name
,
210 struct lttng_snapshot_output
*output
, int wait
)
213 struct lttcomm_session_msg lsm
;
216 ret
= -LTTNG_ERR_INVALID
;
220 memset(&lsm
, 0, sizeof(lsm
));
221 lsm
.cmd_type
= LTTNG_SNAPSHOT_RECORD
;
223 ret
= lttng_strncpy(lsm
.session
.name
, session_name
,
224 sizeof(lsm
.session
.name
));
226 ret
= -LTTNG_ERR_INVALID
;
231 * Not having an output object will use the default one of the session that
232 * would need to be set by a call to add output prior to calling snapshot
236 memcpy(&lsm
.u
.snapshot_record
.output
, output
,
237 sizeof(lsm
.u
.snapshot_record
.output
));
240 /* The wait param is ignored. */
241 ret
= lttng_ctl_ask_sessiond(&lsm
, NULL
);
247 * Return an newly allocated snapshot output object or NULL on error.
249 struct lttng_snapshot_output
*lttng_snapshot_output_create(void)
251 struct lttng_snapshot_output
*output
;
253 output
= zmalloc(sizeof(struct lttng_snapshot_output
));
258 output
->max_size
= (uint64_t) -1ULL;
265 * Free a given snapshot output object.
267 void lttng_snapshot_output_destroy(struct lttng_snapshot_output
*obj
)
275 * Getter family functions of snapshot output.
278 uint32_t lttng_snapshot_output_get_id(struct lttng_snapshot_output
*output
)
283 const char *lttng_snapshot_output_get_name(
284 struct lttng_snapshot_output
*output
)
289 const char *lttng_snapshot_output_get_data_url(struct lttng_snapshot_output
*output
)
291 return output
->data_url
;
294 const char *lttng_snapshot_output_get_ctrl_url(struct lttng_snapshot_output
*output
)
296 return output
->ctrl_url
;
299 uint64_t lttng_snapshot_output_get_maxsize(
300 struct lttng_snapshot_output
*output
)
302 return output
->max_size
;
306 * Setter family functions for snapshot output.
309 int lttng_snapshot_output_set_id(uint32_t id
,
310 struct lttng_snapshot_output
*output
)
312 if (!output
|| id
== 0) {
313 return -LTTNG_ERR_INVALID
;
320 int lttng_snapshot_output_set_size(uint64_t size
,
321 struct lttng_snapshot_output
*output
)
324 return -LTTNG_ERR_INVALID
;
327 output
->max_size
= size
;
331 int lttng_snapshot_output_set_name(const char *name
,
332 struct lttng_snapshot_output
*output
)
336 if (!output
|| !name
) {
337 ret
= -LTTNG_ERR_INVALID
;
341 ret
= lttng_strncpy(output
->name
, name
, sizeof(output
->name
));
343 ret
= -LTTNG_ERR_INVALID
;
351 int lttng_snapshot_output_set_ctrl_url(const char *url
,
352 struct lttng_snapshot_output
*output
)
356 if (!output
|| !url
) {
357 ret
= -LTTNG_ERR_INVALID
;
361 ret
= lttng_strncpy(output
->ctrl_url
, url
, sizeof(output
->ctrl_url
));
363 ret
= -LTTNG_ERR_INVALID
;
371 int lttng_snapshot_output_set_data_url(const char *url
,
372 struct lttng_snapshot_output
*output
)
376 if (!output
|| !url
) {
377 ret
= -LTTNG_ERR_INVALID
;
381 ret
= lttng_strncpy(output
->data_url
, url
, sizeof(output
->data_url
));
383 ret
= -LTTNG_ERR_INVALID
;