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>
27 #include "traceable-app.h"
29 /* Number of element for the list below. */
30 static unsigned int traceable_app_count
;
32 /* Init ust traceabl application's list */
33 struct ltt_traceable_app_list ltt_traceable_app_list
= {
34 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
38 pthread_mutex_t ltt_traceable_app_list_mutex
;
40 /* Internal function */
41 static void add_traceable_app(struct ltt_traceable_app
*lta
);
42 static void del_traceable_app(struct ltt_traceable_app
*lta
);
47 * Add a traceable application structure to the global
48 * list protected by a mutex.
50 static void add_traceable_app(struct ltt_traceable_app
*lta
)
52 pthread_mutex_lock(<t_traceable_app_list_mutex
);
53 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
54 traceable_app_count
++;
55 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
61 * Delete a traceable application structure from the
62 * global list protected by a mutex.
64 static void del_traceable_app(struct ltt_traceable_app
*lta
)
66 pthread_mutex_lock(<t_traceable_app_list_mutex
);
67 cds_list_del(<a
->list
);
69 if (traceable_app_count
!= 0) {
70 traceable_app_count
--;
72 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
76 * register_traceable_app
78 * Using pid and uid (of the app), allocate
79 * a new ltt_traceable_app struct and add it
80 * to the global traceable app list.
82 * On success, return 0, else return malloc ENOMEM.
84 int register_traceable_app(pid_t pid
, uid_t uid
)
86 struct ltt_traceable_app
*lta
;
88 lta
= malloc(sizeof(struct ltt_traceable_app
));
96 add_traceable_app(lta
);
97 DBG("Application %d registered with UID %d", pid
, uid
);
103 * unregister_traceable_app
105 * Unregister app by removing it from the global
106 * traceable app list and freeing the data struct.
108 void unregister_traceable_app(pid_t pid
)
110 struct ltt_traceable_app
*lta
;
112 lta
= find_app_by_pid(pid
);
114 del_traceable_app(lta
);
116 DBG("PID %d unregistered", pid
);
123 * Return traceable_app_count
125 unsigned int get_app_count(void)
127 return traceable_app_count
;
133 * Iterate over the traceable apps list and
134 * return a pointer or NULL if not found.
136 struct ltt_traceable_app
*find_app_by_pid(pid_t pid
)
138 struct ltt_traceable_app
*iter
;
140 pthread_mutex_lock(<t_traceable_app_list_mutex
);
141 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
142 if (iter
->pid
== pid
) {
143 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
148 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
156 * List traceable user-space application and fill an
159 void get_app_list_pids(pid_t
*pids
)
162 struct ltt_traceable_app
*iter
;
164 /* Protected by a mutex here because the threads manage_client
165 * and manage_apps can access this list.
167 pthread_mutex_lock(<t_traceable_app_list_mutex
);
168 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
172 pthread_mutex_unlock(<t_traceable_app_list_mutex
);