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