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 #include "sspt/sspt.h"
25 #include "java_inst.h"
27 #define mm_read_lock(task, mm, atomic, lock) \
28 mm = atomic ? task->active_mm : get_task_mm(task); \
31 panic("ERRR mm_read_lock: mm == NULL\n"); \
35 lock = down_read_trylock(&mm->mmap_sem); \
38 down_read(&mm->mmap_sem); \
41 #define mm_read_unlock(mm, atomic, lock) \
43 up_read(&mm->mmap_sem); \
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 ujprobe_event_pre_handler (struct us_ip *ip, struct pt_regs *regs);
60 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6);
61 int uretprobe_event_handler (struct kretprobe_instance *probe, struct pt_regs *regs, struct us_ip *ip);
66 LIST_HEAD(proc_probes_list);
70 unsigned long android_app_vma_start = 0;
71 unsigned long android_app_vma_end = 0;
72 struct dentry *app_process_dentry = NULL;
73 #endif /* ANDROID_APP */
76 static struct dentry *launchpad_daemon_dentry = NULL;
77 EXPORT_SYMBOL_GPL(launchpad_daemon_dentry);
80 #define print_event(fmt, args...) \
83 sprintf(buf, fmt, ##args); \
84 pack_event_info(US_PROBE_ID, RECORD_ENTRY, "ds", 0x0badc0de, buf); \
87 static inline int is_libonly(void)
89 return !strcmp(us_proc_info.path,"*");
92 // is user-space instrumentation
93 static inline int is_us_instrumentation(void)
95 return !!us_proc_info.path;
98 static struct sspt_procs *get_proc_probes_by_task(struct task_struct *task)
100 struct sspt_procs *procs, *tmp;
103 if (task != current) {
104 printk("ERROR get_proc_probes_by_task: \'task != current\'\n");
108 return us_proc_info.pp;
111 list_for_each_entry_safe(procs, tmp, &proc_probes_list, list) {
112 if (procs->tgid == task->tgid) {
120 static void add_proc_probes(struct task_struct *task, struct sspt_procs *procs)
122 list_add_tail(&procs->list, &proc_probes_list);
125 static struct sspt_procs *get_proc_probes_by_task_or_new(struct task_struct *task)
127 struct sspt_procs *procs = get_proc_probes_by_task(task);
129 procs = sspt_procs_copy(us_proc_info.pp, task);
130 add_proc_probes(task, procs);
137 static int is_slp_app_with_dentry(struct vm_area_struct *vma,
138 struct dentry *dentry)
140 struct vm_area_struct *slp_app_vma = NULL;
142 if (vma->vm_file->f_dentry == launchpad_daemon_dentry) {
144 while (slp_app_vma) {
145 if (slp_app_vma->vm_file) {
146 if (slp_app_vma->vm_file->f_dentry == dentry &&
147 slp_app_vma->vm_pgoff == 0) {
151 slp_app_vma = slp_app_vma->vm_next;
160 static int is_android_app_with_dentry(struct vm_area_struct *vma,
161 struct dentry *dentry)
163 struct vm_area_struct *android_app_vma = NULL;
165 if (vma->vm_file->f_dentry == app_process_dentry) {
166 android_app_vma = vma;
167 while (android_app_vma) {
168 if (android_app_vma->vm_file) {
169 if (android_app_vma->vm_file->f_dentry == dentry) {
170 android_app_vma_start = android_app_vma->vm_start;
171 android_app_vma_end = android_app_vma->vm_end;
175 android_app_vma = android_app_vma->vm_next;
181 #endif /* ANDROID_APP */
183 struct dentry *dentry_by_path(const char *path)
185 struct dentry *dentry;
186 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
188 if (kern_path(path, LOOKUP_FOLLOW, &st_path) != 0) {
189 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
191 if (path_lookup(path, LOOKUP_FOLLOW, &nd) != 0) {
192 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
193 EPRINTF("failed to lookup dentry for path %s!", path);
197 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
200 #elif LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38)
201 dentry = nd.path.dentry;
203 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
204 dentry = st_path.dentry;
206 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25) */
210 static int check_vma(struct vm_area_struct *vma)
213 return vma->vm_file && !(vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC) || (vma->vm_flags & VM_ACCOUNT) ||
214 !(vma->vm_flags & (VM_WRITE | VM_MAYWRITE)) ||
215 !(vma->vm_flags & (VM_READ | VM_MAYREAD)));
217 return vma->vm_file && !(vma->vm_pgoff != 0 || !(vma->vm_flags & VM_EXEC));
221 static int find_task_by_path (const char *path, struct task_struct **p_task, struct list_head *tids)
224 struct task_struct *task;
225 struct vm_area_struct *vma;
226 struct mm_struct *mm;
227 struct dentry *dentry = dentry_by_path(path);
231 /* find corresponding dir entry, this is also check for valid path */
232 // TODO: test - try to instrument process with non-existing path
233 // TODO: test - try to instrument process with existing path and delete file just after start
234 if (dentry == NULL) {
239 for_each_process (task) {
241 if ( 0 != inst_pid && ( inst_pid != task->pid ) )
244 mm = get_task_mm(task);
249 if (check_vma(vma)) {
250 if (vma->vm_file->f_dentry == dentry) {
253 get_task_struct (task);
259 if (is_slp_app_with_dentry(vma, dentry)) {
261 get_task_struct(task);
267 if (is_android_app_with_dentry(vma, dentry)) {
269 get_task_struct(task);
272 #endif /* ANDROID_APP */
276 // only decrement usage count on mm since we cannot sleep here
277 atomic_dec(&mm->mm_users);
284 DPRINTF ("found pid %d for %s.", (*p_task)->pid, path);
285 *p_task = (*p_task)->group_leader;
286 gl_nNotifyTgid = (*p_task)->tgid;
288 DPRINTF ("pid for %s not found!", path);
294 static void set_mapping_file(struct sspt_file *file,
295 const struct sspt_procs *procs,
296 const struct task_struct *task,
297 const struct vm_area_struct *vma);
299 int install_otg_ip(unsigned long addr,
300 kprobe_pre_entry_handler_t pre_handler,
301 unsigned long jp_handler,
302 kretprobe_handler_t rp_handler)
305 struct task_struct *task = current->group_leader;
306 struct mm_struct *mm = task->mm;
309 struct vm_area_struct *vma = find_vma(mm, addr);
310 if (vma && (vma->vm_flags & VM_EXEC) &&
311 vma->vm_file && vma->vm_file->f_dentry) {
312 unsigned long offset_addr = addr - vma->vm_start;
313 struct dentry *dentry = vma->vm_file->f_dentry;
314 char *name = dentry->d_iname;
315 struct sspt_procs *procs = get_proc_probes_by_task(task);
316 struct ip_data pd = {
317 .offset = offset_addr,
318 .pre_handler = pre_handler,
319 .jp_handler = jp_handler,
320 .rp_handler = rp_handler,
324 struct sspt_file *file = sspt_procs_find_file_or_new(procs, dentry, name);
325 struct sspt_page *page = sspt_get_page(file, offset_addr);
326 struct us_ip *ip = sspt_find_ip(page, offset_addr & ~PAGE_MASK);
329 set_mapping_file(file, procs, task, vma);
334 // TODO: sspt_procs_find_file_or_new --> sspt_procs_find_file ?!
335 struct sspt_file *file = sspt_procs_find_file_or_new(procs, dentry, name);
336 sspt_file_add_ip(file, &pd);
338 /* if addr mapping, that probe install, else it be installed in do_page_fault handler */
339 if (page_present(mm, addr)) {
340 ip = sspt_find_ip(page, offset_addr & ~PAGE_MASK);
341 sspt_set_ip_addr(ip, page, file);
344 ret = register_usprobe_my(task, ip);
346 sspt_page_installed(page);
348 printk("ERROR install_otg_ip: ret=%d\n", ret);
359 EXPORT_SYMBOL_GPL(install_otg_ip);
361 static int uninstall_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
363 kernel_probe_t *probe = NULL;
365 if (probes_flags & kflag) {
366 probe = find_probe(addr);
368 iRet = remove_probe_from_list (addr);
370 EPRINTF ("remove_probe_from_list(0x%lx) result=%d!", addr, iRet);
374 probes_flags &= ~kflag;
376 if (us_proc_probes & uflag) {
377 if (!(probes_flags & uflag)) {
379 iRet = unregister_kernel_probe(probe);
381 EPRINTF ("unregister_kernel_probe(0x%lx) result=%d!",
387 us_proc_probes &= ~uflag;
392 static int uninstall_us_proc_probes(struct task_struct *task, struct sspt_procs *procs, enum US_FLAGS flag);
394 int deinst_usr_space_proc (void)
396 int iRet = 0, found = 0;
397 struct task_struct *task = NULL;
399 if (!is_us_instrumentation()) {
403 iRet = uninstall_kernel_probe (pf_addr, US_PROC_PF_INSTLD,
406 EPRINTF ("uninstall_kernel_probe(do_page_fault) result=%d!", iRet);
408 iRet = uninstall_kernel_probe (cp_addr, US_PROC_CP_INSTLD,
411 EPRINTF ("uninstall_kernel_probe(copy_process) result=%d!", iRet);
413 iRet = uninstall_kernel_probe (mr_addr, US_PROC_MR_INSTLD,
416 EPRINTF ("uninstall_kernel_probe(mm_release) result=%d!", iRet);
418 iRet = uninstall_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD,
421 EPRINTF ("uninstall_kernel_probe(do_exit) result=%d!", iRet);
423 iRet = uninstall_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD,
426 EPRINTF ("uninstall_kernel_probe(do_munmap) result=%d!", iRet);
429 struct sspt_procs *procs;
431 for_each_process(task) {
432 procs = get_proc_probes_by_task(task);
434 int ret = uninstall_us_proc_probes(task, procs, US_UNREGS_PROBE);
436 EPRINTF ("failed to uninstall IPs (%d)!", ret);
439 dbi_unregister_all_uprobes(task, 1);
445 if (us_proc_info.tgid == 0)
448 for_each_process (task)
450 if (task->tgid == us_proc_info.tgid)
453 get_task_struct (task);
462 ret = uninstall_us_proc_probes(task, us_proc_info.pp, US_UNREGS_PROBE);
464 EPRINTF ("failed to uninstall IPs %d!", ret);
467 put_task_struct (task);
469 printk("### 1 ### dbi_unregister_all_uprobes:\n");
470 dbi_unregister_all_uprobes(task, 1);
471 us_proc_info.tgid = 0;
472 for(i = 0; i < us_proc_info.libs_count; i++)
473 us_proc_info.p_libs[i].loaded = 0;
479 static int install_kernel_probe (unsigned long addr, int uflag, int kflag, kernel_probe_t ** pprobe)
481 kernel_probe_t *probe = NULL;
484 DPRINTF("us_proc_probes = 0x%x, uflag = 0x%x, "
485 "probes_flags = 0x%x, kflag = 0x%x",
486 us_proc_probes, uflag, probes_flags, kflag);
488 if (!(probes_flags & kflag)) {
489 iRet = add_probe_to_list (addr, &probe);
491 EPRINTF ("add_probe_to_list(0x%lx) result=%d!", addr, iRet);
494 probes_flags |= kflag;
496 if (!(us_proc_probes & uflag)) {
497 if (!(probes_flags & uflag)) {
498 iRet = register_kernel_probe (probe);
500 EPRINTF ("register_kernel_probe(0x%lx) result=%d!", addr, iRet);
504 us_proc_probes |= uflag;
513 static void install_proc_probes(struct task_struct *task, struct sspt_procs *procs, int atomic);
515 int inst_usr_space_proc (void)
518 struct task_struct *task = NULL;
520 if (!is_us_instrumentation()) {
524 DPRINTF("User space instr");
527 launchpad_daemon_dentry = dentry_by_path("/usr/bin/launchpad_preloading_preinitializing_daemon");
528 if (launchpad_daemon_dentry == NULL) {
535 app_process_dentry = dentry_by_path("/system/bin/app_process");
536 if (app_process_dentry == NULL) {
540 android_app_vma_start = 0;
541 android_app_vma_end = 0;
542 #endif /* ANDROID_APP */
544 for (i = 0; i < us_proc_info.libs_count; i++) {
545 us_proc_info.p_libs[i].loaded = 0;
547 /* check whether process is already running
548 * 1) if process is running - look for the libraries in the process maps
549 * 1.1) check if page for symbol does exist
550 * 1.1.1) if page exists - instrument it
551 * 1.1.2) if page does not exist - make sure that do_page_fault handler is installed
552 * 2) if process is not running - make sure that do_page_fault handler is installed
557 // FIXME: clear_task_inst_info();
558 for_each_process (task) {
559 struct sspt_procs *procs;
561 if (task->flags & PF_KTHREAD){
562 DPRINTF("ignored kernel thread %d\n",
567 procs = get_proc_probes_by_task_or_new(task);
568 DPRINTF("trying process");
569 install_proc_probes(task, procs, 1);
570 //put_task_struct (task);
575 ret = find_task_by_path (us_proc_info.path, &task, NULL);
578 DPRINTF("task found. installing probes");
579 us_proc_info.tgid = task->pid;
580 install_proc_probes(task, us_proc_info.pp, 0);
581 put_task_struct (task);
585 // enable 'do_page_fault' probe to detect when they will be loaded
586 ret = install_kernel_probe (pf_addr, US_PROC_PF_INSTLD, 0, &pf_probe);
589 EPRINTF ("install_kernel_probe(do_page_fault) result=%d!", ret);
592 // enable 'do_exit' probe to detect for remove task_struct
593 ret = install_kernel_probe (exit_addr, US_PROC_EXIT_INSTLD, 0, &exit_probe);
596 EPRINTF ("install_kernel_probe(do_exit) result=%d!", ret);
599 /* enable 'copy_process' */
600 ret = install_kernel_probe (cp_addr, US_PROC_CP_INSTLD, 0, &cp_probe);
603 EPRINTF ("instpall_kernel_probe(copy_process) result=%d!", ret);
607 // enable 'mm_release' probe to detect when for remove user space probes
608 ret = install_kernel_probe (mr_addr, US_PROC_MR_INSTLD, 0, &mr_probe);
611 EPRINTF ("install_kernel_probe(mm_release) result=%d!", ret);
615 // enable 'do_munmap' probe to detect when for remove user space probes
616 ret = install_kernel_probe (unmap_addr, US_PROC_UNMAP_INSTLD, 0, &unmap_probe);
619 EPRINTF ("install_kernel_probe(do_munmap) result=%d!", ret);
625 #include "../../tools/gpmu/probes/entry_data.h"
627 void do_page_fault_j_pre_code(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
629 struct task_struct *task = current->group_leader;
631 if (task->flags & PF_KTHREAD) {
632 DPRINTF("ignored kernel thread %d\n", task->pid);
636 if (is_us_instrumentation()) {
637 swap_put_entry_data((void *)addr, &sa_dpf);
640 EXPORT_SYMBOL_GPL(do_page_fault_j_pre_code);
643 unsigned long imi_sum_time = 0;
644 unsigned long imi_sum_hit = 0;
646 static void set_mapping_file(struct sspt_file *file,
647 const struct sspt_procs *procs,
648 const struct task_struct *task,
649 const struct vm_area_struct *vma)
651 int app_flag = (vma->vm_file->f_dentry == procs->dentry);
653 file->vm_start = vma->vm_start;
654 file->vm_end = vma->vm_end;
656 pack_event_info(DYN_LIB_PROBE_ID, RECORD_ENTRY, "dspdd",
657 task->tgid, file->name, vma->vm_start,
658 vma->vm_end - vma->vm_start, app_flag);
661 void print_vma(struct mm_struct *mm);
663 static int register_us_page_probe(struct sspt_page *page,
664 const struct sspt_file *file,
665 struct task_struct *task)
670 spin_lock(&page->lock);
672 if (sspt_page_is_install(page)) {
673 printk("page %lx in %s task[tgid=%u, pid=%u] already installed\n",
674 page->offset, file->dentry->d_iname, task->tgid, task->pid);
679 sspt_page_assert_install(page);
680 sspt_set_all_ip_addr(page, file);
682 list_for_each_entry(ip, &page->ip_list, list) {
683 err = register_usprobe_my(task, ip);
690 sspt_page_installed(page);
693 spin_unlock(&page->lock);
698 static int unregister_us_page_probe(struct task_struct *task,
699 struct sspt_page *page, enum US_FLAGS flag)
704 spin_lock(&page->lock);
705 if (!sspt_page_is_install(page)) {
706 spin_unlock(&page->lock);
710 list_for_each_entry(ip, &page->ip_list, list) {
711 err = unregister_usprobe_my(task, ip, flag);
718 if (flag != US_DISARM) {
719 sspt_page_uninstalled(page);
721 spin_unlock(&page->lock);
726 static void install_page_probes(unsigned long page_addr, struct task_struct *task, struct sspt_procs *procs, int atomic)
729 struct mm_struct *mm;
730 struct vm_area_struct *vma;
732 mm_read_lock(task, mm, atomic, lock);
734 vma = find_vma(mm, page_addr);
735 if (vma && check_vma(vma)) {
736 struct dentry *dentry = vma->vm_file->f_dentry;
737 struct sspt_file *file = sspt_procs_find_file(procs, dentry);
739 struct sspt_page *page;
741 set_mapping_file(file, procs, task, vma);
745 page = sspt_find_page_mapped(file, page_addr);
747 register_us_page_probe(page, file, task);
752 mm_read_unlock(mm, atomic, lock);
755 static void install_file_probes(struct task_struct *task, struct mm_struct *mm, struct sspt_file *file)
757 struct sspt_page *page = NULL;
758 struct hlist_node *node = NULL;
759 struct hlist_head *head = NULL;
760 int i, table_size = (1 << file->page_probes_hash_bits);
762 for (i = 0; i < table_size; ++i) {
763 head = &file->page_probes_table[i];
764 hlist_for_each_entry_rcu(page, node, head, hlist) {
765 if (page_present(mm, page->offset)) {
766 register_us_page_probe(page, file, task);
772 static void install_proc_probes(struct task_struct *task, struct sspt_procs *procs, int atomic)
775 struct vm_area_struct *vma;
776 struct mm_struct *mm;
778 mm_read_lock(task, mm, atomic, lock);
780 for (vma = mm->mmap; vma; vma = vma->vm_next) {
781 if (check_vma(vma)) {
782 struct dentry *dentry = vma->vm_file->f_dentry;
783 struct sspt_file *file = sspt_procs_find_file(procs, dentry);
786 set_mapping_file(file, procs, task, vma);
790 install_file_probes(task, mm, file);
795 mm_read_unlock(mm, atomic, lock);
798 static int check_install_pages_in_file(struct task_struct *task, struct sspt_file *file)
801 int table_size = (1 << file->page_probes_hash_bits);
802 struct sspt_page *page;
803 struct hlist_node *node, *tmp;
804 struct hlist_head *head;
806 for (i = 0; i < table_size; ++i) {
807 head = &file->page_probes_table[i];
808 hlist_for_each_entry_safe (page, node, tmp, head, hlist) {
818 static int unregister_us_file_probes(struct task_struct *task, struct sspt_file *file, enum US_FLAGS flag)
821 int table_size = (1 << file->page_probes_hash_bits);
822 struct sspt_page *page;
823 struct hlist_node *node, *tmp;
824 struct hlist_head *head;
826 for (i = 0; i < table_size; ++i) {
827 head = &file->page_probes_table[i];
828 hlist_for_each_entry_safe (page, node, tmp, head, hlist) {
829 err = unregister_us_page_probe(task, page, flag);
837 if (flag != US_DISARM) {
844 static int uninstall_us_proc_probes(struct task_struct *task, struct sspt_procs *procs, enum US_FLAGS flag)
847 struct sspt_file *file;
849 list_for_each_entry_rcu(file, &procs->file_list, list) {
850 err = unregister_us_file_probes(task, file, flag);
860 static pid_t find_proc_by_task(const struct task_struct *task, struct dentry *dentry)
862 struct vm_area_struct *vma;
863 struct mm_struct *mm = task->active_mm;
868 for (vma = mm->mmap; vma; vma = vma->vm_next) {
869 if (check_vma(vma)) {
870 if (vma->vm_file->f_dentry == dentry) {
874 if (is_slp_app_with_dentry(vma, dentry)) {
879 if (is_android_app_with_dentry(vma, dentry)) {
882 #endif /* ANDROID_APP */
889 void do_page_fault_ret_pre_code (void)
891 struct task_struct *task = current->group_leader;
892 struct mm_struct *mm = task->mm;
893 struct sspt_procs *procs = NULL;
895 * Because process threads have same address space
896 * we instrument only group_leader of all this threads
898 unsigned long addr = 0;
902 struct timeval imi_tv1;
903 struct timeval imi_tv2;
904 #define USEC_IN_SEC_NUM 1000000
906 if (task->flags & PF_KTHREAD) {
907 DPRINTF("ignored kernel thread %d\n", task->pid);
911 if (!is_us_instrumentation()) {
915 addr = (unsigned long)swap_get_entry_data(&sa_dpf);
918 printk("WARNING: do_page_fault_ret_pre_code addr = 0\n");
925 valid_addr = mm && page_present(mm, addr);
931 procs = get_proc_probes_by_task_or_new(task);
934 if (us_proc_info.tgid == 0) {
935 pid_t tgid = find_proc_by_task(task, us_proc_info.m_f_dentry);
937 us_proc_info.tgid = gl_nNotifyTgid = tgid;
939 /* install probes in already mapped memory */
940 install_proc_probes(task, us_proc_info.pp, 1);
944 if (us_proc_info.tgid == task->tgid) {
945 procs = us_proc_info.pp;
950 unsigned long page = addr & PAGE_MASK;
953 do_gettimeofday(&imi_tv1);
954 install_page_probes(page, task, procs, 1);
955 do_gettimeofday(&imi_tv2);
957 imi_sum_time += ((imi_tv2.tv_sec - imi_tv1.tv_sec) * USEC_IN_SEC_NUM +
958 (imi_tv2.tv_usec - imi_tv1.tv_usec));
962 EXPORT_SYMBOL_GPL(do_page_fault_ret_pre_code);
965 void do_exit_probe_pre_code (void)
969 EXPORT_SYMBOL_GPL(do_exit_probe_pre_code);
971 void print_vma(struct mm_struct *mm)
973 struct vm_area_struct *vma;
974 printk("### print_vma: START\n");\
975 printk("### print_vma: START\n");
977 for (vma = mm->mmap; vma; vma = vma->vm_next) {
978 char *x = vma->vm_flags & VM_EXEC ? "x" : "-";
979 char *r = vma->vm_flags & VM_READ ? "r" : "-";
980 char *w = vma->vm_flags & VM_WRITE ? "w" : "-";
981 char *name = vma->vm_file ? (char *)vma->vm_file->f_dentry->d_iname : "N/A";
983 printk("### [%8lx..%8lx] %s%s%s pgoff=\'%8lu\' %s\n",
984 vma->vm_start, vma->vm_end, x, r, w, vma->vm_pgoff, name);
986 printk("### print_vma: END\n");
989 static int remove_unmap_probes(struct task_struct *task, struct sspt_procs *procs, unsigned long start, size_t len)
991 struct mm_struct *mm = task->mm;
992 struct vm_area_struct *vma;
994 if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE - start) {
998 if ((len = PAGE_ALIGN(len)) == 0) {
1002 vma = find_vma(mm, start);
1003 if (vma && check_vma(vma)) {
1004 struct sspt_file *file;
1005 unsigned long end = start + len;
1006 struct dentry *dentry = vma->vm_file->f_dentry;
1008 file = sspt_procs_find_file(procs, dentry);
1010 if (vma->vm_start == start || vma->vm_end == end) {
1011 unregister_us_file_probes(task, file, US_NOT_RP2);
1014 unsigned long page_addr;
1015 struct sspt_page *page;
1017 for (page_addr = vma->vm_start; page_addr < vma->vm_end; page_addr += PAGE_SIZE) {
1018 page = sspt_find_page_mapped(file, page_addr);
1020 unregister_us_page_probe(task, page, US_NOT_RP2);
1024 if (check_install_pages_in_file(task, file)) {
1034 void do_munmap_probe_pre_code(struct mm_struct *mm, unsigned long start, size_t len)
1036 struct sspt_procs *procs = NULL;
1037 struct task_struct *task = current;
1039 //if user-space instrumentation is not set
1040 if (!is_us_instrumentation()) {
1045 procs = get_proc_probes_by_task(task);
1047 if (task->tgid == us_proc_info.tgid) {
1048 procs = us_proc_info.pp;
1053 if (remove_unmap_probes(task, procs, start, len)) {
1054 printk("ERROR do_munmap: start=%lx, len=%x\n", start, len);
1058 EXPORT_SYMBOL_GPL(do_munmap_probe_pre_code);
1060 void mm_release_probe_pre_code(void)
1062 struct task_struct *task = current;
1063 struct sspt_procs *procs = NULL;
1065 if (!is_us_instrumentation() || task->tgid != task->pid) {
1070 procs = get_proc_probes_by_task(task);
1072 if (task->tgid == us_proc_info.tgid) {
1073 procs = get_proc_probes_by_task(task);
1074 us_proc_info.tgid = 0;
1079 int ret = uninstall_us_proc_probes(task, procs, US_NOT_RP2);
1081 EPRINTF ("failed to uninstall IPs (%d)!", ret);
1084 dbi_unregister_all_uprobes(task, 1);
1087 EXPORT_SYMBOL_GPL(mm_release_probe_pre_code);
1090 static void recover_child(struct task_struct *child_task, struct sspt_procs *procs)
1092 uninstall_us_proc_probes(child_task, procs, US_DISARM);
1095 static void rm_uprobes_child(struct task_struct *new_task)
1098 struct sspt_procs *procs = get_proc_probes_by_task(current);
1100 recover_child(new_task, procs);
1103 if(us_proc_info.tgid == current->tgid) {
1104 recover_child(new_task, us_proc_info.pp);
1109 void copy_process_ret_pre_code(struct task_struct *p)
1114 if(p->mm != current->mm) // check flags CLONE_VM
1115 rm_uprobes_child(p);
1118 static DEFINE_PER_CPU(struct us_ip *, gpCurIp) = NULL;
1119 EXPORT_PER_CPU_SYMBOL_GPL(gpCurIp);
1120 static DEFINE_PER_CPU(struct pt_regs *, gpUserRegs) = NULL;
1121 EXPORT_PER_CPU_SYMBOL_GPL(gpUserRegs);
1123 unsigned long ujprobe_event_pre_handler(struct us_ip *ip, struct pt_regs *regs)
1125 __get_cpu_var (gpCurIp) = ip;
1126 __get_cpu_var (gpUserRegs) = regs;
1130 void ujprobe_event_handler (unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
1132 struct us_ip *ip = __get_cpu_var(gpCurIp);
1133 unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1136 struct pt_regs *regs = __get_cpu_var(gpUserRegs);
1137 if (is_java_inst_enabled() && handle_java_event(regs)) {
1140 #endif /* __ANDROID */
1143 #if defined(CONFIG_ARM)
1144 if (ip->offset & 0x01)
1146 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr | 0x01, arg1, arg2, arg3, arg4, arg5, arg6);
1148 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1151 pack_event_info (US_PROBE_ID, RECORD_ENTRY, "ppppppp", addr, arg1, arg2, arg3, arg4, arg5, arg6);
1153 // Mr_Nobody: uncomment for valencia
1154 //unregister_usprobe(current, ip, 1);
1155 dbi_uprobe_return ();
1158 static void send_plt(struct us_ip *ip)
1160 unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1161 struct vm_area_struct *vma = find_vma(current->mm, addr);
1163 if (vma && check_vma(vma)) {
1165 unsigned long real_addr;
1166 unsigned long real_got = vma->vm_flags & VM_EXECUTABLE ?
1168 ip->got_addr + vma->vm_start;
1170 if (!read_proc_vm_atomic(current, real_got, &real_addr, sizeof(real_addr))) {
1171 printk("Failed to read got %lx at memory address %lx!\n", ip->got_addr, real_got);
1175 vma = find_vma(current->mm, real_addr);
1176 if (vma && (vma->vm_start <= real_addr) && (vma->vm_end > real_addr)) {
1177 name = vma->vm_file ? vma->vm_file->f_dentry->d_iname : NULL;
1179 printk("Failed to get vma, includes %lx address\n", real_addr);
1184 pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppsp", addr, real_addr, name, real_addr - vma->vm_start);
1186 pack_event_info(PLT_ADDR_PROBE_ID, RECORD_RET, "ppp", addr, real_addr, real_addr - vma->vm_start);
1191 int uretprobe_event_handler(struct kretprobe_instance *probe, struct pt_regs *regs, struct us_ip *ip)
1193 int retval = regs_return_value(regs);
1194 unsigned long addr = (unsigned long)ip->jprobe.kp.addr;
1196 if (ip->got_addr && ip->flag_got == 0) {
1201 #if defined(CONFIG_ARM)
1202 if (ip->offset & 0x01)
1204 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr | 0x01, retval);
1206 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1209 pack_event_info (US_PROBE_ID, RECORD_RET, "pd", addr, retval);
1211 // Mr_Nobody: uncomment for valencia
1212 //unregister_usprobe(current, ip, 1);
1216 int register_usprobe(struct task_struct *task, struct us_ip *ip, int atomic)
1219 ip->jprobe.kp.tgid = task->tgid;
1221 if (ip->jprobe.entry == NULL) {
1222 ip->jprobe.entry = (kprobe_opcode_t *)ujprobe_event_handler;
1223 DPRINTF("Set default event handler for %x\n", ip->offset);
1226 if (ip->jprobe.pre_entry == NULL) {
1227 ip->jprobe.pre_entry = (kprobe_pre_entry_handler_t)ujprobe_event_pre_handler;
1228 DPRINTF("Set default pre handler for %x\n", ip->offset);
1231 ip->jprobe.priv_arg = ip;
1232 ret = dbi_register_ujprobe(task, &ip->jprobe, atomic);
1234 DPRINTF ("dbi_register_ujprobe() failure %d", ret);
1238 if (ip->flag_retprobe) {
1239 // Mr_Nobody: comment for valencia
1240 ip->retprobe.kp.tgid = task->tgid;
1241 if (ip->retprobe.handler == NULL) {
1242 ip->retprobe.handler = (kretprobe_handler_t)uretprobe_event_handler;
1243 DPRINTF("Set default ret event handler for %x\n", ip->offset);
1246 ip->retprobe.priv_arg = ip;
1247 ret = dbi_register_uretprobe(task, &ip->retprobe, atomic);
1249 EPRINTF ("dbi_register_uretprobe() failure %d", ret);
1257 int unregister_usprobe(struct task_struct *task, struct us_ip *ip, int atomic, int not_rp2)
1259 dbi_unregister_ujprobe(task, &ip->jprobe, atomic);
1261 if (ip->flag_retprobe) {
1262 dbi_unregister_uretprobe(task, &ip->retprobe, atomic, not_rp2);
1268 unsigned long get_stack_size(struct task_struct *task,
1269 struct pt_regs *regs)
1271 #ifdef CONFIG_ADD_THREAD_STACK_INFO
1272 return (task->stack_start - dbi_get_stack_ptr(regs));
1274 struct vm_area_struct *vma = NULL;
1275 struct mm_struct *mm = NULL;
1276 unsigned long result = 0;
1277 int atomic = in_atomic();
1279 mm = (atomic ? task->active_mm: get_task_mm(task));
1283 down_read(&mm->mmap_sem);
1285 vma = find_vma(mm, dbi_get_stack_ptr(regs));
1288 result = vma->vm_end - dbi_get_stack_ptr(regs);
1293 up_read(&mm->mmap_sem);
1301 EXPORT_SYMBOL_GPL(get_stack_size);
1303 unsigned long get_stack(struct task_struct *task, struct pt_regs *regs,
1304 char *buf, unsigned long sz)
1306 unsigned long stack_sz = get_stack_size(task, regs);
1307 unsigned long real_sz = (stack_sz > sz ? sz: stack_sz);
1308 int res = read_proc_vm_atomic(task, dbi_get_stack_ptr(regs), buf, real_sz);
1311 EXPORT_SYMBOL_GPL(get_stack);
1313 int dump_to_trace(probe_id_t probe_id, void *addr, const char *buf,
1316 unsigned long rest_sz = sz;
1317 const char *data = buf;
1319 while (rest_sz >= EVENT_MAX_SIZE) {
1320 pack_event_info(probe_id, RECORD_ENTRY, "pa",
1321 addr, EVENT_MAX_SIZE, data);
1322 rest_sz -= EVENT_MAX_SIZE;
1323 data += EVENT_MAX_SIZE;
1327 pack_event_info(probe_id, RECORD_ENTRY, "pa", addr, rest_sz, data);
1331 EXPORT_SYMBOL_GPL(dump_to_trace);
1333 int dump_backtrace(probe_id_t probe_id, struct task_struct *task,
1334 void *addr, struct pt_regs *regs, unsigned long sz)
1336 unsigned long real_sz = 0;
1339 buf = (char *)kmalloc(sz, GFP_ATOMIC);
1342 real_sz = get_stack(task, regs, buf, sz);
1344 dump_to_trace(probe_id, addr, buf, real_sz);
1351 EXPORT_SYMBOL_GPL(dump_backtrace);
1353 struct kretprobe_instance *find_ri(struct task_struct *task, struct us_ip *ip)
1355 struct hlist_node *item, *tmp_node;
1356 struct kretprobe_instance *ri;
1361 hlist_for_each_safe (item, tmp_node, &ip->retprobe.used_instances) {
1362 ri = hlist_entry (item, struct kretprobe_instance, uflist);
1364 if (ri->task && ri->task->pid == task->pid &&
1365 ri->task->tgid == task->tgid)
1371 EXPORT_SYMBOL_GPL(find_ri);
1373 unsigned long get_ret_addr(struct task_struct *task, struct us_ip *ip)
1375 struct kretprobe_instance *ri = find_ri(task, ip);;
1377 return (unsigned long)ri->ret_addr;
1379 dbi_get_ret_addr(task_pt_regs(task));
1381 EXPORT_SYMBOL_GPL(get_ret_addr);
1383 unsigned long get_entry_sp(struct task_struct *task, struct us_ip *ip)
1385 struct kretprobe_instance *ri = find_ri(task, ip);
1389 return dbi_get_stack_ptr(task_pt_regs(task));
1391 EXPORT_SYMBOL_GPL(get_entry_sp);