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