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.
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
);
45 * Add a traceable application structure to the global list protected by a
48 static void add_traceable_app(struct ltt_traceable_app
*lta
)
50 pthread_mutex_lock(<t_traceable_app_list_mutex
);
51 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
52 traceable_app_count
++;
53 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
57 * Delete a traceable application structure from the global list protected by a
60 static void del_traceable_app(struct ltt_traceable_app
*lta
)
62 pthread_mutex_lock(<t_traceable_app_list_mutex
);
63 cds_list_del(<a
->list
);
65 if (traceable_app_count
!= 0) {
66 traceable_app_count
--;
68 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
72 * Using pid and uid (of the app), allocate a new ltt_traceable_app struct and
73 * add it to the global traceable app list.
75 * On success, return 0, else return malloc ENOMEM.
77 int register_traceable_app(pid_t pid
, uid_t uid
)
79 struct ltt_traceable_app
*lta
;
81 lta
= malloc(sizeof(struct ltt_traceable_app
));
89 add_traceable_app(lta
);
90 DBG("Application %d registered with UID %d", pid
, uid
);
96 * Unregister app by removing it from the global traceable app list and freeing
99 void unregister_traceable_app(pid_t pid
)
101 struct ltt_traceable_app
*lta
;
103 lta
= find_app_by_pid(pid
);
105 del_traceable_app(lta
);
107 DBG("PID %d unregistered", pid
);
112 * Return traceable_app_count
114 unsigned int get_app_count(void)
116 return traceable_app_count
;
120 * Iterate over the traceable apps list and return a pointer or NULL if not
123 struct ltt_traceable_app
*find_app_by_pid(pid_t pid
)
125 struct ltt_traceable_app
*iter
;
127 pthread_mutex_lock(<t_traceable_app_list_mutex
);
128 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
129 if (iter
->pid
== pid
) {
130 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
135 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
141 * List traceable user-space application and fill an array of pids.
143 void get_app_list_pids(pid_t
*pids
)
146 struct ltt_traceable_app
*iter
;
148 /* Protected by a mutex here because the threads manage_client
149 * and manage_apps can access this list.
151 pthread_mutex_lock(<t_traceable_app_list_mutex
);
152 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
156 pthread_mutex_unlock(<t_traceable_app_list_mutex
);