1 ////////////////////////////////////////////////////////////////////////////////////
3 // FILE: us_proc_inst.c
6 // This file is C source for SWAP driver.
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
14 // REVISION DATE: 2008.12.02
16 ////////////////////////////////////////////////////////////////////////////////////
19 #include "us_proc_inst.h"
21 #include "../kprobe/dbi_kprobes_deps.h"
22 #include "../kprobe/dbi_uprobes.h"
24 #define mm_read_lock(task, mm, atomic, lock) \
25 mm = atomic ? task->active_mm : get_task_mm(task); \
28 panic("ERRR mm_read_lock: mm == NULL\n"); \
32 lock = down_read_trylock(&mm->mmap_sem); \
35 down_read(&mm->mmap_sem); \
38 #define mm_read_unlock(mm, atomic, lock) \
40 up_read(&mm->mmap_sem); \
47 DEFINE_PER_CPU (us_proc_vtp_t *, gpVtp) = NULL;
48 DEFINE_PER_CPU (struct pt_regs *, gpCurVtpRegs) = NULL;
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]
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!!!
59 unsigned long (*dbi_ujprobe_event_pre_handler_custom_p)
60 (us_proc_ip_t *, struct pt_regs *) = NULL;
61 EXPORT_SYMBOL(dbi_ujprobe_event_pre_handler_custom_p);
62 void (*dbi_ujprobe_event_handler_custom_p)(void) = NULL;
63 EXPORT_SYMBOL(dbi_ujprobe_event_handler_custom_p);
64 int (*dbi_uretprobe_event_handler_custom_p)
65 (struct kretprobe_instance *, struct pt_regs *, us_proc_ip_t *) = NULL;
66 EXPORT_SYMBOL(dbi_uretprobe_event_handler_custom_p);
68 unsigned long ujprobe_event_pre_handler (us_proc_ip_t * ip, struct pt_regs *regs);
69 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6);
70 int uretprobe_event_handler (struct kretprobe_instance *probe, struct pt_regs *regs, us_proc_ip_t * ip);
72 static int register_usprobe (struct task_struct *task, struct mm_struct *mm, us_proc_ip_t * ip, int atomic, kprobe_opcode_t * islot);
73 static int unregister_usprobe (struct task_struct *task, us_proc_ip_t * ip, int atomic, int no_rp2);
77 struct task_inst_info_node {
78 struct list_head plist;
79 inst_us_proc_t * task_inst_info;
82 LIST_HEAD(task_inst_info_list);
85 struct dentry *launchpad_daemon_dentry = NULL;
86 EXPORT_SYMBOL_GPL(launchpad_daemon_dentry);
90 unsigned long android_app_vma_start = 0;
91 unsigned long android_app_vma_end = 0;
92 struct dentry *app_process_dentry = NULL;
93 #endif /* ANDROID_APP */
96 struct dentry *libdvm_dentry = NULL;
97 /* Defines below are for libdvm.so with md5sum:
98 * 5941c87b49198368e7db726c2977bf1d */
99 #define LIBDVM_ENTRY 0x30a64
100 #define LIBDVM_RETURN 0x30bdc
101 #endif /* __ANDROID */
104 static inline int is_libonly(void)
106 return !strcmp(us_proc_info.path,"*");
109 // is user-space instrumentation
110 static inline int is_us_instrumentation(void)
112 return !!us_proc_info.path;
115 us_proc_otg_ip_t *find_otg_probe(unsigned long addr)
119 list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
120 if (p->ip.offset == addr) {
128 int add_otg_probe_to_list(unsigned long addr, us_proc_otg_ip_t **pprobe)
130 us_proc_otg_ip_t *new_probe;
131 us_proc_otg_ip_t *probe;
136 /* check if such probe does already exist */
137 probe = find_otg_probe(addr);
142 new_probe = kmalloc(sizeof(us_proc_otg_ip_t), GFP_KERNEL);
144 EPRINTF ("no memory for new probe!");
147 memset(new_probe,0, sizeof(us_proc_otg_ip_t));
149 new_probe->ip.offset = addr;
150 new_probe->ip.jprobe.kp.addr =
151 new_probe->ip.retprobe.kp.addr = (kprobe_opcode_t *)addr;
152 new_probe->ip.jprobe.priv_arg =
153 new_probe->ip.retprobe.priv_arg = new_probe;
155 INIT_LIST_HEAD(&new_probe->list);
156 list_add_rcu(&new_probe->list, &otg_us_proc_info);
164 int remove_otg_probe_from_list(unsigned long addr)
168 //check if such probe does exist
169 p = find_otg_probe(addr);
171 /* We do not care about it. Nothing bad. */
175 list_del_rcu(&p->list);
183 static struct proc_probes *proc_probes_copy(struct proc_probes *proc_p);
184 static void print_proc_probes(const struct proc_probes *proc_p);
186 * Prepare copy of instrumentation data for task
187 * in case of library only instrumentation
190 inst_us_proc_t* copy_task_inst_info (struct task_struct *task, inst_us_proc_t * task_inst_info)
193 kprobe_opcode_t *entry_save;
194 kprobe_pre_entry_handler_t pre_entry_save;
195 kretprobe_handler_t handler_save;
197 inst_us_proc_t* copy_info = 0;
199 int unres_ips_count = 0, unres_vtps_count = 0;
201 copy_info = kmalloc (sizeof (inst_us_proc_t), GFP_ATOMIC);
202 memset ((void *) copy_info, 0, sizeof (inst_us_proc_t));
204 copy_info->path = task_inst_info->path;
205 copy_info->m_f_dentry = NULL;
207 copy_info->libs_count = task_inst_info->libs_count;
209 kmalloc (task_inst_info->libs_count * sizeof (us_proc_lib_t), GFP_ATOMIC);
211 if (!copy_info->p_libs) {
212 DPRINTF ("No enough memory for copy_info->p_libs");
215 memcpy (copy_info->p_libs, task_inst_info->p_libs,
216 copy_info->libs_count * sizeof (us_proc_lib_t));
218 for (i = 0; i < copy_info->libs_count; i++) {
219 if (copy_info->p_libs[i].ips_count > 0)
221 unres_ips_count += copy_info->p_libs[i].ips_count;
223 copy_info->p_libs[i].p_ips =
224 kmalloc (copy_info->p_libs[i].ips_count * sizeof (us_proc_ip_t), GFP_ATOMIC);
226 if (!copy_info->p_libs[i].p_ips) {
227 DPRINTF ("No enough memory for copy_info->p_libs[i].p_ips");
231 memcpy (copy_info->p_libs[i].p_ips, task_inst_info->p_libs[i].p_ips,
232 copy_info->p_libs[i].ips_count * sizeof (us_proc_ip_t));
233 for (j = 0; j < copy_info->p_libs[i].ips_count; j++) {
235 entry_save = copy_info->p_libs[i].p_ips[j].jprobe.entry;
236 pre_entry_save = copy_info->p_libs[i].p_ips[j].jprobe.pre_entry;
237 handler_save = copy_info->p_libs[i].p_ips[j].retprobe.handler;
239 copy_info->p_libs[i].p_ips[j].installed = 0;
240 memset(©_info->p_libs[i].p_ips[j].jprobe, 0, sizeof(struct jprobe));
241 memset(©_info->p_libs[i].p_ips[j].retprobe, 0, sizeof(struct kretprobe));
244 copy_info->p_libs[i].p_ips[j].jprobe.entry = entry_save;
245 copy_info->p_libs[i].p_ips[j].jprobe.pre_entry = pre_entry_save;
246 copy_info->p_libs[i].p_ips[j].retprobe.handler = handler_save;
249 unres_ips_count += copy_info->p_libs[i].ips_count;
252 for (j = 0; j < copy_info->p_libs[i].plt_count; j++)
254 copy_info->p_libs[i].p_plt[j].real_func_addr = 0;
257 if (copy_info->p_libs[i].vtps_count > 0) {
258 unres_vtps_count += copy_info->p_libs[i].vtps_count;
260 copy_info->p_libs[i].p_vtps =
261 kmalloc (copy_info->p_libs[i].vtps_count * sizeof (us_proc_vtp_t), GFP_ATOMIC);
263 if (!copy_info->p_libs[i].p_vtps) {
264 DPRINTF ("No enough memory for copy_info->p_libs[i].p_vtps");
268 memcpy (copy_info->p_libs[i].p_vtps, task_inst_info->p_libs[i].p_vtps,
269 copy_info->p_libs[i].vtps_count * sizeof (us_proc_vtp_t));
270 for (j = 0; j < copy_info->p_libs[i].vtps_count; j++) {
271 copy_info->p_libs[i].p_vtps[j].installed = 0;
272 memset (©_info->p_libs[i].p_vtps[j].jprobe, 0, sizeof(struct jprobe));
274 unres_vtps_count = copy_info->p_libs[i].vtps_count;
277 copy_info->p_libs[i].m_f_dentry = task_inst_info->p_libs[i].m_f_dentry;
278 copy_info->p_libs[i].loaded = 0;
280 copy_info->p_libs[i].vma_start = 0;
281 copy_info->p_libs[i].vma_end = 0;
283 copy_info->unres_ips_count = unres_ips_count;
284 copy_info->unres_vtps_count = unres_vtps_count;
286 copy_info->pp = proc_probes_copy(task_inst_info->pp);
288 // print_proc_probes(copy_info->pp);
293 inst_us_proc_t* get_task_inst_node(struct task_struct *task)
295 struct task_inst_info_node *node, *tnode;
297 list_for_each_entry_safe(node, tnode, &task_inst_info_list, plist)
299 if (node && task && node->tgid == task->tgid) {
300 return node->task_inst_info;
306 void put_task_inst_node(struct task_struct *task, inst_us_proc_t *task_inst_info)
308 struct task_inst_info_node * node;
310 node = kmalloc (sizeof(struct task_inst_info_node), GFP_ATOMIC);
312 node->tgid = task->tgid;
313 node->task_inst_info = task_inst_info;
315 list_add_tail (&(node->plist), &task_inst_info_list);
319 void clear_task_inst_info(void)
321 struct list_head *node, *tmp;
323 list_for_each_safe(node, tmp, &task_inst_info_list)
328 static int is_slp_app_with_dentry(struct vm_area_struct *vma,
329 struct dentry *dentry)
331 struct vm_area_struct *slp_app_vma = NULL;
333 if (vma->vm_file->f_dentry == launchpad_daemon_dentry) {
335 while (slp_app_vma) {
336 if (slp_app_vma->vm_file) {
337 if (slp_app_vma->vm_file->f_dentry == dentry &&
338 slp_app_vma->vm_pgoff == 0) {
342 slp_app_vma = slp_app_vma->vm_next;
351 static int is_android_app_with_dentry(struct vm_area_struct *vma,
352 struct dentry *dentry)
354 struct vm_area_struct *android_app_vma = NULL;
356 if (vma->vm_file->f_dentry == app_process_dentry) {
357 android_app_vma = vma;
358 while (android_app_vma) {
359 if (android_app_vma->vm_file) {
360 if (android_app_vma->vm_file->f_dentry == dentry) {
361 android_app_vma_start = android_app_vma->vm_start;
362 android_app_vma_end = android_app_vma->vm_end;
366 android_app_vma = android_app_vma->vm_next;
372 #endif /* ANDROID_APP */
375 void find_libdvm_for_task(struct task_struct *task, inst_us_proc_t *info)
377 struct vm_area_struct *vma = NULL;
378 struct mm_struct *mm = NULL;
380 mm = get_task_mm(task);
385 if (vma->vm_file->f_dentry == libdvm_dentry) {
386 info->libdvm_start = vma->vm_start;
387 info->libdvm_end = vma->vm_end;
396 #endif /* __ANDROID */
398 struct dentry *dentry_by_path(const char *path)
400 struct dentry *dentry;
401 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
403 if (kern_path(path, LOOKUP_FOLLOW, &st_path) != 0) {
404 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
406 if (path_lookup(path, LOOKUP_FOLLOW, &nd) != 0) {
407 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
408 EPRINTF("failed to lookup dentry for path %s!", path);
412 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
415 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38)
416 dentry = nd.path.dentry;
418 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
419 dentry = st_path.dentry;
421 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) */
425 static int find_task_by_path (const char *path, struct task_struct **p_task, struct list_head *tids)
428 struct task_struct *task;
429 struct vm_area_struct *vma;
430 struct mm_struct *mm;
431 struct dentry *dentry = dentry_by_path(path);
435 /* find corresponding dir entry, this is also check for valid path */
436 // TODO: test - try to instrument process with non-existing path
437 // TODO: test - try to instrument process with existing path and delete file just after start
438 if (dentry == NULL) {
443 for_each_process (task) {
445 if ( 0 != inst_pid && ( inst_pid != task->pid ) )
448 mm = get_task_mm(task);
453 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
454 if (vma->vm_file->f_dentry == dentry) {
457 get_task_struct (task);
463 if (is_slp_app_with_dentry(vma, dentry)) {
465 get_task_struct(task);
471 if (is_android_app_with_dentry(vma, dentry)) {
473 get_task_struct(task);
476 #endif /* ANDROID_APP */
480 // only decrement usage count on mm since we cannot sleep here
481 atomic_dec(&mm->mm_users);
488 DPRINTF ("found pid %d for %s.", (*p_task)->pid, path);
489 *p_task = (*p_task)->group_leader;
490 gl_nNotifyTgid = (*p_task)->tgid;
492 DPRINTF ("pid for %s not found!", path);
499 static void us_vtp_event_pre_handler (us_proc_vtp_t * vtp, struct pt_regs *regs)
501 __get_cpu_var(gpVtp) = vtp;
502 __get_cpu_var(gpCurVtpRegs) = regs;
505 static void us_vtp_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
507 us_proc_vtp_t *vtp = __get_cpu_var(gpVtp);
508 #if !defined(CONFIG_X86)
509 struct pt_regs *regs = __get_cpu_var(gpCurVtpRegs);
515 us_proc_vtp_data_t *vtp_data;
521 list_for_each_entry_rcu (vtp_data, &vtp->list, list) {
522 // DPRINTF ("[%d]proc %s(%d): %lx", nCount++, current->comm, current->pid, vtp->addr);
523 fmt[1] = vtp_data->type;
524 if (vtp_data->reg == -1)
525 vaddr = vtp_data->off;
527 vaddr = ARCH_REG_VAL (regs, vtp_data->reg) + vtp_data->off;
528 // DPRINTF ("VTP type '%c'", vtp_data->type);
529 switch (vtp_data->type)
534 if (read_proc_vm_atomic (current, vaddr, &ival, sizeof (ival)) < sizeof (ival))
535 EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
537 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, ival, vtp_data->name);
540 if (read_proc_vm_atomic (current, vaddr, &ival, sizeof (ival)) < sizeof (ival))
541 EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
543 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, ival, vtp_data->name);
546 if (read_proc_vm_atomic (current, vaddr, &cval, sizeof (cval)) < sizeof (cval))
547 EPRINTF ("failed to read vm of proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
549 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, cval, vtp_data->name);
552 if (current->active_mm) {
554 struct vm_area_struct *vma;
557 if (get_user_pages_atomic (current, current->active_mm, vaddr, 1, 0, 1, &page, &vma) <= 0) {
558 EPRINTF ("get_user_pages_atomic failed for proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
561 maddr = kmap_atomic (page, KM_USER0);
562 len = strlen (maddr + (vaddr & ~PAGE_MASK));
563 sval = kmalloc (len + 1, GFP_KERNEL);
565 EPRINTF ("failed to alloc memory for string in proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
567 copy_from_user_page (vma, page, vaddr, sval, maddr + (vaddr & ~PAGE_MASK), len + 1);
568 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, sval, vtp_data->name);
571 kunmap_atomic (maddr, KM_USER0);
572 page_cache_release (page);
575 EPRINTF ("task %s/%u has no mm!", current->comm, current->pid);
578 EPRINTF ("unknown variable type '%c'", vtp_data->type);
581 dbi_uprobe_return ();
584 static int install_mapped_ips (struct task_struct *task, inst_us_proc_t* task_inst_info, int atomic)
586 struct vm_area_struct *vma;
589 unsigned int old_ips_count, old_vtps_count;
591 struct task_struct *t;
592 struct mm_struct *mm;
594 mm = atomic ? task->active_mm : get_task_mm (task);
596 return task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count;
598 old_ips_count = task_inst_info->unres_ips_count;
599 old_vtps_count = task_inst_info->unres_vtps_count;
601 down_read (&mm->mmap_sem);
604 // skip non-text section
606 if (vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || !vma->vm_file || (vma->vm_flags & VM_ACCOUNT) ||
607 !(vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) ||
608 !(vma->vm_flags & (VM_READ | VM_MAYREAD))) {
610 if (vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || !vma->vm_file) {
616 * After process was forked, some time it inherits parent process environment.
617 * We need to renew instrumentation when we detect that process gets own environment.
619 for (i = 0; i < task_inst_info->libs_count; i++) {
620 // struct path tmp_path;
621 // tmp_path.dentry = task_inst_info->p_libs[i].m_f_dentry;
622 // tmp_path.mnt = task_inst_info->p_libs[i].m_vfs_mount;
623 // char* p_path = d_path ( &tmp_path, path_buffer, 255 );
624 // DPRINTF("f_dentry:%x m_f_dentry:%x path:%s", vma->vm_file->f_dentry,
625 // task_inst_info->p_libs[i].m_f_dentry, p_path );
627 //TODO: test - try to instrument non-existing libs
628 if (vma->vm_file->f_dentry == task_inst_info->p_libs[i].m_f_dentry) {
629 // DPRINTF("vm_flags:%x loaded:%x ips_count:%d vtps_count:%d",
630 // vma->vm_flags, task_inst_info->p_libs[i].loaded,
631 // task_inst_info->p_libs[i].ips_count, task_inst_info->p_libs[i].vtps_count );
632 if (!task_inst_info->p_libs[i].loaded) {
633 // DPRINTF("!VM_EXECUTABLE && !loaded");
635 int app_flag = (vma->vm_file->f_dentry == task_inst_info->m_f_dentry);
636 DPRINTF ("post dyn lib event %s/%s", current->comm, task_inst_info->p_libs[i].path);
637 // if we installed something, post library info for those IPs
638 p = strrchr(task_inst_info->p_libs[i].path, '/');
640 p = task_inst_info->p_libs[i].path;
643 task_inst_info->p_libs[i].loaded = 1;
644 task_inst_info->p_libs[i].vma_start = vma->vm_start;
645 task_inst_info->p_libs[i].vma_end = vma->vm_end;
646 task_inst_info->p_libs[i].vma_flag = vma->vm_flags;
647 pack_event_info (DYN_LIB_PROBE_ID, RECORD_ENTRY, "dspdd",
648 task->tgid, p, vma->vm_start, vma->vm_end-vma->vm_start, app_flag);
650 for (k = 0; k < task_inst_info->p_libs[i].ips_count; k++) {
651 DPRINTF("ips_count current:%d", k);
652 if (!task_inst_info->p_libs[i].p_ips[k].installed) {
653 DPRINTF("!installed");
654 addr = task_inst_info->p_libs[i].p_ips[k].offset;
655 addr += vma->vm_start;
656 if (page_present (mm, addr)) {
657 DPRINTF ("pid %d, %s sym is loaded at %lx/%lx.",
658 task->pid, task_inst_info->p_libs[i].path,
659 task_inst_info->p_libs[i].p_ips[k].offset, addr);
660 task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr = (kprobe_opcode_t *) addr;
661 task_inst_info->p_libs[i].p_ips[k].retprobe.kp.addr = (kprobe_opcode_t *) addr;
662 task_inst_info->p_libs[i].p_ips[k].installed = 1;
663 task_inst_info->unres_ips_count--;
664 err = register_usprobe (task, mm, &task_inst_info->p_libs[i].p_ips[k], atomic, 0);
666 DPRINTF ("failed to install IP at %lx/%p. Error %d!",
667 task_inst_info->p_libs[i].p_ips[k].offset,
668 task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
673 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; k++) {
674 DPRINTF("vtps_count current:%d", k);
675 if (!task_inst_info->p_libs[i].p_vtps[k].installed) {
676 DPRINTF("!installed");
677 addr = task_inst_info->p_libs[i].p_vtps[k].addr;
678 if (!(vma->vm_flags & VM_EXECUTABLE))
679 addr += vma->vm_start;
680 if (page_present (mm, addr)) {
681 DPRINTF ("pid %d, %s sym is loaded at %lx/%lx.",
682 task->pid, task_inst_info->p_libs[i].path,
683 task_inst_info->p_libs[i].p_ips[k].offset, addr);
684 task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.tgid = task_inst_info->tgid;
685 task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr = (kprobe_opcode_t *) addr;
686 task_inst_info->p_libs[i].p_vtps[k].jprobe.entry = (kprobe_opcode_t *) us_vtp_event_handler;
687 task_inst_info->p_libs[i].p_vtps[k].jprobe.pre_entry = (kprobe_pre_entry_handler_t) us_vtp_event_pre_handler;
688 task_inst_info->p_libs[i].p_vtps[k].jprobe.priv_arg = &task_inst_info->p_libs[i].p_vtps[k];
689 task_inst_info->p_libs[i].p_vtps[k].installed = 1;
690 task_inst_info->unres_vtps_count--;
691 err = dbi_register_ujprobe (task, mm, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
693 EPRINTF ("failed to install VTP at %p. Error %d!",
694 task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr, err);
702 if (is_java_inst_enabled()
703 && vma->vm_file->f_dentry == libdvm_dentry) {
704 us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
706 && task_inst_info->libdvm_start) {
707 unsigned long addr = LIBDVM_ENTRY + task_inst_info->libdvm_start;
708 if (page_present(mm, addr)) {
709 entp->jprobe.kp.tgid = task->tgid;
710 entp->jprobe.pre_entry = ujprobe_event_pre_handler;
711 entp->jprobe.entry = ujprobe_event_handler;
712 entp->jprobe.priv_arg = entp;
713 entp->jprobe.kp.addr = addr;
714 entp->retprobe.kp.tgid = task->tgid;
715 entp->retprobe.handler = uretprobe_event_handler;
716 entp->retprobe.priv_arg = entp;
717 entp->retprobe.kp.addr = addr;
718 err = register_usprobe(task, mm, entp, atomic, 0);
720 DPRINTF("failed to install IP at %p", addr);
725 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
727 && task_inst_info->libdvm_start) {
728 unsigned long addr = LIBDVM_RETURN + task_inst_info->libdvm_start;
729 if (page_present(mm, addr)) {
730 retp->jprobe.kp.tgid = task->tgid;
731 retp->jprobe.pre_entry = ujprobe_event_pre_handler;
732 retp->jprobe.entry = ujprobe_event_handler;
733 retp->jprobe.priv_arg = retp;
734 retp->jprobe.kp.addr = addr;
735 retp->retprobe.kp.tgid = task->tgid;
736 retp->retprobe.handler = uretprobe_event_handler;
737 retp->retprobe.priv_arg = retp;
738 retp->retprobe.kp.addr = addr;
739 err = register_usprobe(task, mm, retp, atomic, 0);
741 DPRINTF("failed to install IP at %p", addr);
747 #endif /* __ANDROID */
750 list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
751 if (p->ip.installed) {
755 if (!page_present(mm, p->ip.offset)) {
756 DPRINTF("Page isn't present for %p.",
761 err = register_usprobe(task, mm, &p->ip, atomic, 0);
763 DPRINTF("failed to install IP at %lx/%p. Error %d!",
765 p->ip.jprobe.kp.addr, err);
768 task_inst_info->unres_otg_ips_count--;
771 up_read (&mm->mmap_sem);
774 return task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count;
777 static int install_otg_ip(unsigned long addr,
778 unsigned long pre_handler,
779 unsigned long jp_handler,
780 unsigned long rp_handler)
783 us_proc_otg_ip_t *pprobe;
784 struct task_struct *task = current->group_leader;
785 struct mm_struct *mm = task->mm;
787 /* Probe preparing */
788 err = add_otg_probe_to_list(addr, &pprobe);
791 DPRINTF("OTG probe %p already installed.", addr);
794 DPRINTF("Failed to add new OTG probe, err=%d",err);
799 pprobe->ip.jprobe.pre_entry =
800 (kprobe_pre_entry_handler_t)pre_handler;
802 pprobe->ip.jprobe.pre_entry =
803 (kprobe_pre_entry_handler_t)
804 dbi_ujprobe_event_pre_handler_custom_p;
808 pprobe->ip.jprobe.entry =
809 (kprobe_opcode_t *)jp_handler;
811 pprobe->ip.jprobe.entry =
813 dbi_ujprobe_event_handler_custom_p;
816 pprobe->ip.retprobe.handler =
817 (kretprobe_handler_t)rp_handler;
819 pprobe->ip.retprobe.handler =
820 (kretprobe_handler_t)
821 dbi_uretprobe_event_handler_custom_p;
824 pprobe->tgid = task->tgid;
825 if (!page_present(mm, addr)) {
826 DPRINTF("Page isn't present for %p.", addr);
828 us_proc_info.unres_otg_ips_count++;
829 /* Probe will be installed in do_page_fault handler */
832 DPRINTF("Page present for %p.", addr);
834 /* Probe installing */
835 pprobe->ip.installed = 1;
836 err = register_usprobe(current, mm, &pprobe->ip, 1, 0);
838 DPRINTF("failed to install IP at %lx/%p. Error %d!",
839 addr, pprobe->ip.jprobe.kp.addr, err);
844 EXPORT_SYMBOL_GPL(install_otg_ip);
847 static int uninstall_mapped_ips (struct task_struct *task, inst_us_proc_t* task_inst_info, int atomic)
852 for (i = 0; i < task_inst_info->libs_count; i++)
854 DPRINTF ("clear lib %s.", task_inst_info->p_libs[i].path);
855 for (k = 0; k < task_inst_info->p_libs[i].ips_count; k++)
857 if (task_inst_info->p_libs[i].p_ips[k].installed)
859 DPRINTF ("remove IP at %p.", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr);
860 err = unregister_usprobe (task, &task_inst_info->p_libs[i].p_ips[k], atomic, 0);
863 EPRINTF ("failed to uninstall IP at %p. Error %d!", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
866 task_inst_info->unres_ips_count++;
867 task_inst_info->p_libs[i].p_ips[k].installed = 0;
870 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; k++)
872 if (task_inst_info->p_libs[i].p_vtps[k].installed)
874 dbi_unregister_ujprobe (task, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
875 task_inst_info->unres_vtps_count++;
876 task_inst_info->p_libs[i].p_vtps[k].installed = 0;
879 task_inst_info->p_libs[i].loaded = 0;
882 if (is_java_inst_enabled()) {
883 us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
884 if (entp->installed) {
885 unregister_usprobe(task, entp, atomic);
888 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
889 if (retp->installed) {
890 unregister_usprobe(task, retp, atomic);
894 #endif /* __ANDROID */
895 list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
896 if (!p->ip.installed) {
899 DPRINTF("remove OTG IP at %p.", p->ip.offset);
900 err = unregister_usprobe(task, &p->ip, atomic, 0);
902 EPRINTF("failed to uninstall IP at %p. Error %d!",
903 p->ip.jprobe.kp.addr, err);
909 DPRINTF ("Ures IPs %d.", task_inst_info->unres_ips_count);
910 DPRINTF ("Ures VTPs %d.", task_inst_info->unres_vtps_count);
914 static int uninstall_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
916 kernel_probe_t *probe = NULL;
918 if (probes_flags & kflag) {
919 probe = find_probe(addr);
921 iRet = remove_probe_from_list (addr);
923 EPRINTF ("remove_probe_from_list(0x%lx) result=%d!", addr, iRet);
927 probes_flags &= ~kflag;
929 if (us_proc_probes & uflag) {
930 if (!(probes_flags & uflag)) {
932 iRet = unregister_kernel_probe(probe);
934 EPRINTF ("unregister_kernel_probe(0x%lx) result=%d!",
940 us_proc_probes &= ~uflag;
948 static int uninstall_us_proc_probes(struct task_struct *task, struct proc_probes *proc_p, enum US_FLAGS flag);
950 int deinst_usr_space_proc (void)
952 int iRet = 0, found = 0;
953 struct task_struct *task = 0;
954 inst_us_proc_t *task_inst_info = NULL;
956 if (!is_us_instrumentation()) {
960 iRet = uninstall_kernel_probe (pf_addr, US_PROC_PF_INSTLD,
963 EPRINTF ("uninstall_kernel_probe(do_page_fault) result=%d!", iRet);
965 iRet = uninstall_kernel_probe (cp_addr, US_PROC_CP_INSTLD,
968 EPRINTF ("uninstall_kernel_probe(copy_process) result=%d!", iRet);
970 iRet = uninstall_kernel_probe (mr_addr, US_PROC_MR_INSTLD,
973 EPRINTF ("uninstall_kernel_probe(mm_release) result=%d!", iRet);
975 iRet = uninstall_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD,
978 EPRINTF ("uninstall_kernel_probe(do_exit) result=%d!", iRet);
980 iRet = uninstall_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD,
983 EPRINTF ("uninstall_kernel_probe(do_munmap) result=%d!", iRet);
987 for_each_process (task)
989 task_inst_info = get_task_inst_node(task);
992 iRet = uninstall_us_proc_probes(task, task_inst_info->pp, US_UNREGS_PROBE);
994 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
995 dbi_unregister_all_uprobes(task, 1);
1001 if (us_proc_info.tgid == 0)
1004 for_each_process (task)
1006 if (task->tgid == us_proc_info.tgid)
1009 get_task_struct (task);
1018 iRet = uninstall_us_proc_probes(task, us_proc_info.pp, US_UNREGS_PROBE);
1020 EPRINTF ("failed to uninstall IPs %d!", iRet);
1021 put_task_struct (task);
1023 printk("### 1 ### dbi_unregister_all_uprobes:\n");
1024 dbi_unregister_all_uprobes(task, 1);
1025 us_proc_info.tgid = 0;
1026 for(i = 0; i < us_proc_info.libs_count; i++)
1027 us_proc_info.p_libs[i].loaded = 0;
1033 static int install_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
1035 kernel_probe_t *probe = NULL;
1038 DPRINTF("us_proc_probes = 0x%x, uflag = 0x%x, "
1039 "probes_flags = 0x%x, kflag = 0x%x",
1040 us_proc_probes, uflag, probes_flags, kflag);
1042 if (!(probes_flags & kflag)) {
1043 iRet = add_probe_to_list (addr, &probe);
1045 EPRINTF ("add_probe_to_list(0x%lx) result=%d!", addr, iRet);
1048 probes_flags |= kflag;
1050 if (!(us_proc_probes & uflag)) {
1051 if (!(probes_flags & uflag)) {
1052 iRet = register_kernel_probe (probe);
1054 EPRINTF ("register_kernel_probe(0x%lx) result=%d!", addr, iRet);
1058 us_proc_probes |= uflag;
1067 static void install_proc_probes(struct task_struct *task, struct proc_probes *proc_p, int atomic);
1069 int inst_usr_space_proc (void)
1072 struct task_struct *task = 0;
1073 inst_us_proc_t *task_inst_info = NULL;
1075 if (!is_us_instrumentation()) {
1079 DPRINTF("User space instr");
1082 launchpad_daemon_dentry = dentry_by_path("/usr/bin/launchpad_preloading_preinitializing_daemon");
1083 if (launchpad_daemon_dentry == NULL) {
1087 #endif /* SLP_APP */
1090 app_process_dentry = dentry_by_path("/system/bin/app_process");
1091 if (app_process_dentry == NULL) {
1095 android_app_vma_start = 0;
1096 android_app_vma_end = 0;
1097 #endif /* ANDROID_APP */
1100 if (is_java_inst_enabled()) {
1101 libdvm_dentry = dentry_by_path("/system/lib/libdvm.so");
1102 if (libdvm_dentry == NULL) {
1106 memset(&us_proc_info.libdvm_entry_ip, 0, sizeof(us_proc_ip_t));
1107 memset(&us_proc_info.libdvm_return_ip, 0, sizeof(us_proc_ip_t));
1108 us_proc_info.libdvm_start = 0;
1109 us_proc_info.libdvm_end = 0;
1111 #endif /* __ANDROID */
1113 for (i = 0; i < us_proc_info.libs_count; i++) {
1114 us_proc_info.p_libs[i].loaded = 0;
1116 /* check whether process is already running
1117 * 1) if process is running - look for the libraries in the process maps
1118 * 1.1) check if page for symbol does exist
1119 * 1.1.1) if page exists - instrument it
1120 * 1.1.2) if page does not exist - make sure that do_page_fault handler is installed
1121 * 2) if process is not running - make sure that do_page_fault handler is installed
1126 clear_task_inst_info();
1127 for_each_process (task) {
1128 if (task->flags & PF_KTHREAD){
1129 DPRINTF("ignored kernel thread %d\n",
1134 task_inst_info = get_task_inst_node(task);
1135 if (!task_inst_info) {
1137 copy_task_inst_info(task,
1139 put_task_inst_node(task, task_inst_info);
1141 DPRINTF("trying process");
1143 if (is_java_inst_enabled()) {
1144 find_libdvm_for_task(task, task_inst_info);
1146 #endif /* __ANDROID */
1147 install_proc_probes(task, task_inst_info->pp, 1);
1148 //put_task_struct (task);
1149 task_inst_info = NULL;
1154 ret = find_task_by_path (us_proc_info.path, &task, NULL);
1157 DPRINTF("task found. installing probes");
1158 us_proc_info.tgid = task->pid;
1160 if (is_java_inst_enabled()) {
1161 find_libdvm_for_task(task, &us_proc_info);
1163 #endif /* __ANDROID */
1164 install_proc_probes(task, us_proc_info.pp, 0);
1165 put_task_struct (task);
1169 // enable 'do_page_fault' probe to detect when they will be loaded
1170 ret = install_kernel_probe (pf_addr, US_PROC_PF_INSTLD, 0, &pf_probe);
1173 EPRINTF ("install_kernel_probe(do_page_fault) result=%d!", ret);
1176 // enable 'do_exit' probe to detect for remove task_struct
1177 ret = install_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD, 0, &exit_probe);
1180 EPRINTF ("install_kernel_probe(do_exit) result=%d!", ret);
1183 /* enable 'copy_process' */
1184 ret = install_kernel_probe (cp_addr, US_PROC_CP_INSTLD, 0, &cp_probe);
1187 EPRINTF ("instpall_kernel_probe(copy_process) result=%d!", ret);
1191 // enable 'mm_release' probe to detect when for remove user space probes
1192 ret = install_kernel_probe (mr_addr, US_PROC_MR_INSTLD, 0, &mr_probe);
1195 EPRINTF ("install_kernel_probe(mm_release) result=%d!", ret);
1199 // enable 'do_munmap' probe to detect when for remove user space probes
1200 ret = install_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD, 0, &unmap_probe);
1203 EPRINTF ("install_kernel_probe(do_munmap) result=%d!", ret);
1209 #include "../../tools/gpmu/probes/entry_data.h"
1211 extern storage_arg_t sa_dpf;
1213 void do_page_fault_j_pre_code(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
1215 struct task_struct *task = current->group_leader;
1217 if (task->flags & PF_KTHREAD) {
1218 DPRINTF("ignored kernel thread %d\n", task->pid);
1222 if (is_us_instrumentation()) {
1223 if (task->flags & PF_KTHREAD) {
1224 DPRINTF("ignored kernel thread %d\n", task->tgid);
1228 swap_put_entry_data((void *)addr, &sa_dpf);
1231 EXPORT_SYMBOL_GPL(do_page_fault_j_pre_code);
1234 unsigned long imi_sum_time = 0;
1235 unsigned long imi_sum_hit = 0;
1236 EXPORT_SYMBOL_GPL (imi_sum_time);
1237 EXPORT_SYMBOL_GPL (imi_sum_hit);
1239 static void set_mapping_file(struct file_probes *file_p,
1240 const struct proc_probes *proc_p,
1241 const struct task_struct *task,
1242 const struct vm_area_struct *vma)
1244 int app_flag = (vma->vm_file->f_dentry == proc_p->dentry);
1246 // if we installed something, post library info for those IPs
1247 p = strrchr(file_p->path, '/');
1254 file_p->vm_start = vma->vm_start;
1255 file_p->vm_end = vma->vm_end;
1256 pack_event_info(DYN_LIB_PROBE_ID, RECORD_ENTRY, "dspdd",
1257 task->tgid, p, vma->vm_start,
1258 vma->vm_end - vma->vm_start, app_flag);
1261 static int register_us_page_probe(struct page_probes *page_p,
1262 const struct file_probes *file_p,
1263 const struct task_struct *task,
1264 const struct mm_struct *mm)
1269 spin_lock(&page_p->lock);
1271 if (page_p_is_install(page_p)) {
1272 printk("page %x in %s task[tgid=%u, pid=%u] already installed\n",
1273 page_p->offset, file_p->dentry->d_iname, task->tgid, task->pid);
1277 page_p_assert_install(page_p);
1278 page_p_set_all_kp_addr(page_p, file_p);
1280 list_for_each_entry(ip, &page_p->ip_list, list) {
1281 err = register_usprobe_my(task, mm, ip);
1288 page_p_installed(page_p);
1290 spin_unlock(&page_p->lock);
1295 static int unregister_us_page_probe(const struct task_struct *task,
1296 struct page_probes *page_p, enum US_FLAGS flag)
1301 if (page_p->install == 0) {
1305 list_for_each_entry(ip, &page_p->ip_list, list) {
1306 err = unregister_usprobe_my(task, ip, flag);
1313 if (flag != US_DISARM) {
1314 page_p_uninstalled(page_p);
1320 static int check_vma(struct vm_area_struct *vma)
1323 return vma->vm_file && !(vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || (vma->vm_flags & VM_ACCOUNT) ||
1324 !(vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) ||
1325 !(vma->vm_flags & (VM_READ | VM_MAYREAD)));
1327 return vma->vm_file && !(vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC));
1332 static void install_page_probes(unsigned long page, struct task_struct *task, struct proc_probes *proc_p, int atomic)
1335 struct mm_struct *mm;
1336 struct vm_area_struct *vma;
1338 mm_read_lock(task, mm, atomic, lock);
1340 vma = find_vma(mm, page);
1341 if (vma && check_vma(vma)) {
1342 struct file_probes *file_p = proc_p_find_file_p(proc_p, vma);
1344 struct page_probes *page_p;
1345 if (!file_p->loaded) {
1346 set_mapping_file(file_p, proc_p, task, vma);
1350 page_p = file_p_find_page_p(file_p, page);
1352 register_us_page_probe(page_p, file_p, task, mm);
1357 mm_read_unlock(mm, atomic, lock);
1360 static void install_file_probes(struct task_struct *task, struct mm_struct *mm, struct file_probes *file_p)
1362 struct page_probes *page_p = NULL;
1363 struct hlist_node *node = NULL;
1364 struct hlist_head *head = NULL;
1365 int i, table_size = (1 << file_p->page_probes_hash_bits);
1367 for (i = 0; i < table_size; ++i) {
1368 head = &file_p->page_probes_table[i];
1369 hlist_for_each_entry_rcu(page_p, node, head, hlist) {
1370 if (page_present(mm, page_p->offset)) {
1371 register_us_page_probe(page_p, file_p, task, mm);
1377 static void install_proc_probes(struct task_struct *task, struct proc_probes *proc_p, int atomic)
1380 struct vm_area_struct *vma;
1381 struct mm_struct *mm;
1383 mm_read_lock(task, mm, atomic, lock);
1385 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1386 if (check_vma(vma)) {
1387 struct file_probes *file_p = proc_p_find_file_p(proc_p, vma);
1389 if (!file_p->loaded) {
1390 set_mapping_file(file_p, proc_p, task, vma);
1394 install_file_probes(task, mm, file_p);
1399 mm_read_unlock(mm, atomic, lock);
1402 static int unregister_us_file_probes(struct task_struct *task, struct file_probes *file_p, enum US_FLAGS flag)
1405 int table_size = (1 << file_p->page_probes_hash_bits);
1406 struct page_probes *page_p;
1407 struct hlist_node *node, *tmp;
1408 struct hlist_head *head;
1410 for (i = 0; i < table_size; ++i) {
1411 head = &file_p->page_probes_table[i];
1412 hlist_for_each_entry_safe (page_p, node, tmp, head, hlist) {
1413 err = unregister_us_page_probe(task, page_p, flag);
1421 if (flag != US_DISARM) {
1428 static int uninstall_us_proc_probes(struct task_struct *task, struct proc_probes *proc_p, enum US_FLAGS flag)
1432 for (i = 0; i < proc_p->cnt; ++i) {
1433 err = unregister_us_file_probes(task, proc_p->file_p[i], flag);
1443 static pid_t find_proc_by_task(const struct task_struct *task, const struct dentry *dentry)
1445 struct vm_area_struct *vma;
1446 struct mm_struct *mm = task->active_mm;
1451 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1452 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
1453 if (vma->vm_file->f_dentry == dentry) {
1457 if (is_slp_app_with_dentry(vma, dentry)) {
1460 #endif /* SLP_APP */
1462 if (is_android_app_with_dentry(vma, dentry)) {
1465 #endif /* ANDROID_APP */
1472 void do_page_fault_ret_pre_code (void)
1474 struct mm_struct *mm;
1475 struct vm_area_struct *vma = 0;
1476 inst_us_proc_t *task_inst_info = NULL;
1478 * Because process threads have same address space
1479 * we instrument only group_leader of all this threads
1481 struct task_struct *task = current->group_leader;
1483 unsigned long addr = 0, page = 0;
1487 struct timeval imi_tv1;
1488 struct timeval imi_tv2;
1489 #define USEC_IN_SEC_NUM 1000000
1491 if (task->flags & PF_KTHREAD) {
1492 DPRINTF("ignored kernel thread %d\n", task->pid);
1496 if (!is_us_instrumentation()) {
1500 addr = (unsigned long)swap_get_entry_data(&sa_dpf);
1503 printk("WARNING: do_page_fault_ret_pre_code addr = 0\n");
1507 page = addr & PAGE_MASK;
1510 task_inst_info = get_task_inst_node(task);
1511 if (task_inst_info == NULL) {
1512 task_inst_info = copy_task_inst_info(task, &us_proc_info);
1513 put_task_inst_node(task, task_inst_info);
1516 if (!is_java_inst_enabled() &&
1517 (us_proc_info.unres_ips_count +
1518 us_proc_info.unres_vtps_count +
1519 us_proc_info.unres_otg_ips_count) == 0) {
1524 if (us_proc_info.tgid == 0) {
1525 pid_t tgid = find_proc_by_task(task, us_proc_info.m_f_dentry);
1527 us_proc_info.tgid = gl_nNotifyTgid = tgid;
1531 if (us_proc_info.tgid == task->tgid) {
1532 task_inst_info = &us_proc_info;
1536 if (task_inst_info) {
1538 if (is_java_inst_enabled()) {
1539 find_libdvm_for_task(task, &us_proc_info);
1541 #endif /* __ANDROID */
1544 do_gettimeofday(&imi_tv1);
1545 install_page_probes(page, task, task_inst_info->pp, 1);
1546 do_gettimeofday(&imi_tv2);
1548 imi_sum_time += ((imi_tv2.tv_sec - imi_tv1.tv_sec) * USEC_IN_SEC_NUM +
1549 (imi_tv2.tv_usec - imi_tv1.tv_usec));
1553 EXPORT_SYMBOL_GPL(do_page_fault_ret_pre_code);
1556 void do_exit_probe_pre_code (void)
1558 // TODO: remove task
1560 EXPORT_SYMBOL_GPL(do_exit_probe_pre_code);
1562 static int check_addr(unsigned long addr, unsigned long start, size_t len)
1564 if ((addr >= start) && (addr < start + (unsigned long)len)) {
1571 static int remove_unmap_probes(struct task_struct *task, inst_us_proc_t* task_inst_info, unsigned long start, size_t len)
1574 us_proc_otg_ip_t *p;
1576 const int atomic = 1;
1578 for (i = 0; i < task_inst_info->libs_count; ++i) {
1579 for (k = 0; k < task_inst_info->p_libs[i].ips_count; ++k) {
1580 if (task_inst_info->p_libs[i].p_ips[k].installed) {
1581 addr = task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr;
1582 if (check_addr(addr, start, len)) {
1583 err = unregister_usprobe(task, &task_inst_info->p_libs[i].p_ips[k], atomic, 1);
1585 EPRINTF("failed to uninstall IP at %p. Error %d!", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
1588 task_inst_info->unres_ips_count++;
1589 task_inst_info->p_libs[i].p_ips[k].installed = 0;
1593 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; ++k) {
1594 if (task_inst_info->p_libs[i].p_vtps[k].installed) {
1595 addr = task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr;
1596 if (check_addr(addr, start, len)) {
1597 dbi_unregister_ujprobe(task, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
1598 task_inst_info->unres_vtps_count++;
1599 task_inst_info->p_libs[i].p_vtps[k].installed = 0;
1605 if (is_java_inst_enabled()) {
1606 us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
1607 if (entp->installed) {
1608 addr = entp->jprobe.kp.addr;
1609 if (check_addr(addr, start, len)) {
1610 unregister_usprobe(task, entp, atomic, 1);
1611 entp->installed = 0;
1614 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
1615 if (retp->installed) {
1616 addr = retp->jprobe.kp.addr;
1617 if (check_addr(addr, start, len)) {
1618 unregister_usprobe(task, retp, atomic, 1);
1619 retp->installed = 0;
1623 #endif /* __ANDROID */
1625 // remove OTG-probes
1626 list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
1627 if (!p->ip.installed) {
1631 addr = p->ip.jprobe.kp.addr;
1632 if (check_addr(addr, start, len) == 0) {
1636 err = unregister_usprobe(task, &p->ip, atomic, 1);
1638 EPRINTF("failed to uninstall IP at %p. Error %d!",
1639 p->ip.jprobe.kp.addr, err);
1642 p->ip.installed = 0;
1643 remove_otg_probe_from_list(p->ip.offset);
1649 void do_munmap_probe_pre_code(struct mm_struct *mm, unsigned long start, size_t len)
1651 inst_us_proc_t *task_inst_info = NULL;
1652 struct task_struct *task = current;
1654 //if user-space instrumentation is not set
1655 if (!us_proc_info.path || task->tgid != task->pid)
1658 if (!strcmp(us_proc_info.path,"*")) {
1659 task_inst_info = get_task_inst_node(task);
1661 if (task->tgid == us_proc_info.tgid) {
1662 task_inst_info = &us_proc_info;
1666 if (task_inst_info) {
1667 remove_unmap_probes(task, task_inst_info, start, len);
1670 EXPORT_SYMBOL_GPL(do_munmap_probe_pre_code);
1672 void mm_release_probe_pre_code(void)
1675 struct task_struct *task;
1677 if (!is_us_instrumentation() || current->tgid != current->pid) {
1682 inst_us_proc_t *task_inst_info = get_task_inst_node(current);
1683 if (task_inst_info) {
1684 iRet = uninstall_us_proc_probes(current, task_inst_info->pp, US_NOT_RP2);
1686 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
1689 dbi_unregister_all_uprobes(current, 1);
1692 if (current->tgid == us_proc_info.tgid && current->tgid == current->pid) {
1694 iRet = uninstall_us_proc_probes(current, us_proc_info.pp, US_NOT_RP2);
1696 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
1699 dbi_unregister_all_uprobes(current, 1);
1700 us_proc_info.tgid = 0;
1701 for(i = 0; i < us_proc_info.libs_count; i++) {
1702 us_proc_info.p_libs[i].loaded = 0;
1707 EXPORT_SYMBOL_GPL(mm_release_probe_pre_code);
1710 static void recover_child(struct task_struct *child_task, inst_us_proc_t *parent_iup)
1712 uninstall_us_proc_probes(child_task, parent_iup->pp, US_DISARM);
1715 static void rm_uprobes_child(struct task_struct *new_task)
1718 inst_us_proc_t *task_inst_info = get_task_inst_node(current);
1720 recover_child(new_task, task_inst_info);
1722 if(us_proc_info.tgid == current->tgid) {
1723 recover_child(new_task, &us_proc_info);
1728 void copy_process_ret_pre_code(struct task_struct *p)
1733 if(p->mm != current->mm) // check flags CLONE_VM
1734 rm_uprobes_child(p);
1738 DEFINE_PER_CPU (us_proc_ip_t *, gpCurIp) = NULL;
1739 EXPORT_PER_CPU_SYMBOL_GPL(gpCurIp);
1740 DEFINE_PER_CPU(struct pt_regs *, gpUserRegs) = NULL;
1741 EXPORT_PER_CPU_SYMBOL_GPL(gpUserRegs);
1744 unsigned long ujprobe_event_pre_handler (us_proc_ip_t * ip, struct pt_regs *regs)
1746 __get_cpu_var (gpCurIp) = ip;
1747 __get_cpu_var (gpUserRegs) = regs;
1752 int handle_java_event(unsigned long addr)
1754 unsigned long start = 0;
1755 struct pt_regs *regs = __get_cpu_var(gpUserRegs);
1758 /* TODO: some stuff here */
1760 start = us_proc_info.libdvm_start;
1762 unsigned long end = us_proc_info.libdvm_end;
1764 if (addr == start + LIBDVM_ENTRY) {
1765 unsigned long *p_met = (unsigned long *)regs->ARM_r0;
1766 char *met_name = p_met ? (char *)(p_met[4]) : 0;
1767 unsigned long *p_cl = p_met ? (unsigned long *)p_met[0] : 0;
1768 char *cl_name = p_cl ? (char *)(p_cl[6]) : 0;
1769 if (!cl_name || !met_name) {
1770 EPRINTF("warn: class name or method name null\n");
1772 pack_event_info(JAVA_PROBE_ID, RECORD_ENTRY, "pss", addr, cl_name, met_name);
1774 dbi_uprobe_return ();
1778 if (addr == start + LIBDVM_RETURN) {
1779 unsigned long *p_th = (unsigned long *)regs->ARM_r6;
1780 unsigned long *p_st = p_th;
1781 unsigned long *p_met = p_st ? (unsigned long *)p_st[2] : 0;
1782 char *met_name = p_met ? (char *)(p_met[4]) : 0;
1783 unsigned long *p_cl = p_met ? (unsigned long *)p_met[0] : 0;
1784 char *cl_name = p_cl ? (char *)(p_cl[6]) : 0;
1785 if (!cl_name || !met_name) {
1786 EPRINTF("warn: class name or method name null\n");
1788 pack_event_info(JAVA_PROBE_ID, RECORD_RET, "pss", addr, cl_name, met_name);
1790 dbi_uprobe_return ();
1796 #endif /* __ANDROID */
1798 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
1800 us_proc_ip_t *ip = __get_cpu_var (gpCurIp);
1801 unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1804 if (is_java_inst_enabled() && handle_java_event(addr)) {
1807 #endif /* __ANDROID */
1810 #if defined(CONFIG_ARM)
1811 if (ip->offset & 0x01)
1813 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr | 0x01, arg1, arg2, arg3, arg4, arg5, arg6);
1815 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1818 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1820 // Mr_Nobody: uncomment for valencia
1821 //unregister_usprobe(current, ip, 1);
1822 dbi_uprobe_return ();
1825 void find_plt_address(unsigned long addr)
1827 inst_us_proc_t *task_inst_info = NULL;
1830 struct vm_area_struct *vma;
1831 us_proc_lib_t *p_lib = NULL;
1832 char *szLibPath = NULL;
1834 // Search for library structure to check whether this function plt or not
1835 if (strcmp(us_proc_info.path, "*")){
1836 // If app lib instrumentation
1837 task_inst_info = &us_proc_info;
1839 // If lib only instrumentation
1840 task_inst_info = get_task_inst_node(current);
1842 if ((task_inst_info != NULL) && (task_inst_info->is_plt != 0)) {
1843 for (i = 0; i < task_inst_info->libs_count; i++)
1845 if ((task_inst_info->p_libs[i].loaded)
1846 && (task_inst_info->p_libs[i].plt_count > 0)
1847 && (addr > task_inst_info->p_libs[i].vma_start)
1848 && (addr < task_inst_info->p_libs[i].vma_end))
1850 p_lib = &(task_inst_info->p_libs[i]);
1854 if (p_lib != NULL) {
1855 for (i = 0; i < p_lib->plt_count; i++)
1857 if (addr == p_lib->p_plt[i].func_addr + p_lib->vma_start) {
1858 unsigned long real_got;
1859 if (p_lib->vma_flag & VM_EXECUTABLE) {
1860 real_got = p_lib->p_plt[i].got_addr;
1862 real_got = p_lib->p_plt[i].got_addr + p_lib->vma_start;
1864 if (!read_proc_vm_atomic(current, (unsigned long)(real_got), &real_addr, sizeof(unsigned long))) {
1865 printk("Failed to read got %p at memory address %p!\n", p_lib->p_plt[i].got_addr, real_got);
1868 if (real_addr != p_lib->p_plt[i].real_func_addr) {
1869 p_lib->p_plt[i].real_func_addr = real_addr;
1870 vma = find_vma(current->mm, real_addr);
1871 if ((vma != NULL) && (vma->vm_start <= real_addr) && (vma->vm_end > real_addr)) {
1872 if (vma->vm_file != NULL) {
1873 szLibPath = &(vma->vm_file->f_dentry->d_iname);
1876 printk("Failed to get vma, includes %x address\n", real_addr);
1880 pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppsp", addr, real_addr, szLibPath, real_addr - vma->vm_start);
1883 pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppp", addr, real_addr, real_addr - vma->vm_start);
1895 int uretprobe_event_handler (struct kretprobe_instance *probe, struct pt_regs *regs, us_proc_ip_t * ip)
1897 int retval = regs_return_value(regs);
1898 unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1900 find_plt_address(addr);
1902 #if defined(CONFIG_ARM)
1903 if (ip->offset & 0x01)
1905 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr | 0x01, retval);
1907 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1910 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1912 // Mr_Nobody: uncomment for valencia
1913 //unregister_usprobe(current, ip, 1);
1917 static int register_usprobe (struct task_struct *task, struct mm_struct *mm, us_proc_ip_t * ip, int atomic, kprobe_opcode_t * islot)
1920 ip->jprobe.kp.tgid = task->tgid;
1921 //ip->jprobe.kp.addr = (kprobe_opcode_t *) addr;
1923 if(!ip->jprobe.entry) {
1924 if (dbi_ujprobe_event_handler_custom_p != NULL)
1926 ip->jprobe.entry = (kprobe_opcode_t *) dbi_ujprobe_event_handler_custom_p;
1927 DPRINTF("Set custom event handler for %x\n", ip->offset);
1931 ip->jprobe.entry = (kprobe_opcode_t *) ujprobe_event_handler;
1932 DPRINTF("Set default event handler for %x\n", ip->offset);
1935 if(!ip->jprobe.pre_entry) {
1936 if (dbi_ujprobe_event_pre_handler_custom_p != NULL)
1938 ip->jprobe.pre_entry = (kprobe_pre_entry_handler_t) dbi_ujprobe_event_pre_handler_custom_p;
1939 DPRINTF("Set custom pre handler for %x\n", ip->offset);
1943 ip->jprobe.pre_entry = (kprobe_pre_entry_handler_t) ujprobe_event_pre_handler;
1944 DPRINTF("Set default pre handler for %x\n", ip->offset);
1947 ip->jprobe.priv_arg = ip;
1948 ret = dbi_register_ujprobe (task, mm, &ip->jprobe, atomic);
1951 DPRINTF ("dbi_register_ujprobe() failure %d", ret);
1955 // Mr_Nobody: comment for valencia
1956 ip->retprobe.kp.tgid = task->tgid;
1957 //ip->retprobe.kp.addr = (kprobe_opcode_t *) addr;
1958 if(!ip->retprobe.handler) {
1959 if (dbi_uretprobe_event_handler_custom_p != NULL)
1960 ip->retprobe.handler = (kretprobe_handler_t) dbi_uretprobe_event_handler_custom_p;
1962 ip->retprobe.handler = (kretprobe_handler_t) uretprobe_event_handler;
1963 //DPRINTF("Failed custom dbi_uretprobe_event_handler_custom_p");
1966 ip->retprobe.priv_arg = ip;
1967 ret = dbi_register_uretprobe (task, mm, &ip->retprobe, atomic);
1970 EPRINTF ("dbi_register_uretprobe() failure %d", ret);
1976 static int unregister_usprobe (struct task_struct *task, us_proc_ip_t * ip, int atomic, int not_rp2)
1978 dbi_unregister_ujprobe (task, &ip->jprobe, atomic);
1979 dbi_unregister_uretprobe (task, &ip->retprobe, atomic, not_rp2);
1983 unsigned long get_stack_size(struct task_struct *task,
1984 struct pt_regs *regs)
1986 #ifdef CONFIG_ADD_THREAD_STACK_INFO
1987 return (task->stack_start - dbi_get_stack_ptr(regs));
1989 struct vm_area_struct *vma = NULL;
1990 struct mm_struct *mm = NULL;
1991 unsigned long result = 0;
1992 int atomic = in_atomic();
1994 mm = (atomic ? task->active_mm: get_task_mm(task));
1998 down_read(&mm->mmap_sem);
2000 vma = find_vma(mm, dbi_get_stack_ptr(regs));
2003 result = vma->vm_end - dbi_get_stack_ptr(regs);
2008 up_read(&mm->mmap_sem);
2016 EXPORT_SYMBOL_GPL(get_stack_size);
2018 unsigned long get_stack(struct task_struct *task, struct pt_regs *regs,
2019 char *buf, unsigned long sz)
2021 unsigned long stack_sz = get_stack_size(task, regs);
2022 unsigned long real_sz = (stack_sz > sz ? sz: stack_sz);
2023 int res = read_proc_vm_atomic(task, dbi_get_stack_ptr(regs), buf, real_sz);
2026 EXPORT_SYMBOL_GPL(get_stack);
2028 int dump_to_trace(probe_id_t probe_id, void *addr, const char *buf,
2031 unsigned long rest_sz = sz;
2032 const char *data = buf;
2034 while (rest_sz >= EVENT_MAX_SIZE) {
2035 pack_event_info(probe_id, RECORD_ENTRY, "pa",
2036 addr, EVENT_MAX_SIZE, data);
2037 rest_sz -= EVENT_MAX_SIZE;
2038 data += EVENT_MAX_SIZE;
2042 pack_event_info(probe_id, RECORD_ENTRY, "pa", addr, rest_sz, data);
2046 EXPORT_SYMBOL_GPL(dump_to_trace);
2048 int dump_backtrace(probe_id_t probe_id, struct task_struct *task,
2049 void *addr, struct pt_regs *regs, unsigned long sz)
2051 unsigned long real_sz = 0;
2054 buf = (char *)kmalloc(sz, GFP_ATOMIC);
2057 real_sz = get_stack(task, regs, buf, sz);
2059 dump_to_trace(probe_id, addr, buf, real_sz);
2066 EXPORT_SYMBOL_GPL(dump_backtrace);
2068 unsigned long get_ret_addr(struct task_struct *task, us_proc_ip_t *ip)
2070 unsigned long retaddr = 0;
2071 struct hlist_node *item, *tmp_node;
2072 struct kretprobe_instance *ri;
2075 hlist_for_each_safe (item, tmp_node, &ip->retprobe.used_instances) {
2076 ri = hlist_entry (item, struct kretprobe_instance, uflist);
2078 if (ri->task && ri->task->pid == task->pid &&
2079 ri->task->tgid == task->tgid)
2080 retaddr = (unsigned long)ri->ret_addr;
2087 return dbi_get_ret_addr(task_pt_regs(task));
2089 EXPORT_SYMBOL_GPL(get_ret_addr);