Small plt refactoring
[kernel/swap-modules.git] / driver / us_proc_inst.c
1 ////////////////////////////////////////////////////////////////////////////////////
2 //
3 //      FILE:           us_proc_inst.c
4 //
5 //      DESCRIPTION:
6 //      This file is C source for SWAP driver.
7 //
8 //      SEE ALSO:       us_proc_inst.h
9 //      AUTHOR:         A.Gerenkov, E. Gorelkina
10 //      COMPANY NAME:   Samsung Research Center in Moscow
11 //      DEPT NAME:      Advanced Software Group
12 //      CREATED:        2008.06.02
13 //      VERSION:        1.0
14 //      REVISION DATE:  2008.12.02
15 //
16 ////////////////////////////////////////////////////////////////////////////////////
17
18 #include "module.h"
19 #include "us_proc_inst.h"
20
21 #include "../kprobe/dbi_kprobes_deps.h"
22 #include "../kprobe/dbi_uprobes.h"
23
24 DEFINE_PER_CPU (us_proc_vtp_t *, gpVtp) = NULL;
25 DEFINE_PER_CPU (struct pt_regs *, gpCurVtpRegs) = NULL;
26
27 #if defined(CONFIG_MIPS)
28 #       define ARCH_REG_VAL(regs, idx)  regs->regs[idx]
29 #elif defined(CONFIG_ARM)
30 #       define ARCH_REG_VAL(regs, idx)  regs->uregs[idx]
31 #else
32 #       define ARCH_REG_VAL(regs, idx)  0
33 #       warning ARCH_REG_VAL is not implemented for this architecture. FBI will work improperly or even crash!!!
34 #endif // ARCH
35
36 unsigned long (*dbi_ujprobe_event_pre_handler_custom_p)
37 (us_proc_ip_t *, struct pt_regs *) = NULL;
38 EXPORT_SYMBOL(dbi_ujprobe_event_pre_handler_custom_p);
39 void (*dbi_ujprobe_event_handler_custom_p)(void) = NULL;
40 EXPORT_SYMBOL(dbi_ujprobe_event_handler_custom_p);
41 int (*dbi_uretprobe_event_handler_custom_p)
42 (struct kretprobe_instance *, struct pt_regs *, us_proc_ip_t *) = NULL;
43 EXPORT_SYMBOL(dbi_uretprobe_event_handler_custom_p);
44
45 unsigned long ujprobe_event_pre_handler (us_proc_ip_t * ip, struct pt_regs *regs);
46 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6);
47 int uretprobe_event_handler (struct kretprobe_instance *probe, struct pt_regs *regs, us_proc_ip_t * ip);
48
49 static int register_usprobe (struct task_struct *task, struct mm_struct *mm, us_proc_ip_t * ip, int atomic, kprobe_opcode_t * islot);
50 static int unregister_usprobe (struct task_struct *task, us_proc_ip_t * ip, int atomic);
51
52 int us_proc_probes;
53
54 struct task_inst_info_node {
55         struct list_head      plist;
56         inst_us_proc_t *      task_inst_info;
57         int                   tgid;
58 };
59 LIST_HEAD(task_inst_info_list);
60
61 #ifdef SLP_APP
62 struct dentry *launchpad_daemon_dentry = NULL;
63 EXPORT_SYMBOL_GPL(launchpad_daemon_dentry);
64 #endif /* SLP_APP */
65
66 #ifdef ANDROID_APP
67 unsigned long android_app_vma_start = 0;
68 unsigned long android_app_vma_end = 0;
69 struct dentry *app_process_dentry = NULL;
70 #endif /* ANDROID_APP */
71
72 #ifdef __ANDROID
73 struct dentry *libdvm_dentry = NULL;
74 /* Defines below are for libdvm.so with md5sum:
75  * 5941c87b49198368e7db726c2977bf1d */
76 #define LIBDVM_ENTRY 0x30a64
77 #define LIBDVM_RETURN 0x30bdc
78 #endif /* __ANDROID */
79
80 us_proc_otg_ip_t *find_otg_probe(unsigned long addr)
81 {
82         us_proc_otg_ip_t *p;
83
84         list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
85                 if (p->ip.offset == addr) {
86                         return p;
87                 }
88         }
89
90         return NULL;
91 }
92
93 int add_otg_probe_to_list(unsigned long addr, us_proc_otg_ip_t **pprobe)
94 {
95         us_proc_otg_ip_t *new_probe;
96         us_proc_otg_ip_t *probe;
97
98         if (pprobe) {
99                 *pprobe = NULL;
100         }
101         /* check if such probe does already exist */
102         probe = find_otg_probe(addr);
103         if (probe) {
104                 return 1;
105         }
106
107         new_probe = kmalloc(sizeof(us_proc_otg_ip_t), GFP_KERNEL);
108         if (!new_probe) {
109                 EPRINTF ("no memory for new probe!");
110                 return -ENOMEM;
111         }
112         memset(new_probe,0, sizeof(us_proc_otg_ip_t));
113
114         new_probe->ip.offset = addr;
115         new_probe->ip.jprobe.kp.addr =
116                 new_probe->ip.retprobe.kp.addr = (kprobe_opcode_t *)addr;
117         new_probe->ip.jprobe.priv_arg =
118                 new_probe->ip.retprobe.priv_arg = new_probe;
119
120         INIT_LIST_HEAD(&new_probe->list);
121         list_add_rcu(&new_probe->list, &otg_us_proc_info);
122
123         if (pprobe) {
124                 *pprobe = new_probe;
125         }
126         return 0;
127 }
128
129 int remove_otg_probe_from_list(unsigned long addr)
130 {
131         us_proc_otg_ip_t *p;
132
133         //check if such probe does exist
134         p = find_otg_probe(addr);
135         if (!p) {
136                 /* We do not care about it. Nothing bad. */
137                 return 0;
138         }
139
140         list_del_rcu(&p->list);
141
142         kfree (p);
143
144         return 0;
145 }
146
147 /**
148  * Prepare copy of instrumentation data for task
149  * in case of library only instrumentation
150  */
151
152 inst_us_proc_t* copy_task_inst_info (struct task_struct *task, inst_us_proc_t * task_inst_info)
153 {
154         int i, j;
155         kprobe_opcode_t *entry_save;
156         kprobe_pre_entry_handler_t pre_entry_save;
157         kretprobe_handler_t handler_save;
158
159         inst_us_proc_t* copy_info = 0;
160
161         int unres_ips_count = 0, unres_vtps_count = 0;
162
163         copy_info = kmalloc (sizeof (inst_us_proc_t), GFP_ATOMIC);
164         memset ((void *) copy_info, 0, sizeof (inst_us_proc_t));
165
166         copy_info->path = task_inst_info->path;
167         copy_info->m_f_dentry = NULL;
168
169         copy_info->libs_count = task_inst_info->libs_count;
170         copy_info->p_libs =
171                 kmalloc (task_inst_info->libs_count * sizeof (us_proc_lib_t), GFP_ATOMIC);
172
173         if (!copy_info->p_libs) {
174                 DPRINTF ("No enough memory for copy_info->p_libs");
175                 return NULL;
176         }
177         memcpy (copy_info->p_libs, task_inst_info->p_libs,
178                         copy_info->libs_count * sizeof (us_proc_lib_t));
179
180         for (i = 0; i < copy_info->libs_count; i++) {
181                 if (copy_info->p_libs[i].ips_count > 0)
182                 {
183                         unres_ips_count += copy_info->p_libs[i].ips_count;
184
185                         copy_info->p_libs[i].p_ips =
186                                 kmalloc (copy_info->p_libs[i].ips_count * sizeof (us_proc_ip_t), GFP_ATOMIC);
187
188                         if (!copy_info->p_libs[i].p_ips) {
189                                 DPRINTF ("No enough memory for copy_info->p_libs[i].p_ips");
190                                 return NULL;
191                         }
192
193                         memcpy (copy_info->p_libs[i].p_ips, task_inst_info->p_libs[i].p_ips,
194                                         copy_info->p_libs[i].ips_count * sizeof (us_proc_ip_t));
195                         for (j = 0; j < copy_info->p_libs[i].ips_count; j++) {
196                                 // save handlers
197                                 entry_save = copy_info->p_libs[i].p_ips[j].jprobe.entry;
198                                 pre_entry_save = copy_info->p_libs[i].p_ips[j].jprobe.pre_entry;
199                                 handler_save = copy_info->p_libs[i].p_ips[j].retprobe.handler;
200
201                                 copy_info->p_libs[i].p_ips[j].installed = 0;
202                                 memset(&copy_info->p_libs[i].p_ips[j].jprobe, 0, sizeof(struct jprobe));
203                                 memset(&copy_info->p_libs[i].p_ips[j].retprobe, 0, sizeof(struct kretprobe));
204
205                                 // restore handlers
206                                 copy_info->p_libs[i].p_ips[j].jprobe.entry = entry_save;
207                                 copy_info->p_libs[i].p_ips[j].jprobe.pre_entry = pre_entry_save;
208                                 copy_info->p_libs[i].p_ips[j].retprobe.handler = handler_save;
209                         }
210
211                         unres_ips_count += copy_info->p_libs[i].ips_count;
212                 }
213
214                 for (j = 0; j < copy_info->p_libs[i].plt_count; j++)
215                 {
216                         copy_info->p_libs[i].p_plt[j].real_func_addr = 0;
217                 }
218
219                 if (copy_info->p_libs[i].vtps_count > 0) {
220                         unres_vtps_count += copy_info->p_libs[i].vtps_count;
221
222                         copy_info->p_libs[i].p_vtps =
223                                 kmalloc (copy_info->p_libs[i].vtps_count * sizeof (us_proc_vtp_t), GFP_ATOMIC);
224
225                         if (!copy_info->p_libs[i].p_vtps) {
226                                 DPRINTF ("No enough memory for copy_info->p_libs[i].p_vtps");
227                                 return NULL;
228                         }
229
230                         memcpy (copy_info->p_libs[i].p_vtps, task_inst_info->p_libs[i].p_vtps,
231                                         copy_info->p_libs[i].vtps_count * sizeof (us_proc_vtp_t));
232                         for (j = 0; j < copy_info->p_libs[i].vtps_count; j++) {
233                                 copy_info->p_libs[i].p_vtps[j].installed = 0;
234                                 memset (&copy_info->p_libs[i].p_vtps[j].jprobe, 0, sizeof(struct jprobe));
235                         }
236                         unres_vtps_count = copy_info->p_libs[i].vtps_count;
237                 }
238
239                 copy_info->p_libs[i].m_f_dentry = task_inst_info->p_libs[i].m_f_dentry;
240                 copy_info->p_libs[i].loaded = 0;
241
242                 copy_info->p_libs[i].vma_start = 0;
243                 copy_info->p_libs[i].vma_end = 0;
244         }
245         copy_info->unres_ips_count = unres_ips_count;
246         copy_info->unres_vtps_count = unres_vtps_count;
247
248         return copy_info;
249 }
250
251 inst_us_proc_t* get_task_inst_node(struct task_struct *task)
252 {
253         struct task_inst_info_node *node, *tnode;
254
255         list_for_each_entry_safe(node, tnode, &task_inst_info_list, plist)
256         {
257                 if (node && task && node->tgid == task->tgid) {
258                         return node->task_inst_info;
259                 }
260         }
261         return NULL;
262 }
263
264 void put_task_inst_node(struct task_struct *task, inst_us_proc_t *task_inst_info)
265 {
266         struct task_inst_info_node * node;
267
268         node = kmalloc (sizeof(struct task_inst_info_node), GFP_ATOMIC);
269
270         node->tgid = task->tgid;
271         node->task_inst_info = task_inst_info;
272
273         list_add_tail (&(node->plist), &task_inst_info_list);
274 }
275
276
277 void clear_task_inst_info(void)
278 {
279         struct list_head *node, *tmp;
280
281         list_for_each_safe(node, tmp, &task_inst_info_list)
282                 list_del(node);
283 }
284
285 #ifdef SLP_APP
286 static int is_slp_app_with_dentry(struct vm_area_struct *vma,
287                                                                   struct dentry *dentry)
288 {
289         struct vm_area_struct *slp_app_vma = NULL;
290
291         if (vma->vm_file->f_dentry == launchpad_daemon_dentry) {
292                 slp_app_vma = vma;
293                 while (slp_app_vma) {
294                         if (slp_app_vma->vm_file) {
295                                 if (slp_app_vma->vm_file->f_dentry == dentry &&
296                                         slp_app_vma->vm_pgoff == 0) {
297                                         return 1;
298                                 }
299                         }
300                         slp_app_vma = slp_app_vma->vm_next;
301                 }
302         }
303
304         return 0;
305 }
306 #endif /* SLP_APP */
307
308 #ifdef ANDROID_APP
309 static int is_android_app_with_dentry(struct vm_area_struct *vma,
310                                                                           struct dentry *dentry)
311 {
312         struct vm_area_struct *android_app_vma = NULL;
313
314         if (vma->vm_file->f_dentry == app_process_dentry) {
315                 android_app_vma = vma;
316                 while (android_app_vma) {
317                         if (android_app_vma->vm_file) {
318                                 if (android_app_vma->vm_file->f_dentry == dentry) {
319                                         android_app_vma_start = android_app_vma->vm_start;
320                                         android_app_vma_end = android_app_vma->vm_end;
321                                         return 1;
322                                 }
323                         }
324                         android_app_vma = android_app_vma->vm_next;
325                 }
326         }
327
328         return 0;
329 }
330 #endif /* ANDROID_APP */
331
332 #ifdef __ANDROID
333 void find_libdvm_for_task(struct task_struct *task, inst_us_proc_t *info)
334 {
335         struct vm_area_struct *vma = NULL;
336         struct mm_struct *mm = NULL;
337
338         mm = get_task_mm(task);
339         if (mm) {
340                 vma = mm->mmap;
341                 while (vma) {
342                         if (vma->vm_file) {
343                                 if (vma->vm_file->f_dentry == libdvm_dentry) {
344                                         info->libdvm_start = vma->vm_start;
345                                         info->libdvm_end = vma->vm_end;
346                                         break;
347                                 }
348                         }
349                         vma = vma->vm_next;
350                 }
351                 mmput(mm);
352         }
353 }
354 #endif /* __ANDROID */
355
356 static int find_task_by_path (const char *path, struct task_struct **p_task, struct list_head *tids)
357 {
358         int found = 0;
359         struct task_struct *task;
360         struct vm_area_struct *vma;
361         struct mm_struct *mm;
362 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
363         struct path s_path;
364 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
365         struct nameidata nd;
366 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
367
368         *p_task = 0;
369
370         /* find corresponding dir entry, this is also check for valid path */
371         // TODO: test - try to instrument process with non-existing path
372         // TODO: test - try to instrument process  with existing path and delete file just after start
373 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
374         if (kern_path(us_proc_info.path, LOOKUP_FOLLOW, &s_path) != 0) {
375 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
376         if (path_lookup(us_proc_info.path, LOOKUP_FOLLOW, &nd) != 0) {
377 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
378                 EPRINTF ("failed to lookup dentry for path %s!", path);
379                 return -EINVAL;
380         }
381
382         rcu_read_lock();
383         for_each_process (task) {
384
385                 if  ( 0 != inst_pid && ( inst_pid != task->pid ) )
386                         continue;
387
388                 mm = get_task_mm(task);
389                 if (!mm)
390                         continue;
391                 vma = mm->mmap;
392                 while (vma) {
393                         if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
394 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
395                                 if (vma->vm_file->f_dentry == nd.dentry) {
396 #else
397 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
398                                 if (vma->vm_file->f_dentry == s_path.dentry  ) {
399 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
400                                 if (vma->vm_file->f_dentry == nd.path.dentry  ) {
401 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
402 #endif
403                                         if (!*p_task) {
404                                                 *p_task = task;
405                                                 get_task_struct (task);
406                                         }
407                                                 //break;
408                                 }
409 #ifdef SLP_APP
410                                 if (!*p_task) {
411 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
412                                         if (is_slp_app_with_dentry(vma, nd.dentry)) {
413 #else
414 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
415                                         if (is_slp_app_with_dentry(vma, s_path.dentry)) {
416 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
417                                         if (is_slp_app_with_dentry(vma, nd.path.dentry)) {
418 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
419 #endif
420                                                 *p_task = task;
421                                                 get_task_struct(task);
422                                         }
423                                 }
424 #endif /* SLP_APP */
425 #ifdef ANDROID_APP
426                                 if (!*p_task) {
427 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
428                                         if (is_android_app_with_dentry(vma, nd.dentry)) {
429 #else
430 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
431                                         if (is_android_app_with_dentry(vma, s_path.dentry)) {
432 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
433                                         if (is_android_app_with_dentry(vma, nd.path.dentry)) {
434 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
435 #endif
436                                                 *p_task = task;
437                                                 get_task_struct(task);
438                                         }
439                                 }
440 #endif /* ANDROID_APP */
441                         }
442                         vma = vma->vm_next;
443                 }
444                 // only decrement usage count on mm since we cannot sleep here
445                 atomic_dec(&mm->mm_users);
446                 if (found)
447                         break;
448         }
449         rcu_read_unlock();
450
451         if (*p_task) {
452                 DPRINTF ("found pid %d for %s.", (*p_task)->pid, path);
453                 *p_task = (*p_task)->group_leader;
454                 gl_nNotifyTgid = (*p_task)->tgid;
455         } else {
456                 DPRINTF ("pid for %s not found!", path);
457         }
458
459 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
460         path_release (&nd);
461 #else
462 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
463         path_put (&s_path);
464 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
465         path_put (&nd.path);
466 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
467 #endif
468         return 0;
469 }
470
471
472 static void us_vtp_event_pre_handler (us_proc_vtp_t * vtp, struct pt_regs *regs)
473 {
474         __get_cpu_var(gpVtp) = vtp;
475         __get_cpu_var(gpCurVtpRegs) = regs;
476 }
477
478 static void us_vtp_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
479 {
480         us_proc_vtp_t *vtp = __get_cpu_var(gpVtp);
481 #if !defined(CONFIG_X86)
482         struct pt_regs *regs = __get_cpu_var(gpCurVtpRegs);
483 #endif
484         char fmt[4];
485         unsigned long vaddr;
486         long ival;
487         char cval, *sval;
488         us_proc_vtp_data_t *vtp_data;
489 unsigned long ll;
490         fmt[0] = 'p';
491         fmt[3] = 0;
492         fmt[2] = 's';
493
494         list_for_each_entry_rcu (vtp_data, &vtp->list, list) {
495                 //              DPRINTF ("[%d]proc %s(%d): %lx", nCount++, current->comm, current->pid, vtp->addr);
496                 fmt[1] = vtp_data->type;
497                 if (vtp_data->reg == -1)
498                         vaddr = vtp_data->off;
499                 else
500                         vaddr = ARCH_REG_VAL (regs, vtp_data->reg) + vtp_data->off;
501                 //              DPRINTF ("VTP type '%c'", vtp_data->type);
502                 switch (vtp_data->type)
503                 {
504                         case 'd':
505                         case 'x':
506                         case 'p':
507                                 if (read_proc_vm_atomic (current, vaddr, &ival, sizeof (ival)) < sizeof (ival))
508                                         EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
509                                 else
510                                         pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, ival, vtp_data->name);
511                                 break;
512                         case 'f':
513                                 if (read_proc_vm_atomic (current, vaddr, &ival, sizeof (ival)) < sizeof (ival))
514                                         EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
515                                 else
516                                         pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, ival, vtp_data->name);
517                                 break;
518                         case 'c':
519                                 if (read_proc_vm_atomic (current, vaddr, &cval, sizeof (cval)) < sizeof (cval))
520                                         EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
521                                 else
522                                         pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, cval, vtp_data->name);
523                                 break;
524                         case 's':
525                                 if (current->active_mm) {
526                                         struct page *page;
527                                         struct vm_area_struct *vma;
528                                         void *maddr;
529                                         int len;
530                                         if (get_user_pages_atomic (current, current->active_mm, vaddr, 1, 0, 1, &page, &vma) <= 0) {
531                                                 EPRINTF ("get_user_pages_atomic failed for proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
532                                                 break;
533                                         }
534                                         maddr = kmap_atomic (page, KM_USER0);
535                                         len = strlen (maddr + (vaddr & ~PAGE_MASK));
536                                         sval = kmalloc (len + 1, GFP_KERNEL);
537                                         if (!sval)
538                                                 EPRINTF ("failed to alloc memory for string in proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
539                                         else {
540                                                 copy_from_user_page (vma, page, vaddr, sval, maddr + (vaddr & ~PAGE_MASK), len + 1);
541                                                 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, sval,  vtp_data->name);
542                                                 kfree (sval);
543                                         }
544                                         kunmap_atomic (maddr, KM_USER0);
545                                         page_cache_release (page);
546                                 }
547                                 else
548                                         EPRINTF ("task %s/%u has no mm!", current->comm, current->pid);
549                                 break;
550                         default:
551                                 EPRINTF ("unknown variable type '%c'", vtp_data->type);
552                 }
553         }
554         dbi_uprobe_return ();
555 }
556
557 static int install_mapped_ips (struct task_struct *task, inst_us_proc_t* task_inst_info, int atomic)
558 {
559         struct vm_area_struct *vma;
560         int i, k, err;
561         unsigned long addr;
562         unsigned int old_ips_count, old_vtps_count;
563         us_proc_otg_ip_t *p;
564         struct task_struct *t;
565         struct mm_struct *mm;
566
567         mm = atomic ? task->active_mm : get_task_mm (task);
568         if (!mm) {
569                 return task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count;
570         }
571         old_ips_count = task_inst_info->unres_ips_count;
572         old_vtps_count = task_inst_info->unres_vtps_count;
573         if(!atomic)
574                 down_read (&mm->mmap_sem);
575         vma = mm->mmap;
576         while (vma) {
577                 // skip non-text section
578 #ifndef __ANDROID
579                 if (vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || !vma->vm_file || (vma->vm_flags & VM_ACCOUNT) ||
580                         !(vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) ||
581                         !(vma->vm_flags & (VM_READ | VM_MAYREAD))) {
582 #else // __ANDROID
583                 if (vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || !vma->vm_file) {
584 #endif // __ANDROID
585                         vma = vma->vm_next;
586                         continue;
587                 }
588                 /**
589                  * After process was forked, some time it inherits parent process environment.
590                  * We need to renew instrumentation when we detect that process gets own environment.
591                  */
592                 for (i = 0; i < task_inst_info->libs_count; i++) {
593 //                      struct path tmp_path;
594 //                      tmp_path.dentry = task_inst_info->p_libs[i].m_f_dentry;
595 //                      tmp_path.mnt = task_inst_info->p_libs[i].m_vfs_mount;
596 //                      char* p_path = d_path ( &tmp_path, path_buffer, 255 );
597 //                      DPRINTF("f_dentry:%x m_f_dentry:%x path:%s", vma->vm_file->f_dentry,
598 //                              task_inst_info->p_libs[i].m_f_dentry, p_path );
599
600                         //TODO: test - try to instrument non-existing libs
601                         if (vma->vm_file->f_dentry == task_inst_info->p_libs[i].m_f_dentry) {
602 //                              DPRINTF("vm_flags:%x loaded:%x ips_count:%d vtps_count:%d",
603 //                                              vma->vm_flags, task_inst_info->p_libs[i].loaded,
604 //                                              task_inst_info->p_libs[i].ips_count, task_inst_info->p_libs[i].vtps_count );
605                                 if (!task_inst_info->p_libs[i].loaded) {
606 //                                      DPRINTF("!VM_EXECUTABLE && !loaded");
607                                         char *p;
608                                         int app_flag = (vma->vm_file->f_dentry == task_inst_info->m_f_dentry);
609                                         DPRINTF ("post dyn lib event %s/%s", current->comm, task_inst_info->p_libs[i].path);
610                                         // if we installed something, post library info for those IPs
611                                         p = strrchr(task_inst_info->p_libs[i].path, '/');
612                                         if(!p)
613                                                 p = task_inst_info->p_libs[i].path;
614                                         else
615                                                 p++;
616                                         task_inst_info->p_libs[i].loaded = 1;
617                                         task_inst_info->p_libs[i].vma_start = vma->vm_start;
618                                         task_inst_info->p_libs[i].vma_end = vma->vm_end;
619                                         pack_event_info (DYN_LIB_PROBE_ID, RECORD_ENTRY, "dspdd",
620                                                         task->tgid, p, vma->vm_start, vma->vm_end-vma->vm_start, app_flag);
621                                 }
622                                 for (k = 0; k < task_inst_info->p_libs[i].ips_count; k++) {
623                                         DPRINTF("ips_count current:%d", k);
624                                         if (!task_inst_info->p_libs[i].p_ips[k].installed) {
625                                                 DPRINTF("!installed");
626                                                 addr = task_inst_info->p_libs[i].p_ips[k].offset;
627                                                 addr += vma->vm_start;
628                                                 if (page_present (mm, addr)) {
629                                                         DPRINTF ("pid %d, %s sym is loaded at %lx/%lx.",
630                                                                 task->pid, task_inst_info->p_libs[i].path,
631                                                                 task_inst_info->p_libs[i].p_ips[k].offset, addr);
632                                                         task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr = (kprobe_opcode_t *) addr;
633                                                         task_inst_info->p_libs[i].p_ips[k].retprobe.kp.addr = (kprobe_opcode_t *) addr;
634                                                         task_inst_info->p_libs[i].p_ips[k].installed = 1;
635                                                         task_inst_info->unres_ips_count--;
636                                                         err = register_usprobe (task, mm, &task_inst_info->p_libs[i].p_ips[k], atomic, 0);
637                                                         if (err != 0) {
638                                                                 DPRINTF ("failed to install IP at %lx/%p. Error %d!",
639                                                                         task_inst_info->p_libs[i].p_ips[k].offset,
640                                                                         task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
641                                                         }
642                                                 }
643                                         }
644                                 }
645                                 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; k++) {
646                                         DPRINTF("vtps_count current:%d", k);
647                                         if (!task_inst_info->p_libs[i].p_vtps[k].installed) {
648                                                 DPRINTF("!installed");
649                                                 addr = task_inst_info->p_libs[i].p_vtps[k].addr;
650                                                 if (!(vma->vm_flags & VM_EXECUTABLE))
651                                                         addr += vma->vm_start;
652                                                 if (page_present (mm, addr)) {
653                                                         DPRINTF ("pid %d, %s sym is loaded at %lx/%lx.",
654                                                                 task->pid, task_inst_info->p_libs[i].path,
655                                                                 task_inst_info->p_libs[i].p_ips[k].offset, addr);
656                                                         task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.tgid = task_inst_info->tgid;
657                                                         task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr = (kprobe_opcode_t *) addr;
658                                                         task_inst_info->p_libs[i].p_vtps[k].jprobe.entry = (kprobe_opcode_t *) us_vtp_event_handler;
659                                                         task_inst_info->p_libs[i].p_vtps[k].jprobe.pre_entry = (kprobe_pre_entry_handler_t) us_vtp_event_pre_handler;
660                                                         task_inst_info->p_libs[i].p_vtps[k].jprobe.priv_arg = &task_inst_info->p_libs[i].p_vtps[k];
661                                                         task_inst_info->p_libs[i].p_vtps[k].installed = 1;
662                                                         task_inst_info->unres_vtps_count--;
663                                                         err = dbi_register_ujprobe (task, mm, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
664                                                         if ( err != 0 ) {
665                                                                 EPRINTF ("failed to install VTP at %p. Error %d!",
666                                                                                 task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr, err);
667                                                         }
668                                                 }
669                                         }
670                                 }
671                         }
672                 }
673 #ifdef __ANDROID
674                 if (is_java_inst_enabled()
675                     && vma->vm_file->f_dentry == libdvm_dentry) {
676                         us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
677                         if (!entp->installed
678                             && task_inst_info->libdvm_start) {
679                                 unsigned long addr = LIBDVM_ENTRY + task_inst_info->libdvm_start;
680                                 if (page_present(mm, addr)) {
681                                         entp->jprobe.kp.tgid = task->tgid;
682                                         entp->jprobe.pre_entry = ujprobe_event_pre_handler;
683                                         entp->jprobe.entry = ujprobe_event_handler;
684                                         entp->jprobe.priv_arg = entp;
685                                         entp->jprobe.kp.addr = addr;
686                                         entp->retprobe.kp.tgid = task->tgid;
687                                         entp->retprobe.handler = uretprobe_event_handler;
688                                         entp->retprobe.priv_arg = entp;
689                                         entp->retprobe.kp.addr = addr;
690                                         err = register_usprobe(task, mm, entp, atomic, 0);
691                                         if (err != 0) {
692                                                 DPRINTF("failed to install IP at %p", addr);
693                                         }
694                                 }
695                                 entp->installed = 1;
696                         }
697                         us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
698                         if (!retp->installed
699                             && task_inst_info->libdvm_start) {
700                                 unsigned long addr = LIBDVM_RETURN + task_inst_info->libdvm_start;
701                                 if (page_present(mm, addr)) {
702                                         retp->jprobe.kp.tgid = task->tgid;
703                                         retp->jprobe.pre_entry = ujprobe_event_pre_handler;
704                                         retp->jprobe.entry = ujprobe_event_handler;
705                                         retp->jprobe.priv_arg = retp;
706                                         retp->jprobe.kp.addr = addr;
707                                         retp->retprobe.kp.tgid = task->tgid;
708                                         retp->retprobe.handler = uretprobe_event_handler;
709                                         retp->retprobe.priv_arg = retp;
710                                         retp->retprobe.kp.addr = addr;
711                                         err = register_usprobe(task, mm, retp, atomic, 0);
712                                         if (err != 0) {
713                                                 DPRINTF("failed to install IP at %p", addr);
714                                         }
715                                 }
716                                 retp->installed = 1;
717                         }
718                 }
719 #endif /* __ANDROID */
720                 vma = vma->vm_next;
721         }
722         list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
723                 if (p->ip.installed) {
724                         continue;
725                 }
726                 rcu_read_lock();
727 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
728                 t = find_task_by_pid(p->tgid);
729 #else
730                 t = pid_task(find_pid_ns(p->tgid, &init_pid_ns),
731                              PIDTYPE_PID);
732 #endif
733                 if (t){
734                         get_task_struct(t);
735                 }
736                 rcu_read_unlock();
737                 if (!t) {
738                         DPRINTF("task for pid %d not found! Dead probe?",
739                                   p->tgid);
740                         continue;
741                 }
742                 if (!t->active_mm) {
743                         continue;
744                 }
745                 if (!page_present(t->active_mm, p->ip.offset)) {
746                         DPRINTF("Page isn't present for %p.",
747                                 p->ip.offset);
748                         continue;
749                 }
750                 p->ip.installed = 1;
751                 err = register_usprobe(current, t->active_mm,
752                                        &p->ip, atomic, 0);
753                 if (err != 0) {
754                         DPRINTF("failed to install IP at %lx/%p. Error %d!",
755                                 p->ip.offset,
756                                 p->ip.jprobe.kp.addr, err);
757                         return err;
758                 }
759                 task_inst_info->unres_otg_ips_count--;
760         }
761         if (!atomic) {
762                 up_read (&mm->mmap_sem);
763                 mmput (mm);
764         }
765         return task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count;
766 }
767
768 static int install_otg_ip(unsigned long addr,
769                       unsigned long pre_handler,
770                       unsigned long jp_handler,
771                       unsigned long rp_handler)
772 {
773         int err;
774         us_proc_otg_ip_t *pprobe;
775         struct mm_struct *mm;
776
777         inst_us_proc_t *task_inst_info = NULL;
778         struct task_struct *task;
779
780         /* Probe preparing */
781         err = add_otg_probe_to_list(addr, &pprobe);
782         if (err) {
783                 if (err == 1) {
784                         DPRINTF("OTG probe %p already installed.", addr);
785                         return 0;
786                 } else {
787                         DPRINTF("Failed to add new OTG probe, err=%d",err);
788                         return err;
789                 }
790         }
791         if (pre_handler) {
792                 pprobe->ip.jprobe.pre_entry =
793                         (kprobe_pre_entry_handler_t)pre_handler;
794         } else {
795                 pprobe->ip.jprobe.pre_entry =
796                         (kprobe_pre_entry_handler_t)
797                         dbi_ujprobe_event_pre_handler_custom_p;
798
799         }
800         if (jp_handler) {
801                 pprobe->ip.jprobe.entry =
802                         (kprobe_opcode_t *)jp_handler;
803         } else {
804                 pprobe->ip.jprobe.entry =
805                         (kprobe_opcode_t *)
806                         dbi_ujprobe_event_handler_custom_p;
807         }
808         if (rp_handler) {
809                 pprobe->ip.retprobe.handler =
810                         (kretprobe_handler_t)rp_handler;
811         } else {
812                 pprobe->ip.retprobe.handler =
813                         (kretprobe_handler_t)
814                         dbi_uretprobe_event_handler_custom_p;
815         }
816
817         mm = get_task_mm(current);
818         if (!page_present(mm, addr)) {
819                 DPRINTF("Page isn't present for %p.", addr);
820
821                 pprobe->tgid = current->tgid;
822                 task = current->group_leader;
823
824                 task_inst_info = get_task_inst_node(task);
825                 if (!task_inst_info)
826                 {
827                         task_inst_info = copy_task_inst_info(task, &us_proc_info);
828                         put_task_inst_node(task, task_inst_info);
829                 }
830                 us_proc_info.unres_otg_ips_count++;
831                 /* Probe will be installed in do_page_fault handler */
832                 return 0;
833         }
834         DPRINTF("Page present for %p.", addr);
835
836         /* Probe installing */
837         pprobe->tgid = current->tgid;
838         pprobe->ip.installed = 1;
839         err = register_usprobe(current, mm, &pprobe->ip, 1, 0);
840         if (err != 0) {
841                 DPRINTF("failed to install IP at %lx/%p. Error %d!",
842                          addr, pprobe->ip.jprobe.kp.addr, err);
843                 return err;
844         }
845         return 0;
846 }
847 EXPORT_SYMBOL_GPL(install_otg_ip);
848
849
850 static int uninstall_mapped_ips (struct task_struct *task,  inst_us_proc_t* task_inst_info, int atomic)
851 {
852         int i, k, err;
853         us_proc_otg_ip_t *p;
854
855         for (i = 0; i < task_inst_info->libs_count; i++)
856         {
857                 DPRINTF ("clear lib %s.", task_inst_info->p_libs[i].path);
858                 for (k = 0; k < task_inst_info->p_libs[i].ips_count; k++)
859                 {
860                         if (task_inst_info->p_libs[i].p_ips[k].installed)
861                         {
862                                 DPRINTF ("remove IP at %p.", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr);
863                                 err = unregister_usprobe (task, &task_inst_info->p_libs[i].p_ips[k], atomic);
864                                 if (err != 0)
865                                 {
866                                         EPRINTF ("failed to uninstall IP at %p. Error %d!", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
867                                         continue;
868                                 }
869                                 task_inst_info->unres_ips_count++;
870                                 task_inst_info->p_libs[i].p_ips[k].installed = 0;
871                         }
872                 }
873                 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; k++)
874                 {
875                         if (task_inst_info->p_libs[i].p_vtps[k].installed)
876                         {
877                                 dbi_unregister_ujprobe (task, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
878                                 task_inst_info->unres_vtps_count++;
879                                 task_inst_info->p_libs[i].p_vtps[k].installed = 0;
880                         }
881                 }
882                 task_inst_info->p_libs[i].loaded = 0;
883         }
884 #ifdef __ANDROID
885         if (is_java_inst_enabled()) {
886                 us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
887                 if (entp->installed) {
888                         unregister_usprobe(task, entp, atomic);
889                         entp->installed = 0;
890                 }
891                 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
892                 if (retp->installed) {
893                         unregister_usprobe(task, retp, atomic);
894                         retp->installed = 0;
895                 }
896         }
897 #endif /* __ANDROID */
898         list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
899                 if (!p->ip.installed) {
900                         continue;
901                 }
902                 DPRINTF("remove OTG IP at %p.", p->ip.offset);
903                 err = unregister_usprobe(task, &p->ip, atomic);
904                 if (err != 0) {
905                         EPRINTF("failed to uninstall IP at %p. Error %d!",
906                                  p->ip.jprobe.kp.addr, err);
907                         continue;
908                 }
909                 p->ip.installed = 0;
910         }
911
912         DPRINTF ("Ures IPs  %d.", task_inst_info->unres_ips_count);
913         DPRINTF ("Ures VTPs %d.", task_inst_info->unres_vtps_count);
914         return 0;
915 }
916
917 void send_sig_jprobe_event_handler (int sig, struct siginfo *info, struct task_struct *t, struct sigpending *signals)
918 {
919         int iRet, del = 0;
920         struct task_struct *task;
921         inst_us_proc_t *task_inst_info = NULL;
922
923         //if user-space instrumentation is not set
924         if (!us_proc_info.path)
925             return;
926
927         if (sig != SIGKILL)
928                 return;
929
930         if (!strcmp(us_proc_info.path,"*"))
931         {
932                 task_inst_info = get_task_inst_node(t);
933                 if (task_inst_info)
934                 {
935                         iRet = uninstall_mapped_ips (t, task_inst_info, 1);
936                         if (iRet != 0)
937                                 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
938                         dbi_unregister_all_uprobes(t, 1);
939                         return;
940                 }
941         }
942         else
943         {
944                 if (current->tgid != us_proc_info.tgid)
945                         return;
946                         del = 1;
947
948                 // look for another process with the same tgid
949                 rcu_read_lock ();
950                 for_each_process (task)
951                 {
952                         if ((task->pid != t->pid) && (task->tgid == us_proc_info.tgid))
953                         {
954                                 del = 0;
955                                 break;
956                         }
957                 }
958                 rcu_read_unlock ();
959                 if (del)
960                 {
961                         DPRINTF ("%s(%d) send_signal SIGKILL for the last target proc %s(%d)",
962                                         current->comm, current->pid, t->comm, t->pid);
963                         iRet = uninstall_mapped_ips (t, &us_proc_info, 1);
964                         if (iRet != 0)
965                                 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
966                 }
967         }
968 }
969 static int uninstall_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
970 {
971         kernel_probe_t *probe = NULL;
972         int iRet = 0;
973         if (probes_flags & kflag) {
974                 probe = find_probe(addr);
975                 if (probe) {
976                         iRet = remove_probe_from_list (addr);
977                         if (iRet)
978                                 EPRINTF ("remove_probe_from_list(0x%lx) result=%d!", addr, iRet);
979                         if (pprobe)
980                                 *pprobe = NULL;
981                 }
982                 probes_flags &= ~kflag;
983         }
984         if (us_proc_probes & uflag) {
985                 if (!(probes_flags & uflag)) {
986                         if (probe) {
987                                 iRet = unregister_kernel_probe(probe);
988                                 if (iRet) {
989                                         EPRINTF ("unregister_kernel_probe(0x%lx) result=%d!",
990                                                         addr, iRet);
991                                         return iRet;
992                                 }
993                         }
994                 }
995                 us_proc_probes &= ~uflag;
996         }
997         return iRet;
998 }
999
1000 int deinst_usr_space_proc (void)
1001 {
1002         int iRet = 0, found = 0;
1003         struct task_struct *task = 0;
1004         inst_us_proc_t *task_inst_info = NULL;
1005
1006         //if user-space instrumentation is not set
1007         if (!us_proc_info.path)
1008                 return 0;
1009
1010         iRet = uninstall_kernel_probe (pf_addr, US_PROC_PF_INSTLD,
1011                         0, &pf_probe);
1012         if (iRet)
1013                 EPRINTF ("uninstall_kernel_probe(do_page_fault) result=%d!", iRet);
1014
1015         iRet = uninstall_kernel_probe (cp_addr, US_PROC_CP_INSTLD,
1016                         0, &cp_probe);
1017         if (iRet)
1018                 EPRINTF ("uninstall_kernel_probe(copy_process) result=%d!", iRet);
1019
1020         iRet = uninstall_kernel_probe (mr_addr, US_PROC_MR_INSTLD,
1021                         0, &mr_probe);
1022         if (iRet)
1023                 EPRINTF ("uninstall_kernel_probe(mm_release) result=%d!", iRet);
1024
1025         iRet = uninstall_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD,
1026                         0, &exit_probe);
1027         if (iRet)
1028                 EPRINTF ("uninstall_kernel_probe(do_exit) result=%d!", iRet);
1029
1030         iRet = uninstall_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD,
1031                         0, &unmap_probe);
1032         if (iRet)
1033                 EPRINTF ("uninstall_kernel_probe(do_munmap) result=%d!", iRet);
1034
1035         if (!strcmp(us_proc_info.path,"*"))
1036         {
1037                 for_each_process (task)
1038                 {
1039                         task_inst_info = get_task_inst_node(task);
1040                         if (task_inst_info)
1041                         {
1042                                 iRet = uninstall_mapped_ips (task, task_inst_info, 1);
1043                                 if (iRet != 0)
1044                                         EPRINTF ("failed to uninstall IPs (%d)!", iRet);
1045                                 dbi_unregister_all_uprobes(task, 1);
1046                         }
1047                 }
1048         }
1049         else
1050         {
1051                 if (us_proc_info.tgid == 0)
1052                         return 0;
1053                         rcu_read_lock ();
1054                 for_each_process (task)
1055                 {
1056                         if (task->tgid == us_proc_info.tgid)
1057                         {
1058                                 found = 1;
1059                                 get_task_struct (task);
1060                                 break;
1061                         }
1062                 }
1063                 rcu_read_unlock ();
1064                 if (found)
1065                 {
1066                         int i;
1067                         // uninstall IPs
1068                         iRet = uninstall_mapped_ips (task, &us_proc_info, 0);
1069                         if (iRet != 0)
1070                         EPRINTF ("failed to uninstall IPs %d!", iRet);
1071                         put_task_struct (task);
1072                         dbi_unregister_all_uprobes(task, 1);
1073                         us_proc_info.tgid = 0;
1074                         for(i = 0; i < us_proc_info.libs_count; i++)
1075                                 us_proc_info.p_libs[i].loaded = 0;
1076                 }
1077         }
1078
1079         return iRet;
1080 }
1081 static int install_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
1082 {
1083         kernel_probe_t *probe = NULL;
1084         int iRet = 0;
1085
1086         DPRINTF("us_proc_probes = 0x%x, uflag = 0x%x, "
1087                         "probes_flags = 0x%x, kflag = 0x%x",
1088                         us_proc_probes, uflag, probes_flags, kflag);
1089
1090         if (!(probes_flags & kflag)) {
1091                 iRet = add_probe_to_list (addr, &probe);
1092                 if (iRet) {
1093                         EPRINTF ("add_probe_to_list(0x%lx) result=%d!", addr, iRet);
1094                         return iRet;
1095                 }
1096                 probes_flags |= kflag;
1097         }
1098         if (!(us_proc_probes & uflag)) {
1099                 if (!(probes_flags & uflag)) {
1100                         iRet = register_kernel_probe (probe);
1101                         if (iRet) {
1102                                 EPRINTF ("register_kernel_probe(0x%lx) result=%d!", addr, iRet);
1103                                 return iRet;
1104                         }
1105                 }
1106                 us_proc_probes |= uflag;
1107         }
1108
1109         if (probe)
1110                 *pprobe = probe;
1111
1112         return 0;
1113 }
1114
1115 static struct dentry *get_dentry(const char *path)
1116 {
1117 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
1118         struct path st_path;
1119         if (kern_path(path, LOOKUP_FOLLOW, &st_path) != 0) {
1120 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1121         struct nameidata nd;
1122         if (path_lookup(path, LOOKUP_FOLLOW, &nd) != 0) {
1123 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1124                 EPRINTF("failed to lookup dentry for path %s!", path);
1125                 return NULL;
1126         }
1127
1128 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
1129         return nd.dentry;
1130 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38)
1131         return nd.path.dentry;
1132 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
1133         return st_path.dentry;
1134 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) */
1135 }
1136
1137 int inst_usr_space_proc (void)
1138 {
1139         int ret, i;
1140         struct task_struct *task = 0;
1141         inst_us_proc_t *task_inst_info = NULL;
1142
1143         //if user-space instrumentation is not set
1144         if (!us_proc_info.path)
1145                 return 0;
1146
1147         DPRINTF("User space instr");
1148
1149 #ifdef SLP_APP
1150         launchpad_daemon_dentry = get_dentry("/usr/bin/launchpad_preloading_preinitializing_daemon");
1151         if (launchpad_daemon_dentry == NULL) {
1152                 return -EINVAL;
1153         }
1154
1155 #endif /* SLP_APP */
1156
1157 #ifdef ANDROID_APP
1158         app_process_dentry = get_dentry("/system/bin/app_process");
1159         if (app_process_dentry == NULL) {
1160                 return -EINVAL;
1161         }
1162
1163         android_app_vma_start = 0;
1164         android_app_vma_end = 0;
1165 #endif /* ANDROID_APP */
1166
1167 #ifdef __ANDROID
1168         if (is_java_inst_enabled()) {
1169                 libdvm_dentry = get_dentry("/system/lib/libdvm.so");
1170                 if (libdvm_dentry == NULL) {
1171                         return -EINVAL;
1172                 }
1173
1174                 memset(&us_proc_info.libdvm_entry_ip, 0, sizeof(us_proc_ip_t));
1175                 memset(&us_proc_info.libdvm_return_ip, 0, sizeof(us_proc_ip_t));
1176                 us_proc_info.libdvm_start = 0;
1177                 us_proc_info.libdvm_end = 0;
1178         }
1179 #endif /* __ANDROID */
1180
1181         for (i = 0; i < us_proc_info.libs_count; i++) {
1182                 us_proc_info.p_libs[i].loaded = 0;
1183         }
1184         /* check whether process is already running
1185          * 1) if process is running - look for the libraries in the process maps
1186          * 1.1) check if page for symbol does exist
1187          * 1.1.1) if page exists - instrument it
1188          * 1.1.2) if page does not exist - make sure that do_page_fault handler is installed
1189          * 2) if process is not running - make sure that do_page_fault handler is installed
1190          * */
1191
1192         if (!strcmp(us_proc_info.path,"*"))
1193         {
1194                 clear_task_inst_info();
1195                 for_each_process (task) {
1196                         if (task->flags & PF_KTHREAD){
1197                                 DPRINTF("ignored kernel thread %d\n",
1198                                         task->pid);
1199                                 continue;
1200                         }
1201
1202                         task_inst_info = get_task_inst_node(task);
1203                         if (!task_inst_info) {
1204                                 task_inst_info =
1205                                         copy_task_inst_info(task,
1206                                                             &us_proc_info);
1207                                 put_task_inst_node(task, task_inst_info);
1208                         }
1209                         DPRINTF("trying process");
1210 #ifdef __ANDROID
1211                         if (is_java_inst_enabled()) {
1212                                 find_libdvm_for_task(task, task_inst_info);
1213                         }
1214 #endif /* __ANDROID */
1215                         install_mapped_ips (task, task_inst_info, 1);
1216                         //put_task_struct (task);
1217                         task_inst_info = NULL;
1218                 }
1219         }
1220         else
1221         {
1222                 ret = find_task_by_path (us_proc_info.path, &task, NULL);
1223                 if ( task  )
1224                 {
1225                         DPRINTF("task found. installing probes");
1226                         us_proc_info.tgid = task->pid;
1227 #ifdef __ANDROID
1228                         if (is_java_inst_enabled()) {
1229                                 find_libdvm_for_task(task, &us_proc_info);
1230                         }
1231 #endif /* __ANDROID */
1232                         install_mapped_ips (task, &us_proc_info, 0);
1233                         put_task_struct (task);
1234                 }
1235         }
1236
1237         // enable 'do_page_fault' probe to detect when they will be loaded
1238         ret = install_kernel_probe (pf_addr, US_PROC_PF_INSTLD, 0, &pf_probe);
1239         if (ret != 0)
1240         {
1241                 EPRINTF ("install_kernel_probe(do_page_fault) result=%d!", ret);
1242                 return ret;
1243         }
1244         // enable 'do_exit' probe to detect for remove task_struct
1245         ret = install_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD, 0, &exit_probe);
1246         if (ret != 0)
1247         {
1248                 EPRINTF ("install_kernel_probe(do_exit) result=%d!", ret);
1249                 return ret;
1250         }
1251         /* enable 'copy_process' */
1252         ret = install_kernel_probe (cp_addr, US_PROC_CP_INSTLD, 0, &cp_probe);
1253         if (ret != 0)
1254         {
1255                 EPRINTF ("instpall_kernel_probe(copy_process) result=%d!", ret);
1256                 return ret;
1257         }
1258
1259         // enable 'mm_release' probe to detect when for remove user space probes
1260         ret = install_kernel_probe (mr_addr, US_PROC_MR_INSTLD, 0, &mr_probe);
1261         if (ret != 0)
1262         {
1263                 EPRINTF ("install_kernel_probe(mm_release) result=%d!", ret);
1264                 return ret;
1265         }
1266
1267         // enable 'do_munmap' probe to detect when for remove user space probes
1268         ret = install_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD, 0, &unmap_probe);
1269         if (ret != 0)
1270         {
1271                 EPRINTF ("install_kernel_probe(do_munmap) result=%d!", ret);
1272                 return ret;
1273         }
1274         return 0;
1275 }
1276
1277 void do_page_fault_ret_pre_code (void)
1278 {
1279         struct mm_struct *mm;
1280         struct vm_area_struct *vma = 0;
1281         inst_us_proc_t *task_inst_info = NULL;
1282         /*
1283          * Because process threads have same address space
1284          * we instrument only group_leader of all this threads
1285          */
1286         struct task_struct *task = current->group_leader;
1287
1288         //if user-space instrumentation is not set
1289         if (!us_proc_info.path)
1290                 return;
1291
1292         if (task->flags & PF_KTHREAD) {
1293                 DPRINTF("ignored kernel thread %d\n", task->pid);
1294                 return;
1295         }
1296
1297
1298         if (!strcmp(us_proc_info.path,"*"))
1299         {
1300                 task_inst_info = get_task_inst_node(task);
1301                 if (!task_inst_info)
1302                 {
1303                         task_inst_info = copy_task_inst_info(task,
1304                                                              &us_proc_info);
1305                         put_task_inst_node(task, task_inst_info);
1306 #ifdef __ANDROID
1307                         if (is_java_inst_enabled()) {
1308                                 find_libdvm_for_task(task, task_inst_info);
1309                         }
1310 #endif /* __ANDROID */
1311                 }
1312                 install_mapped_ips (task, task_inst_info, 1);
1313                 return;
1314         }
1315
1316         task_inst_info = &us_proc_info;
1317         //DPRINTF("do_page_fault from proc %d-%d-%d", current->pid, task_inst_info->tgid, task_inst_info->unres_ips_count);
1318         if (!is_java_inst_enabled()
1319             && (task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count
1320                 + task_inst_info->unres_otg_ips_count) == 0)
1321         {
1322                 //DPRINTF("do_page_fault: there no unresolved IPs");
1323                 return;
1324         }
1325
1326         if (task_inst_info->tgid == 0)
1327         {
1328                 mm = task->active_mm;
1329                 if (mm)
1330                 {
1331 //                      down_read (&mm->mmap_sem);
1332                         vma = mm->mmap;
1333                         while (vma)
1334                         {
1335                                 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
1336                                 {
1337                                         if (vma->vm_file->f_dentry == task_inst_info->m_f_dentry)
1338                                         {
1339                                                 break;
1340                                         }
1341 #ifdef SLP_APP
1342                                         if (is_slp_app_with_dentry(vma, task_inst_info->m_f_dentry)) {
1343                                                 break;
1344                                         }
1345 #endif /* SLP_APP */
1346 #ifdef ANDROID_APP
1347                                         if (is_android_app_with_dentry(vma, task_inst_info->m_f_dentry)) {
1348                                                 break;
1349                                         }
1350 #endif /* ANDROID_APP */
1351                                 }
1352                                 vma = vma->vm_next;
1353                         }
1354 //                      up_read (&mm->mmap_sem);
1355 //                      mmput (mm);
1356                 } else {
1357                         //                      DPRINTF ("proc %s/%d has no mm", current->comm, current->pid);
1358                 }
1359                 if (vma)
1360                 {
1361                      DPRINTF ("do_page_fault found target proc %s(%d)", task->comm, task->pid);
1362                      task_inst_info->tgid = task->pid;
1363                      gl_nNotifyTgid = task->tgid;
1364                 }
1365         }
1366         if (task_inst_info->tgid == task->tgid)
1367         {
1368                 //DPRINTF("do_page_fault from target proc %d", task_inst_info->tgid);
1369 #ifdef __ANDROID
1370                 if (is_java_inst_enabled()) {
1371                         find_libdvm_for_task(task, &us_proc_info);
1372                 }
1373 #endif /* __ANDROID */
1374                 install_mapped_ips (task, &us_proc_info, 1);
1375         }
1376         //DPRINTF("do_page_fault from proc %d-%d exit", task->pid, task_inst_info->pid);
1377 }
1378
1379 EXPORT_SYMBOL_GPL(do_page_fault_ret_pre_code);
1380
1381
1382 void do_exit_probe_pre_code (void)
1383 {
1384         // TODO: remove task
1385 }
1386 EXPORT_SYMBOL_GPL(do_exit_probe_pre_code);
1387
1388 static int check_addr(unsigned long addr, unsigned long start, size_t len)
1389 {
1390         if ((addr >= start) && (addr < start + (unsigned long)len)) {
1391                 return 1;
1392         }
1393
1394         return 0;
1395 }
1396
1397 static int remove_unmap_probes(struct task_struct *task, inst_us_proc_t* task_inst_info, unsigned long start, size_t len)
1398 {
1399         int i, k, err;
1400         us_proc_otg_ip_t *p;
1401         unsigned long addr;
1402         const int atomic = 1;
1403
1404         for (i = 0; i < task_inst_info->libs_count; ++i) {
1405                 for (k = 0; k < task_inst_info->p_libs[i].ips_count; ++k) {
1406                         if (task_inst_info->p_libs[i].p_ips[k].installed) {
1407                                 addr = task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr;
1408                                 if (check_addr(addr, start, len)) {
1409                                         err = unregister_usprobe(task, &task_inst_info->p_libs[i].p_ips[k], atomic);
1410                                         if (err != 0) {
1411                                                 EPRINTF("failed to uninstall IP at %p. Error %d!", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
1412                                                 continue;
1413                                         }
1414                                         task_inst_info->unres_ips_count++;
1415                                         task_inst_info->p_libs[i].p_ips[k].installed = 0;
1416                                 }
1417                         }
1418                 }
1419                 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; ++k) {
1420                         if (task_inst_info->p_libs[i].p_vtps[k].installed) {
1421                                 addr = task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr;
1422                                 if (check_addr(addr, start, len)) {
1423                                         dbi_unregister_ujprobe(task, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
1424                                         task_inst_info->unres_vtps_count++;
1425                                         task_inst_info->p_libs[i].p_vtps[k].installed = 0;
1426                                 }
1427                         }
1428                 }
1429         }
1430 #ifdef __ANDROID
1431         if (is_java_inst_enabled()) {
1432                 us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
1433                 if (entp->installed) {
1434                         addr = entp->jprobe.kp.addr;
1435                         if (check_addr(addr, start, len)) {
1436                                 unregister_usprobe(task, entp, atomic);
1437                                 entp->installed = 0;
1438                         }
1439                 }
1440                 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
1441                 if (retp->installed) {
1442                         addr = retp->jprobe.kp.addr;
1443                         if (check_addr(addr, start, len)) {
1444                                 unregister_usprobe(task, retp, atomic);
1445                                 retp->installed = 0;
1446                         }
1447                 }
1448         }
1449 #endif /* __ANDROID */
1450         list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
1451                 if (!p->ip.installed) {
1452                         continue;
1453                 }
1454
1455                 addr = p->ip.jprobe.kp.addr;
1456                 if (check_addr(addr, start, len) == 0) {
1457                         continue;
1458                 }
1459
1460                 err = unregister_usprobe(task, &p->ip, atomic);
1461                 if (err != 0) {
1462                         EPRINTF("failed to uninstall IP at %p. Error %d!",
1463                                  p->ip.jprobe.kp.addr, err);
1464                         continue;
1465                 }
1466                 p->ip.installed = 0;
1467         }
1468
1469         return 0;
1470 }
1471
1472 void do_munmap_probe_pre_code(struct mm_struct *mm, unsigned long start, size_t len)
1473 {
1474         inst_us_proc_t *task_inst_info = NULL;
1475         struct task_struct *task = current;
1476
1477         //if user-space instrumentation is not set
1478         if (!us_proc_info.path || task->tgid != task->pid)
1479                 return;
1480
1481         if (!strcmp(us_proc_info.path,"*")) {
1482                 task_inst_info = get_task_inst_node(task);
1483         } else {
1484                 if (task->tgid == us_proc_info.tgid) {
1485                         task_inst_info = &us_proc_info;
1486                 }
1487         }
1488
1489         if (task_inst_info) {
1490                 remove_unmap_probes(task, task_inst_info, start, len);
1491         }
1492 }
1493 EXPORT_SYMBOL_GPL(do_munmap_probe_pre_code);
1494
1495 void mm_release_probe_pre_code(void)
1496 {
1497         int iRet, del = 0;
1498         struct task_struct *task;
1499         inst_us_proc_t *task_inst_info = NULL;
1500
1501         //if user-space instrumentation is not set
1502         if (!us_proc_info.path || current->tgid != current->pid)
1503                 return;
1504
1505         if (!strcmp(us_proc_info.path,"*"))
1506         {
1507                 task_inst_info = get_task_inst_node(current);
1508                 if (task_inst_info)
1509                 {
1510                         iRet = uninstall_mapped_ips (current, task_inst_info, 1);
1511                         if (iRet != 0)
1512                                 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
1513                         dbi_unregister_all_uprobes(current, 1);
1514                 }
1515         }
1516         else
1517         {
1518                 if (current->tgid != us_proc_info.tgid)
1519                         return;
1520                         del = 1;
1521                 // look for another process with the same tgid
1522                 rcu_read_lock ();
1523                 for_each_process (task)
1524                 {
1525                         if ((task->pid != current->pid) && (task->tgid == us_proc_info.tgid))
1526                         {
1527                                 del = 0;
1528                                 break;
1529                         }
1530                 }
1531                 rcu_read_unlock ();
1532                 if (del)
1533                 {
1534                         int i;
1535                         iRet = uninstall_mapped_ips (current, &us_proc_info, 1);
1536                         if (iRet != 0)
1537                                 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
1538                         dbi_unregister_all_uprobes(current, 1);
1539                         us_proc_info.tgid = 0;
1540                         for(i = 0; i < us_proc_info.libs_count; i++)
1541                                 us_proc_info.p_libs[i].loaded = 0;
1542                 }
1543         }
1544 }
1545 EXPORT_SYMBOL_GPL(mm_release_probe_pre_code);
1546
1547
1548 static void recover_child(struct task_struct *child_task, inst_us_proc_t *parent_iup)
1549 {
1550         int i, k;
1551         for(i = 0; i < parent_iup->libs_count; ++i)
1552         {
1553                 for(k = 0; k < parent_iup->p_libs[i].ips_count; ++k)
1554                         if(parent_iup->p_libs[i].p_ips[k].installed)
1555                                 arch_disarm_uprobe(&parent_iup->p_libs[i].p_ips[k].jprobe.kp, child_task);
1556
1557                 for(k = 0; k < parent_iup->p_libs[i].vtps_count; ++k)
1558                         if(parent_iup->p_libs[i].p_vtps[k].installed)
1559                                 arch_disarm_uprobe(&parent_iup->p_libs[i].p_vtps[k].jprobe.kp, child_task);
1560         }
1561 }
1562
1563 static void rm_uprobes_child(struct task_struct *new_task)
1564 {
1565         if(!strcmp(us_proc_info.path, "*")) {
1566                 inst_us_proc_t *task_inst_info = get_task_inst_node(current);
1567                 if(task_inst_info)
1568                         recover_child(new_task, task_inst_info);
1569         } else {
1570                 if(us_proc_info.tgid == current->tgid) {
1571                         recover_child(new_task, &us_proc_info);
1572                 }
1573         }
1574 }
1575
1576 void copy_process_ret_pre_code(struct task_struct *p)
1577 {
1578         if(!p || IS_ERR(p))
1579                 return;
1580
1581         if(p->mm != current->mm)    // check flags CLONE_VM
1582                 rm_uprobes_child(p);
1583 }
1584
1585
1586 DEFINE_PER_CPU (us_proc_ip_t *, gpCurIp) = NULL;
1587 EXPORT_PER_CPU_SYMBOL_GPL(gpCurIp);
1588 DEFINE_PER_CPU(struct pt_regs *, gpUserRegs) = NULL;
1589 EXPORT_PER_CPU_SYMBOL_GPL(gpUserRegs);
1590
1591
1592 unsigned long ujprobe_event_pre_handler (us_proc_ip_t * ip, struct pt_regs *regs)
1593 {
1594         __get_cpu_var (gpCurIp) = ip;
1595         __get_cpu_var (gpUserRegs) = regs;
1596         return 0;
1597 }
1598
1599 #ifdef __ANDROID
1600 int handle_java_event(unsigned long addr)
1601 {
1602         unsigned long start = 0;
1603         struct pt_regs *regs = __get_cpu_var(gpUserRegs);
1604
1605         if (!strcmp(us_proc_info.path, "*")) {
1606                 /* TODO: some stuff here */
1607         } else {
1608                 start = us_proc_info.libdvm_start;
1609         }
1610         unsigned long end = us_proc_info.libdvm_end;
1611
1612         if (addr == start + LIBDVM_ENTRY) {
1613                 unsigned long *p_met = (unsigned long *)regs->ARM_r0;
1614                 char *met_name = p_met ? (char *)(p_met[4]) : 0;
1615                 unsigned long *p_cl = p_met ? (unsigned long *)p_met[0] : 0;
1616                 char *cl_name = p_cl ? (char *)(p_cl[6]) : 0;
1617                 if (!cl_name || !met_name) {
1618                         EPRINTF("warn: class name or method name null\n");
1619                 } else {
1620                         pack_event_info(JAVA_PROBE_ID, RECORD_ENTRY, "pss", addr, cl_name, met_name);
1621                 }
1622                 dbi_uprobe_return ();
1623                 return 1;
1624         }
1625
1626         if (addr == start + LIBDVM_RETURN) {
1627                 unsigned long *p_th = (unsigned long *)regs->ARM_r6;
1628                 unsigned long *p_st = p_th;
1629                 unsigned long *p_met = p_st ? (unsigned long *)p_st[2] : 0;
1630                 char *met_name = p_met ? (char *)(p_met[4]) : 0;
1631                 unsigned long *p_cl = p_met ? (unsigned long *)p_met[0] : 0;
1632                 char *cl_name = p_cl ? (char *)(p_cl[6]) : 0;
1633                 if (!cl_name || !met_name) {
1634                         EPRINTF("warn: class name or method name null\n");
1635                 } else {
1636                         pack_event_info(JAVA_PROBE_ID, RECORD_RET, "pss", addr, cl_name, met_name);
1637                 }
1638                 dbi_uprobe_return ();
1639                 return 1;
1640         }
1641
1642         return 0;
1643 }
1644 #endif /* __ANDROID */
1645
1646 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
1647 {
1648         us_proc_ip_t *ip = __get_cpu_var (gpCurIp);
1649         unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1650
1651 #ifdef __ANDROID
1652         if (is_java_inst_enabled() && handle_java_event(addr)) {
1653                 return;
1654         }
1655 #endif /* __ANDROID */
1656
1657
1658 #if defined(CONFIG_ARM)
1659         if (ip->offset & 0x01)
1660         {
1661                 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr | 0x01, arg1, arg2, arg3, arg4, arg5, arg6);
1662         }else{
1663                 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1664         }
1665 #else
1666         pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1667 #endif
1668         // Mr_Nobody: uncomment for valencia
1669         //unregister_usprobe(current, ip, 1);
1670         dbi_uprobe_return ();
1671 }
1672
1673 void find_plt_address(unsigned long addr)
1674 {
1675         inst_us_proc_t *task_inst_info = NULL;
1676         int i;
1677         unsigned real_addr;
1678         struct vm_area_struct *vma;
1679         us_proc_lib_t *p_lib = NULL;
1680         char *szLibPath = NULL;
1681
1682         // Search for library structure to check whether this function plt or not
1683         if (strcmp(us_proc_info.path, "*")){
1684                 // If app lib instrumentation
1685                 task_inst_info = &us_proc_info;
1686         } else {
1687                 // If lib only instrumentation
1688                 task_inst_info = get_task_inst_node(current);
1689         }
1690         if ((task_inst_info != NULL) && (task_inst_info->is_plt != 0)) {
1691                 for (i = 0; i < task_inst_info->libs_count; i++)
1692                 {
1693                         if ((task_inst_info->p_libs[i].loaded)
1694                                 && (task_inst_info->p_libs[i].plt_count > 0)
1695                                 && (addr > task_inst_info->p_libs[i].vma_start)
1696                                 && (addr < task_inst_info->p_libs[i].vma_end)) 
1697                         {
1698                                 p_lib = &(task_inst_info->p_libs[i]);
1699                                 break;
1700                         }
1701                 }
1702                 if (p_lib != NULL) {
1703                         for (i = 0; i < p_lib->plt_count; i++)
1704                         {
1705                                 if (addr == p_lib->p_plt[i].func_addr + p_lib->vma_start) {
1706                                         unsigned long real_got;
1707                                         if (strcmp(p_lib->path, task_inst_info->path)) {
1708                                                 real_got = p_lib->p_plt[i].got_addr - 0x8000 + p_lib->vma_start;
1709                                         } else {
1710                                                 real_got = p_lib->p_plt[i].got_addr;
1711                                         }
1712                                         if (!read_proc_vm_atomic(current, (unsigned long)(real_got), &real_addr, sizeof(unsigned long))) {
1713                                                 printk("Failed to read got %p at memory address %p!\n", p_lib->p_plt[i].got_addr, real_got);
1714                                                 break;
1715                                         }
1716                                         if (real_addr != p_lib->p_plt[i].real_func_addr) {
1717                                                 p_lib->p_plt[i].real_func_addr =  real_addr;
1718                                                 vma = find_vma(current->mm, real_addr);
1719                                                 if ((vma->vm_start <= real_addr) && (vma->vm_end > real_addr)) {
1720                                                         if (vma->vm_file != NULL) {
1721                                                                 szLibPath = &(vma->vm_file->f_dentry->d_iname);
1722                                                         }
1723                                                 }
1724                                                 if (szLibPath) {
1725                                                         pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppsp", addr, real_addr, szLibPath, real_addr - vma->vm_start);
1726                                                         break;
1727                                                 } else {
1728                                                         pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppp", addr, real_addr, real_addr - vma->vm_start);
1729                                                         break;
1730                                                 }
1731                                         } else {
1732                                                 break;
1733                                         }
1734                                 }
1735                         }
1736                 }
1737         }
1738 }
1739
1740 int uretprobe_event_handler (struct kretprobe_instance *probe, struct pt_regs *regs, us_proc_ip_t * ip)
1741 {
1742         int retval = regs_return_value(regs);
1743         unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1744
1745         find_plt_address(addr);
1746
1747 #if defined(CONFIG_ARM)
1748         if (ip->offset & 0x01)
1749         {
1750                 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr | 0x01, retval);
1751         }else{
1752                 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1753         }
1754 #else
1755         pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1756 #endif
1757         // Mr_Nobody: uncomment for valencia
1758         //unregister_usprobe(current, ip, 1);
1759         return 0;
1760 }
1761
1762 static int register_usprobe (struct task_struct *task, struct mm_struct *mm, us_proc_ip_t * ip, int atomic, kprobe_opcode_t * islot)
1763 {
1764         int ret = 0;
1765         ip->jprobe.kp.tgid = task->tgid;
1766         //ip->jprobe.kp.addr = (kprobe_opcode_t *) addr;
1767         if(!ip->jprobe.entry) {
1768                 if (dbi_ujprobe_event_handler_custom_p != NULL)
1769                 {
1770                         ip->jprobe.entry = (kprobe_opcode_t *) dbi_ujprobe_event_handler_custom_p;
1771                         DPRINTF("Set custom event handler for %x\n", ip->offset);
1772                 }
1773                 else
1774                 {
1775                         ip->jprobe.entry = (kprobe_opcode_t *) ujprobe_event_handler;
1776                         DPRINTF("Set default event handler for %x\n", ip->offset);
1777                 }
1778         }
1779         if(!ip->jprobe.pre_entry) {
1780                 if (dbi_ujprobe_event_pre_handler_custom_p != NULL)
1781                 {
1782                         ip->jprobe.pre_entry = (kprobe_pre_entry_handler_t) dbi_ujprobe_event_pre_handler_custom_p;
1783                         DPRINTF("Set custom pre handler for %x\n", ip->offset);
1784                 }
1785                 else
1786                 {
1787                         ip->jprobe.pre_entry = (kprobe_pre_entry_handler_t) ujprobe_event_pre_handler;
1788                         DPRINTF("Set default pre handler for %x\n", ip->offset);
1789                 }
1790         }
1791         ip->jprobe.priv_arg = ip;
1792         ret = dbi_register_ujprobe (task, mm, &ip->jprobe, atomic);
1793         if (ret)
1794         {
1795                 DPRINTF ("dbi_register_ujprobe() failure %d", ret);
1796                 return ret;
1797         }
1798
1799         // Mr_Nobody: comment for valencia
1800         ip->retprobe.kp.tgid = task->tgid;
1801         //ip->retprobe.kp.addr = (kprobe_opcode_t *) addr;
1802         if(!ip->retprobe.handler) {
1803                 if (dbi_uretprobe_event_handler_custom_p != NULL)
1804                         ip->retprobe.handler = (kretprobe_handler_t) dbi_uretprobe_event_handler_custom_p;
1805                 else {
1806                         ip->retprobe.handler = (kretprobe_handler_t) uretprobe_event_handler;
1807                         //DPRINTF("Failed custom dbi_uretprobe_event_handler_custom_p");
1808                 }
1809         }
1810         ip->retprobe.priv_arg = ip;
1811         ret = dbi_register_uretprobe (task, mm, &ip->retprobe, atomic);
1812         if (ret)
1813         {
1814                 EPRINTF ("dbi_register_uretprobe() failure %d", ret);
1815                 return ret;
1816         }
1817         return 0;
1818 }
1819
1820 static int unregister_usprobe (struct task_struct *task, us_proc_ip_t * ip, int atomic)
1821 {
1822         dbi_unregister_ujprobe (task, &ip->jprobe, atomic);
1823         dbi_unregister_uretprobe (task, &ip->retprobe, atomic);
1824         return 0;
1825 }
1826
1827 unsigned long get_stack_size(struct task_struct *task,
1828                 struct pt_regs *regs)
1829 {
1830 #ifdef CONFIG_ADD_THREAD_STACK_INFO
1831         return (task->stack_start - dbi_get_stack_ptr(regs));
1832 #else
1833         struct vm_area_struct *vma = NULL;
1834         struct mm_struct *mm = NULL;
1835         unsigned long result = 0;
1836     int atomic = in_atomic();
1837
1838         mm = (atomic ? task->active_mm: get_task_mm(task));
1839
1840         if (mm) {
1841                 if (!atomic)
1842                         down_read(&mm->mmap_sem);
1843
1844                 vma = find_vma(mm, dbi_get_stack_ptr(regs));
1845
1846                 if (vma)
1847                         result = vma->vm_end - dbi_get_stack_ptr(regs);
1848                 else
1849                         result = 0;
1850
1851                 if (!atomic) {
1852                         up_read(&mm->mmap_sem);
1853                         mmput(mm);
1854                 }
1855         }
1856
1857         return result;
1858 #endif
1859 }
1860 EXPORT_SYMBOL_GPL(get_stack_size);
1861
1862 unsigned long get_stack(struct task_struct *task, struct pt_regs *regs,
1863                 char *buf, unsigned long sz)
1864 {
1865         unsigned long stack_sz = get_stack_size(task, regs);
1866         unsigned long real_sz = (stack_sz > sz ? sz: stack_sz);
1867         int res = read_proc_vm_atomic(task, dbi_get_stack_ptr(regs), buf, real_sz);
1868         return res;
1869 }
1870 EXPORT_SYMBOL_GPL(get_stack);
1871
1872 int dump_to_trace(probe_id_t probe_id, void *addr, const char *buf,
1873                 unsigned long sz)
1874 {
1875         unsigned long rest_sz = sz;
1876         const char *data = buf;
1877
1878         while (rest_sz >= EVENT_MAX_SIZE) {
1879                 pack_event_info(probe_id, RECORD_ENTRY, "pa",
1880                                 addr, EVENT_MAX_SIZE, data);
1881                 rest_sz -= EVENT_MAX_SIZE;
1882                 data += EVENT_MAX_SIZE;
1883         }
1884
1885         if (rest_sz > 0)
1886                 pack_event_info(probe_id, RECORD_ENTRY, "pa", addr, rest_sz, data);
1887
1888         return 0;
1889 }
1890 EXPORT_SYMBOL_GPL(dump_to_trace);
1891
1892 int dump_backtrace(probe_id_t probe_id, struct task_struct *task,
1893                 void *addr, struct pt_regs *regs, unsigned long sz)
1894 {
1895         unsigned long real_sz = 0;
1896         char *buf = NULL;
1897
1898         buf = (char *)kmalloc(sz, GFP_ATOMIC);
1899
1900         if (buf != NULL) {
1901                 real_sz = get_stack(task, regs, buf, sz);
1902                 if (real_sz > 0)
1903                         dump_to_trace(probe_id, addr, buf, real_sz);
1904                 kfree(buf);
1905                 return 0;
1906         } else {
1907                 return -1;
1908         }
1909 }
1910 EXPORT_SYMBOL_GPL(dump_backtrace);
1911
1912 unsigned long get_ret_addr(struct task_struct *task, us_proc_ip_t *ip)
1913 {
1914         unsigned long retaddr = 0;
1915         struct hlist_node *item, *tmp_node;
1916         struct kretprobe_instance *ri;
1917
1918         if (ip) {
1919                 hlist_for_each_safe (item, tmp_node, &ip->retprobe.used_instances) {
1920                         ri = hlist_entry (item, struct kretprobe_instance, uflist);
1921
1922                         if (ri->task && ri->task->pid == task->pid &&
1923                                         ri->task->tgid == task->tgid)
1924                                 retaddr = (unsigned long)ri->ret_addr;
1925                 }
1926         }
1927
1928         if (retaddr)
1929                 return retaddr;
1930         else
1931                 return dbi_get_ret_addr(task_pt_regs(task));
1932 }
1933 EXPORT_SYMBOL_GPL(get_ret_addr);
1934
1935 /*function call removes all OTG-probes installed in library "lib_to_delete"*/
1936 void otg_probe_list_clean(char* lib_to_delete)
1937 {
1938         struct task_struct *task = current->group_leader;
1939         struct mm_struct *mm;
1940         struct vm_area_struct *vma = 0;
1941         char *filename = "";
1942         char *buf = "";
1943         unsigned long int addr_max = 0;
1944         unsigned long int addr_min = 0;
1945         int err;
1946         us_proc_otg_ip_t *p;
1947
1948         mm = task->active_mm;
1949 /*find in process space map file with name "lib_to_delete" and flag VM_EXEC
1950 and save address borders of this file*/
1951         if (mm) {
1952                 vma = mm->mmap;
1953                 while (vma) {
1954                         if(vma->vm_file) {
1955                                 if(vma->vm_file->f_dentry) {
1956                                         filename = d_path(&vma->vm_file->f_path, buf, 256);
1957                                         if((strcmp(lib_to_delete, filename) == 0) && (vma->vm_flags & VM_EXEC)) {
1958                                                 addr_min = vma->vm_start;
1959                                                 addr_max = vma->vm_end;
1960                                                 break;
1961                                         }
1962                                 }
1963                         }
1964                         vma = vma->vm_next;
1965                 }
1966         }
1967 /*remove OTG-probe if its address is between addr_min and addr_max*/
1968         list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
1969                 if (!p->ip.installed) {
1970                         continue;
1971                 }
1972                 if ( ((unsigned long)p->ip.jprobe.kp.addr <  addr_max) &&
1973                      ((unsigned long)p->ip.jprobe.kp.addr >= addr_min) ) {
1974                         err = unregister_usprobe(task, &p->ip, 1);
1975                         if (err != 0) {
1976                                 EPRINTF("failed to uninstall IP at %p. Error %d!",
1977                                          p->ip.jprobe.kp.addr, err);
1978                                 continue;
1979                         }
1980                         p->ip.installed = 0;
1981                 }
1982         }
1983 }
1984 EXPORT_SYMBOL_GPL(otg_probe_list_clean);