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_name
89 * Return a ltt_session structure ptr that matches name.
90 * If no session found, NULL is returned.
92 struct ltt_session
*find_session_by_name(char *name
)
95 struct ltt_session
*iter
;
97 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
98 if (strncmp(iter
->name
, name
, strlen(name
)) == 0) {
114 * Delete session from the global session list
115 * and free the memory.
117 * Return -1 if no session is found.
118 * On success, return 1;
120 int destroy_session(char *name
)
123 struct ltt_session
*iter
;
125 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
126 if (strcmp(iter
->name
, name
) == 0) {
127 DBG("Destroying session %s", iter
->name
);
128 del_session_list(iter
);
141 * Create a brand new session and add it to the global session list.
143 int create_session(char *name
, char *path
)
146 char date_time
[NAME_MAX
];
147 struct ltt_session
*new_session
;
151 new_session
= find_session_by_name(name
);
152 if (new_session
!= NULL
) {
157 /* Allocate session data structure */
158 new_session
= malloc(sizeof(struct ltt_session
));
159 if (new_session
== NULL
) {
165 /* Define session name */
167 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
172 ERR("No session name given");
177 /* Define session system path */
179 if (strstr(name
, "auto-") == NULL
) {
181 timeinfo
= localtime(&rawtime
);
182 strftime(date_time
, sizeof(date_time
), "-%Y%m%d-%H%M%S", timeinfo
);
187 if (asprintf(&new_session
->path
, "%s/%s%s", path
, name
, date_time
) < 0) {
192 ERR("No session path given");
198 * Set consumer (identifier) to 0. This means that there is
199 * NO consumer attach to that session yet.
201 new_session
->ust_consumer
= 0;
203 /* Init kernel session */
204 new_session
->kernel_session
= NULL
;
207 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
209 /* Set trace list counter */
210 new_session
->ust_trace_count
= 0;
212 /* Add new session to the global session list */
213 add_session_list(new_session
);
215 DBG("Tracing session %s created in %s", name
, new_session
->path
);
221 if (new_session
!= NULL
) {
233 * Iterate over the global session list and fill the lttng_session array.
235 void get_lttng_session(struct lttng_session
*sessions
)
238 struct ltt_session
*iter
;
239 struct lttng_session lsess
;
241 DBG("Getting all available session");
243 /* Iterate over session list and append data after
244 * the control struct in the buffer.
246 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
247 strncpy(lsess
.path
, iter
->path
, sizeof(lsess
.path
));
248 lsess
.path
[sizeof(lsess
.path
) - 1] = '\0';
249 strncpy(lsess
.name
, iter
->name
, sizeof(lsess
.name
));
250 lsess
.name
[sizeof(lsess
.name
) - 1] = '\0';
251 memcpy(&sessions
[i
], &lsess
, sizeof(lsess
));
253 /* Reset struct for next pass */
254 memset(&lsess
, 0, sizeof(lsess
));