2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <urcu/list.h>
30 static unsigned int session_count
;
32 /* Static internal function */
33 static void add_session_list(struct ltt_session
*ls
);
34 static void del_session_list(struct ltt_session
*ls
);
36 /* Init global session list */
37 struct ltt_session_list ltt_session_list
= {
38 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
44 * Return a pointer to the session list.
46 struct ltt_session_list
*get_session_list(void)
48 return <t_session_list
;
54 * Return session_count
56 unsigned int get_session_count(void)
64 * Add a ltt_session structure to the global list.
66 static void add_session_list(struct ltt_session
*ls
)
68 cds_list_add(&ls
->list
, <t_session_list
.head
);
75 * Delete a ltt_session structure to the global list.
77 static void del_session_list(struct ltt_session
*ls
)
79 cds_list_del(&ls
->list
);
81 if (session_count
!= 0) {
87 * find_session_by_uuid
89 * Return a ltt_session structure ptr that matches the uuid.
91 struct ltt_session
*find_session_by_uuid(uuid_t session_id
)
94 struct ltt_session
*iter
;
96 /* Sanity check for NULL session_id */
97 if (uuid_is_null(session_id
)) {
101 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
102 if (uuid_compare(iter
->uuid
, session_id
) == 0) {
116 * find_session_by_name
118 * Return a ltt_session structure ptr that matches name.
119 * If no session found, NULL is returned.
121 struct ltt_session
*find_session_by_name(char *name
)
124 struct ltt_session
*iter
;
126 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
127 if (strncmp(iter
->name
, name
, strlen(name
)) == 0) {
143 * Delete session from the global session list
144 * and free the memory.
146 * Return -1 if no session is found.
147 * On success, return 1;
149 int destroy_session(char *name
)
152 struct ltt_session
*iter
;
154 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
155 if (strcmp(iter
->name
, name
) == 0) {
156 DBG("Destroying session %s", iter
->name
);
157 del_session_list(iter
);
170 * Create a brand new session and add it to the global session list.
172 int create_session(char *name
, char *path
)
175 char date_time
[NAME_MAX
];
176 struct ltt_session
*new_session
;
180 new_session
= find_session_by_name(name
);
181 if (new_session
!= NULL
) {
186 /* Allocate session data structure */
187 new_session
= malloc(sizeof(struct ltt_session
));
188 if (new_session
== NULL
) {
194 /* Define session name */
196 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
201 ERR("No session name given");
206 /* Define session system path */
208 if (strstr(name
, "auto-") == NULL
) {
210 timeinfo
= localtime(&rawtime
);
211 strftime(date_time
, sizeof(date_time
), "-%Y%m%d-%H%M%S", timeinfo
);
216 if (asprintf(&new_session
->path
, "%s/%s%s", path
, name
, date_time
) < 0) {
221 ERR("No session path given");
226 /* UUID generation */
227 uuid_generate(new_session
->uuid
);
230 * Set consumer (identifier) to 0. This means that there is
231 * NO consumer attach to that session yet.
233 new_session
->ust_consumer
= 0;
235 /* Init kernel session */
236 new_session
->kernel_session
= NULL
;
237 new_session
->kern_session_count
= 0;
240 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
242 /* Set trace list counter */
243 new_session
->ust_trace_count
= 0;
245 /* Add new session to the global session list */
246 add_session_list(new_session
);
248 DBG("Tracing session %s created in %s", name
, new_session
->path
);
254 if (new_session
!= NULL
) {
266 * Iterate over the global session list and fill the lttng_session array.
268 void get_lttng_session(struct lttng_session
*sessions
)
271 struct ltt_session
*iter
;
272 struct lttng_session lsess
;
274 DBG("Getting all available session");
276 /* Iterate over session list and append data after
277 * the control struct in the buffer.
279 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
280 strncpy(lsess
.path
, iter
->path
, sizeof(lsess
.path
));
281 lsess
.path
[sizeof(lsess
.path
) - 1] = '\0';
282 strncpy(lsess
.name
, iter
->name
, sizeof(lsess
.name
));
283 lsess
.name
[sizeof(lsess
.name
) - 1] = '\0';
284 memcpy(&sessions
[i
], &lsess
, sizeof(lsess
));
286 /* Reset struct for next pass */
287 memset(&lsess
, 0, sizeof(lsess
));