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_PER_CPU (us_proc_vtp_t *, gpVtp) = NULL;
25 DEFINE_PER_CPU (struct pt_regs *, gpCurVtpRegs) = NULL;
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]
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!!!
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);
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);
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);
54 struct task_inst_info_node {
55 struct list_head plist;
56 inst_us_proc_t * task_inst_info;
59 LIST_HEAD(task_inst_info_list);
62 struct dentry *launchpad_daemon_dentry = NULL;
63 EXPORT_SYMBOL_GPL(launchpad_daemon_dentry);
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 */
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 */
80 us_proc_otg_ip_t *find_otg_probe(unsigned long addr)
84 list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
85 if (p->ip.offset == addr) {
93 int add_otg_probe_to_list(unsigned long addr, us_proc_otg_ip_t **pprobe)
95 us_proc_otg_ip_t *new_probe;
96 us_proc_otg_ip_t *probe;
101 /* check if such probe does already exist */
102 probe = find_otg_probe(addr);
107 new_probe = kmalloc(sizeof(us_proc_otg_ip_t), GFP_KERNEL);
109 EPRINTF ("no memory for new probe!");
112 memset(new_probe,0, sizeof(us_proc_otg_ip_t));
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;
120 INIT_LIST_HEAD(&new_probe->list);
121 list_add_rcu(&new_probe->list, &otg_us_proc_info);
129 int remove_otg_probe_from_list(unsigned long addr)
133 //check if such probe does exist
134 p = find_otg_probe(addr);
136 /* We do not care about it. Nothing bad. */
140 list_del_rcu(&p->list);
148 * Prepare copy of instrumentation data for task
149 * in case of library only instrumentation
152 inst_us_proc_t* copy_task_inst_info (struct task_struct *task, inst_us_proc_t * task_inst_info)
155 kprobe_opcode_t *entry_save;
156 kprobe_pre_entry_handler_t pre_entry_save;
157 kretprobe_handler_t handler_save;
159 inst_us_proc_t* copy_info = 0;
161 int unres_ips_count = 0, unres_vtps_count = 0;
163 copy_info = kmalloc (sizeof (inst_us_proc_t), GFP_ATOMIC);
164 memset ((void *) copy_info, 0, sizeof (inst_us_proc_t));
166 copy_info->path = task_inst_info->path;
167 copy_info->m_f_dentry = NULL;
169 copy_info->libs_count = task_inst_info->libs_count;
171 kmalloc (task_inst_info->libs_count * sizeof (us_proc_lib_t), GFP_ATOMIC);
173 if (!copy_info->p_libs) {
174 DPRINTF ("No enough memory for copy_info->p_libs");
177 memcpy (copy_info->p_libs, task_inst_info->p_libs,
178 copy_info->libs_count * sizeof (us_proc_lib_t));
180 for (i = 0; i < copy_info->libs_count; i++) {
181 if (copy_info->p_libs[i].ips_count > 0)
183 unres_ips_count += copy_info->p_libs[i].ips_count;
185 copy_info->p_libs[i].p_ips =
186 kmalloc (copy_info->p_libs[i].ips_count * sizeof (us_proc_ip_t), GFP_ATOMIC);
188 if (!copy_info->p_libs[i].p_ips) {
189 DPRINTF ("No enough memory for copy_info->p_libs[i].p_ips");
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++) {
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;
201 copy_info->p_libs[i].p_ips[j].installed = 0;
202 memset(©_info->p_libs[i].p_ips[j].jprobe, 0, sizeof(struct jprobe));
203 memset(©_info->p_libs[i].p_ips[j].retprobe, 0, sizeof(struct kretprobe));
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;
211 unres_ips_count += copy_info->p_libs[i].ips_count;
214 for (j = 0; j < copy_info->p_libs[i].plt_count; j++)
216 copy_info->p_libs[i].p_plt[j].real_func_addr = 0;
219 if (copy_info->p_libs[i].vtps_count > 0) {
220 unres_vtps_count += copy_info->p_libs[i].vtps_count;
222 copy_info->p_libs[i].p_vtps =
223 kmalloc (copy_info->p_libs[i].vtps_count * sizeof (us_proc_vtp_t), GFP_ATOMIC);
225 if (!copy_info->p_libs[i].p_vtps) {
226 DPRINTF ("No enough memory for copy_info->p_libs[i].p_vtps");
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 (©_info->p_libs[i].p_vtps[j].jprobe, 0, sizeof(struct jprobe));
236 unres_vtps_count = copy_info->p_libs[i].vtps_count;
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;
242 copy_info->p_libs[i].vma_start = 0;
243 copy_info->p_libs[i].vma_end = 0;
245 copy_info->unres_ips_count = unres_ips_count;
246 copy_info->unres_vtps_count = unres_vtps_count;
251 inst_us_proc_t* get_task_inst_node(struct task_struct *task)
253 struct task_inst_info_node *node, *tnode;
255 list_for_each_entry_safe(node, tnode, &task_inst_info_list, plist)
257 if (node && task && node->tgid == task->tgid) {
258 return node->task_inst_info;
264 void put_task_inst_node(struct task_struct *task, inst_us_proc_t *task_inst_info)
266 struct task_inst_info_node * node;
268 node = kmalloc (sizeof(struct task_inst_info_node), GFP_ATOMIC);
270 node->tgid = task->tgid;
271 node->task_inst_info = task_inst_info;
273 list_add_tail (&(node->plist), &task_inst_info_list);
277 void clear_task_inst_info(void)
279 struct list_head *node, *tmp;
281 list_for_each_safe(node, tmp, &task_inst_info_list)
286 static int is_slp_app_with_dentry(struct vm_area_struct *vma,
287 struct dentry *dentry)
289 struct vm_area_struct *slp_app_vma = NULL;
291 if (vma->vm_file->f_dentry == launchpad_daemon_dentry) {
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) {
300 slp_app_vma = slp_app_vma->vm_next;
309 static int is_android_app_with_dentry(struct vm_area_struct *vma,
310 struct dentry *dentry)
312 struct vm_area_struct *android_app_vma = NULL;
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;
324 android_app_vma = android_app_vma->vm_next;
330 #endif /* ANDROID_APP */
333 void find_libdvm_for_task(struct task_struct *task, inst_us_proc_t *info)
335 struct vm_area_struct *vma = NULL;
336 struct mm_struct *mm = NULL;
338 mm = get_task_mm(task);
343 if (vma->vm_file->f_dentry == libdvm_dentry) {
344 info->libdvm_start = vma->vm_start;
345 info->libdvm_end = vma->vm_end;
354 #endif /* __ANDROID */
356 struct dentry *dentry_by_path(const char *path)
358 struct dentry *dentry;
359 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
361 if (kern_path(path, LOOKUP_FOLLOW, &st_path) != 0) {
362 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
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);
370 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
373 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38)
374 dentry = nd.path.dentry;
376 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
377 dentry = st_path.dentry;
379 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) */
383 static int find_task_by_path (const char *path, struct task_struct **p_task, struct list_head *tids)
386 struct task_struct *task;
387 struct vm_area_struct *vma;
388 struct mm_struct *mm;
389 struct dentry *dentry = dentry_by_path(path);
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) {
401 for_each_process (task) {
403 if ( 0 != inst_pid && ( inst_pid != task->pid ) )
406 mm = get_task_mm(task);
411 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
412 if (vma->vm_file->f_dentry == dentry) {
415 get_task_struct (task);
421 if (is_slp_app_with_dentry(vma, dentry)) {
423 get_task_struct(task);
429 if (is_android_app_with_dentry(vma, dentry)) {
431 get_task_struct(task);
434 #endif /* ANDROID_APP */
438 // only decrement usage count on mm since we cannot sleep here
439 atomic_dec(&mm->mm_users);
446 DPRINTF ("found pid %d for %s.", (*p_task)->pid, path);
447 *p_task = (*p_task)->group_leader;
448 gl_nNotifyTgid = (*p_task)->tgid;
450 DPRINTF ("pid for %s not found!", path);
457 static void us_vtp_event_pre_handler (us_proc_vtp_t * vtp, struct pt_regs *regs)
459 __get_cpu_var(gpVtp) = vtp;
460 __get_cpu_var(gpCurVtpRegs) = regs;
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)
465 us_proc_vtp_t *vtp = __get_cpu_var(gpVtp);
466 #if !defined(CONFIG_X86)
467 struct pt_regs *regs = __get_cpu_var(gpCurVtpRegs);
473 us_proc_vtp_data_t *vtp_data;
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;
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)
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);
495 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, ival, vtp_data->name);
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);
501 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, ival, vtp_data->name);
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);
507 pack_event_info (VTP_PROBE_ID, RECORD_ENTRY, fmt, vtp->jprobe.kp.addr, cval, vtp_data->name);
510 if (current->active_mm) {
512 struct vm_area_struct *vma;
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);
519 maddr = kmap_atomic (page, KM_USER0);
520 len = strlen (maddr + (vaddr & ~PAGE_MASK));
521 sval = kmalloc (len + 1, GFP_KERNEL);
523 EPRINTF ("failed to alloc memory for string in proc %s/%u addr %lu!", current->comm, current->pid, vaddr);
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);
529 kunmap_atomic (maddr, KM_USER0);
530 page_cache_release (page);
533 EPRINTF ("task %s/%u has no mm!", current->comm, current->pid);
536 EPRINTF ("unknown variable type '%c'", vtp_data->type);
539 dbi_uprobe_return ();
542 static int install_mapped_ips (struct task_struct *task, inst_us_proc_t* task_inst_info, int atomic)
544 struct vm_area_struct *vma;
547 unsigned int old_ips_count, old_vtps_count;
549 struct task_struct *t;
550 struct mm_struct *mm;
552 mm = atomic ? task->active_mm : get_task_mm (task);
554 return task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count;
556 old_ips_count = task_inst_info->unres_ips_count;
557 old_vtps_count = task_inst_info->unres_vtps_count;
559 down_read (&mm->mmap_sem);
562 // skip non-text section
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))) {
568 if (vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || !vma->vm_file) {
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.
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 );
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");
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, '/');
598 p = task_inst_info->p_libs[i].path;
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);
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);
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);
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);
651 EPRINTF ("failed to install VTP at %p. Error %d!",
652 task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr, err);
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;
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);
678 DPRINTF("failed to install IP at %p", addr);
683 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
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);
699 DPRINTF("failed to install IP at %p", addr);
705 #endif /* __ANDROID */
708 list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
709 if (p->ip.installed) {
713 if (!page_present(mm, p->ip.offset)) {
714 DPRINTF("Page isn't present for %p.",
719 err = register_usprobe(task, mm, &p->ip, atomic, 0);
721 DPRINTF("failed to install IP at %lx/%p. Error %d!",
723 p->ip.jprobe.kp.addr, err);
726 task_inst_info->unres_otg_ips_count--;
729 up_read (&mm->mmap_sem);
732 return task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count;
735 static int install_otg_ip(unsigned long addr,
736 unsigned long pre_handler,
737 unsigned long jp_handler,
738 unsigned long rp_handler)
741 us_proc_otg_ip_t *pprobe;
742 struct task_struct *task = current->group_leader;
743 struct mm_struct *mm = task->mm;
745 /* Probe preparing */
746 err = add_otg_probe_to_list(addr, &pprobe);
749 DPRINTF("OTG probe %p already installed.", addr);
752 DPRINTF("Failed to add new OTG probe, err=%d",err);
757 pprobe->ip.jprobe.pre_entry =
758 (kprobe_pre_entry_handler_t)pre_handler;
760 pprobe->ip.jprobe.pre_entry =
761 (kprobe_pre_entry_handler_t)
762 dbi_ujprobe_event_pre_handler_custom_p;
766 pprobe->ip.jprobe.entry =
767 (kprobe_opcode_t *)jp_handler;
769 pprobe->ip.jprobe.entry =
771 dbi_ujprobe_event_handler_custom_p;
774 pprobe->ip.retprobe.handler =
775 (kretprobe_handler_t)rp_handler;
777 pprobe->ip.retprobe.handler =
778 (kretprobe_handler_t)
779 dbi_uretprobe_event_handler_custom_p;
782 pprobe->tgid = task->tgid;
783 if (!page_present(mm, addr)) {
784 DPRINTF("Page isn't present for %p.", addr);
786 us_proc_info.unres_otg_ips_count++;
787 /* Probe will be installed in do_page_fault handler */
790 DPRINTF("Page present for %p.", addr);
792 /* Probe installing */
793 pprobe->ip.installed = 1;
794 err = register_usprobe(current, mm, &pprobe->ip, 1, 0);
796 DPRINTF("failed to install IP at %lx/%p. Error %d!",
797 addr, pprobe->ip.jprobe.kp.addr, err);
802 EXPORT_SYMBOL_GPL(install_otg_ip);
805 static int uninstall_mapped_ips (struct task_struct *task, inst_us_proc_t* task_inst_info, int atomic)
810 for (i = 0; i < task_inst_info->libs_count; i++)
812 DPRINTF ("clear lib %s.", task_inst_info->p_libs[i].path);
813 for (k = 0; k < task_inst_info->p_libs[i].ips_count; k++)
815 if (task_inst_info->p_libs[i].p_ips[k].installed)
817 DPRINTF ("remove IP at %p.", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr);
818 err = unregister_usprobe (task, &task_inst_info->p_libs[i].p_ips[k], atomic);
821 EPRINTF ("failed to uninstall IP at %p. Error %d!", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
824 task_inst_info->unres_ips_count++;
825 task_inst_info->p_libs[i].p_ips[k].installed = 0;
828 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; k++)
830 if (task_inst_info->p_libs[i].p_vtps[k].installed)
832 dbi_unregister_ujprobe (task, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
833 task_inst_info->unres_vtps_count++;
834 task_inst_info->p_libs[i].p_vtps[k].installed = 0;
837 task_inst_info->p_libs[i].loaded = 0;
840 if (is_java_inst_enabled()) {
841 us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
842 if (entp->installed) {
843 unregister_usprobe(task, entp, atomic);
846 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
847 if (retp->installed) {
848 unregister_usprobe(task, retp, atomic);
852 #endif /* __ANDROID */
853 list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
854 if (!p->ip.installed) {
857 DPRINTF("remove OTG IP at %p.", p->ip.offset);
858 err = unregister_usprobe(task, &p->ip, atomic);
860 EPRINTF("failed to uninstall IP at %p. Error %d!",
861 p->ip.jprobe.kp.addr, err);
867 DPRINTF ("Ures IPs %d.", task_inst_info->unres_ips_count);
868 DPRINTF ("Ures VTPs %d.", task_inst_info->unres_vtps_count);
872 void send_sig_jprobe_event_handler (int sig, struct siginfo *info, struct task_struct *t, struct sigpending *signals)
875 struct task_struct *task;
876 inst_us_proc_t *task_inst_info = NULL;
878 //if user-space instrumentation is not set
879 if (!us_proc_info.path)
885 if (!strcmp(us_proc_info.path,"*"))
887 task_inst_info = get_task_inst_node(t);
890 iRet = uninstall_mapped_ips (t, task_inst_info, 1);
892 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
893 dbi_unregister_all_uprobes(t, 1);
899 if (current->tgid != us_proc_info.tgid)
903 // look for another process with the same tgid
905 for_each_process (task)
907 if ((task->pid != t->pid) && (task->tgid == us_proc_info.tgid))
916 DPRINTF ("%s(%d) send_signal SIGKILL for the last target proc %s(%d)",
917 current->comm, current->pid, t->comm, t->pid);
918 iRet = uninstall_mapped_ips (t, &us_proc_info, 1);
920 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
924 static int uninstall_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
926 kernel_probe_t *probe = NULL;
928 if (probes_flags & kflag) {
929 probe = find_probe(addr);
931 iRet = remove_probe_from_list (addr);
933 EPRINTF ("remove_probe_from_list(0x%lx) result=%d!", addr, iRet);
937 probes_flags &= ~kflag;
939 if (us_proc_probes & uflag) {
940 if (!(probes_flags & uflag)) {
942 iRet = unregister_kernel_probe(probe);
944 EPRINTF ("unregister_kernel_probe(0x%lx) result=%d!",
950 us_proc_probes &= ~uflag;
955 int deinst_usr_space_proc (void)
957 int iRet = 0, found = 0;
958 struct task_struct *task = 0;
959 inst_us_proc_t *task_inst_info = NULL;
961 //if user-space instrumentation is not set
962 if (!us_proc_info.path)
965 iRet = uninstall_kernel_probe (pf_addr, US_PROC_PF_INSTLD,
968 EPRINTF ("uninstall_kernel_probe(do_page_fault) result=%d!", iRet);
970 iRet = uninstall_kernel_probe (cp_addr, US_PROC_CP_INSTLD,
973 EPRINTF ("uninstall_kernel_probe(copy_process) result=%d!", iRet);
975 iRet = uninstall_kernel_probe (mr_addr, US_PROC_MR_INSTLD,
978 EPRINTF ("uninstall_kernel_probe(mm_release) result=%d!", iRet);
980 iRet = uninstall_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD,
983 EPRINTF ("uninstall_kernel_probe(do_exit) result=%d!", iRet);
985 iRet = uninstall_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD,
988 EPRINTF ("uninstall_kernel_probe(do_munmap) result=%d!", iRet);
990 if (!strcmp(us_proc_info.path,"*"))
992 for_each_process (task)
994 task_inst_info = get_task_inst_node(task);
997 iRet = uninstall_mapped_ips (task, task_inst_info, 1);
999 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
1000 dbi_unregister_all_uprobes(task, 1);
1006 if (us_proc_info.tgid == 0)
1009 for_each_process (task)
1011 if (task->tgid == us_proc_info.tgid)
1014 get_task_struct (task);
1023 iRet = uninstall_mapped_ips (task, &us_proc_info, 0);
1025 EPRINTF ("failed to uninstall IPs %d!", iRet);
1026 put_task_struct (task);
1027 dbi_unregister_all_uprobes(task, 1);
1028 us_proc_info.tgid = 0;
1029 for(i = 0; i < us_proc_info.libs_count; i++)
1030 us_proc_info.p_libs[i].loaded = 0;
1036 static int install_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
1038 kernel_probe_t *probe = NULL;
1041 DPRINTF("us_proc_probes = 0x%x, uflag = 0x%x, "
1042 "probes_flags = 0x%x, kflag = 0x%x",
1043 us_proc_probes, uflag, probes_flags, kflag);
1045 if (!(probes_flags & kflag)) {
1046 iRet = add_probe_to_list (addr, &probe);
1048 EPRINTF ("add_probe_to_list(0x%lx) result=%d!", addr, iRet);
1051 probes_flags |= kflag;
1053 if (!(us_proc_probes & uflag)) {
1054 if (!(probes_flags & uflag)) {
1055 iRet = register_kernel_probe (probe);
1057 EPRINTF ("register_kernel_probe(0x%lx) result=%d!", addr, iRet);
1061 us_proc_probes |= uflag;
1070 int inst_usr_space_proc (void)
1073 struct task_struct *task = 0;
1074 inst_us_proc_t *task_inst_info = NULL;
1076 //if user-space instrumentation is not set
1077 if (!us_proc_info.path)
1080 DPRINTF("User space instr");
1083 launchpad_daemon_dentry = dentry_by_path("/usr/bin/launchpad_preloading_preinitializing_daemon");
1084 if (launchpad_daemon_dentry == NULL) {
1088 #endif /* SLP_APP */
1091 app_process_dentry = dentry_by_path("/system/bin/app_process");
1092 if (app_process_dentry == NULL) {
1096 android_app_vma_start = 0;
1097 android_app_vma_end = 0;
1098 #endif /* ANDROID_APP */
1101 if (is_java_inst_enabled()) {
1102 libdvm_dentry = dentry_by_path("/system/lib/libdvm.so");
1103 if (libdvm_dentry == NULL) {
1107 memset(&us_proc_info.libdvm_entry_ip, 0, sizeof(us_proc_ip_t));
1108 memset(&us_proc_info.libdvm_return_ip, 0, sizeof(us_proc_ip_t));
1109 us_proc_info.libdvm_start = 0;
1110 us_proc_info.libdvm_end = 0;
1112 #endif /* __ANDROID */
1114 for (i = 0; i < us_proc_info.libs_count; i++) {
1115 us_proc_info.p_libs[i].loaded = 0;
1117 /* check whether process is already running
1118 * 1) if process is running - look for the libraries in the process maps
1119 * 1.1) check if page for symbol does exist
1120 * 1.1.1) if page exists - instrument it
1121 * 1.1.2) if page does not exist - make sure that do_page_fault handler is installed
1122 * 2) if process is not running - make sure that do_page_fault handler is installed
1125 if (!strcmp(us_proc_info.path,"*"))
1127 clear_task_inst_info();
1128 for_each_process (task) {
1129 if (task->flags & PF_KTHREAD){
1130 DPRINTF("ignored kernel thread %d\n",
1135 task_inst_info = get_task_inst_node(task);
1136 if (!task_inst_info) {
1138 copy_task_inst_info(task,
1140 put_task_inst_node(task, task_inst_info);
1142 DPRINTF("trying process");
1144 if (is_java_inst_enabled()) {
1145 find_libdvm_for_task(task, task_inst_info);
1147 #endif /* __ANDROID */
1148 install_mapped_ips (task, task_inst_info, 1);
1149 //put_task_struct (task);
1150 task_inst_info = NULL;
1155 ret = find_task_by_path (us_proc_info.path, &task, NULL);
1158 DPRINTF("task found. installing probes");
1159 us_proc_info.tgid = task->pid;
1161 if (is_java_inst_enabled()) {
1162 find_libdvm_for_task(task, &us_proc_info);
1164 #endif /* __ANDROID */
1165 install_mapped_ips (task, &us_proc_info, 0);
1166 put_task_struct (task);
1170 // enable 'do_page_fault' probe to detect when they will be loaded
1171 ret = install_kernel_probe (pf_addr, US_PROC_PF_INSTLD, 0, &pf_probe);
1174 EPRINTF ("install_kernel_probe(do_page_fault) result=%d!", ret);
1177 // enable 'do_exit' probe to detect for remove task_struct
1178 ret = install_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD, 0, &exit_probe);
1181 EPRINTF ("install_kernel_probe(do_exit) result=%d!", ret);
1184 /* enable 'copy_process' */
1185 ret = install_kernel_probe (cp_addr, US_PROC_CP_INSTLD, 0, &cp_probe);
1188 EPRINTF ("instpall_kernel_probe(copy_process) result=%d!", ret);
1192 // enable 'mm_release' probe to detect when for remove user space probes
1193 ret = install_kernel_probe (mr_addr, US_PROC_MR_INSTLD, 0, &mr_probe);
1196 EPRINTF ("install_kernel_probe(mm_release) result=%d!", ret);
1200 // enable 'do_munmap' probe to detect when for remove user space probes
1201 ret = install_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD, 0, &unmap_probe);
1204 EPRINTF ("install_kernel_probe(do_munmap) result=%d!", ret);
1210 void do_page_fault_ret_pre_code (void)
1212 struct mm_struct *mm;
1213 struct vm_area_struct *vma = 0;
1214 inst_us_proc_t *task_inst_info = NULL;
1216 * Because process threads have same address space
1217 * we instrument only group_leader of all this threads
1219 struct task_struct *task = current->group_leader;
1221 //if user-space instrumentation is not set
1222 if (!us_proc_info.path)
1225 if (task->flags & PF_KTHREAD) {
1226 DPRINTF("ignored kernel thread %d\n", task->pid);
1231 if (!strcmp(us_proc_info.path,"*"))
1233 task_inst_info = get_task_inst_node(task);
1234 if (!task_inst_info)
1236 task_inst_info = copy_task_inst_info(task,
1238 put_task_inst_node(task, task_inst_info);
1240 if (is_java_inst_enabled()) {
1241 find_libdvm_for_task(task, task_inst_info);
1243 #endif /* __ANDROID */
1245 install_mapped_ips (task, task_inst_info, 1);
1249 task_inst_info = &us_proc_info;
1250 //DPRINTF("do_page_fault from proc %d-%d-%d", current->pid, task_inst_info->tgid, task_inst_info->unres_ips_count);
1251 if (!is_java_inst_enabled()
1252 && (task_inst_info->unres_ips_count + task_inst_info->unres_vtps_count
1253 + task_inst_info->unres_otg_ips_count) == 0)
1255 //DPRINTF("do_page_fault: there no unresolved IPs");
1259 if (task_inst_info->tgid == 0)
1261 mm = task->active_mm;
1264 // down_read (&mm->mmap_sem);
1268 if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
1270 if (vma->vm_file->f_dentry == task_inst_info->m_f_dentry)
1275 if (is_slp_app_with_dentry(vma, task_inst_info->m_f_dentry)) {
1278 #endif /* SLP_APP */
1280 if (is_android_app_with_dentry(vma, task_inst_info->m_f_dentry)) {
1283 #endif /* ANDROID_APP */
1287 // up_read (&mm->mmap_sem);
1290 // DPRINTF ("proc %s/%d has no mm", current->comm, current->pid);
1294 DPRINTF ("do_page_fault found target proc %s(%d)", task->comm, task->pid);
1295 task_inst_info->tgid = task->pid;
1296 gl_nNotifyTgid = task->tgid;
1299 if (task_inst_info->tgid == task->tgid)
1301 //DPRINTF("do_page_fault from target proc %d", task_inst_info->tgid);
1303 if (is_java_inst_enabled()) {
1304 find_libdvm_for_task(task, &us_proc_info);
1306 #endif /* __ANDROID */
1307 install_mapped_ips (task, &us_proc_info, 1);
1309 //DPRINTF("do_page_fault from proc %d-%d exit", task->pid, task_inst_info->pid);
1312 EXPORT_SYMBOL_GPL(do_page_fault_ret_pre_code);
1315 void do_exit_probe_pre_code (void)
1317 // TODO: remove task
1319 EXPORT_SYMBOL_GPL(do_exit_probe_pre_code);
1321 static int check_addr(unsigned long addr, unsigned long start, size_t len)
1323 if ((addr >= start) && (addr < start + (unsigned long)len)) {
1330 static int remove_unmap_probes(struct task_struct *task, inst_us_proc_t* task_inst_info, unsigned long start, size_t len)
1333 us_proc_otg_ip_t *p;
1335 const int atomic = 1;
1337 for (i = 0; i < task_inst_info->libs_count; ++i) {
1338 for (k = 0; k < task_inst_info->p_libs[i].ips_count; ++k) {
1339 if (task_inst_info->p_libs[i].p_ips[k].installed) {
1340 addr = task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr;
1341 if (check_addr(addr, start, len)) {
1342 err = unregister_usprobe(task, &task_inst_info->p_libs[i].p_ips[k], atomic);
1344 EPRINTF("failed to uninstall IP at %p. Error %d!", task_inst_info->p_libs[i].p_ips[k].jprobe.kp.addr, err);
1347 task_inst_info->unres_ips_count++;
1348 task_inst_info->p_libs[i].p_ips[k].installed = 0;
1352 for (k = 0; k < task_inst_info->p_libs[i].vtps_count; ++k) {
1353 if (task_inst_info->p_libs[i].p_vtps[k].installed) {
1354 addr = task_inst_info->p_libs[i].p_vtps[k].jprobe.kp.addr;
1355 if (check_addr(addr, start, len)) {
1356 dbi_unregister_ujprobe(task, &task_inst_info->p_libs[i].p_vtps[k].jprobe, atomic);
1357 task_inst_info->unres_vtps_count++;
1358 task_inst_info->p_libs[i].p_vtps[k].installed = 0;
1364 if (is_java_inst_enabled()) {
1365 us_proc_ip_t *entp = &task_inst_info->libdvm_entry_ip;
1366 if (entp->installed) {
1367 addr = entp->jprobe.kp.addr;
1368 if (check_addr(addr, start, len)) {
1369 unregister_usprobe(task, entp, atomic);
1370 entp->installed = 0;
1373 us_proc_ip_t *retp = &task_inst_info->libdvm_return_ip;
1374 if (retp->installed) {
1375 addr = retp->jprobe.kp.addr;
1376 if (check_addr(addr, start, len)) {
1377 unregister_usprobe(task, retp, atomic);
1378 retp->installed = 0;
1382 #endif /* __ANDROID */
1384 // remove OTG-probes
1385 list_for_each_entry_rcu (p, &otg_us_proc_info, list) {
1386 if (!p->ip.installed) {
1390 addr = p->ip.jprobe.kp.addr;
1391 if (check_addr(addr, start, len) == 0) {
1395 err = unregister_usprobe(task, &p->ip, atomic);
1397 EPRINTF("failed to uninstall IP at %p. Error %d!",
1398 p->ip.jprobe.kp.addr, err);
1401 p->ip.installed = 0;
1402 remove_otg_probe_from_list(p->ip.offset);
1408 void do_munmap_probe_pre_code(struct mm_struct *mm, unsigned long start, size_t len)
1410 inst_us_proc_t *task_inst_info = NULL;
1411 struct task_struct *task = current;
1413 //if user-space instrumentation is not set
1414 if (!us_proc_info.path || task->tgid != task->pid)
1417 if (!strcmp(us_proc_info.path,"*")) {
1418 task_inst_info = get_task_inst_node(task);
1420 if (task->tgid == us_proc_info.tgid) {
1421 task_inst_info = &us_proc_info;
1425 if (task_inst_info) {
1426 remove_unmap_probes(task, task_inst_info, start, len);
1429 EXPORT_SYMBOL_GPL(do_munmap_probe_pre_code);
1431 void mm_release_probe_pre_code(void)
1434 struct task_struct *task;
1435 inst_us_proc_t *task_inst_info = NULL;
1437 //if user-space instrumentation is not set
1438 if (!us_proc_info.path || current->tgid != current->pid)
1441 if (!strcmp(us_proc_info.path,"*"))
1443 task_inst_info = get_task_inst_node(current);
1446 iRet = uninstall_mapped_ips (current, task_inst_info, 1);
1448 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
1449 dbi_unregister_all_uprobes(current, 1);
1454 if (current->tgid != us_proc_info.tgid)
1457 // look for another process with the same tgid
1459 for_each_process (task)
1461 if ((task->pid != current->pid) && (task->tgid == us_proc_info.tgid))
1471 iRet = uninstall_mapped_ips (current, &us_proc_info, 1);
1473 EPRINTF ("failed to uninstall IPs (%d)!", iRet);
1474 dbi_unregister_all_uprobes(current, 1);
1475 us_proc_info.tgid = 0;
1476 for(i = 0; i < us_proc_info.libs_count; i++)
1477 us_proc_info.p_libs[i].loaded = 0;
1481 EXPORT_SYMBOL_GPL(mm_release_probe_pre_code);
1484 static void recover_child(struct task_struct *child_task, inst_us_proc_t *parent_iup)
1487 for(i = 0; i < parent_iup->libs_count; ++i)
1489 for(k = 0; k < parent_iup->p_libs[i].ips_count; ++k)
1490 if(parent_iup->p_libs[i].p_ips[k].installed)
1491 arch_disarm_uprobe(&parent_iup->p_libs[i].p_ips[k].jprobe.kp, child_task);
1493 for(k = 0; k < parent_iup->p_libs[i].vtps_count; ++k)
1494 if(parent_iup->p_libs[i].p_vtps[k].installed)
1495 arch_disarm_uprobe(&parent_iup->p_libs[i].p_vtps[k].jprobe.kp, child_task);
1499 static void rm_uprobes_child(struct task_struct *new_task)
1501 if(!strcmp(us_proc_info.path, "*")) {
1502 inst_us_proc_t *task_inst_info = get_task_inst_node(current);
1504 recover_child(new_task, task_inst_info);
1506 if(us_proc_info.tgid == current->tgid) {
1507 recover_child(new_task, &us_proc_info);
1512 void copy_process_ret_pre_code(struct task_struct *p)
1517 if(p->mm != current->mm) // check flags CLONE_VM
1518 rm_uprobes_child(p);
1522 DEFINE_PER_CPU (us_proc_ip_t *, gpCurIp) = NULL;
1523 EXPORT_PER_CPU_SYMBOL_GPL(gpCurIp);
1524 DEFINE_PER_CPU(struct pt_regs *, gpUserRegs) = NULL;
1525 EXPORT_PER_CPU_SYMBOL_GPL(gpUserRegs);
1528 unsigned long ujprobe_event_pre_handler (us_proc_ip_t * ip, struct pt_regs *regs)
1530 __get_cpu_var (gpCurIp) = ip;
1531 __get_cpu_var (gpUserRegs) = regs;
1536 int handle_java_event(unsigned long addr)
1538 unsigned long start = 0;
1539 struct pt_regs *regs = __get_cpu_var(gpUserRegs);
1541 if (!strcmp(us_proc_info.path, "*")) {
1542 /* TODO: some stuff here */
1544 start = us_proc_info.libdvm_start;
1546 unsigned long end = us_proc_info.libdvm_end;
1548 if (addr == start + LIBDVM_ENTRY) {
1549 unsigned long *p_met = (unsigned long *)regs->ARM_r0;
1550 char *met_name = p_met ? (char *)(p_met[4]) : 0;
1551 unsigned long *p_cl = p_met ? (unsigned long *)p_met[0] : 0;
1552 char *cl_name = p_cl ? (char *)(p_cl[6]) : 0;
1553 if (!cl_name || !met_name) {
1554 EPRINTF("warn: class name or method name null\n");
1556 pack_event_info(JAVA_PROBE_ID, RECORD_ENTRY, "pss", addr, cl_name, met_name);
1558 dbi_uprobe_return ();
1562 if (addr == start + LIBDVM_RETURN) {
1563 unsigned long *p_th = (unsigned long *)regs->ARM_r6;
1564 unsigned long *p_st = p_th;
1565 unsigned long *p_met = p_st ? (unsigned long *)p_st[2] : 0;
1566 char *met_name = p_met ? (char *)(p_met[4]) : 0;
1567 unsigned long *p_cl = p_met ? (unsigned long *)p_met[0] : 0;
1568 char *cl_name = p_cl ? (char *)(p_cl[6]) : 0;
1569 if (!cl_name || !met_name) {
1570 EPRINTF("warn: class name or method name null\n");
1572 pack_event_info(JAVA_PROBE_ID, RECORD_RET, "pss", addr, cl_name, met_name);
1574 dbi_uprobe_return ();
1580 #endif /* __ANDROID */
1582 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
1584 us_proc_ip_t *ip = __get_cpu_var (gpCurIp);
1585 unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1588 if (is_java_inst_enabled() && handle_java_event(addr)) {
1591 #endif /* __ANDROID */
1594 #if defined(CONFIG_ARM)
1595 if (ip->offset & 0x01)
1597 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr | 0x01, arg1, arg2, arg3, arg4, arg5, arg6);
1599 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1602 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1604 // Mr_Nobody: uncomment for valencia
1605 //unregister_usprobe(current, ip, 1);
1606 dbi_uprobe_return ();
1609 void find_plt_address(unsigned long addr)
1611 inst_us_proc_t *task_inst_info = NULL;
1614 struct vm_area_struct *vma;
1615 us_proc_lib_t *p_lib = NULL;
1616 char *szLibPath = NULL;
1618 // Search for library structure to check whether this function plt or not
1619 if (strcmp(us_proc_info.path, "*")){
1620 // If app lib instrumentation
1621 task_inst_info = &us_proc_info;
1623 // If lib only instrumentation
1624 task_inst_info = get_task_inst_node(current);
1626 if ((task_inst_info != NULL) && (task_inst_info->is_plt != 0)) {
1627 for (i = 0; i < task_inst_info->libs_count; i++)
1629 if ((task_inst_info->p_libs[i].loaded)
1630 && (task_inst_info->p_libs[i].plt_count > 0)
1631 && (addr > task_inst_info->p_libs[i].vma_start)
1632 && (addr < task_inst_info->p_libs[i].vma_end))
1634 p_lib = &(task_inst_info->p_libs[i]);
1638 if (p_lib != NULL) {
1639 for (i = 0; i < p_lib->plt_count; i++)
1641 if (addr == p_lib->p_plt[i].func_addr + p_lib->vma_start) {
1642 unsigned long real_got;
1643 if (p_lib->vma_flag & VM_EXECUTABLE) {
1644 real_got = p_lib->p_plt[i].got_addr;
1646 real_got = p_lib->p_plt[i].got_addr + p_lib->vma_start;
1648 if (!read_proc_vm_atomic(current, (unsigned long)(real_got), &real_addr, sizeof(unsigned long))) {
1649 printk("Failed to read got %p at memory address %p!\n", p_lib->p_plt[i].got_addr, real_got);
1652 if (real_addr != p_lib->p_plt[i].real_func_addr) {
1653 p_lib->p_plt[i].real_func_addr = real_addr;
1654 vma = find_vma(current->mm, real_addr);
1655 if ((vma != NULL) && (vma->vm_start <= real_addr) && (vma->vm_end > real_addr)) {
1656 if (vma->vm_file != NULL) {
1657 szLibPath = &(vma->vm_file->f_dentry->d_iname);
1660 printk("Failed to get vma, includes %x address\n", real_addr);
1664 pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppsp", addr, real_addr, szLibPath, real_addr - vma->vm_start);
1667 pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppp", addr, real_addr, real_addr - vma->vm_start);
1679 int uretprobe_event_handler (struct kretprobe_instance *probe, struct pt_regs *regs, us_proc_ip_t * ip)
1681 int retval = regs_return_value(regs);
1682 unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1684 find_plt_address(addr);
1686 #if defined(CONFIG_ARM)
1687 if (ip->offset & 0x01)
1689 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr | 0x01, retval);
1691 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1694 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1696 // Mr_Nobody: uncomment for valencia
1697 //unregister_usprobe(current, ip, 1);
1701 static int register_usprobe (struct task_struct *task, struct mm_struct *mm, us_proc_ip_t * ip, int atomic, kprobe_opcode_t * islot)
1704 ip->jprobe.kp.tgid = task->tgid;
1705 //ip->jprobe.kp.addr = (kprobe_opcode_t *) addr;
1706 if(!ip->jprobe.entry) {
1707 if (dbi_ujprobe_event_handler_custom_p != NULL)
1709 ip->jprobe.entry = (kprobe_opcode_t *) dbi_ujprobe_event_handler_custom_p;
1710 DPRINTF("Set custom event handler for %x\n", ip->offset);
1714 ip->jprobe.entry = (kprobe_opcode_t *) ujprobe_event_handler;
1715 DPRINTF("Set default event handler for %x\n", ip->offset);
1718 if(!ip->jprobe.pre_entry) {
1719 if (dbi_ujprobe_event_pre_handler_custom_p != NULL)
1721 ip->jprobe.pre_entry = (kprobe_pre_entry_handler_t) dbi_ujprobe_event_pre_handler_custom_p;
1722 DPRINTF("Set custom pre handler for %x\n", ip->offset);
1726 ip->jprobe.pre_entry = (kprobe_pre_entry_handler_t) ujprobe_event_pre_handler;
1727 DPRINTF("Set default pre handler for %x\n", ip->offset);
1730 ip->jprobe.priv_arg = ip;
1731 ret = dbi_register_ujprobe (task, mm, &ip->jprobe, atomic);
1734 DPRINTF ("dbi_register_ujprobe() failure %d", ret);
1738 // Mr_Nobody: comment for valencia
1739 ip->retprobe.kp.tgid = task->tgid;
1740 //ip->retprobe.kp.addr = (kprobe_opcode_t *) addr;
1741 if(!ip->retprobe.handler) {
1742 if (dbi_uretprobe_event_handler_custom_p != NULL)
1743 ip->retprobe.handler = (kretprobe_handler_t) dbi_uretprobe_event_handler_custom_p;
1745 ip->retprobe.handler = (kretprobe_handler_t) uretprobe_event_handler;
1746 //DPRINTF("Failed custom dbi_uretprobe_event_handler_custom_p");
1749 ip->retprobe.priv_arg = ip;
1750 ret = dbi_register_uretprobe (task, mm, &ip->retprobe, atomic);
1753 EPRINTF ("dbi_register_uretprobe() failure %d", ret);
1759 static int unregister_usprobe (struct task_struct *task, us_proc_ip_t * ip, int atomic)
1761 dbi_unregister_ujprobe (task, &ip->jprobe, atomic);
1762 dbi_unregister_uretprobe (task, &ip->retprobe, atomic);
1766 unsigned long get_stack_size(struct task_struct *task,
1767 struct pt_regs *regs)
1769 #ifdef CONFIG_ADD_THREAD_STACK_INFO
1770 return (task->stack_start - dbi_get_stack_ptr(regs));
1772 struct vm_area_struct *vma = NULL;
1773 struct mm_struct *mm = NULL;
1774 unsigned long result = 0;
1775 int atomic = in_atomic();
1777 mm = (atomic ? task->active_mm: get_task_mm(task));
1781 down_read(&mm->mmap_sem);
1783 vma = find_vma(mm, dbi_get_stack_ptr(regs));
1786 result = vma->vm_end - dbi_get_stack_ptr(regs);
1791 up_read(&mm->mmap_sem);
1799 EXPORT_SYMBOL_GPL(get_stack_size);
1801 unsigned long get_stack(struct task_struct *task, struct pt_regs *regs,
1802 char *buf, unsigned long sz)
1804 unsigned long stack_sz = get_stack_size(task, regs);
1805 unsigned long real_sz = (stack_sz > sz ? sz: stack_sz);
1806 int res = read_proc_vm_atomic(task, dbi_get_stack_ptr(regs), buf, real_sz);
1809 EXPORT_SYMBOL_GPL(get_stack);
1811 int dump_to_trace(probe_id_t probe_id, void *addr, const char *buf,
1814 unsigned long rest_sz = sz;
1815 const char *data = buf;
1817 while (rest_sz >= EVENT_MAX_SIZE) {
1818 pack_event_info(probe_id, RECORD_ENTRY, "pa",
1819 addr, EVENT_MAX_SIZE, data);
1820 rest_sz -= EVENT_MAX_SIZE;
1821 data += EVENT_MAX_SIZE;
1825 pack_event_info(probe_id, RECORD_ENTRY, "pa", addr, rest_sz, data);
1829 EXPORT_SYMBOL_GPL(dump_to_trace);
1831 int dump_backtrace(probe_id_t probe_id, struct task_struct *task,
1832 void *addr, struct pt_regs *regs, unsigned long sz)
1834 unsigned long real_sz = 0;
1837 buf = (char *)kmalloc(sz, GFP_ATOMIC);
1840 real_sz = get_stack(task, regs, buf, sz);
1842 dump_to_trace(probe_id, addr, buf, real_sz);
1849 EXPORT_SYMBOL_GPL(dump_backtrace);
1851 unsigned long get_ret_addr(struct task_struct *task, us_proc_ip_t *ip)
1853 unsigned long retaddr = 0;
1854 struct hlist_node *item, *tmp_node;
1855 struct kretprobe_instance *ri;
1858 hlist_for_each_safe (item, tmp_node, &ip->retprobe.used_instances) {
1859 ri = hlist_entry (item, struct kretprobe_instance, uflist);
1861 if (ri->task && ri->task->pid == task->pid &&
1862 ri->task->tgid == task->tgid)
1863 retaddr = (unsigned long)ri->ret_addr;
1870 return dbi_get_ret_addr(task_pt_regs(task));
1872 EXPORT_SYMBOL_GPL(get_ret_addr);