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; only version 2
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.
26 #include <urcu/list.h>
34 * No ltt_session.lock is taken here because those data structure are widely
35 * spread across the lttng-tools code base so before caling functions below
36 * that can read/write a session, the caller MUST acquire the session lock
37 * using lock_session() and unlock_session().
41 * Init tracing session list.
43 * Please see session.h for more explanation and correct usage of the list.
45 static struct ltt_session_list ltt_session_list
= {
46 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
47 .lock
= PTHREAD_MUTEX_INITIALIZER
,
54 * Add a ltt_session structure to the global list.
56 * The caller MUST acquire the session list lock before.
58 static void add_session_list(struct ltt_session
*ls
)
60 cds_list_add(&ls
->list
, <t_session_list
.head
);
61 ltt_session_list
.count
++;
67 * Delete a ltt_session structure to the global list.
69 * The caller MUST acquire the session list lock before.
71 static void del_session_list(struct ltt_session
*ls
)
73 cds_list_del(&ls
->list
);
75 if (ltt_session_list
.count
> 0) {
76 ltt_session_list
.count
--;
83 * Return a pointer to the session list.
85 struct ltt_session_list
*get_session_list(void)
87 return <t_session_list
;
91 * Acquire session list lock
93 void lock_session_list(void)
95 pthread_mutex_lock(<t_session_list
.lock
);
99 * Release session list lock
101 void unlock_session_list(void)
103 pthread_mutex_unlock(<t_session_list
.lock
);
107 * Acquire session lock
109 void lock_session(struct ltt_session
*session
)
111 pthread_mutex_lock(&session
->lock
);
115 * Release session lock
117 void unlock_session(struct ltt_session
*session
)
119 pthread_mutex_unlock(&session
->lock
);
123 * find_session_by_name
125 * Return a ltt_session structure ptr that matches name.
126 * If no session found, NULL is returned.
128 struct ltt_session
*find_session_by_name(char *name
)
131 struct ltt_session
*iter
;
134 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
135 if (strncmp(iter
->name
, name
, strlen(name
)) == 0) {
140 unlock_session_list();
152 * Delete session from the session list and free the memory.
154 * Return -1 if no session is found. On success, return 1;
156 int destroy_session(char *name
)
159 struct ltt_session
*iter
;
162 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
163 if (strcmp(iter
->name
, name
) == 0) {
164 DBG("Destroying session %s", iter
->name
);
165 del_session_list(iter
);
168 pthread_mutex_destroy(&iter
->lock
);
174 unlock_session_list();
182 * Create a brand new session and add it to the session list.
184 int create_session(char *name
, char *path
)
187 char date_time
[NAME_MAX
];
188 struct ltt_session
*new_session
;
192 new_session
= find_session_by_name(name
);
193 if (new_session
!= NULL
) {
198 /* Allocate session data structure */
199 new_session
= malloc(sizeof(struct ltt_session
));
200 if (new_session
== NULL
) {
206 /* Define session name */
208 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
213 ERR("No session name given");
218 /* Define session system path */
220 if (strstr(name
, "auto-") == NULL
) {
222 timeinfo
= localtime(&rawtime
);
223 strftime(date_time
, sizeof(date_time
), "-%Y%m%d-%H%M%S", timeinfo
);
228 if (asprintf(&new_session
->path
, "%s/%s%s", path
, name
, date_time
) < 0) {
233 ERR("No session path given");
238 /* Init kernel session */
239 new_session
->kernel_session
= NULL
;
242 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
244 /* Set trace list counter */
245 new_session
->ust_trace_count
= 0;
247 /* Add new session to the session list */
249 add_session_list(new_session
);
250 unlock_session_list();
253 pthread_mutex_init(&new_session
->lock
, NULL
);
255 DBG("Tracing session %s created in %s", new_session
->name
, new_session
->path
);
261 if (new_session
!= NULL
) {