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