[IMPROVE] create slot_manager
[kernel/swap-modules.git] / driver / sspt / sspt_procs.c
1 /*
2  *  Dynamic Binary Instrumentation Module based on KProbes
3  *  modules/driver/sspt/sspt_procs.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013         Vyacheslav Cherkashin <v.cherkashin@samsung.com>
22  *
23  */
24
25 #include "sspt_procs.h"
26 #include <linux/slab.h>
27 #include <linux/list.h>
28
29 extern struct list_head proc_probes_list;
30
31 struct sspt_procs *sspt_procs_create(struct dentry* dentry, pid_t tgid)
32 {
33         struct sspt_procs *procs = kmalloc(sizeof(*procs), GFP_ATOMIC);
34
35         if (procs) {
36                 INIT_LIST_HEAD(&procs->list);
37                 procs->tgid = tgid;
38                 procs->dentry = dentry;
39                 procs->sm = NULL;
40                 INIT_LIST_HEAD(&procs->file_list);
41         }
42
43         return procs;
44 }
45
46 void sspt_procs_free(struct sspt_procs *procs)
47 {
48         struct sspt_file *file, *n;
49         list_for_each_entry_safe(file, n, &procs->file_list, list) {
50                 list_del(&file->list);
51                 sspt_file_free(file);
52         }
53
54         kfree(procs);
55 }
56
57 // TODO: remove "us_proc_info"
58 #include "../storage.h"
59 extern inst_us_proc_t us_proc_info;
60
61 void sspt_procs_free_all(void)
62 {
63         // is user-space instrumentation
64         if (us_proc_info.path == NULL) {
65                 return;
66         }
67
68         if (strcmp(us_proc_info.path,"*") == 0) {
69                 // app
70                 sspt_procs_free(us_proc_info.pp);
71                 us_proc_info.pp = NULL;
72         } else {
73                 // libonly
74                 struct sspt_procs *procs, *n;
75                 list_for_each_entry_safe(procs, n, &proc_probes_list, list) {
76                         list_del(&procs->list);
77                         sspt_procs_free(procs);
78                 }
79         }
80 }
81
82 static void sspt_procs_add_file(struct sspt_procs *procs, struct sspt_file *file)
83 {
84         list_add(&file->list, &procs->file_list);
85         file->procs = procs;
86 }
87
88 struct sspt_file *sspt_procs_find_file_or_new(struct sspt_procs *procs,
89                 struct dentry *dentry, char *name)
90 {
91         struct sspt_file *file;
92
93         list_for_each_entry(file, &procs->file_list, list) {
94                 if (file->dentry == dentry) {
95                         return file;
96                 }
97         }
98
99         file = sspt_file_create(name, dentry, 10);
100         sspt_procs_add_file(procs, file);
101
102         return file;
103 }
104
105 void sspt_procs_add_ip_data(struct sspt_procs *procs, struct dentry* dentry,
106                 char *name, struct ip_data *ip_d)
107 {
108         struct sspt_file *file = sspt_procs_find_file_or_new(procs, dentry, name);
109         sspt_file_add_ip(file, ip_d);
110 }
111
112 struct sspt_procs *sspt_procs_copy(struct sspt_procs *procs, struct task_struct *task)
113 {
114         struct sspt_file *file;
115         struct sspt_procs *procs_out = sspt_procs_create(procs->dentry, task->tgid);
116
117         list_for_each_entry(file, &procs->file_list, list) {
118                 sspt_procs_add_file(procs_out, sspt_file_copy(file));
119         }
120
121         return procs_out;
122 }
123
124 struct sspt_file *sspt_procs_find_file(struct sspt_procs *procs, struct dentry *dentry)
125 {
126         struct sspt_file *file;
127
128         list_for_each_entry(file, &procs->file_list, list) {
129                 if (dentry == file->dentry) {
130                         return file;
131                 }
132         }
133
134         return NULL;
135 }