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
23 #include <common/sessiond-comm/sessiond-comm.h>
24 #include <lttng/lttng-error.h>
25 #include <lttng/snapshot.h>
26 #include <lttng/snapshot-internal.h>
28 #include "lttng-ctl-helper.h"
31 * Add an output object to a session identified by name.
33 * Return 0 on success or else a negative LTTNG_ERR code.
35 int lttng_snapshot_add_output(const char *session_name
,
36 struct lttng_snapshot_output
*output
)
39 struct lttcomm_session_msg lsm
;
40 struct lttcomm_lttng_output_id
*reply
;
42 if (!session_name
|| !output
) {
43 return -LTTNG_ERR_INVALID
;
46 memset(&lsm
, 0, sizeof(lsm
));
47 lsm
.cmd_type
= LTTNG_SNAPSHOT_ADD_OUTPUT
;
49 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
50 sizeof(lsm
.session
.name
));
51 memcpy(&lsm
.u
.snapshot_output
.output
, output
,
52 sizeof(lsm
.u
.snapshot_output
.output
));
54 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &reply
);
59 output
->id
= reply
->id
;
66 * Delete an output object to a session identified by name.
68 * Return 0 on success or else a negative LTTNG_ERR code.
70 int lttng_snapshot_del_output(const char *session_name
,
71 struct lttng_snapshot_output
*output
)
73 struct lttcomm_session_msg lsm
;
75 if (!session_name
|| !output
) {
76 return -LTTNG_ERR_INVALID
;
79 memset(&lsm
, 0, sizeof(lsm
));
80 lsm
.cmd_type
= LTTNG_SNAPSHOT_DEL_OUTPUT
;
82 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
83 sizeof(lsm
.session
.name
));
84 memcpy(&lsm
.u
.snapshot_output
.output
, output
,
85 sizeof(lsm
.u
.snapshot_output
.output
));
87 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
91 * List all snapshot output(s) of a session identified by name. The output list
92 * object is populated and can be iterated over with the get_next call below.
94 * Return 0 on success or else a negative LTTNG_ERR code and the list pointer
97 int lttng_snapshot_list_output(const char *session_name
,
98 struct lttng_snapshot_output_list
**list
)
101 struct lttcomm_session_msg lsm
;
102 struct lttng_snapshot_output_list
*new_list
= NULL
;
104 if (!session_name
|| !list
) {
105 ret
= -LTTNG_ERR_INVALID
;
109 memset(&lsm
, 0, sizeof(lsm
));
110 lsm
.cmd_type
= LTTNG_SNAPSHOT_LIST_OUTPUT
;
112 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
113 sizeof(lsm
.session
.name
));
115 new_list
= zmalloc(sizeof(*new_list
));
117 ret
= -LTTNG_ERR_NOMEM
;
121 ret
= lttng_ctl_ask_sessiond(&lsm
, (void **) &new_list
->array
);
126 new_list
->count
= ret
/ sizeof(struct lttng_snapshot_output
);
137 * Return the next available snapshot output object in the given list. A list
138 * output command MUST have been done before.
140 * Return the next object on success or else NULL indicating the end of the
143 struct lttng_snapshot_output
*lttng_snapshot_output_list_get_next(
144 struct lttng_snapshot_output_list
*list
)
146 struct lttng_snapshot_output
*output
= NULL
;
152 /* We've reached the end. */
153 if (list
->index
== list
->count
) {
157 output
= &list
->array
[list
->index
];
166 * Free an output list object.
168 void lttng_snapshot_output_list_destroy(struct lttng_snapshot_output_list
*list
)
179 * Snapshot a trace for the given session.
181 * The output object can be NULL but an add output MUST be done prior to this
182 * call. If it's not NULL, it will be used to snapshot a trace.
184 * The wait parameter is ignored for now. The snapshot record command will
185 * ALWAYS wait for the snapshot to complete before returning meaning the
186 * snapshot has been written on disk or streamed over the network to a relayd.
188 * Return 0 on success or else a negative LTTNG_ERR value.
190 int lttng_snapshot_record(const char *session_name
,
191 struct lttng_snapshot_output
*output
, int wait
)
193 struct lttcomm_session_msg lsm
;
196 return -LTTNG_ERR_INVALID
;
199 memset(&lsm
, 0, sizeof(lsm
));
200 lsm
.cmd_type
= LTTNG_SNAPSHOT_RECORD
;
202 lttng_ctl_copy_string(lsm
.session
.name
, session_name
,
203 sizeof(lsm
.session
.name
));
206 * Not having an output object will use the default one of the session that
207 * would need to be set by a call to add output prior to calling snapshot
211 memcpy(&lsm
.u
.snapshot_record
.output
, output
,
212 sizeof(lsm
.u
.snapshot_record
.output
));
215 /* The wait param is ignored. */
217 return lttng_ctl_ask_sessiond(&lsm
, NULL
);
221 * Return an newly allocated snapshot output object or NULL on error.
223 struct lttng_snapshot_output
*lttng_snapshot_output_create(void)
225 struct lttng_snapshot_output
*output
;
227 output
= zmalloc(sizeof(struct lttng_snapshot_output
));
232 output
->max_size
= (uint64_t) -1ULL;
239 * Free a given snapshot output object.
241 void lttng_snapshot_output_destroy(struct lttng_snapshot_output
*obj
)
249 * Getter family functions of snapshot output.
252 uint32_t lttng_snapshot_output_get_id(struct lttng_snapshot_output
*output
)
257 const char *lttng_snapshot_output_get_name(
258 struct lttng_snapshot_output
*output
)
263 const char *lttng_snapshot_output_get_data_url(struct lttng_snapshot_output
*output
)
265 return output
->data_url
;
268 const char *lttng_snapshot_output_get_ctrl_url(struct lttng_snapshot_output
*output
)
270 return output
->ctrl_url
;
273 uint64_t lttng_snapshot_output_get_maxsize(
274 struct lttng_snapshot_output
*output
)
276 return output
->max_size
;
280 * Setter family functions for snapshot output.
283 int lttng_snapshot_output_set_id(uint32_t id
,
284 struct lttng_snapshot_output
*output
)
286 if (!output
|| id
== 0) {
287 return -LTTNG_ERR_INVALID
;
294 int lttng_snapshot_output_set_size(uint64_t size
,
295 struct lttng_snapshot_output
*output
)
298 return -LTTNG_ERR_INVALID
;
301 output
->max_size
= size
;
305 int lttng_snapshot_output_set_name(const char *name
,
306 struct lttng_snapshot_output
*output
)
308 if (!output
|| !name
) {
309 return -LTTNG_ERR_INVALID
;
312 lttng_ctl_copy_string(output
->name
, name
, sizeof(output
->name
));
316 int lttng_snapshot_output_set_ctrl_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
->ctrl_url
, url
, sizeof(output
->ctrl_url
));
327 int lttng_snapshot_output_set_data_url(const char *url
,
328 struct lttng_snapshot_output
*output
)
330 if (!output
|| !url
) {
331 return -LTTNG_ERR_INVALID
;
334 lttng_ctl_copy_string(output
->data_url
, url
, sizeof(output
->data_url
));