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.
31 #include "benchmark.h"
33 /* Init ust traceable application's list */
34 static struct ust_app_list ust_app_list
= {
35 .head
= CDS_LIST_HEAD_INIT(ust_app_list
.head
),
36 .lock
= PTHREAD_MUTEX_INITIALIZER
,
41 * Add a traceable application structure to the global list.
43 static void add_app_to_list(struct ust_app
*lta
)
45 cds_list_add(<a
->list
, &ust_app_list
.head
);
50 * Delete a traceable application structure from the global list.
52 static void del_app_from_list(struct ust_app
*lta
)
54 struct ltt_ust_channel
*chan
;
56 cds_list_del(<a
->list
);
58 if (ust_app_list
.count
> 0) {
62 cds_list_for_each_entry(chan
, <a
->channels
.head
, list
) {
63 trace_ust_destroy_channel(chan
);
68 * Iterate over the traceable apps list and return a pointer or NULL if not
71 static struct ust_app
*find_app_by_sock(int sock
)
75 cds_list_for_each_entry(iter
, &ust_app_list
.head
, list
) {
76 if (iter
->sock
== sock
) {
86 * Return pointer to traceable apps list.
88 struct ust_app_list
*ust_app_get_list(void)
94 * Acquire traceable apps list lock.
96 void ust_app_lock_list(void)
98 pthread_mutex_lock(&ust_app_list
.lock
);
102 * Release traceable apps list lock.
104 void ust_app_unlock_list(void)
106 pthread_mutex_unlock(&ust_app_list
.lock
);
110 * Iterate over the traceable apps list and return a pointer or NULL if not
113 struct ust_app
*ust_app_get_by_pid(pid_t pid
)
115 struct ust_app
*iter
;
117 cds_list_for_each_entry(iter
, &ust_app_list
.head
, list
) {
118 if (iter
->pid
== pid
) {
120 DBG2("Found traceable app by pid %d", pid
);
125 DBG2("Traceable app with pid %d not found", pid
);
131 * Using pid and uid (of the app), allocate a new ust_app struct and
132 * add it to the global traceable app list.
134 * On success, return 0, else return malloc ENOMEM.
136 int ust_app_register(struct ust_register_msg
*msg
, int sock
)
140 lta
= malloc(sizeof(struct ust_app
));
149 lta
->ppid
= msg
->ppid
;
150 lta
->v_major
= msg
->major
;
151 lta
->v_minor
= msg
->minor
;
153 strncpy(lta
->name
, msg
->name
, sizeof(lta
->name
));
154 lta
->name
[16] = '\0';
155 CDS_INIT_LIST_HEAD(<a
->channels
.head
);
158 add_app_to_list(lta
);
159 ust_app_unlock_list();
161 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
162 " (version %d.%d)", lta
->pid
, lta
->ppid
, lta
->uid
, lta
->gid
,
163 lta
->sock
, lta
->name
, lta
->v_major
, lta
->v_minor
);
169 * Unregister app by removing it from the global traceable app list and freeing
172 * The socket is already closed at this point so no close to sock.
174 void ust_app_unregister(int sock
)
178 tracepoint(ust_unregister_start
);
181 lta
= find_app_by_sock(sock
);
183 DBG("PID %d unregistered with sock %d", lta
->pid
, sock
);
184 del_app_from_list(lta
);
188 ust_app_unlock_list();
190 tracepoint(ust_unregister_stop
);
194 * Return traceable_app_count
196 unsigned int ust_app_list_count(void)
201 count
= ust_app_list
.count
;
202 ust_app_unlock_list();
208 * Free and clean all traceable apps of the global list.
210 void ust_app_clean_list(void)
212 struct ust_app
*iter
, *tmp
;
215 * Don't acquire list lock here. This function should be called from
216 * cleanup() functions meaning that the program will exit.
218 cds_list_for_each_entry_safe(iter
, tmp
, &ust_app_list
.head
, list
) {
219 del_app_from_list(iter
);